Skip to content

MQE-1782 #497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Tests\unit\Magento\FunctionalTestingFramework\Console;

use AspectMock\Test as AspectMock;
use PHPUnit\Framework\TestCase;
use Magento\FunctionalTestingFramework\Console\BaseGenerateCommand;
use Magento\FunctionalTestingFramework\Suite\Objects\SuiteObject;
use Magento\FunctionalTestingFramework\Suite\Handlers\SuiteObjectHandler;
use Magento\FunctionalTestingFramework\Test\Objects\TestObject;
use Magento\FunctionalTestingFramework\Test\Handlers\TestObjectHandler;

class BaseGenerateCommandTest extends TestCase
{
public function tearDown()
{
AspectMock::clean();
}

public function testOneTestOneSuiteConfig()
{
$testOne = new TestObject('Test1', [], [], []);
$suiteOne = new SuiteObject('Suite1', ['Test1' => $testOne], [], []);

$testArray = ['Test1' => $testOne];
$suiteArray = ['Suite1' => $suiteOne];

$this->mockHandlers($testArray, $suiteArray);

$actual = json_decode($this->callTestConfig(['Test1']), true);
$expected = ['tests' => null, 'suites' => ['Suite1' => ['Test1']]];
$this->assertEquals($expected, $actual);
}

public function testOneTestTwoSuitesConfig()
{
$testOne = new TestObject('Test1', [], [], []);
$suiteOne = new SuiteObject('Suite1', ['Test1' => $testOne], [], []);
$suiteTwo = new SuiteObject('Suite2', ['Test1' => $testOne], [], []);

$testArray = ['Test1' => $testOne];
$suiteArray = ['Suite1' => $suiteOne, 'Suite2' => $suiteTwo];

$this->mockHandlers($testArray, $suiteArray);

$actual = json_decode($this->callTestConfig(['Test1']), true);
$expected = ['tests' => null, 'suites' => ['Suite1' => ['Test1'], 'Suite2' => ['Test1']]];
$this->assertEquals($expected, $actual);
}

public function testOneTestOneGroup()
{
$testOne = new TestObject('Test1', [], ['group' => ['Group1']], []);

$testArray = ['Test1' => $testOne];
$suiteArray = [];

$this->mockHandlers($testArray, $suiteArray);

$actual = json_decode($this->callGroupConfig(['Group1']), true);
$expected = ['tests' => ['Test1'], 'suites' => null];
$this->assertEquals($expected, $actual);
}

public function testThreeTestsTwoGroup()
{
$testOne = new TestObject('Test1', [], ['group' => ['Group1']], []);
$testTwo = new TestObject('Test2', [], ['group' => ['Group1']], []);
$testThree = new TestObject('Test3', [], ['group' => ['Group2']], []);

$testArray = ['Test1' => $testOne, 'Test2' => $testTwo, 'Test3' => $testThree];
$suiteArray = [];

$this->mockHandlers($testArray, $suiteArray);

$actual = json_decode($this->callGroupConfig(['Group1', 'Group2']), true);
$expected = ['tests' => ['Test1', 'Test2', 'Test3'], 'suites' => null];
$this->assertEquals($expected, $actual);
}

public function testOneTestOneSuiteOneGroupConfig()
{
$testOne = new TestObject('Test1', [], ['group' => ['Group1']], []);
$suiteOne = new SuiteObject('Suite1', ['Test1' => $testOne], [], []);

$testArray = ['Test1' => $testOne];
$suiteArray = ['Suite1' => $suiteOne];

$this->mockHandlers($testArray, $suiteArray);

$actual = json_decode($this->callGroupConfig(['Group1']), true);
$expected = ['tests' => null, 'suites' => ['Suite1' => ['Test1']]];
$this->assertEquals($expected, $actual);
}

public function testTwoTestOneSuiteTwoGroupConfig()
{
$testOne = new TestObject('Test1', [], ['group' => ['Group1']], []);
$testTwo = new TestObject('Test2', [], ['group' => ['Group2']], []);
$suiteOne = new SuiteObject('Suite1', ['Test1' => $testOne, 'Test2' => $testTwo], [], []);

$testArray = ['Test1' => $testOne, 'Test2' => $testTwo];
$suiteArray = ['Suite1' => $suiteOne];

$this->mockHandlers($testArray, $suiteArray);

$actual = json_decode($this->callGroupConfig(['Group1', 'Group2']), true);
$expected = ['tests' => null, 'suites' => ['Suite1' => ['Test1', 'Test2']]];
$this->assertEquals($expected, $actual);
}

public function testTwoTestTwoSuiteOneGroupConfig()
{
$testOne = new TestObject('Test1', [], ['group' => ['Group1']], []);
$testTwo = new TestObject('Test2', [], ['group' => ['Group1']], []);
$suiteOne = new SuiteObject('Suite1', ['Test1' => $testOne], [], []);
$suiteTwo = new SuiteObject('Suite2', ['Test2' => $testTwo], [], []);

$testArray = ['Test1' => $testOne, 'Test2' => $testTwo];
$suiteArray = ['Suite1' => $suiteOne, 'Suite2' => $suiteTwo];

$this->mockHandlers($testArray, $suiteArray);

$actual = json_decode($this->callGroupConfig(['Group1']), true);
$expected = ['tests' => null, 'suites' => ['Suite1' => ['Test1'], 'Suite2' => ['Test2']]];
$this->assertEquals($expected, $actual);
}

/**
* Test specific usecase of a test that is in a group with the group being called along with the suite
* i.e. run:group Group1 Suite1
* @throws \Exception
*/
public function testThreeTestOneSuiteOneGroupMix()
{
$testOne = new TestObject('Test1', [], [], []);
$testTwo = new TestObject('Test2', [], [], []);
$testThree = new TestObject('Test3', [], ['group' => ['Group1']], []);
$suiteOne = new SuiteObject(
'Suite1',
['Test1' => $testOne, 'Test2' => $testTwo, 'Test3' => $testThree],
[],
[]
);

$testArray = ['Test1' => $testOne, 'Test2' => $testTwo, 'Test3' => $testThree];
$suiteArray = ['Suite1' => $suiteOne];

$this->mockHandlers($testArray, $suiteArray);

$actual = json_decode($this->callGroupConfig(['Group1', 'Suite1']), true);
$expected = ['tests' => null, 'suites' => ['Suite1' => []]];
$this->assertEquals($expected, $actual);
}

/**
* Mock handlers to skip parsing
* @param array $testArray
* @param array $suiteArray
* @throws \Exception
*/
public function mockHandlers($testArray, $suiteArray)
{
AspectMock::double(TestObjectHandler::class, ['initTestData' => ''])->make();
$handler = TestObjectHandler::getInstance();
$property = new \ReflectionProperty(TestObjectHandler::class, 'tests');
$property->setAccessible(true);
$property->setValue($handler, $testArray);

AspectMock::double(SuiteObjectHandler::class, ['initSuiteData' => ''])->make();
$handler = SuiteObjectHandler::getInstance();
$property = new \ReflectionProperty(SuiteObjectHandler::class, 'suiteObjects');
$property->setAccessible(true);
$property->setValue($handler, $suiteArray);
}

/**
* Changes visibility and runs getTestAndSuiteConfiguration
* @param array $testArray
* @return string
*/
public function callTestConfig($testArray)
{
$command = new BaseGenerateCommand();
$class = new \ReflectionClass($command);
$method = $class->getMethod('getTestAndSuiteConfiguration');
$method->setAccessible(true);
return $method->invokeArgs($command, [$testArray]);
}

/**
* Changes visibility and runs getGroupAndSuiteConfiguration
* @param array $groupArray
* @return string
*/
public function callGroupConfig($groupArray)
{
$command = new BaseGenerateCommand();
$class = new \ReflectionClass($command);
$method = $class->getMethod('getGroupAndSuiteConfiguration');
$method->setAccessible(true);
return $method->invokeArgs($command, [$groupArray]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Magento\FunctionalTestingFramework\Console;

use Magento\FunctionalTestingFramework\Test\Handlers\TestObjectHandler;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -75,7 +76,6 @@ protected function removeGeneratedDirectory(OutputInterface $output, bool $verbo
* @return false|string
* @throws \Magento\FunctionalTestingFramework\Exceptions\XmlException
*/

protected function getTestAndSuiteConfiguration(array $tests)
{
$testConfiguration['tests'] = null;
Expand Down Expand Up @@ -103,4 +103,72 @@ protected function getTestAndSuiteConfiguration(array $tests)
$testConfigurationJson = json_encode($testConfiguration);
return $testConfigurationJson;
}

/**
* Returns an array of test configuration to be used as an argument for generation of tests
* This function uses group or suite names for generation
* @return false|string
* @throws \Magento\FunctionalTestingFramework\Exceptions\XmlException
*/
protected function getGroupAndSuiteConfiguration(array $groupOrSuiteNames)
{
$result['tests'] = [];
$result['suites'] = [];

$groups = [];
$suites = [];

$allSuites = SuiteObjectHandler::getInstance()->getAllObjects();
$testsInSuites = SuiteObjectHandler::getInstance()->getAllTestReferences();

foreach ($groupOrSuiteNames as $groupOrSuiteName) {
if (array_key_exists($groupOrSuiteName, $allSuites)) {
$suites[] = $groupOrSuiteName;
} else {
$groups[] = $groupOrSuiteName;
}
}

foreach ($suites as $suite) {
$result['suites'][$suite] = [];
}

foreach ($groups as $group) {
$testsInGroup = TestObjectHandler::getInstance()->getTestsByGroup($group);

$testsInGroupAndNotInAnySuite = array_diff(
array_keys($testsInGroup),
array_keys($testsInSuites)
);

$testsInGroupAndInAnySuite = array_diff(
array_keys($testsInGroup),
$testsInGroupAndNotInAnySuite
);

foreach ($testsInGroupAndInAnySuite as $testInGroupAndInAnySuite) {
$suiteName = $testsInSuites[$testInGroupAndInAnySuite][0];
if (array_search($suiteName, $suites) !== false) {
// Suite is already being called to run in its entirety, do not filter list
continue;
}
$result['suites'][$suiteName][] = $testInGroupAndInAnySuite;
}

$result['tests'] = array_merge(
$result['tests'],
$testsInGroupAndNotInAnySuite
);
}

if (empty($result['tests'])) {
$result['tests'] = null;
}
if (empty($result['suites'])) {
$result['suites'] = null;
}

$json = json_encode($result);
return $json;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,32 +111,4 @@ function ($type, $buffer) use ($output) {
}
);
}

/**
* Returns a json string to be used as an argument for generation of a group or suite
*
* @param array $groups
* @return string
* @throws \Magento\FunctionalTestingFramework\Exceptions\XmlException
*/
private function getGroupAndSuiteConfiguration(array $groups)
{
$testConfiguration['tests'] = [];
$testConfiguration['suites'] = null;
$availableSuites = SuiteObjectHandler::getInstance()->getAllObjects();

foreach ($groups as $group) {
if (array_key_exists($group, $availableSuites)) {
$testConfiguration['suites'][$group] = [];
}

$testConfiguration['tests'] = array_merge(
$testConfiguration['tests'],
array_keys(TestObjectHandler::getInstance()->getTestsByGroup($group))
);
}

$testConfigurationJson = json_encode($testConfiguration);
return $testConfigurationJson;
}
}