Skip to content

Commit 888d7cd

Browse files
MFTF-33305: Eliminate AspectMock from ObjectExtensionUtilTest
1 parent b6555a1 commit 888d7cd

File tree

2 files changed

+112
-74
lines changed

2 files changed

+112
-74
lines changed

dev/tests/unit/Magento/FunctionalTestFramework/Test/Util/ObjectExtensionUtilTest.php

Lines changed: 90 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,40 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace tests\unit\Magento\FunctionalTestFramework\Test\Util;
89

9-
use AspectMock\Proxy\Verifier;
10-
use AspectMock\Test as AspectMock;
11-
use Magento\FunctionalTestingFramework\ObjectManager\ObjectManager;
12-
use Magento\FunctionalTestingFramework\ObjectManagerFactory;
10+
use Exception;
11+
use Magento\FunctionalTestingFramework\ObjectManager;
1312
use Magento\FunctionalTestingFramework\Test\Handlers\ActionGroupObjectHandler;
1413
use Magento\FunctionalTestingFramework\Test\Handlers\TestObjectHandler;
1514
use Magento\FunctionalTestingFramework\Test\Parsers\ActionGroupDataParser;
1615
use Magento\FunctionalTestingFramework\Test\Parsers\TestDataParser;
17-
use Magento\FunctionalTestingFramework\Util\Logger\LoggingUtil;
18-
use Monolog\Handler\TestHandler;
19-
use Monolog\Logger;
2016
use PHPUnit\Framework\TestCase;
17+
use ReflectionProperty;
18+
use tests\unit\Util\MockModuleResolverBuilder;
2119
use tests\unit\Util\TestDataArrayBuilder;
2220
use tests\unit\Util\TestLoggingUtil;
23-
use tests\unit\Util\MockModuleResolverBuilder;
2421

2522
class ObjectExtensionUtilTest extends TestCase
2623
{
2724
/**
28-
* Before test functionality
25+
* Before test functionality.
26+
*
2927
* @return void
28+
* @throws Exception
3029
*/
31-
public function setUp(): void
30+
protected function setUp(): void
3231
{
3332
TestLoggingUtil::getInstance()->setMockLoggingUtil();
3433
$resolverMock = new MockModuleResolverBuilder();
3534
$resolverMock->setup();
3635
}
3736

3837
/**
39-
* After class functionality
38+
* After class functionality.
39+
*
4040
* @return void
4141
*/
4242
public static function tearDownAfterClass(): void
@@ -45,10 +45,12 @@ public static function tearDownAfterClass(): void
4545
}
4646

4747
/**
48-
* Tests generating a test that extends another test
49-
* @throws \Exception
48+
* Tests generating a test that extends another test.
49+
*
50+
* @return void
51+
* @throws Exception
5052
*/
51-
public function testGenerateExtendedTest()
53+
public function testGenerateExtendedTest(): void
5254
{
5355
$mockActions = [
5456
"mockStep" => ["nodeName" => "mockNode", "stepKey" => "mockStep"]
@@ -86,10 +88,12 @@ public function testGenerateExtendedTest()
8688
}
8789

8890
/**
89-
* Tests generating a test that extends another test
90-
* @throws \Exception
91+
* Tests generating a test that extends another test.
92+
*
93+
* @return void
94+
* @throws Exception
9195
*/
92-
public function testGenerateExtendedWithHooks()
96+
public function testGenerateExtendedWithHooks(): void
9397
{
9498
$mockBeforeHooks = [
9599
"beforeHookAction" => ["nodeName" => "mockNodeBefore", "stepKey" => "mockStepBefore"]
@@ -132,10 +136,12 @@ public function testGenerateExtendedWithHooks()
132136
}
133137

134138
/**
135-
* Tests generating a test that extends another test
136-
* @throws \Exception
139+
* Tests generating a test that extends another test.
140+
*
141+
* @return void
142+
* @throws Exception
137143
*/
138-
public function testExtendedTestNoParent()
144+
public function testExtendedTestNoParent(): void
139145
{
140146
$testDataArrayBuilder = new TestDataArrayBuilder();
141147
$mockExtendedTest = $testDataArrayBuilder
@@ -158,10 +164,12 @@ public function testExtendedTestNoParent()
158164
}
159165

160166
/**
161-
* Tests generating a test that extends another test
162-
* @throws \Exception
167+
* Tests generating a test that extends another test.
168+
*
169+
* @return void
170+
* @throws Exception
163171
*/
164-
public function testExtendingExtendedTest()
172+
public function testExtendingExtendedTest(): void
165173
{
166174
$testDataArrayBuilder = new TestDataArrayBuilder();
167175
$mockParentTest = $testDataArrayBuilder
@@ -200,10 +208,12 @@ public function testExtendingExtendedTest()
200208
}
201209

202210
/**
203-
* Tests generating an action group that extends another action group
204-
* @throws \Exception
211+
* Tests generating an action group that extends another action group.
212+
*
213+
* @return void
214+
* @throws Exception
205215
*/
206-
public function testGenerateExtendedActionGroup()
216+
public function testGenerateExtendedActionGroup(): void
207217
{
208218
$mockSimpleActionGroup = [
209219
"nodeName" => "actionGroup",
@@ -259,10 +269,12 @@ public function testGenerateExtendedActionGroup()
259269
}
260270

261271
/**
262-
* Tests generating an action group that extends an action group that does not exist
263-
* @throws \Exception
272+
* Tests generating an action group that extends an action group that does not exist.
273+
*
274+
* @return void
275+
* @throws Exception
264276
*/
265-
public function testGenerateExtendedActionGroupNoParent()
277+
public function testGenerateExtendedActionGroupNoParent(): void
266278
{
267279
$mockExtendedActionGroup = [
268280
"nodeName" => "actionGroup",
@@ -292,10 +304,12 @@ public function testGenerateExtendedActionGroupNoParent()
292304
}
293305

294306
/**
295-
* Tests generating an action group that extends another action group that is already extended
296-
* @throws \Exception
307+
* Tests generating an action group that extends another action group that is already extended.
308+
*
309+
* @return void
310+
* @throws Exception
297311
*/
298-
public function testExtendingExtendedActionGroup()
312+
public function testExtendingExtendedActionGroup(): void
299313
{
300314
$mockParentActionGroup = [
301315
"nodeName" => "actionGroup",
@@ -333,7 +347,7 @@ public function testExtendingExtendedActionGroup()
333347
// parse and generate test object with mocked data
334348
try {
335349
ActionGroupObjectHandler::getInstance()->getObject('mockExtendedActionGroup');
336-
} catch (\Exception $e) {
350+
} catch (Exception $e) {
337351
// validate log statement
338352
TestLoggingUtil::getInstance()->validateMockLogStatement(
339353
'error',
@@ -347,11 +361,12 @@ public function testExtendingExtendedActionGroup()
347361
}
348362

349363
/**
350-
* Tests generating a test that extends a skipped parent test
364+
* Tests generating a test that extends a skipped parent test.
351365
*
352-
* @throws \Exception
366+
* @return void
367+
* @throws Exception
353368
*/
354-
public function testExtendedTestSkippedParent()
369+
public function testExtendedTestSkippedParent(): void
355370
{
356371
$testDataArrayBuilder = new TestDataArrayBuilder();
357372
$mockParentTest = $testDataArrayBuilder
@@ -384,43 +399,55 @@ public function testExtendedTestSkippedParent()
384399
/**
385400
* Function used to set mock for parser return and force init method to run between tests.
386401
*
387-
* @param array $testData
388-
* @throws \Exception
402+
* @param array|null $testData
403+
* @param array|null $actionGroupData
404+
*
405+
* @return void
406+
* @throws Exception
389407
*/
390-
private function setMockTestOutput($testData = null, $actionGroupData = null)
408+
private function setMockTestOutput(array $testData = null, array $actionGroupData = null): void
391409
{
392410
// clear test object handler value to inject parsed content
393-
$property = new \ReflectionProperty(TestObjectHandler::class, 'testObjectHandler');
411+
$property = new ReflectionProperty(TestObjectHandler::class, 'testObjectHandler');
394412
$property->setAccessible(true);
395413
$property->setValue(null);
396414

397415
// clear test object handler value to inject parsed content
398-
$property = new \ReflectionProperty(ActionGroupObjectHandler::class, 'instance');
416+
$property = new ReflectionProperty(ActionGroupObjectHandler::class, 'instance');
399417
$property->setAccessible(true);
400418
$property->setValue(null);
401419

402-
$mockDataParser = AspectMock::double(TestDataParser::class, ['readTestData' => $testData])->make();
403-
$mockActionGroupParser = AspectMock::double(
404-
ActionGroupDataParser::class,
405-
['readActionGroupData' => $actionGroupData]
406-
)->make();
407-
$instance = AspectMock::double(
408-
ObjectManager::class,
409-
[
410-
'create' => function ($className) use (
411-
$mockDataParser,
412-
$mockActionGroupParser
413-
) {
414-
if ($className == TestDataParser::class) {
415-
return $mockDataParser;
416-
}
417-
if ($className == ActionGroupDataParser::class) {
418-
return $mockActionGroupParser;
420+
$mockDataParser = $this->createMock(TestDataParser::class);
421+
$mockDataParser
422+
->method('readTestData')
423+
->willReturn($testData);
424+
425+
$mockActionGroupParser = $this->createMock(ActionGroupDataParser::class);
426+
$mockActionGroupParser
427+
->method('readActionGroupData')
428+
->willReturn($actionGroupData);
429+
430+
$instance = $this->createMock(ObjectManager::class);
431+
$instance
432+
->method('create')
433+
->will(
434+
$this->returnCallback(
435+
function ($className) use ($mockDataParser, $mockActionGroupParser) {
436+
if ($className === TestDataParser::class) {
437+
return $mockDataParser;
438+
}
439+
440+
if ($className === ActionGroupDataParser::class) {
441+
return $mockActionGroupParser;
442+
}
443+
444+
return null;
419445
}
420-
}
421-
]
422-
)->make();
423-
// bypass the private constructor
424-
AspectMock::double(ObjectManagerFactory::class, ['getObjectManager' => $instance]);
446+
)
447+
);
448+
// clear object manager value to inject expected instance
449+
$property = new ReflectionProperty(ObjectManager::class, 'instance');
450+
$property->setAccessible(true);
451+
$property->setValue($instance);
425452
}
426453
}

dev/tests/unit/Util/MockModuleResolverBuilder.php

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,48 @@
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\Util;
79

810
use AspectMock\Test as AspectMock;
11+
use Exception;
12+
use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig;
913
use Magento\FunctionalTestingFramework\ObjectManager;
10-
use Magento\FunctionalTestingFramework\ObjectManagerFactory;
1114
use Magento\FunctionalTestingFramework\Util\ModuleResolver;
12-
use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig;
15+
use ReflectionProperty;
1316

1417
class MockModuleResolverBuilder
1518
{
1619
/**
17-
* Default paths for mock ModuleResolver
20+
* Default paths for mock ModuleResolver.
1821
*
1922
* @var array
2023
*/
2124
private $defaultPaths = ['Magento_Module' => '/base/path/some/other/path/Magento/Module'];
2225

2326
/**
24-
* Mock ModuleResolver builder
27+
* Mock ModuleResolver builder.
28+
*
29+
* @param array|null $paths
2530
*
26-
* @param array $paths
2731
* @return void
28-
* @throws \Exception
32+
* @throws Exception
2933
*/
30-
public function setup($paths = null)
34+
public function setup(array $paths = null): void
3135
{
3236
if (empty($paths)) {
3337
$paths = $this->defaultPaths;
3438
}
3539

3640
$mockConfig = AspectMock::double(MftfApplicationConfig::class, ['forceGenerateEnabled' => false]);
3741
$instance = AspectMock::double(ObjectManager::class, ['create' => $mockConfig->make(), 'get' => null])->make();
38-
AspectMock::double(ObjectManagerFactory::class, ['getObjectManager' => $instance]);
42+
// clear object manager value to inject expected instance
43+
$property = new ReflectionProperty(ObjectManager::class, 'instance');
44+
$property->setAccessible(true);
45+
$property->setValue($instance);
3946

40-
$property = new \ReflectionProperty(ModuleResolver::class, 'instance');
47+
$property = new ReflectionProperty(ModuleResolver::class, 'instance');
4148
$property->setAccessible(true);
4249
$property->setValue(null);
4350

@@ -51,10 +58,14 @@ public function setup($paths = null)
5158
);
5259
$instance = AspectMock::double(ObjectManager::class, ['create' => $mockResolver->make(), 'get' => null])
5360
->make();
54-
AspectMock::double(ObjectManagerFactory::class, ['getObjectManager' => $instance]);
61+
62+
// clear object manager value to inject expected instance
63+
$property = new ReflectionProperty(ObjectManager::class, 'instance');
64+
$property->setAccessible(true);
65+
$property->setValue($instance);
5566

5667
$resolver = ModuleResolver::getInstance();
57-
$property = new \ReflectionProperty(ModuleResolver::class, 'enabledModuleNameAndPaths');
68+
$property = new ReflectionProperty(ModuleResolver::class, 'enabledModuleNameAndPaths');
5869
$property->setAccessible(true);
5970
$property->setValue($resolver, $paths);
6071
}

0 commit comments

Comments
 (0)