Skip to content

Commit bbec23a

Browse files
33292: aspect mock replacement, unit test refactoring
1 parent a449f07 commit bbec23a

File tree

1 file changed

+83
-32
lines changed

1 file changed

+83
-32
lines changed

dev/tests/unit/Magento/FunctionalTestFramework/Console/BaseGenerateCommandTest.php

Lines changed: 83 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,46 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace tests\unit\Magento\FunctionalTestFramework\Console;
79

8-
use PHPUnit\Framework\TestCase;
10+
use Exception;
911
use Magento\FunctionalTestingFramework\Console\BaseGenerateCommand;
10-
use Magento\FunctionalTestingFramework\Suite\Objects\SuiteObject;
1112
use Magento\FunctionalTestingFramework\Suite\Handlers\SuiteObjectHandler;
12-
use Magento\FunctionalTestingFramework\Test\Objects\TestObject;
13+
use Magento\FunctionalTestingFramework\Suite\Objects\SuiteObject;
1314
use Magento\FunctionalTestingFramework\Test\Handlers\TestObjectHandler;
15+
use Magento\FunctionalTestingFramework\Test\Objects\TestObject;
16+
use PHPUnit\Framework\TestCase;
17+
use ReflectionClass;
18+
use ReflectionException;
19+
use ReflectionProperty;
1420

1521
class BaseGenerateCommandTest extends TestCase
1622
{
17-
public function tearDown(): void
23+
/**
24+
* @inheritDoc
25+
*/
26+
protected function tearDown(): void
1827
{
1928
$handler = TestObjectHandler::getInstance();
20-
$property = new \ReflectionProperty(TestObjectHandler::class, 'tests');
21-
$property->setAccessible(true);
22-
$property->setValue($handler, []);
29+
$testsProperty = new ReflectionProperty(TestObjectHandler::class, 'tests');
30+
$testsProperty->setAccessible(true);
31+
$testsProperty->setValue($handler, []);
32+
$testObjectHandlerProperty = new ReflectionProperty(TestObjectHandler::class, 'testObjectHandler');
33+
$testObjectHandlerProperty->setAccessible(true);
34+
$testObjectHandlerProperty->setValue($handler);
2335

2436
$handler = SuiteObjectHandler::getInstance();
25-
$property = new \ReflectionProperty(SuiteObjectHandler::class, 'suiteObjects');
26-
$property->setAccessible(true);
27-
$property->setValue($handler, []);
37+
$suiteObjectsProperty = new ReflectionProperty(SuiteObjectHandler::class, 'suiteObjects');
38+
$suiteObjectsProperty->setAccessible(true);
39+
$suiteObjectsProperty->setValue($handler, []);
40+
$suiteObjectHandlerProperty = new ReflectionProperty(SuiteObjectHandler::class, 'instance');
41+
$suiteObjectHandlerProperty->setAccessible(true);
42+
$suiteObjectHandlerProperty->setValue($handler);
2843
}
29-
public function testOneTestOneSuiteConfig()
44+
45+
public function testOneTestOneSuiteConfig(): void
3046
{
3147
$testOne = new TestObject('Test1', [], [], []);
3248
$suiteOne = new SuiteObject('Suite1', ['Test1' => $testOne], [], []);
@@ -41,7 +57,7 @@ public function testOneTestOneSuiteConfig()
4157
$this->assertEquals($expected, $actual);
4258
}
4359

44-
public function testOneTestTwoSuitesConfig()
60+
public function testOneTestTwoSuitesConfig(): void
4561
{
4662
$testOne = new TestObject('Test1', [], [], []);
4763
$suiteOne = new SuiteObject('Suite1', ['Test1' => $testOne], [], []);
@@ -57,7 +73,7 @@ public function testOneTestTwoSuitesConfig()
5773
$this->assertEquals($expected, $actual);
5874
}
5975

60-
public function testOneTestOneGroup()
76+
public function testOneTestOneGroup(): void
6177
{
6278
$testOne = new TestObject('Test1', [], ['group' => ['Group1']], []);
6379

@@ -71,7 +87,7 @@ public function testOneTestOneGroup()
7187
$this->assertEquals($expected, $actual);
7288
}
7389

74-
public function testThreeTestsTwoGroup()
90+
public function testThreeTestsTwoGroup(): void
7591
{
7692
$testOne = new TestObject('Test1', [], ['group' => ['Group1']], []);
7793
$testTwo = new TestObject('Test2', [], ['group' => ['Group1']], []);
@@ -87,7 +103,7 @@ public function testThreeTestsTwoGroup()
87103
$this->assertEquals($expected, $actual);
88104
}
89105

90-
public function testOneTestOneSuiteOneGroupConfig()
106+
public function testOneTestOneSuiteOneGroupConfig(): void
91107
{
92108
$testOne = new TestObject('Test1', [], ['group' => ['Group1']], []);
93109
$suiteOne = new SuiteObject('Suite1', ['Test1' => $testOne], [], []);
@@ -102,7 +118,7 @@ public function testOneTestOneSuiteOneGroupConfig()
102118
$this->assertEquals($expected, $actual);
103119
}
104120

105-
public function testTwoTestOneSuiteTwoGroupConfig()
121+
public function testTwoTestOneSuiteTwoGroupConfig(): void
106122
{
107123
$testOne = new TestObject('Test1', [], ['group' => ['Group1']], []);
108124
$testTwo = new TestObject('Test2', [], ['group' => ['Group2']], []);
@@ -118,7 +134,7 @@ public function testTwoTestOneSuiteTwoGroupConfig()
118134
$this->assertEquals($expected, $actual);
119135
}
120136

121-
public function testTwoTestTwoSuiteOneGroupConfig()
137+
public function testTwoTestTwoSuiteOneGroupConfig(): void
122138
{
123139
$testOne = new TestObject('Test1', [], ['group' => ['Group1']], []);
124140
$testTwo = new TestObject('Test2', [], ['group' => ['Group1']], []);
@@ -137,10 +153,12 @@ public function testTwoTestTwoSuiteOneGroupConfig()
137153

138154
/**
139155
* Test specific usecase of a test that is in a group with the group being called along with the suite
140-
* i.e. run:group Group1 Suite1
141-
* @throws \Exception
156+
* i.e. run:group Group1 Suite1.
157+
*
158+
* @return void
159+
* @throws Exception
142160
*/
143-
public function testThreeTestOneSuiteOneGroupMix()
161+
public function testThreeTestOneSuiteOneGroupMix(): void
144162
{
145163
$testOne = new TestObject('Test1', [], [], []);
146164
$testTwo = new TestObject('Test2', [], [], []);
@@ -162,7 +180,7 @@ public function testThreeTestOneSuiteOneGroupMix()
162180
$this->assertEquals($expected, $actual);
163181
}
164182

165-
public function testSuiteToTestSyntax()
183+
public function testSuiteToTestSyntax(): void
166184
{
167185
$testOne = new TestObject('Test1', [], [], []);
168186
$suiteOne = new SuiteObject(
@@ -181,49 +199,82 @@ public function testSuiteToTestSyntax()
181199
}
182200

183201
/**
184-
* Mock handlers to skip parsing
202+
* Mock handlers to skip parsing.
203+
*
185204
* @param array $testArray
186205
* @param array $suiteArray
187-
* @throws \Exception
206+
*
207+
* @return void
208+
* @throws Exception
188209
*/
189-
public function mockHandlers($testArray, $suiteArray)
210+
public function mockHandlers(array $testArray, array $suiteArray): void
190211
{
212+
// bypass the initTestData method
213+
$testObjectHandlerClass = new ReflectionClass(TestObjectHandler::class);
214+
$constructor = $testObjectHandlerClass->getConstructor();
215+
$constructor->setAccessible(true);
216+
$testObjectHandlerObject = $testObjectHandlerClass->newInstanceWithoutConstructor();
217+
$constructor->invoke($testObjectHandlerObject);
218+
219+
$testObjectHandlerProperty = new ReflectionProperty(TestObjectHandler::class, 'testObjectHandler');
220+
$testObjectHandlerProperty->setAccessible(true);
221+
$testObjectHandlerProperty->setValue($testObjectHandlerObject);
222+
191223
$handler = TestObjectHandler::getInstance();
192-
$property = new \ReflectionProperty(TestObjectHandler::class, 'tests');
224+
$property = new ReflectionProperty(TestObjectHandler::class, 'tests');
193225
$property->setAccessible(true);
194226
$property->setValue($handler, $testArray);
195227

228+
// bypass the initTestData method
229+
$suiteObjectHandlerClass = new ReflectionClass(SuiteObjectHandler::class);
230+
$constructor = $suiteObjectHandlerClass->getConstructor();
231+
$constructor->setAccessible(true);
232+
$suiteObjectHandlerObject = $suiteObjectHandlerClass->newInstanceWithoutConstructor();
233+
$constructor->invoke($suiteObjectHandlerObject);
234+
235+
$suiteObjectHandlerProperty = new ReflectionProperty(SuiteObjectHandler::class, 'instance');
236+
$suiteObjectHandlerProperty->setAccessible(true);
237+
$suiteObjectHandlerProperty->setValue($suiteObjectHandlerObject);
238+
196239
$handler = SuiteObjectHandler::getInstance();
197-
$property = new \ReflectionProperty(SuiteObjectHandler::class, 'suiteObjects');
240+
$property = new ReflectionProperty(SuiteObjectHandler::class, 'suiteObjects');
198241
$property->setAccessible(true);
199242
$property->setValue($handler, $suiteArray);
200243
}
201244

202245
/**
203-
* Changes visibility and runs getTestAndSuiteConfiguration
246+
* Changes visibility and runs getTestAndSuiteConfiguration.
247+
*
204248
* @param array $testArray
249+
*
205250
* @return string
251+
* @throws ReflectionException
206252
*/
207-
public function callTestConfig($testArray)
253+
public function callTestConfig(array $testArray): string
208254
{
209255
$command = new BaseGenerateCommand();
210-
$class = new \ReflectionClass($command);
256+
$class = new ReflectionClass($command);
211257
$method = $class->getMethod('getTestAndSuiteConfiguration');
212258
$method->setAccessible(true);
259+
213260
return $method->invokeArgs($command, [$testArray]);
214261
}
215262

216263
/**
217-
* Changes visibility and runs getGroupAndSuiteConfiguration
264+
* Changes visibility and runs getGroupAndSuiteConfiguration.
265+
*
218266
* @param array $groupArray
267+
*
219268
* @return string
269+
* @throws ReflectionException
220270
*/
221-
public function callGroupConfig($groupArray)
271+
public function callGroupConfig(array $groupArray): string
222272
{
223273
$command = new BaseGenerateCommand();
224-
$class = new \ReflectionClass($command);
274+
$class = new ReflectionClass($command);
225275
$method = $class->getMethod('getGroupAndSuiteConfiguration');
226276
$method->setAccessible(true);
277+
227278
return $method->invokeArgs($command, [$groupArray]);
228279
}
229280
}

0 commit comments

Comments
 (0)