From 888d7cde0f1e3ab1a7a85a18ce26963918e76cc7 Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Sun, 4 Jul 2021 19:01:53 +0300 Subject: [PATCH 1/4] MFTF-33305: Eliminate AspectMock from ObjectExtensionUtilTest --- .../Test/Util/ObjectExtensionUtilTest.php | 153 ++++++++++-------- .../unit/Util/MockModuleResolverBuilder.php | 33 ++-- 2 files changed, 112 insertions(+), 74 deletions(-) diff --git a/dev/tests/unit/Magento/FunctionalTestFramework/Test/Util/ObjectExtensionUtilTest.php b/dev/tests/unit/Magento/FunctionalTestFramework/Test/Util/ObjectExtensionUtilTest.php index 6bf2da577..fc1893e0b 100644 --- a/dev/tests/unit/Magento/FunctionalTestFramework/Test/Util/ObjectExtensionUtilTest.php +++ b/dev/tests/unit/Magento/FunctionalTestFramework/Test/Util/ObjectExtensionUtilTest.php @@ -3,32 +3,31 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +declare(strict_types=1); namespace tests\unit\Magento\FunctionalTestFramework\Test\Util; -use AspectMock\Proxy\Verifier; -use AspectMock\Test as AspectMock; -use Magento\FunctionalTestingFramework\ObjectManager\ObjectManager; -use Magento\FunctionalTestingFramework\ObjectManagerFactory; +use Exception; +use Magento\FunctionalTestingFramework\ObjectManager; use Magento\FunctionalTestingFramework\Test\Handlers\ActionGroupObjectHandler; use Magento\FunctionalTestingFramework\Test\Handlers\TestObjectHandler; use Magento\FunctionalTestingFramework\Test\Parsers\ActionGroupDataParser; use Magento\FunctionalTestingFramework\Test\Parsers\TestDataParser; -use Magento\FunctionalTestingFramework\Util\Logger\LoggingUtil; -use Monolog\Handler\TestHandler; -use Monolog\Logger; use PHPUnit\Framework\TestCase; +use ReflectionProperty; +use tests\unit\Util\MockModuleResolverBuilder; use tests\unit\Util\TestDataArrayBuilder; use tests\unit\Util\TestLoggingUtil; -use tests\unit\Util\MockModuleResolverBuilder; class ObjectExtensionUtilTest extends TestCase { /** - * Before test functionality + * Before test functionality. + * * @return void + * @throws Exception */ - public function setUp(): void + protected function setUp(): void { TestLoggingUtil::getInstance()->setMockLoggingUtil(); $resolverMock = new MockModuleResolverBuilder(); @@ -36,7 +35,8 @@ public function setUp(): void } /** - * After class functionality + * After class functionality. + * * @return void */ public static function tearDownAfterClass(): void @@ -45,10 +45,12 @@ public static function tearDownAfterClass(): void } /** - * Tests generating a test that extends another test - * @throws \Exception + * Tests generating a test that extends another test. + * + * @return void + * @throws Exception */ - public function testGenerateExtendedTest() + public function testGenerateExtendedTest(): void { $mockActions = [ "mockStep" => ["nodeName" => "mockNode", "stepKey" => "mockStep"] @@ -86,10 +88,12 @@ public function testGenerateExtendedTest() } /** - * Tests generating a test that extends another test - * @throws \Exception + * Tests generating a test that extends another test. + * + * @return void + * @throws Exception */ - public function testGenerateExtendedWithHooks() + public function testGenerateExtendedWithHooks(): void { $mockBeforeHooks = [ "beforeHookAction" => ["nodeName" => "mockNodeBefore", "stepKey" => "mockStepBefore"] @@ -132,10 +136,12 @@ public function testGenerateExtendedWithHooks() } /** - * Tests generating a test that extends another test - * @throws \Exception + * Tests generating a test that extends another test. + * + * @return void + * @throws Exception */ - public function testExtendedTestNoParent() + public function testExtendedTestNoParent(): void { $testDataArrayBuilder = new TestDataArrayBuilder(); $mockExtendedTest = $testDataArrayBuilder @@ -158,10 +164,12 @@ public function testExtendedTestNoParent() } /** - * Tests generating a test that extends another test - * @throws \Exception + * Tests generating a test that extends another test. + * + * @return void + * @throws Exception */ - public function testExtendingExtendedTest() + public function testExtendingExtendedTest(): void { $testDataArrayBuilder = new TestDataArrayBuilder(); $mockParentTest = $testDataArrayBuilder @@ -200,10 +208,12 @@ public function testExtendingExtendedTest() } /** - * Tests generating an action group that extends another action group - * @throws \Exception + * Tests generating an action group that extends another action group. + * + * @return void + * @throws Exception */ - public function testGenerateExtendedActionGroup() + public function testGenerateExtendedActionGroup(): void { $mockSimpleActionGroup = [ "nodeName" => "actionGroup", @@ -259,10 +269,12 @@ public function testGenerateExtendedActionGroup() } /** - * Tests generating an action group that extends an action group that does not exist - * @throws \Exception + * Tests generating an action group that extends an action group that does not exist. + * + * @return void + * @throws Exception */ - public function testGenerateExtendedActionGroupNoParent() + public function testGenerateExtendedActionGroupNoParent(): void { $mockExtendedActionGroup = [ "nodeName" => "actionGroup", @@ -292,10 +304,12 @@ public function testGenerateExtendedActionGroupNoParent() } /** - * Tests generating an action group that extends another action group that is already extended - * @throws \Exception + * Tests generating an action group that extends another action group that is already extended. + * + * @return void + * @throws Exception */ - public function testExtendingExtendedActionGroup() + public function testExtendingExtendedActionGroup(): void { $mockParentActionGroup = [ "nodeName" => "actionGroup", @@ -333,7 +347,7 @@ public function testExtendingExtendedActionGroup() // parse and generate test object with mocked data try { ActionGroupObjectHandler::getInstance()->getObject('mockExtendedActionGroup'); - } catch (\Exception $e) { + } catch (Exception $e) { // validate log statement TestLoggingUtil::getInstance()->validateMockLogStatement( 'error', @@ -347,11 +361,12 @@ public function testExtendingExtendedActionGroup() } /** - * Tests generating a test that extends a skipped parent test + * Tests generating a test that extends a skipped parent test. * - * @throws \Exception + * @return void + * @throws Exception */ - public function testExtendedTestSkippedParent() + public function testExtendedTestSkippedParent(): void { $testDataArrayBuilder = new TestDataArrayBuilder(); $mockParentTest = $testDataArrayBuilder @@ -384,43 +399,55 @@ public function testExtendedTestSkippedParent() /** * Function used to set mock for parser return and force init method to run between tests. * - * @param array $testData - * @throws \Exception + * @param array|null $testData + * @param array|null $actionGroupData + * + * @return void + * @throws Exception */ - private function setMockTestOutput($testData = null, $actionGroupData = null) + private function setMockTestOutput(array $testData = null, array $actionGroupData = null): void { // clear test object handler value to inject parsed content - $property = new \ReflectionProperty(TestObjectHandler::class, 'testObjectHandler'); + $property = new ReflectionProperty(TestObjectHandler::class, 'testObjectHandler'); $property->setAccessible(true); $property->setValue(null); // clear test object handler value to inject parsed content - $property = new \ReflectionProperty(ActionGroupObjectHandler::class, 'instance'); + $property = new ReflectionProperty(ActionGroupObjectHandler::class, 'instance'); $property->setAccessible(true); $property->setValue(null); - $mockDataParser = AspectMock::double(TestDataParser::class, ['readTestData' => $testData])->make(); - $mockActionGroupParser = AspectMock::double( - ActionGroupDataParser::class, - ['readActionGroupData' => $actionGroupData] - )->make(); - $instance = AspectMock::double( - ObjectManager::class, - [ - 'create' => function ($className) use ( - $mockDataParser, - $mockActionGroupParser - ) { - if ($className == TestDataParser::class) { - return $mockDataParser; - } - if ($className == ActionGroupDataParser::class) { - return $mockActionGroupParser; + $mockDataParser = $this->createMock(TestDataParser::class); + $mockDataParser + ->method('readTestData') + ->willReturn($testData); + + $mockActionGroupParser = $this->createMock(ActionGroupDataParser::class); + $mockActionGroupParser + ->method('readActionGroupData') + ->willReturn($actionGroupData); + + $instance = $this->createMock(ObjectManager::class); + $instance + ->method('create') + ->will( + $this->returnCallback( + function ($className) use ($mockDataParser, $mockActionGroupParser) { + if ($className === TestDataParser::class) { + return $mockDataParser; + } + + if ($className === ActionGroupDataParser::class) { + return $mockActionGroupParser; + } + + return null; } - } - ] - )->make(); - // bypass the private constructor - AspectMock::double(ObjectManagerFactory::class, ['getObjectManager' => $instance]); + ) + ); + // clear object manager value to inject expected instance + $property = new ReflectionProperty(ObjectManager::class, 'instance'); + $property->setAccessible(true); + $property->setValue($instance); } } diff --git a/dev/tests/unit/Util/MockModuleResolverBuilder.php b/dev/tests/unit/Util/MockModuleResolverBuilder.php index 0e1b6fc31..8bf30aa00 100644 --- a/dev/tests/unit/Util/MockModuleResolverBuilder.php +++ b/dev/tests/unit/Util/MockModuleResolverBuilder.php @@ -3,31 +3,35 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +declare(strict_types=1); + namespace tests\unit\Util; use AspectMock\Test as AspectMock; +use Exception; +use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig; use Magento\FunctionalTestingFramework\ObjectManager; -use Magento\FunctionalTestingFramework\ObjectManagerFactory; use Magento\FunctionalTestingFramework\Util\ModuleResolver; -use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig; +use ReflectionProperty; class MockModuleResolverBuilder { /** - * Default paths for mock ModuleResolver + * Default paths for mock ModuleResolver. * * @var array */ private $defaultPaths = ['Magento_Module' => '/base/path/some/other/path/Magento/Module']; /** - * Mock ModuleResolver builder + * Mock ModuleResolver builder. + * + * @param array|null $paths * - * @param array $paths * @return void - * @throws \Exception + * @throws Exception */ - public function setup($paths = null) + public function setup(array $paths = null): void { if (empty($paths)) { $paths = $this->defaultPaths; @@ -35,9 +39,12 @@ public function setup($paths = null) $mockConfig = AspectMock::double(MftfApplicationConfig::class, ['forceGenerateEnabled' => false]); $instance = AspectMock::double(ObjectManager::class, ['create' => $mockConfig->make(), 'get' => null])->make(); - AspectMock::double(ObjectManagerFactory::class, ['getObjectManager' => $instance]); + // clear object manager value to inject expected instance + $property = new ReflectionProperty(ObjectManager::class, 'instance'); + $property->setAccessible(true); + $property->setValue($instance); - $property = new \ReflectionProperty(ModuleResolver::class, 'instance'); + $property = new ReflectionProperty(ModuleResolver::class, 'instance'); $property->setAccessible(true); $property->setValue(null); @@ -51,10 +58,14 @@ public function setup($paths = null) ); $instance = AspectMock::double(ObjectManager::class, ['create' => $mockResolver->make(), 'get' => null]) ->make(); - AspectMock::double(ObjectManagerFactory::class, ['getObjectManager' => $instance]); + + // clear object manager value to inject expected instance + $property = new ReflectionProperty(ObjectManager::class, 'instance'); + $property->setAccessible(true); + $property->setValue($instance); $resolver = ModuleResolver::getInstance(); - $property = new \ReflectionProperty(ModuleResolver::class, 'enabledModuleNameAndPaths'); + $property = new ReflectionProperty(ModuleResolver::class, 'enabledModuleNameAndPaths'); $property->setAccessible(true); $property->setValue($resolver, $paths); } From ef4f9ea708446c2baff77304df1a0549b1ccaedf Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Mon, 5 Jul 2021 11:41:38 +0300 Subject: [PATCH 2/4] MFTF-33305: Clearing AspectMock mocking before each test --- dev/tests/unit/Util/MagentoTestCase.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/dev/tests/unit/Util/MagentoTestCase.php b/dev/tests/unit/Util/MagentoTestCase.php index 7760acfc6..c34236fc1 100644 --- a/dev/tests/unit/Util/MagentoTestCase.php +++ b/dev/tests/unit/Util/MagentoTestCase.php @@ -19,6 +19,9 @@ public static function setUpBeforeClass(): void if (!self::fileExists(DOCS_OUTPUT_DIR)) { mkdir(DOCS_OUTPUT_DIR, 0755, true); } + // Should be used to clean AspectMock mocking before using PHPUnit mocking and Reflection. + AspectMock::clean(); + parent::setUpBeforeClass(); parent::setUpBeforeClass(); } From 15a240ec7a5875846088e2d534dd47cbab42bb5d Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Wed, 21 Jul 2021 08:35:19 +0300 Subject: [PATCH 3/4] 33305: Removed an extra line --- dev/tests/unit/Util/MagentoTestCase.php | 1 - 1 file changed, 1 deletion(-) diff --git a/dev/tests/unit/Util/MagentoTestCase.php b/dev/tests/unit/Util/MagentoTestCase.php index c34236fc1..bb38a0b5c 100644 --- a/dev/tests/unit/Util/MagentoTestCase.php +++ b/dev/tests/unit/Util/MagentoTestCase.php @@ -22,7 +22,6 @@ public static function setUpBeforeClass(): void // Should be used to clean AspectMock mocking before using PHPUnit mocking and Reflection. AspectMock::clean(); parent::setUpBeforeClass(); - parent::setUpBeforeClass(); } /** From 1fdb5b9b96dd7cd0c668c2f30b06a7bd2e50bb95 Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Thu, 22 Jul 2021 12:36:17 +0300 Subject: [PATCH 4/4] 33305: Code refactoring after code review --- .../Test/Util/ObjectExtensionUtilTest.php | 133 +++++++++--------- .../unit/Util/MockModuleResolverBuilder.php | 33 ++--- 2 files changed, 76 insertions(+), 90 deletions(-) diff --git a/dev/tests/unit/Magento/FunctionalTestFramework/Test/Util/ObjectExtensionUtilTest.php b/dev/tests/unit/Magento/FunctionalTestFramework/Test/Util/ObjectExtensionUtilTest.php index fc1893e0b..b30e4b50e 100644 --- a/dev/tests/unit/Magento/FunctionalTestFramework/Test/Util/ObjectExtensionUtilTest.php +++ b/dev/tests/unit/Magento/FunctionalTestFramework/Test/Util/ObjectExtensionUtilTest.php @@ -15,7 +15,6 @@ use Magento\FunctionalTestingFramework\Test\Parsers\TestDataParser; use PHPUnit\Framework\TestCase; use ReflectionProperty; -use tests\unit\Util\MockModuleResolverBuilder; use tests\unit\Util\TestDataArrayBuilder; use tests\unit\Util\TestLoggingUtil; @@ -30,8 +29,6 @@ class ObjectExtensionUtilTest extends TestCase protected function setUp(): void { TestLoggingUtil::getInstance()->setMockLoggingUtil(); - $resolverMock = new MockModuleResolverBuilder(); - $resolverMock->setup(); } /** @@ -53,7 +50,7 @@ public static function tearDownAfterClass(): void public function testGenerateExtendedTest(): void { $mockActions = [ - "mockStep" => ["nodeName" => "mockNode", "stepKey" => "mockStep"] + 'mockStep' => ['nodeName' => 'mockNode', 'stepKey' => 'mockStep'] ]; $testDataArrayBuilder = new TestDataArrayBuilder(); @@ -66,7 +63,7 @@ public function testGenerateExtendedTest(): void $mockExtendedTest = $testDataArrayBuilder ->withName('extendedTest') ->withAnnotations(['title' => [['value' => 'extendedTest']]]) - ->withTestReference("simpleTest") + ->withTestReference('simpleTest') ->build(); $mockTestData = array_merge($mockSimpleTest, $mockExtendedTest); @@ -83,8 +80,8 @@ public function testGenerateExtendedTest(): void ); // assert that expected test is generated - $this->assertEquals($testObject->getParentName(), "simpleTest"); - $this->assertArrayHasKey("mockStep", $testObject->getOrderedActions()); + $this->assertEquals($testObject->getParentName(), 'simpleTest'); + $this->assertArrayHasKey('mockStep', $testObject->getOrderedActions()); } /** @@ -96,10 +93,10 @@ public function testGenerateExtendedTest(): void public function testGenerateExtendedWithHooks(): void { $mockBeforeHooks = [ - "beforeHookAction" => ["nodeName" => "mockNodeBefore", "stepKey" => "mockStepBefore"] + 'beforeHookAction' => ['nodeName' => 'mockNodeBefore', 'stepKey' => 'mockStepBefore'] ]; $mockAfterHooks = [ - "afterHookAction" => ["nodeName" => "mockNodeAfter", "stepKey" => "mockStepAfter"] + 'afterHookAction' => ['nodeName' => 'mockNodeAfter', 'stepKey' => 'mockStepAfter'] ]; $testDataArrayBuilder = new TestDataArrayBuilder(); @@ -113,7 +110,7 @@ public function testGenerateExtendedWithHooks(): void $mockExtendedTest = $testDataArrayBuilder ->withName('extendedTest') ->withAnnotations(['title' => [['value' => 'extendedTest']]]) - ->withTestReference("simpleTest") + ->withTestReference('simpleTest') ->build(); $mockTestData = array_merge($mockSimpleTest, $mockExtendedTest); @@ -130,9 +127,9 @@ public function testGenerateExtendedWithHooks(): void ); // assert that expected test is generated - $this->assertEquals($testObject->getParentName(), "simpleTest"); - $this->assertArrayHasKey("mockStepBefore", $testObject->getHooks()['before']->getActions()); - $this->assertArrayHasKey("mockStepAfter", $testObject->getHooks()['after']->getActions()); + $this->assertEquals($testObject->getParentName(), 'simpleTest'); + $this->assertArrayHasKey('mockStepBefore', $testObject->getHooks()['before']->getActions()); + $this->assertArrayHasKey('mockStepAfter', $testObject->getHooks()['after']->getActions()); } /** @@ -146,7 +143,7 @@ public function testExtendedTestNoParent(): void $testDataArrayBuilder = new TestDataArrayBuilder(); $mockExtendedTest = $testDataArrayBuilder ->withName('extendedTest') - ->withTestReference("simpleTest") + ->withTestReference('simpleTest') ->build(); $mockTestData = array_merge($mockExtendedTest); @@ -158,7 +155,7 @@ public function testExtendedTestNoParent(): void // validate log statement TestLoggingUtil::getInstance()->validateMockLogStatement( 'debug', - "parent test not defined. test will be skipped", + 'parent test not defined. test will be skipped', ['parent' => 'simpleTest', 'test' => 'extendedTest'] ); } @@ -181,19 +178,19 @@ public function testExtendingExtendedTest(): void ->withName('simpleTest') ->withAnnotations(['title' => [['value' => 'simpleTest']]]) ->withTestActions() - ->withTestReference("anotherTest") + ->withTestReference('anotherTest') ->build(); $mockExtendedTest = $testDataArrayBuilder ->withName('extendedTest') ->withAnnotations(['title' => [['value' => 'extendedTest']]]) - ->withTestReference("simpleTest") + ->withTestReference('simpleTest') ->build(); $mockTestData = array_merge($mockParentTest, $mockSimpleTest, $mockExtendedTest); $this->setMockTestOutput($mockTestData); - $this->expectExceptionMessage("Cannot extend a test that already extends another test. Test: simpleTest"); + $this->expectExceptionMessage('Cannot extend a test that already extends another test. Test: simpleTest'); // parse and generate test object with mocked data TestObjectHandler::getInstance()->getObject('extendedTest'); @@ -201,10 +198,10 @@ public function testExtendingExtendedTest(): void // validate log statement TestLoggingUtil::getInstance()->validateMockLogStatement( 'debug', - "parent test not defined. test will be skipped", + 'parent test not defined. test will be skipped', ['parent' => 'simpleTest', 'test' => 'extendedTest'] ); - $this->expectOutputString("Extending Test: anotherTest => simpleTest" . PHP_EOL); + $this->expectOutputString('Extending Test: anotherTest => simpleTest' . PHP_EOL); } /** @@ -216,30 +213,30 @@ public function testExtendingExtendedTest(): void public function testGenerateExtendedActionGroup(): void { $mockSimpleActionGroup = [ - "nodeName" => "actionGroup", - "name" => "mockSimpleActionGroup", - "filename" => "someFile", - "commentHere" => [ - "nodeName" => "comment", - "selector" => "selector", - "stepKey" => "commentHere" + 'nodeName' => 'actionGroup', + 'name' => 'mockSimpleActionGroup', + 'filename' => 'someFile', + 'commentHere' => [ + 'nodeName' => 'comment', + 'selector' => 'selector', + 'stepKey' => 'commentHere' ], - "parentComment" => [ - "nodeName" => "comment", - "selector" => "parentSelector", - "stepKey" => "parentComment" + 'parentComment' => [ + 'nodeName' => 'comment', + 'selector' => 'parentSelector', + 'stepKey' => 'parentComment' ], ]; $mockExtendedActionGroup = [ - "nodeName" => "actionGroup", - "name" => "mockExtendedActionGroup", - "filename" => "someFile", - "extends" => "mockSimpleActionGroup", - "commentHere" => [ - "nodeName" => "comment", - "selector" => "otherSelector", - "stepKey" => "commentHere" + 'nodeName' => 'actionGroup', + 'name' => 'mockExtendedActionGroup', + 'filename' => 'someFile', + 'extends' => 'mockSimpleActionGroup', + 'commentHere' => [ + 'nodeName' => 'comment', + 'selector' => 'otherSelector', + 'stepKey' => 'commentHere' ], ]; @@ -262,10 +259,10 @@ public function testGenerateExtendedActionGroup(): void ); // assert that expected test is generated - $this->assertEquals("mockSimpleActionGroup", $actionGroupObject->getParentName()); + $this->assertEquals('mockSimpleActionGroup', $actionGroupObject->getParentName()); $actions = $actionGroupObject->getActions(); - $this->assertEquals("otherSelector", $actions["commentHere"]->getCustomActionAttributes()["selector"]); - $this->assertEquals("parentSelector", $actions["parentComment"]->getCustomActionAttributes()["selector"]); + $this->assertEquals('otherSelector', $actions['commentHere']->getCustomActionAttributes()['selector']); + $this->assertEquals('parentSelector', $actions['parentComment']->getCustomActionAttributes()['selector']); } /** @@ -277,14 +274,14 @@ public function testGenerateExtendedActionGroup(): void public function testGenerateExtendedActionGroupNoParent(): void { $mockExtendedActionGroup = [ - "nodeName" => "actionGroup", - "name" => "mockExtendedActionGroup", - "filename" => "someFile", - "extends" => "mockSimpleActionGroup", - "commentHere" => [ - "nodeName" => "comment", - "selector" => "otherSelector", - "stepKey" => "commentHere" + 'nodeName' => 'actionGroup', + 'name' => 'mockExtendedActionGroup', + 'filename' => 'someFile', + 'extends' => 'mockSimpleActionGroup', + 'commentHere' => [ + 'nodeName' => 'comment', + 'selector' => 'otherSelector', + 'stepKey' => 'commentHere' ], ]; @@ -296,7 +293,7 @@ public function testGenerateExtendedActionGroupNoParent(): void $this->setMockTestOutput(null, $mockActionGroupData); $this->expectExceptionMessage( - "Parent Action Group mockSimpleActionGroup not defined for Test " . $mockExtendedActionGroup['name'] + 'Parent Action Group mockSimpleActionGroup not defined for Test ' . $mockExtendedActionGroup['name'] ); // parse and generate test object with mocked data @@ -312,23 +309,23 @@ public function testGenerateExtendedActionGroupNoParent(): void public function testExtendingExtendedActionGroup(): void { $mockParentActionGroup = [ - "nodeName" => "actionGroup", - "name" => "mockParentActionGroup", - "filename" => "someFile" + 'nodeName' => 'actionGroup', + 'name' => 'mockParentActionGroup', + 'filename' => 'someFile' ]; $mockSimpleActionGroup = [ - "nodeName" => "actionGroup", - "name" => "mockSimpleActionGroup", - "filename" => "someFile", - "extends" => "mockParentActionGroup", + 'nodeName' => 'actionGroup', + 'name' => 'mockSimpleActionGroup', + 'filename' => 'someFile', + 'extends' => 'mockParentActionGroup' ]; $mockExtendedActionGroup = [ - "nodeName" => "actionGroup", - "name" => "mockExtendedActionGroup", - "filename" => "someFile", - "extends" => "mockSimpleActionGroup", + 'nodeName' => 'actionGroup', + 'name' => 'mockExtendedActionGroup', + 'filename' => 'someFile', + 'extends' => 'mockSimpleActionGroup' ]; $mockActionGroupData = [ @@ -341,22 +338,22 @@ public function testExtendingExtendedActionGroup(): void $this->setMockTestOutput(null, $mockActionGroupData); $this->expectExceptionMessage( - "Cannot extend an action group that already extends another action group. " . $mockSimpleActionGroup['name'] + 'Cannot extend an action group that already extends another action group. ' . $mockSimpleActionGroup['name'] ); // parse and generate test object with mocked data try { ActionGroupObjectHandler::getInstance()->getObject('mockExtendedActionGroup'); - } catch (Exception $e) { + } catch (Exception $exception) { // validate log statement TestLoggingUtil::getInstance()->validateMockLogStatement( 'error', - "Cannot extend an action group that already extends another action group. " . + 'Cannot extend an action group that already extends another action group. ' . $mockSimpleActionGroup['name'], ['parent' => $mockSimpleActionGroup['name'], 'actionGroup' => $mockExtendedActionGroup['name']] ); - throw $e; + throw $exception; } } @@ -379,7 +376,7 @@ public function testExtendedTestSkippedParent(): void $testDataArrayBuilder->reset(); $mockExtendedTest = $testDataArrayBuilder ->withName('extendTest') - ->withTestReference("baseTest") + ->withTestReference('baseTest') ->build(); $mockTestData = array_merge($mockParentTest, $mockExtendedTest); @@ -391,7 +388,7 @@ public function testExtendedTestSkippedParent(): void // validate log statement TestLoggingUtil::getInstance()->validateMockLogStatement( 'debug', - "extendTest is skipped due to ParentTestIsSkipped", + 'extendTest is skipped due to ParentTestIsSkipped', [] ); } diff --git a/dev/tests/unit/Util/MockModuleResolverBuilder.php b/dev/tests/unit/Util/MockModuleResolverBuilder.php index 8bf30aa00..0e1b6fc31 100644 --- a/dev/tests/unit/Util/MockModuleResolverBuilder.php +++ b/dev/tests/unit/Util/MockModuleResolverBuilder.php @@ -3,35 +3,31 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ -declare(strict_types=1); - namespace tests\unit\Util; use AspectMock\Test as AspectMock; -use Exception; -use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig; use Magento\FunctionalTestingFramework\ObjectManager; +use Magento\FunctionalTestingFramework\ObjectManagerFactory; use Magento\FunctionalTestingFramework\Util\ModuleResolver; -use ReflectionProperty; +use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig; class MockModuleResolverBuilder { /** - * Default paths for mock ModuleResolver. + * Default paths for mock ModuleResolver * * @var array */ private $defaultPaths = ['Magento_Module' => '/base/path/some/other/path/Magento/Module']; /** - * Mock ModuleResolver builder. - * - * @param array|null $paths + * Mock ModuleResolver builder * + * @param array $paths * @return void - * @throws Exception + * @throws \Exception */ - public function setup(array $paths = null): void + public function setup($paths = null) { if (empty($paths)) { $paths = $this->defaultPaths; @@ -39,12 +35,9 @@ public function setup(array $paths = null): void $mockConfig = AspectMock::double(MftfApplicationConfig::class, ['forceGenerateEnabled' => false]); $instance = AspectMock::double(ObjectManager::class, ['create' => $mockConfig->make(), 'get' => null])->make(); - // clear object manager value to inject expected instance - $property = new ReflectionProperty(ObjectManager::class, 'instance'); - $property->setAccessible(true); - $property->setValue($instance); + AspectMock::double(ObjectManagerFactory::class, ['getObjectManager' => $instance]); - $property = new ReflectionProperty(ModuleResolver::class, 'instance'); + $property = new \ReflectionProperty(ModuleResolver::class, 'instance'); $property->setAccessible(true); $property->setValue(null); @@ -58,14 +51,10 @@ public function setup(array $paths = null): void ); $instance = AspectMock::double(ObjectManager::class, ['create' => $mockResolver->make(), 'get' => null]) ->make(); - - // clear object manager value to inject expected instance - $property = new ReflectionProperty(ObjectManager::class, 'instance'); - $property->setAccessible(true); - $property->setValue($instance); + AspectMock::double(ObjectManagerFactory::class, ['getObjectManager' => $instance]); $resolver = ModuleResolver::getInstance(); - $property = new ReflectionProperty(ModuleResolver::class, 'enabledModuleNameAndPaths'); + $property = new \ReflectionProperty(ModuleResolver::class, 'enabledModuleNameAndPaths'); $property->setAccessible(true); $property->setValue($resolver, $paths); }