Skip to content

Commit 640c2c9

Browse files
authored
Merge pull request #497 from magento/MQE-1782
MQE-1782
2 parents c3f7c05 + 8016574 commit 640c2c9

File tree

3 files changed

+276
-29
lines changed

3 files changed

+276
-29
lines changed
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Tests\unit\Magento\FunctionalTestingFramework\Console;
7+
8+
use AspectMock\Test as AspectMock;
9+
use PHPUnit\Framework\TestCase;
10+
use Magento\FunctionalTestingFramework\Console\BaseGenerateCommand;
11+
use Magento\FunctionalTestingFramework\Suite\Objects\SuiteObject;
12+
use Magento\FunctionalTestingFramework\Suite\Handlers\SuiteObjectHandler;
13+
use Magento\FunctionalTestingFramework\Test\Objects\TestObject;
14+
use Magento\FunctionalTestingFramework\Test\Handlers\TestObjectHandler;
15+
16+
class BaseGenerateCommandTest extends TestCase
17+
{
18+
public function tearDown()
19+
{
20+
AspectMock::clean();
21+
}
22+
23+
public function testOneTestOneSuiteConfig()
24+
{
25+
$testOne = new TestObject('Test1', [], [], []);
26+
$suiteOne = new SuiteObject('Suite1', ['Test1' => $testOne], [], []);
27+
28+
$testArray = ['Test1' => $testOne];
29+
$suiteArray = ['Suite1' => $suiteOne];
30+
31+
$this->mockHandlers($testArray, $suiteArray);
32+
33+
$actual = json_decode($this->callTestConfig(['Test1']), true);
34+
$expected = ['tests' => null, 'suites' => ['Suite1' => ['Test1']]];
35+
$this->assertEquals($expected, $actual);
36+
}
37+
38+
public function testOneTestTwoSuitesConfig()
39+
{
40+
$testOne = new TestObject('Test1', [], [], []);
41+
$suiteOne = new SuiteObject('Suite1', ['Test1' => $testOne], [], []);
42+
$suiteTwo = new SuiteObject('Suite2', ['Test1' => $testOne], [], []);
43+
44+
$testArray = ['Test1' => $testOne];
45+
$suiteArray = ['Suite1' => $suiteOne, 'Suite2' => $suiteTwo];
46+
47+
$this->mockHandlers($testArray, $suiteArray);
48+
49+
$actual = json_decode($this->callTestConfig(['Test1']), true);
50+
$expected = ['tests' => null, 'suites' => ['Suite1' => ['Test1'], 'Suite2' => ['Test1']]];
51+
$this->assertEquals($expected, $actual);
52+
}
53+
54+
public function testOneTestOneGroup()
55+
{
56+
$testOne = new TestObject('Test1', [], ['group' => ['Group1']], []);
57+
58+
$testArray = ['Test1' => $testOne];
59+
$suiteArray = [];
60+
61+
$this->mockHandlers($testArray, $suiteArray);
62+
63+
$actual = json_decode($this->callGroupConfig(['Group1']), true);
64+
$expected = ['tests' => ['Test1'], 'suites' => null];
65+
$this->assertEquals($expected, $actual);
66+
}
67+
68+
public function testThreeTestsTwoGroup()
69+
{
70+
$testOne = new TestObject('Test1', [], ['group' => ['Group1']], []);
71+
$testTwo = new TestObject('Test2', [], ['group' => ['Group1']], []);
72+
$testThree = new TestObject('Test3', [], ['group' => ['Group2']], []);
73+
74+
$testArray = ['Test1' => $testOne, 'Test2' => $testTwo, 'Test3' => $testThree];
75+
$suiteArray = [];
76+
77+
$this->mockHandlers($testArray, $suiteArray);
78+
79+
$actual = json_decode($this->callGroupConfig(['Group1', 'Group2']), true);
80+
$expected = ['tests' => ['Test1', 'Test2', 'Test3'], 'suites' => null];
81+
$this->assertEquals($expected, $actual);
82+
}
83+
84+
public function testOneTestOneSuiteOneGroupConfig()
85+
{
86+
$testOne = new TestObject('Test1', [], ['group' => ['Group1']], []);
87+
$suiteOne = new SuiteObject('Suite1', ['Test1' => $testOne], [], []);
88+
89+
$testArray = ['Test1' => $testOne];
90+
$suiteArray = ['Suite1' => $suiteOne];
91+
92+
$this->mockHandlers($testArray, $suiteArray);
93+
94+
$actual = json_decode($this->callGroupConfig(['Group1']), true);
95+
$expected = ['tests' => null, 'suites' => ['Suite1' => ['Test1']]];
96+
$this->assertEquals($expected, $actual);
97+
}
98+
99+
public function testTwoTestOneSuiteTwoGroupConfig()
100+
{
101+
$testOne = new TestObject('Test1', [], ['group' => ['Group1']], []);
102+
$testTwo = new TestObject('Test2', [], ['group' => ['Group2']], []);
103+
$suiteOne = new SuiteObject('Suite1', ['Test1' => $testOne, 'Test2' => $testTwo], [], []);
104+
105+
$testArray = ['Test1' => $testOne, 'Test2' => $testTwo];
106+
$suiteArray = ['Suite1' => $suiteOne];
107+
108+
$this->mockHandlers($testArray, $suiteArray);
109+
110+
$actual = json_decode($this->callGroupConfig(['Group1', 'Group2']), true);
111+
$expected = ['tests' => null, 'suites' => ['Suite1' => ['Test1', 'Test2']]];
112+
$this->assertEquals($expected, $actual);
113+
}
114+
115+
public function testTwoTestTwoSuiteOneGroupConfig()
116+
{
117+
$testOne = new TestObject('Test1', [], ['group' => ['Group1']], []);
118+
$testTwo = new TestObject('Test2', [], ['group' => ['Group1']], []);
119+
$suiteOne = new SuiteObject('Suite1', ['Test1' => $testOne], [], []);
120+
$suiteTwo = new SuiteObject('Suite2', ['Test2' => $testTwo], [], []);
121+
122+
$testArray = ['Test1' => $testOne, 'Test2' => $testTwo];
123+
$suiteArray = ['Suite1' => $suiteOne, 'Suite2' => $suiteTwo];
124+
125+
$this->mockHandlers($testArray, $suiteArray);
126+
127+
$actual = json_decode($this->callGroupConfig(['Group1']), true);
128+
$expected = ['tests' => null, 'suites' => ['Suite1' => ['Test1'], 'Suite2' => ['Test2']]];
129+
$this->assertEquals($expected, $actual);
130+
}
131+
132+
/**
133+
* Test specific usecase of a test that is in a group with the group being called along with the suite
134+
* i.e. run:group Group1 Suite1
135+
* @throws \Exception
136+
*/
137+
public function testThreeTestOneSuiteOneGroupMix()
138+
{
139+
$testOne = new TestObject('Test1', [], [], []);
140+
$testTwo = new TestObject('Test2', [], [], []);
141+
$testThree = new TestObject('Test3', [], ['group' => ['Group1']], []);
142+
$suiteOne = new SuiteObject(
143+
'Suite1',
144+
['Test1' => $testOne, 'Test2' => $testTwo, 'Test3' => $testThree],
145+
[],
146+
[]
147+
);
148+
149+
$testArray = ['Test1' => $testOne, 'Test2' => $testTwo, 'Test3' => $testThree];
150+
$suiteArray = ['Suite1' => $suiteOne];
151+
152+
$this->mockHandlers($testArray, $suiteArray);
153+
154+
$actual = json_decode($this->callGroupConfig(['Group1', 'Suite1']), true);
155+
$expected = ['tests' => null, 'suites' => ['Suite1' => []]];
156+
$this->assertEquals($expected, $actual);
157+
}
158+
159+
/**
160+
* Mock handlers to skip parsing
161+
* @param array $testArray
162+
* @param array $suiteArray
163+
* @throws \Exception
164+
*/
165+
public function mockHandlers($testArray, $suiteArray)
166+
{
167+
AspectMock::double(TestObjectHandler::class, ['initTestData' => ''])->make();
168+
$handler = TestObjectHandler::getInstance();
169+
$property = new \ReflectionProperty(TestObjectHandler::class, 'tests');
170+
$property->setAccessible(true);
171+
$property->setValue($handler, $testArray);
172+
173+
AspectMock::double(SuiteObjectHandler::class, ['initSuiteData' => ''])->make();
174+
$handler = SuiteObjectHandler::getInstance();
175+
$property = new \ReflectionProperty(SuiteObjectHandler::class, 'suiteObjects');
176+
$property->setAccessible(true);
177+
$property->setValue($handler, $suiteArray);
178+
}
179+
180+
/**
181+
* Changes visibility and runs getTestAndSuiteConfiguration
182+
* @param array $testArray
183+
* @return string
184+
*/
185+
public function callTestConfig($testArray)
186+
{
187+
$command = new BaseGenerateCommand();
188+
$class = new \ReflectionClass($command);
189+
$method = $class->getMethod('getTestAndSuiteConfiguration');
190+
$method->setAccessible(true);
191+
return $method->invokeArgs($command, [$testArray]);
192+
}
193+
194+
/**
195+
* Changes visibility and runs getGroupAndSuiteConfiguration
196+
* @param array $groupArray
197+
* @return string
198+
*/
199+
public function callGroupConfig($groupArray)
200+
{
201+
$command = new BaseGenerateCommand();
202+
$class = new \ReflectionClass($command);
203+
$method = $class->getMethod('getGroupAndSuiteConfiguration');
204+
$method->setAccessible(true);
205+
return $method->invokeArgs($command, [$groupArray]);
206+
}
207+
}

src/Magento/FunctionalTestingFramework/Console/BaseGenerateCommand.php

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Magento\FunctionalTestingFramework\Console;
1010

11+
use Magento\FunctionalTestingFramework\Test\Handlers\TestObjectHandler;
1112
use Symfony\Component\Console\Command\Command;
1213
use Symfony\Component\Console\Input\InputOption;
1314
use Symfony\Component\Console\Output\OutputInterface;
@@ -75,7 +76,6 @@ protected function removeGeneratedDirectory(OutputInterface $output, bool $verbo
7576
* @return false|string
7677
* @throws \Magento\FunctionalTestingFramework\Exceptions\XmlException
7778
*/
78-
7979
protected function getTestAndSuiteConfiguration(array $tests)
8080
{
8181
$testConfiguration['tests'] = null;
@@ -103,4 +103,72 @@ protected function getTestAndSuiteConfiguration(array $tests)
103103
$testConfigurationJson = json_encode($testConfiguration);
104104
return $testConfigurationJson;
105105
}
106+
107+
/**
108+
* Returns an array of test configuration to be used as an argument for generation of tests
109+
* This function uses group or suite names for generation
110+
* @return false|string
111+
* @throws \Magento\FunctionalTestingFramework\Exceptions\XmlException
112+
*/
113+
protected function getGroupAndSuiteConfiguration(array $groupOrSuiteNames)
114+
{
115+
$result['tests'] = [];
116+
$result['suites'] = [];
117+
118+
$groups = [];
119+
$suites = [];
120+
121+
$allSuites = SuiteObjectHandler::getInstance()->getAllObjects();
122+
$testsInSuites = SuiteObjectHandler::getInstance()->getAllTestReferences();
123+
124+
foreach ($groupOrSuiteNames as $groupOrSuiteName) {
125+
if (array_key_exists($groupOrSuiteName, $allSuites)) {
126+
$suites[] = $groupOrSuiteName;
127+
} else {
128+
$groups[] = $groupOrSuiteName;
129+
}
130+
}
131+
132+
foreach ($suites as $suite) {
133+
$result['suites'][$suite] = [];
134+
}
135+
136+
foreach ($groups as $group) {
137+
$testsInGroup = TestObjectHandler::getInstance()->getTestsByGroup($group);
138+
139+
$testsInGroupAndNotInAnySuite = array_diff(
140+
array_keys($testsInGroup),
141+
array_keys($testsInSuites)
142+
);
143+
144+
$testsInGroupAndInAnySuite = array_diff(
145+
array_keys($testsInGroup),
146+
$testsInGroupAndNotInAnySuite
147+
);
148+
149+
foreach ($testsInGroupAndInAnySuite as $testInGroupAndInAnySuite) {
150+
$suiteName = $testsInSuites[$testInGroupAndInAnySuite][0];
151+
if (array_search($suiteName, $suites) !== false) {
152+
// Suite is already being called to run in its entirety, do not filter list
153+
continue;
154+
}
155+
$result['suites'][$suiteName][] = $testInGroupAndInAnySuite;
156+
}
157+
158+
$result['tests'] = array_merge(
159+
$result['tests'],
160+
$testsInGroupAndNotInAnySuite
161+
);
162+
}
163+
164+
if (empty($result['tests'])) {
165+
$result['tests'] = null;
166+
}
167+
if (empty($result['suites'])) {
168+
$result['suites'] = null;
169+
}
170+
171+
$json = json_encode($result);
172+
return $json;
173+
}
106174
}

src/Magento/FunctionalTestingFramework/Console/RunTestGroupCommand.php

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -111,32 +111,4 @@ function ($type, $buffer) use ($output) {
111111
}
112112
);
113113
}
114-
115-
/**
116-
* Returns a json string to be used as an argument for generation of a group or suite
117-
*
118-
* @param array $groups
119-
* @return string
120-
* @throws \Magento\FunctionalTestingFramework\Exceptions\XmlException
121-
*/
122-
private function getGroupAndSuiteConfiguration(array $groups)
123-
{
124-
$testConfiguration['tests'] = [];
125-
$testConfiguration['suites'] = null;
126-
$availableSuites = SuiteObjectHandler::getInstance()->getAllObjects();
127-
128-
foreach ($groups as $group) {
129-
if (array_key_exists($group, $availableSuites)) {
130-
$testConfiguration['suites'][$group] = [];
131-
}
132-
133-
$testConfiguration['tests'] = array_merge(
134-
$testConfiguration['tests'],
135-
array_keys(TestObjectHandler::getInstance()->getTestsByGroup($group))
136-
);
137-
}
138-
139-
$testConfigurationJson = json_encode($testConfiguration);
140-
return $testConfigurationJson;
141-
}
142114
}

0 commit comments

Comments
 (0)