diff --git a/dev/tests/unit/Magento/FunctionalTestFramework/Util/ModuleResolverTest.php b/dev/tests/unit/Magento/FunctionalTestFramework/Util/ModuleResolverTest.php index f43f82fe2..cec3b9461 100644 --- a/dev/tests/unit/Magento/FunctionalTestFramework/Util/ModuleResolverTest.php +++ b/dev/tests/unit/Magento/FunctionalTestFramework/Util/ModuleResolverTest.php @@ -3,81 +3,92 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +declare(strict_types=1); namespace tests\unit\Magento\FunctionalTestFramework\Util; -use AspectMock\Proxy\Verifier; -use AspectMock\Test as AspectMock; - +use Exception; use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig; use Magento\FunctionalTestingFramework\Exceptions\FastFailException; -use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException; -use Magento\FunctionalTestingFramework\ObjectManager; -use Magento\FunctionalTestingFramework\ObjectManagerFactory; -use Magento\FunctionalTestingFramework\Util\Logger\LoggingUtil; +use Magento\FunctionalTestingFramework\Util\ModuleResolver\ModuleResolverService; use Magento\FunctionalTestingFramework\Util\ModuleResolver; +use PHPUnit\Framework\MockObject\MockObject; +use ReflectionProperty; use tests\unit\Util\MagentoTestCase; -use PHPUnit\Runner\Exception; use tests\unit\Util\TestLoggingUtil; class ModuleResolverTest extends MagentoTestCase { /** - * Before test functionality + * Before test functionality. + * * @return void */ - public function setUp(): void + protected function setUp(): void { TestLoggingUtil::getInstance()->setMockLoggingUtil(); } /** - * After class functionality + * After class functionality. + * * @return void */ public static function tearDownAfterClass(): void { TestLoggingUtil::getInstance()->clearMockLoggingUtil(); + + $moduleResolverServiceInstance = new ReflectionProperty(ModuleResolverService::class, 'INSTANCE'); + $moduleResolverServiceInstance->setAccessible(true); + $moduleResolverServiceInstance->setValue(null); + + $mftfAppConfigInstance = new ReflectionProperty(MftfApplicationConfig::class, 'MFTF_APPLICATION_CONTEXT'); + $mftfAppConfigInstance->setAccessible(true); + $mftfAppConfigInstance->setValue(null); } /** - * Validate that Paths that are already set are returned - * @throws \Exception + * Validate that Paths that are already set are returned. + * + * @return void + * @throws Exception */ - public function testGetModulePathsAlreadySet() + public function testGetModulePathsAlreadySet(): void { - $this->setMockResolverClass(); $resolver = ModuleResolver::getInstance(); - $this->setMockResolverProperties($resolver, ["example" . DIRECTORY_SEPARATOR . "paths"]); - $this->assertEquals(["example" . DIRECTORY_SEPARATOR . "paths"], $resolver->getModulesPath()); + $this->setMockResolverProperties($resolver, ['example' . DIRECTORY_SEPARATOR . 'paths']); + $this->assertEquals(['example' . DIRECTORY_SEPARATOR . 'paths'], $resolver->getModulesPath()); } /** - * Validate paths are aggregated correctly - * @throws \Exception + * Validate paths are aggregated correctly. + * + * @return void + * @throws Exception */ - public function testGetModulePathsAggregate() + public function testGetModulePathsAggregate(): void { $this->mockForceGenerate(false); - $this->setMockResolverClass( - false, - null, - null, - null, - [ - 'some' . DIRECTORY_SEPARATOR . 'path' . DIRECTORY_SEPARATOR . 'example' => ['example'], - 'other' . DIRECTORY_SEPARATOR . 'path' . DIRECTORY_SEPARATOR . 'sample' => ['sample'], - ], - null, - [ - 'Magento_example' => 'some' . DIRECTORY_SEPARATOR . 'path' . DIRECTORY_SEPARATOR . 'example', - 'Magento_sample' => 'other' . DIRECTORY_SEPARATOR . 'path' . DIRECTORY_SEPARATOR . 'sample', - ], - null, - null, - [], - [] - ); + + $moduleResolverService = $this->createMock(ModuleResolverService::class); + $moduleResolverService->expects($this->any()) + ->method('getRegisteredModuleList') + ->willReturn( + [ + 'Magento_example' => 'some' . DIRECTORY_SEPARATOR . 'path' . DIRECTORY_SEPARATOR . 'example', + 'Magento_sample' => 'other' . DIRECTORY_SEPARATOR . 'path' . DIRECTORY_SEPARATOR . 'sample' + ] + ); + $moduleResolverService->expects($this->any()) + ->method('aggregateTestModulePaths') + ->willReturn( + [ + 'some' . DIRECTORY_SEPARATOR . 'path' . DIRECTORY_SEPARATOR . 'example' => ['example'], + 'other' . DIRECTORY_SEPARATOR . 'path' . DIRECTORY_SEPARATOR . 'sample' => ['sample'] + ] + ); + + $this->setMockResolverCreatorProperties($moduleResolverService); $resolver = ModuleResolver::getInstance(); $this->setMockResolverProperties($resolver, null, [0 => 'Magento_example', 1 => 'Magento_sample']); $this->assertEquals( @@ -90,141 +101,34 @@ public function testGetModulePathsAggregate() } /** - * Validate aggregateTestModulePaths() when module path part of DEV_TESTS - * - * @throws \Exception - */ - public function testAggregateTestModulePathsDevTests() - { - $origin = TESTS_MODULE_PATH; - $modulePath = ModuleResolver::DEV_TESTS . DIRECTORY_SEPARATOR . "Magento"; - putenv("TESTS_MODULE_PATH=$modulePath"); - - $this->mockForceGenerate(false); - $mockResolver = $this->setMockResolverClass( - true, - [], - null, - null, - [], - [], - [], - null, - null, - [], - [], - null, - function ($arg) { - return $arg; - }, - function ($arg) { - return $arg; - } - ); - $resolver = ModuleResolver::getInstance(); - $this->setMockResolverProperties($resolver, null, null); - $this->assertEquals( - [], - $resolver->getModulesPath() - ); - - $mockResolver->verifyNeverInvoked('globRelevantPaths', [$modulePath, '']); - - putenv("TESTS_MODULE_PATH=$origin"); - } - - /** - * Validate correct path locations are fed into globRelevantPaths - * @throws \Exception - */ - public function testGetModulePathsLocations() - { - // clear test object handler value to inject parsed content - $property = new \ReflectionProperty(ModuleResolver::class, 'instance'); - $property->setAccessible(true); - $property->setValue(null); - - $this->mockForceGenerate(false); - $mockResolver = $this->setMockResolverClass( - true, - [], - null, - null, - [], - [], - [], - null, - null, - [], - [], - null, - function ($arg) { - return $arg; - }, - function ($arg) { - return $arg; - } - ); - $resolver = ModuleResolver::getInstance(); - $this->setMockResolverProperties($resolver, null, null); - $this->assertEquals( - [], - $resolver->getModulesPath() - ); - - // Define the Module paths from app/code - $magentoBaseCodePath = MAGENTO_BP; - - // Define the Module paths from default TESTS_MODULE_PATH - $modulePath = defined('TESTS_MODULE_PATH') ? TESTS_MODULE_PATH : TESTS_BP; - - $mockResolver->verifyInvoked('globRelevantPaths', [$modulePath, '']); - $mockResolver->verifyInvoked( - 'globRelevantPaths', - [$magentoBaseCodePath . DIRECTORY_SEPARATOR . "vendor" , 'Test' . DIRECTORY_SEPARATOR .'Mftf'] - ); - $mockResolver->verifyInvoked( - 'globRelevantPaths', - [ - $magentoBaseCodePath . DIRECTORY_SEPARATOR . "app" . DIRECTORY_SEPARATOR . "code", - 'Test' . DIRECTORY_SEPARATOR .'Mftf' - ] - ); - } - - /** - * Validate aggregateTestModulePathsFromComposerJson + * Validate aggregateTestModulePathsFromComposerJson. * - * @throws \Exception + * @return void + * @throws Exception */ - public function testAggregateTestModulePathsFromComposerJson() + public function testAggregateTestModulePathsFromComposerJson(): void { $this->mockForceGenerate(false); - $this->setMockResolverClass( - false, - null, // getEnabledModules - null, // applyCustomMethods - null, // globRelevantWrapper - [], // relevantPath - null, // getCustomModulePaths - null, // getRegisteredModuleList - null, // aggregateTestModulePathsFromComposerJson - [], // aggregateTestModulePathsFromComposerInstaller - [ - 'composer' . DIRECTORY_SEPARATOR . 'json' . DIRECTORY_SEPARATOR . 'pathA' => - [ - 'Magento_ModuleA', - 'Magento_ModuleB' - ], - 'composer' . DIRECTORY_SEPARATOR . 'json' . DIRECTORY_SEPARATOR . 'pathB' => - [ - 'Magento_ModuleB', - 'Magento_ModuleC' - ], - ], // getComposerJsonTestModulePaths - [] // getComposerInstalledTestModulePaths - ); + $moduleResolverService = $this->createMock(ModuleResolverService::class); + $moduleResolverService->expects($this->any()) + ->method('getComposerJsonTestModulePaths') + ->willReturn( + [ + 'composer' . DIRECTORY_SEPARATOR . 'json' . DIRECTORY_SEPARATOR . 'pathA' => + [ + 'Magento_ModuleA', + 'Magento_ModuleB' + ], + 'composer' . DIRECTORY_SEPARATOR . 'json' . DIRECTORY_SEPARATOR . 'pathB' => + [ + 'Magento_ModuleB', + 'Magento_ModuleC' + ] + ] + ); + + $this->setMockResolverCreatorProperties($moduleResolverService); $resolver = ModuleResolver::getInstance(); $this->setMockResolverProperties($resolver, null, [0 => 'Magento_ModuleB', 1 => 'Magento_ModuleC']); $this->assertEquals( @@ -236,90 +140,33 @@ public function testAggregateTestModulePathsFromComposerJson() } /** - * Validate getComposerJsonTestModulePaths with paths invocation - * - * @throws \Exception - */ - public function testGetComposerJsonTestModulePathsForPathInvocation() - { - $this->mockForceGenerate(false); - $mockResolver = $this->setMockResolverClass( - false, - [], - null, - null, - [], - null, - null, - null, - null, - [], - [] - ); - - $resolver = ModuleResolver::getInstance(); - $this->setMockResolverProperties($resolver, null, null); - $this->assertEquals( - [], - $resolver->getModulesPath() - ); - - // Expected dev tests path - $expectedSearchPaths[] = MAGENTO_BP - . DIRECTORY_SEPARATOR - . 'dev' - . DIRECTORY_SEPARATOR - . 'tests' - . DIRECTORY_SEPARATOR - . 'acceptance' - . DIRECTORY_SEPARATOR - . 'tests' - . DIRECTORY_SEPARATOR - . 'functional'; - - // Expected test module path - $testModulePath = defined('TESTS_MODULE_PATH') ? TESTS_MODULE_PATH : TESTS_BP; - - if (array_search($testModulePath, $expectedSearchPaths) === false) { - $expectedSearchPaths[] = $testModulePath; - } - - $mockResolver->verifyInvoked('getComposerJsonTestModulePaths', [$expectedSearchPaths]); - } - - /** - * Validate aggregateTestModulePathsFromComposerInstaller + * Validate aggregateTestModulePathsFromComposerInstaller. * - * @throws \Exception + * @return void + * @throws Exception */ - public function testAggregateTestModulePathsFromComposerInstaller() + public function testAggregateTestModulePathsFromComposerInstaller(): void { $this->mockForceGenerate(false); - $this->setMockResolverClass( - false, - null, - null, - null, - [], - null, - null, - null, - null, - [], - [ - 'composer' . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR . 'pathA' => - [ - 'Magento_ModuleA', - 'Magento_ModuleB' - ], - 'composer' . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR . 'pathB' => - [ - 'Magento_ModuleB', - 'Magento_ModuleC' - ], - ] - ); - + $moduleResolverService = $this->createMock(ModuleResolverService::class); + $moduleResolverService->expects($this->any()) + ->method('getComposerInstalledTestModulePaths') + ->willReturn( + [ + 'composer' . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR . 'pathA' => + [ + 'Magento_ModuleA', + 'Magento_ModuleB' + ], + 'composer' . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR . 'pathB' => + [ + 'Magento_ModuleB', + 'Magento_ModuleC' + ] + ] + ); + + $this->setMockResolverCreatorProperties($moduleResolverService); $resolver = ModuleResolver::getInstance(); $this->setMockResolverProperties( $resolver, @@ -336,93 +183,62 @@ public function testAggregateTestModulePathsFromComposerInstaller() } /** - * Validate getComposerInstalledTestModulePaths with paths invocation - * - * @throws \Exception - */ - public function testGetComposerInstalledTestModulePathsForPathInvocation() - { - $this->mockForceGenerate(false); - $mockResolver = $this->setMockResolverClass( - false, - [], - null, - null, - [], - null, - null, - null, - null, - [], - [] - ); - - $resolver = ModuleResolver::getInstance(); - $this->setMockResolverProperties($resolver, null, null); - $this->assertEquals( - [], - $resolver->getModulesPath() - ); - - // Expected file path - $expectedSearchPath = MAGENTO_BP . DIRECTORY_SEPARATOR . 'composer.json'; - - $mockResolver->verifyInvoked('getComposerInstalledTestModulePaths', [$expectedSearchPath]); - } - - /** - * Validate mergeModulePaths() and flipAndFilterModulePathsArray() + * Validate mergeModulePaths() and flipAndFilterModulePathsArray(). * - * @throws \Exception + * @return void + * @throws Exception */ - public function testMergeFlipAndFilterModulePathsNoForceGenerate() + public function testMergeFlipAndFilterModulePathsNoForceGenerate(): void { $this->mockForceGenerate(false); - $this->setMockResolverClass( - false, - null, - null, - null, - null, - null, - null, - null, - null, - [ - 'composer' . DIRECTORY_SEPARATOR . 'json' . DIRECTORY_SEPARATOR . 'pathA' => - [ - 'Magento_ModuleA' - ], - 'composer' . DIRECTORY_SEPARATOR . 'json' . DIRECTORY_SEPARATOR . 'pathB' => - [ - 'Magento_ModuleB' - ], - ], - [ - 'composer' . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR . 'pathD' => - [ - 'Magento_ModuleD' - ], - 'composer' . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR . 'pathE' => - [ - 'Magento_ModuleE' - ], - 'composer' . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR . 'pathC' => - [ - 'Magento_ModuleC', - 'Magento_ModuleB', - ], - ], - [ - 'some' . DIRECTORY_SEPARATOR . 'path' . DIRECTORY_SEPARATOR . 'example' => ['Magento_Example'], - 'other' . DIRECTORY_SEPARATOR . 'path' . DIRECTORY_SEPARATOR . 'sample' => ['Magento_Sample'], - 'some' . DIRECTORY_SEPARATOR . 'path' . DIRECTORY_SEPARATOR . 'path1' => ['Magento_Path1'], - 'other' . DIRECTORY_SEPARATOR . 'path' . DIRECTORY_SEPARATOR . 'path2' => ['Magento_Path2'], - 'some' . DIRECTORY_SEPARATOR . 'path' . DIRECTORY_SEPARATOR . 'path3' => ['Magento_Path3'], - 'other' . DIRECTORY_SEPARATOR . 'path' . DIRECTORY_SEPARATOR . 'path4' => ['Magento_Path4'], - ] - ); - + $moduleResolverService = $this->createMock(ModuleResolverService::class); + $moduleResolverService->expects($this->any()) + ->method('getComposerJsonTestModulePaths') + ->willReturn( + [ + 'composer' . DIRECTORY_SEPARATOR . 'json' . DIRECTORY_SEPARATOR . 'pathA' => + [ + 'Magento_ModuleA' + ], + 'composer' . DIRECTORY_SEPARATOR . 'json' . DIRECTORY_SEPARATOR . 'pathB' => + [ + 'Magento_ModuleB' + ] + ] + ); + $moduleResolverService->expects($this->any()) + ->method('getComposerInstalledTestModulePaths') + ->willReturn( + [ + 'composer' . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR . 'pathD' => + [ + 'Magento_ModuleD' + ], + 'composer' . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR . 'pathE' => + [ + 'Magento_ModuleE' + ], + 'composer' . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR . 'pathC' => + [ + 'Magento_ModuleC', + 'Magento_ModuleB', + ] + ] + ); + $moduleResolverService->expects($this->any()) + ->method('aggregateTestModulePaths') + ->willReturn( + [ + 'some' . DIRECTORY_SEPARATOR . 'path' . DIRECTORY_SEPARATOR . 'example' => ['Magento_Example'], + 'other' . DIRECTORY_SEPARATOR . 'path' . DIRECTORY_SEPARATOR . 'sample' => ['Magento_Sample'], + 'some' . DIRECTORY_SEPARATOR . 'path' . DIRECTORY_SEPARATOR . 'path1' => ['Magento_Path1'], + 'other' . DIRECTORY_SEPARATOR . 'path' . DIRECTORY_SEPARATOR . 'path2' => ['Magento_Path2'], + 'some' . DIRECTORY_SEPARATOR . 'path' . DIRECTORY_SEPARATOR . 'path3' => ['Magento_Path3'], + 'other' . DIRECTORY_SEPARATOR . 'path' . DIRECTORY_SEPARATOR . 'path4' => ['Magento_Path4'] + ] + ); + + $this->setMockResolverCreatorProperties($moduleResolverService); $resolver = ModuleResolver::getInstance(); $this->setMockResolverProperties( $resolver, @@ -435,7 +251,7 @@ public function testMergeFlipAndFilterModulePathsNoForceGenerate() 4 => 'Magento_ModuleB', 5 => 'Magento_ModuleD', 6 => 'Magento_Otherexample', - 7 => 'Magento_ModuleC', + 7 => 'Magento_ModuleC' ] ); $this->assertEquals( @@ -446,7 +262,7 @@ public function testMergeFlipAndFilterModulePathsNoForceGenerate() 'some' . DIRECTORY_SEPARATOR . 'path' . DIRECTORY_SEPARATOR . 'example', 'composer' . DIRECTORY_SEPARATOR . 'json' . DIRECTORY_SEPARATOR . 'pathB', 'composer' . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR . 'pathD', - 'composer' . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR . 'pathC', + 'composer' . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR . 'pathC' ], $resolver->getModulesPath() @@ -454,55 +270,59 @@ public function testMergeFlipAndFilterModulePathsNoForceGenerate() } /** - * Validate mergeModulePaths() and flipAndSortModulePathsArray() + * Validate mergeModulePaths() and flipAndSortModulePathsArray(). * - * @throws \Exception + * @return void + * @throws Exception */ - public function testMergeFlipNoSortModulePathsNoForceGenerate() + public function testMergeFlipNoSortModulePathsNoForceGenerate(): void { $this->mockForceGenerate(false); - $this->setMockResolverClass( - false, - null, - null, - null, - null, - null, - null, - null, - null, - [ - 'composer' . DIRECTORY_SEPARATOR . 'json' . DIRECTORY_SEPARATOR - . 'Magento' . DIRECTORY_SEPARATOR . 'ModuleA' => - [ - 'Magento_ModuleA' - ], - 'composer' . DIRECTORY_SEPARATOR . 'json' . DIRECTORY_SEPARATOR - . 'Magento' . DIRECTORY_SEPARATOR . 'ModuleBC' => - [ - 'Magento_ModuleB', - 'Magento_ModuleC', - ], - ], - [ - 'composer' . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR - . 'Magento' . DIRECTORY_SEPARATOR . 'ModuleCD' => - [ - 'Magento_ModuleC', - 'Magento_ModuleD' - ], - 'composer' . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR - . 'Magento' . DIRECTORY_SEPARATOR . 'ModuleE' => - [ - 'Magento_ModuleE' - ], - ], - [ - 'some' . DIRECTORY_SEPARATOR . 'path' . DIRECTORY_SEPARATOR . 'example' => ['Magento_Example'], - 'other' . DIRECTORY_SEPARATOR . 'path' . DIRECTORY_SEPARATOR . 'sample' => ['Magento_Sample'], - ] - ); - + $moduleResolverService = $this->createMock(ModuleResolverService::class); + $moduleResolverService->expects($this->any()) + ->method('getComposerJsonTestModulePaths') + ->willReturn( + [ + 'composer' . DIRECTORY_SEPARATOR . 'json' . DIRECTORY_SEPARATOR + . 'Magento' . DIRECTORY_SEPARATOR . 'ModuleA' => + [ + 'Magento_ModuleA' + ], + 'composer' . DIRECTORY_SEPARATOR . 'json' . DIRECTORY_SEPARATOR + . 'Magento' . DIRECTORY_SEPARATOR . 'ModuleBC' => + [ + 'Magento_ModuleB', + 'Magento_ModuleC' + ] + ] + ); + $moduleResolverService->expects($this->any()) + ->method('getComposerInstalledTestModulePaths') + ->willReturn( + [ + 'composer' . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR + . 'Magento' . DIRECTORY_SEPARATOR . 'ModuleCD' => + [ + 'Magento_ModuleC', + 'Magento_ModuleD' + ], + 'composer' . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR + . 'Magento' . DIRECTORY_SEPARATOR . 'ModuleE' => + [ + 'Magento_ModuleE' + ] + ] + ); + $moduleResolverService->expects($this->any()) + ->method('aggregateTestModulePaths') + ->willReturn( + [ + 'some' . DIRECTORY_SEPARATOR . 'path' . DIRECTORY_SEPARATOR . 'example' => ['Magento_Example'], + 'other' . DIRECTORY_SEPARATOR . 'path' . DIRECTORY_SEPARATOR . 'sample' => ['Magento_Sample'] + ] + ); + + $this->setMockResolverCreatorProperties($moduleResolverService); $resolver = ModuleResolver::getInstance(); $this->setMockResolverProperties( $resolver, @@ -529,55 +349,59 @@ public function testMergeFlipNoSortModulePathsNoForceGenerate() } /** - * Validate mergeModulePaths() and flipAndSortModulePathsArray() + * Validate mergeModulePaths() and flipAndSortModulePathsArray(). * - * @throws \Exception + * @return void + * @throws Exception */ - public function testMergeFlipAndSortModulePathsForceGenerate() + public function testMergeFlipAndSortModulePathsForceGenerate(): void { $this->mockForceGenerate(true); - $this->setMockResolverClass( - false, - null, - null, - null, - null, - null, - null, - null, - null, - [ - 'composer' . DIRECTORY_SEPARATOR . 'json' . DIRECTORY_SEPARATOR + $moduleResolverService = $this->createMock(ModuleResolverService::class); + $moduleResolverService->expects($this->any()) + ->method('getComposerJsonTestModulePaths') + ->willReturn( + [ + 'composer' . DIRECTORY_SEPARATOR . 'json' . DIRECTORY_SEPARATOR . 'Magento' . DIRECTORY_SEPARATOR . 'ModuleA' => - [ - 'Magento_ModuleA' - ], - 'composer' . DIRECTORY_SEPARATOR . 'json' . DIRECTORY_SEPARATOR + [ + 'Magento_ModuleA' + ], + 'composer' . DIRECTORY_SEPARATOR . 'json' . DIRECTORY_SEPARATOR . 'Magento' . DIRECTORY_SEPARATOR . 'ModuleBC' => - [ - 'Magento_ModuleB', - 'Magento_ModuleC', - ], - ], - [ - 'composer' . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR + [ + 'Magento_ModuleB', + 'Magento_ModuleC' + ] + ] + ); + $moduleResolverService->expects($this->any()) + ->method('getComposerInstalledTestModulePaths') + ->willReturn( + [ + 'composer' . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR . 'Magento' . DIRECTORY_SEPARATOR . 'ModuleCD' => - [ - 'Magento_ModuleC', - 'Magento_ModuleD' - ], - 'composer' . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR + [ + 'Magento_ModuleC', + 'Magento_ModuleD' + ], + 'composer' . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR . 'Magento' . DIRECTORY_SEPARATOR . 'ModuleD' => - [ - 'Magento_ModuleD' - ], - ], - [ - 'some' . DIRECTORY_SEPARATOR . 'path' . DIRECTORY_SEPARATOR . 'example' => ['Magento_Example'], - 'other' . DIRECTORY_SEPARATOR . 'path' . DIRECTORY_SEPARATOR . 'sample' => ['Magento_Sample'], - ] - ); - + [ + 'Magento_ModuleD' + ] + ] + ); + $moduleResolverService->expects($this->any()) + ->method('aggregateTestModulePaths') + ->willReturn( + [ + 'some' . DIRECTORY_SEPARATOR . 'path' . DIRECTORY_SEPARATOR . 'example' => ['Magento_Example'], + 'other' . DIRECTORY_SEPARATOR . 'path' . DIRECTORY_SEPARATOR . 'sample' => ['Magento_Sample'] + ] + ); + + $this->setMockResolverCreatorProperties($moduleResolverService); $resolver = ModuleResolver::getInstance(); $this->setMockResolverProperties( $resolver, @@ -609,45 +433,45 @@ public function testMergeFlipAndSortModulePathsForceGenerate() } /** - * Validate logging warning in flipAndFilterModulePathsArray() + * Validate logging warning in flipAndFilterModulePathsArray(). * - * @throws \Exception + * @return void + * @throws Exception */ - public function testMergeFlipAndFilterModulePathsWithLogging() + public function testMergeFlipAndFilterModulePathsWithLogging(): void { $this->mockForceGenerate(false); - $this->setMockResolverClass( - false, - null, - null, - null, - [], - null, - null, - null, - null, - [ - 'composer' . DIRECTORY_SEPARATOR . 'json' . DIRECTORY_SEPARATOR . 'pathA' => - [ - 'Magento_ModuleA' - ], - 'composer' . DIRECTORY_SEPARATOR . 'json' . DIRECTORY_SEPARATOR . 'pathB' => - [ - 'Magento_ModuleB' - ], - ], - [ - 'composer' . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR . 'pathA' => - [ - 'Magento_ModuleA' - ], - 'composer' . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR . 'pathB' => - [ - 'Magento_ModuleC' - ], - ] - ); - + $moduleResolverService = $this->createMock(ModuleResolverService::class); + $moduleResolverService->expects($this->any()) + ->method('getComposerJsonTestModulePaths') + ->willReturn( + [ + 'composer' . DIRECTORY_SEPARATOR . 'json' . DIRECTORY_SEPARATOR . 'pathA' => + [ + 'Magento_ModuleA' + ], + 'composer' . DIRECTORY_SEPARATOR . 'json' . DIRECTORY_SEPARATOR . 'pathB' => + [ + 'Magento_ModuleB' + ] + ] + ); + $moduleResolverService->expects($this->any()) + ->method('getComposerInstalledTestModulePaths') + ->willReturn( + [ + 'composer' . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR . 'pathA' => + [ + 'Magento_ModuleA' + ], + 'composer' . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR . 'pathB' => + [ + 'Magento_ModuleC' + ] + ] + ); + + $this->setMockResolverCreatorProperties($moduleResolverService); $resolver = ModuleResolver::getInstance(); $this->setMockResolverProperties( $resolver, @@ -666,37 +490,30 @@ public function testMergeFlipAndFilterModulePathsWithLogging() ], $resolver->getModulesPath() ); + $warnMsg = 'Path: composer' . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR; $warnMsg .= 'pathA is ignored by ModuleResolver. ' . PHP_EOL . 'Path: composer' . DIRECTORY_SEPARATOR; $warnMsg .= 'json' . DIRECTORY_SEPARATOR . 'pathA is set for Module: Magento_ModuleA' . PHP_EOL; - TestLoggingUtil::getInstance()->validateMockLogStatement( - 'warning', - $warnMsg, - [] - ); + TestLoggingUtil::getInstance()->validateMockLogStatement('warning', $warnMsg, []); } /** - * Validate custom modules are added - * @throws \Exception + * Validate custom modules are added. + * + * @return void + * @throws Exception */ - public function testApplyCustomModuleMethods() + public function testApplyCustomModuleMethods(): void { - $this->setMockResolverClass( - false, - null, - null, - null, - [], - [ 'Magento_Module' => 'otherPath'], - null, - null, - null, - [], - [] - ); + $this->mockForceGenerate(true); + $moduleResolverService = $this->createMock(ModuleResolverService::class); + $moduleResolverService->expects($this->any()) + ->method('getCustomModulePaths') + ->willReturn(['Magento_Module' => 'otherPath']); + + $this->setMockResolverCreatorProperties($moduleResolverService); $resolver = ModuleResolver::getInstance(); - $this->setMockResolverProperties($resolver, null, null, null); + $this->setMockResolverProperties($resolver); $this->assertEquals(['otherPath'], $resolver->getModulesPath()); TestLoggingUtil::getInstance()->validateMockLogStatement( 'info', @@ -709,35 +526,24 @@ public function testApplyCustomModuleMethods() * Validate blocklisted modules are removed * Module paths are sorted according to module name in alphabetically ascending order * - * @throws \Exception + * @throws Exception */ - public function testGetModulePathsBlocklist() + public function testGetModulePathsBlocklist(): void { - $this->setMockResolverClass( - false, - null, - null, - null, - [], - null, - null, - null, - null, - [], - [], - [ - 'thisPath/some/path4' => ['Some_Module4'], - 'devTests/Magento/path3' => ['Magento_Module3'], - 'appCode/Magento/path2' => ['Magento_Module2'], - 'vendor/amazon/path1' => ['Amazon_Module1'], - ], - function ($arg) { - return $arg; - }, - function ($arg) { - return $arg; - } - ); + $this->mockForceGenerate(true); + $moduleResolverService = $this->createMock(ModuleResolverService::class); + $moduleResolverService->expects($this->any()) + ->method('aggregateTestModulePaths') + ->willReturn( + [ + 'thisPath/some/path4' => ['Some_Module4'], + 'devTests/Magento/path3' => ['Magento_Module3'], + 'appCode/Magento/path2' => ['Magento_Module2'], + 'vendor/amazon/path1' => ['Amazon_Module1'] + ] + ); + + $this->setMockResolverCreatorProperties($moduleResolverService); $resolver = ModuleResolver::getInstance(); $this->setMockResolverProperties($resolver, null, null, ['Magento_Module3']); $this->assertEquals( @@ -752,68 +558,33 @@ function ($arg) { } /** - * Validate that getEnabledModules errors out when no Admin Token is returned and --force is false - * @throws \Exception + * Validate that getEnabledModules errors out when no Admin Token is returned and --force is false. + * + * @return void + * @throws Exception */ - public function testGetModulePathsNoAdminToken() + public function testGetModulePathsNoAdminToken(): void { // Set --force to false $this->mockForceGenerate(false); - - // Mock ModuleResolver and $enabledModulesPath - $this->setMockResolverClass( - false, - null, - ["example" . DIRECTORY_SEPARATOR . "paths"], - [], - null, - null, - null, - null, - null, - [], - [] - ); $resolver = ModuleResolver::getInstance(); - $this->setMockResolverProperties($resolver, null, null); + $this->setMockResolverProperties($resolver); - // Cannot Generate if no --force was passed in and no Admin Token is returned succesfully + // Cannot Generate if no --force was passed in and no Admin Token is returned successfully $this->expectException(FastFailException::class); $resolver->getModulesPath(); } - /** - * Validates that getAdminToken is not called when --force is enabled - */ - public function testGetAdminTokenNotCalledWhenForce() - { - // Set --force to true - $this->mockForceGenerate(true); - - // Mock ModuleResolver and applyCustomModuleMethods() - $mockResolver = $this->setMockResolverClass(); - $resolver = ModuleResolver::getInstance(); - $this->setMockResolverProperties($resolver, null, null); - $resolver->getModulesPath(); - $mockResolver->verifyNeverInvoked("getAdminToken"); - - // verifyNeverInvoked does not add to assertion count - $this->addToAssertionCount(1); - } - /** * Verify the getAdminToken method returns throws an exception if ENV is not fully loaded. */ - public function testGetAdminTokenWithMissingEnv() + public function testGetAdminTokenWithMissingEnv(): void { - // Set --force to true + // Set --force to false $this->mockForceGenerate(false); // Unset env unset($_ENV['MAGENTO_ADMIN_USERNAME']); - - // Mock ModuleResolver and applyCustomModuleMethods() - $mockResolver = $this->setMockResolverClass(); $resolver = ModuleResolver::getInstance(); // Expect exception @@ -824,13 +595,10 @@ public function testGetAdminTokenWithMissingEnv() /** * Verify the getAdminToken method returns throws an exception if Token was bad. */ - public function testGetAdminTokenWithBadResponse() + public function testGetAdminTokenWithBadResponse(): void { - // Set --force to true + // Set --force to false $this->mockForceGenerate(false); - - // Mock ModuleResolver and applyCustomModuleMethods() - $mockResolver = $this->setMockResolverClass(); $resolver = ModuleResolver::getInstance(); // Expect exception @@ -839,162 +607,78 @@ public function testGetAdminTokenWithBadResponse() } /** - * Function used to set mock for parser return and force init method to run between tests. - * - * @param string $mockToken - * @param array $mockGetEnabledModules - * @param string[] $mockApplyCustomMethods - * @param string[] $mockGlobRelevantWrapper - * @param string[] $mockRelevantPaths - * @param string[] $mockGetCustomModulePaths - * @param string[] $mockGetRegisteredModuleList - * @param string[] $mockAggregateTestModulePathsFromComposerJson - * @param string[] $mockAggregateTestModulePathsFromComposerInstaller - * @param string[] $mockGetComposerJsonTestModulePaths - * @param string[] $mockGetComposerInstalledTestModulePaths - * @param string[] $mockAggregateTestModulePaths - * @param string[] $mockNormalizeModuleNames - * @param string[] $mockFlipAndFilterModulePathsArray - * @param string[] $mockFlipAndSortModulePathsArray - * @throws \Exception - * @return Verifier ModuleResolver double - */ - private function setMockResolverClass( - $mockToken = null, - $mockGetEnabledModules = null, - $mockApplyCustomMethods = null, - $mockGlobRelevantWrapper = null, - $mockRelevantPaths = null, - $mockGetCustomModulePaths = null, - $mockGetRegisteredModuleList = null, - $mockAggregateTestModulePathsFromComposerJson = null, - $mockAggregateTestModulePathsFromComposerInstaller = null, - $mockGetComposerJsonTestModulePaths = null, - $mockGetComposerInstalledTestModulePaths = null, - $mockAggregateTestModulePaths = null, - $mockNormalizeModuleNames = null, - $mockFlipAndFilterModulePathsArray = null, - $mockFlipAndSortModulePathsArray = null - ) { - $property = new \ReflectionProperty(ModuleResolver::class, 'instance'); - $property->setAccessible(true); - $property->setValue(null); - - $mockMethods = []; - if (isset($mockToken)) { - $mockMethods['getAdminToken'] = $mockToken; - } - if (isset($mockGetEnabledModules)) { - $mockMethods['getEnabledModules'] = $mockGetEnabledModules; - } - if (isset($mockApplyCustomMethods)) { - $mockMethods['applyCustomModuleMethods'] = $mockApplyCustomMethods; - } - if (isset($mockGlobRelevantWrapper)) { - $mockMethods['globRelevantWrapper'] = $mockGlobRelevantWrapper; - } - if (isset($mockRelevantPaths)) { - $mockMethods['globRelevantPaths'] = $mockRelevantPaths; - } - if (isset($mockGetCustomModulePaths)) { - $mockMethods['getCustomModulePaths'] = $mockGetCustomModulePaths; - } - if (isset($mockGetRegisteredModuleList)) { - $mockMethods['getRegisteredModuleList'] = $mockGetRegisteredModuleList; - } - if (isset($mockAggregateTestModulePathsFromComposerJson)) { - $mockMethods['aggregateTestModulePathsFromComposerJson'] = $mockAggregateTestModulePathsFromComposerJson; - } - if (isset($mockAggregateTestModulePathsFromComposerInstaller)) { - $mockMethods['aggregateTestModulePathsFromComposerInstaller'] = - $mockAggregateTestModulePathsFromComposerInstaller; - } - if (isset($mockGetComposerJsonTestModulePaths)) { - $mockMethods['getComposerJsonTestModulePaths'] = $mockGetComposerJsonTestModulePaths; - } - if (isset($mockGetComposerInstalledTestModulePaths)) { - $mockMethods['getComposerInstalledTestModulePaths'] = $mockGetComposerInstalledTestModulePaths; - } - if (isset($mockAggregateTestModulePaths)) { - $mockMethods['aggregateTestModulePaths'] = $mockAggregateTestModulePaths; - } - if (isset($mockNormalizeModuleNames)) { - $mockMethods['normalizeModuleNames'] = $mockNormalizeModuleNames; - } - if (isset($mockFlipAndFilterModulePathsArray)) { - $mockMethods['flipAndFilterModulePathsArray'] = $mockFlipAndFilterModulePathsArray; - } - if (isset($mockFlipAndSortModulePathsArray)) { - $mockMethods['flipAndSortModulePathsArray'] = $mockFlipAndSortModulePathsArray; - } - $mockResolver = AspectMock::double( - ModuleResolver::class, - $mockMethods - ); - $instance = AspectMock::double( - ObjectManager::class, - ['create' => $mockResolver->make(), 'get' => null] - )->make(); - // bypass the private constructor - AspectMock::double(ObjectManagerFactory::class, ['getObjectManager' => $instance]); - - return $mockResolver; - } - - /** - * Function used to set mock for Resolver properties + * Function used to set mock for Resolver properties. * * @param ModuleResolver $instance * @param array $mockPaths * @param array $mockModules * @param array $mockBlocklist - * @throws \Exception + * + * @return void + * @throws Exception */ - private function setMockResolverProperties($instance, $mockPaths = null, $mockModules = null, $mockBlocklist = []) - { - $property = new \ReflectionProperty(ModuleResolver::class, 'enabledModulePaths'); + private function setMockResolverProperties( + ModuleResolver $instance, + $mockPaths = null, + $mockModules = null, + $mockBlocklist = [] + ): void { + $property = new ReflectionProperty(ModuleResolver::class, 'enabledModulePaths'); $property->setAccessible(true); $property->setValue($instance, $mockPaths); - $property = new \ReflectionProperty(ModuleResolver::class, 'enabledModules'); + $property = new ReflectionProperty(ModuleResolver::class, 'enabledModules'); $property->setAccessible(true); $property->setValue($instance, $mockModules); - $property = new \ReflectionProperty(ModuleResolver::class, 'moduleBlocklist'); + $property = new ReflectionProperty(ModuleResolver::class, 'moduleBlocklist'); $property->setAccessible(true); $property->setValue($instance, $mockBlocklist); } + /** + * Function used to set mock for ResolverCreator properties. + * + * @param MockObject $moduleResolverService + * + * @return void + */ + private function setMockResolverCreatorProperties(MockObject $moduleResolverService): void + { + $property = new ReflectionProperty(ModuleResolverService::class, 'INSTANCE'); + $property->setAccessible(true); + $property->setValue($moduleResolverService); + } + /** * Mocks MftfApplicationConfig->forceGenerateEnabled() - * @param $forceGenerate - * @throws \Exception + * @param bool $forceGenerate + * * @return void + * @throws Exception */ - private function mockForceGenerate($forceGenerate) + private function mockForceGenerate(bool $forceGenerate): void { - $mockConfig = AspectMock::double( - MftfApplicationConfig::class, - ['forceGenerateEnabled' => $forceGenerate] - ); - $instance = AspectMock::double( - ObjectManager::class, - ['create' => $mockConfig->make(), 'get' => null] - )->make(); - AspectMock::double(ObjectManagerFactory::class, ['getObjectManager' => $instance]); + $mockConfig = $this->createMock(MftfApplicationConfig::class); + $mockConfig->expects($this->once()) + ->method('forceGenerateEnabled') + ->willReturn($forceGenerate); + + $property = new ReflectionProperty(MftfApplicationConfig::class, 'MFTF_APPLICATION_CONTEXT'); + $property->setAccessible(true); + $property->setValue($mockConfig); } /** - * After method functionality + * After method functionality. + * * @return void */ protected function tearDown(): void { // re set env if (!isset($_ENV['MAGENTO_ADMIN_USERNAME'])) { - $_ENV['MAGENTO_ADMIN_USERNAME'] = "admin"; + $_ENV['MAGENTO_ADMIN_USERNAME'] = 'admin'; } - - AspectMock::clean(); } } diff --git a/dev/tests/unit/Magento/FunctionalTestFramework/Util/TestGeneratorTest.php b/dev/tests/unit/Magento/FunctionalTestFramework/Util/TestGeneratorTest.php index 66a2919d4..800c506f1 100644 --- a/dev/tests/unit/Magento/FunctionalTestFramework/Util/TestGeneratorTest.php +++ b/dev/tests/unit/Magento/FunctionalTestFramework/Util/TestGeneratorTest.php @@ -12,6 +12,7 @@ use Magento\FunctionalTestingFramework\Exceptions\TestReferenceException; use Magento\FunctionalTestingFramework\Filter\FilterList; use Magento\FunctionalTestingFramework\ObjectManager; +use Magento\FunctionalTestingFramework\Test\Handlers\TestObjectHandler; use Magento\FunctionalTestingFramework\Test\Objects\ActionObject; use Magento\FunctionalTestingFramework\Test\Objects\TestHookObject; use Magento\FunctionalTestingFramework\Test\Objects\TestObject; @@ -19,6 +20,7 @@ use Magento\FunctionalTestingFramework\Util\GenerationErrorHandler; use Magento\FunctionalTestingFramework\Util\ModuleResolver; use Magento\FunctionalTestingFramework\Util\TestGenerator; +use ReflectionClass; use ReflectionProperty; use tests\unit\Util\MagentoTestCase; use tests\unit\Util\TestLoggingUtil; @@ -75,6 +77,8 @@ public function testEntityException(): void $testObject = new TestObject('sampleTest', ['merge123' => $actionObject], [], [], 'filename'); $testGeneratorObject = TestGenerator::getInstance('', ['sampleTest' => $testObject]); + $this->mockTestObjectHandler(); + $testGeneratorObject->createAllTestFiles(null, []); // assert that no exception for createAllTestFiles and generation error is stored in GenerationErrorHandler @@ -231,5 +235,25 @@ public static function tearDownAfterClass(): void $mftfAppConfigInstance = new ReflectionProperty(MftfApplicationConfig::class, 'MFTF_APPLICATION_CONTEXT'); $mftfAppConfigInstance->setAccessible(true); $mftfAppConfigInstance->setValue(null); + + $property = new ReflectionProperty(TestObjectHandler::class, 'testObjectHandler'); + $property->setAccessible(true); + $property->setValue(null); + } + + /** + * Mock test object handler for test. + */ + private function mockTestObjectHandler(): void + { + $testObjectHandlerClass = new ReflectionClass(TestObjectHandler::class); + $testObjectHandlerConstructor = $testObjectHandlerClass->getConstructor(); + $testObjectHandlerConstructor->setAccessible(true); + $testObjectHandler = $testObjectHandlerClass->newInstanceWithoutConstructor(); + $testObjectHandlerConstructor->invoke($testObjectHandler); + + $property = new ReflectionProperty(TestObjectHandler::class, 'testObjectHandler'); + $property->setAccessible(true); + $property->setValue($testObjectHandler); } } diff --git a/src/Magento/FunctionalTestingFramework/Util/ModuleResolver.php b/src/Magento/FunctionalTestingFramework/Util/ModuleResolver.php index b9a74de84..09ff90dfc 100644 --- a/src/Magento/FunctionalTestingFramework/Util/ModuleResolver.php +++ b/src/Magento/FunctionalTestingFramework/Util/ModuleResolver.php @@ -8,6 +8,7 @@ use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig; use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException; +use Magento\FunctionalTestingFramework\Util\ModuleResolver\ModuleResolverService; use Magento\FunctionalTestingFramework\Util\Logger\LoggingUtil; use Magento\FunctionalTestingFramework\Util\Path\FilePathFormatter; use Magento\FunctionalTestingFramework\Util\Path\UrlFormatter; @@ -136,27 +137,6 @@ class ModuleResolver 'SampleTests', 'SampleTemplates' ]; - /** - * Registered module list in magento system under test - * - * @var array - */ - private $registeredModuleList = []; - - /** - * Composer json based test module paths - * - * @var array - */ - private $composerJsonModulePaths = null; - - /** - * Composer installed test module paths - * - * @var array - */ - private $composerInstalledModulePaths = null; - /** * Get ModuleResolver instance. * @@ -311,90 +291,7 @@ protected function getModuleAllowlist() */ private function aggregateTestModulePaths() { - $allModulePaths = []; - - // Define the Module paths from magento bp - $magentoBaseCodePath = FilePathFormatter::format(MAGENTO_BP, false); - - // Define the Module paths from default TESTS_MODULE_PATH - $modulePath = defined('TESTS_MODULE_PATH') ? TESTS_MODULE_PATH : TESTS_BP; - $modulePath = FilePathFormatter::format($modulePath, false); - - // If $modulePath is DEV_TESTS path, we don't need to search by pattern - if (strpos($modulePath, self::DEV_TESTS) === false) { - $codePathsToPattern[$modulePath] = ''; - } - - $vendorCodePath = DIRECTORY_SEPARATOR . self::VENDOR; - $codePathsToPattern[$magentoBaseCodePath . $vendorCodePath] = self::TEST_MFTF_PATTERN; - - $appCodePath = DIRECTORY_SEPARATOR . self::APP_CODE; - $codePathsToPattern[$magentoBaseCodePath . $appCodePath] = self::TEST_MFTF_PATTERN; - - foreach ($codePathsToPattern as $codePath => $pattern) { - $allModulePaths = array_merge_recursive($allModulePaths, $this->globRelevantPaths($codePath, $pattern)); - } - - return $allModulePaths; - } - - /** - * Function which takes a code path and a pattern and determines if there are any matching subdir paths. Matches - * are returned as an associative array keyed by basename (the last dir excluding pattern) to an array containing - * the matching path. - * - * @param string $testPath - * @param string $pattern - * @return array - */ - private function globRelevantPaths($testPath, $pattern) - { - $modulePaths = []; - $relevantPaths = []; - - if (file_exists($testPath)) { - $relevantPaths = $this->globRelevantWrapper($testPath, $pattern); - } - - foreach ($relevantPaths as $codePath) { - // Reduce magento/app/code/Magento/AdminGws/ to magento/app/code/Magento/AdminGws to read symlink - // Symlinks must be resolved otherwise they will not match Magento's filepath to the module - $potentialSymlink = str_replace(DIRECTORY_SEPARATOR . $pattern, "", $codePath); - if (is_link($potentialSymlink)) { - $codePath = realpath($potentialSymlink) . DIRECTORY_SEPARATOR . $pattern; - } - $mainModName = basename(str_replace($pattern, '', $codePath)); - $modulePaths[$codePath] = [$mainModName]; - - if (MftfApplicationConfig::getConfig()->verboseEnabled()) { - LoggingUtil::getInstance()->getLogger(ModuleResolver::class)->debug( - "including module", - ['module' => $mainModName, 'path' => $codePath] - ); - } - } - - return $modulePaths; - } - - /** - * Glob wrapper for globRelevantPaths function - * - * @param string $testPath - * @param string $pattern - * @return array - */ - private static function globRelevantWrapper($testPath, $pattern) - { - if ($pattern == "") { - return glob($testPath . '*' . DIRECTORY_SEPARATOR . '*' . $pattern); - } - $subDirectory = "*" . DIRECTORY_SEPARATOR; - $directories = glob($testPath . $subDirectory . $pattern, GLOB_ONLYDIR); - foreach (glob($testPath . $subDirectory, GLOB_ONLYDIR) as $dir) { - $directories = array_merge_recursive($directories, self::globRelevantWrapper($dir, $pattern)); - } - return $directories; + return ModuleResolverService::getInstance()->aggregateTestModulePaths(); } /** @@ -428,23 +325,14 @@ private function aggregateTestModulePathsFromComposerJson() * Retrieve all module code paths that have test module composer json files * * @param array $codePaths + * * @return array */ private function getComposerJsonTestModulePaths($codePaths) { - if (null !== $this->composerJsonModulePaths) { - return $this->composerJsonModulePaths; - } - try { - $this->composerJsonModulePaths = []; - $resolver = new ComposerModuleResolver(); - $this->composerJsonModulePaths = $resolver->getTestModulesFromPaths($codePaths); - } catch (TestFrameworkException $e) { - } - - return $this->composerJsonModulePaths; + return ModuleResolverService::getInstance()->getComposerJsonTestModulePaths($codePaths); } - + /** * Aggregate all code paths with composer installed test modules * @@ -462,22 +350,13 @@ private function aggregateTestModulePathsFromComposerInstaller() /** * Retrieve composer installed test module code paths * - * @params string $composerFile + * @param string $composerFile + * * @return array */ private function getComposerInstalledTestModulePaths($composerFile) { - if (null !== $this->composerInstalledModulePaths) { - return $this->composerInstalledModulePaths; - } - try { - $this->composerInstalledModulePaths = []; - $resolver = new ComposerModuleResolver(); - $this->composerInstalledModulePaths = $resolver->getComposerInstalledTestModules($composerFile); - } catch (TestFrameworkException $e) { - } - - return $this->composerInstalledModulePaths; + return ModuleResolverService::getInstance()->getComposerInstalledTestModulePaths($composerFile); } /** @@ -739,18 +618,7 @@ private function removeBlocklistModules($modulePaths) */ private function getCustomModulePaths() { - $customModulePaths = []; - $paths = getenv(self::CUSTOM_MODULE_PATHS); - - if (!$paths) { - return $customModulePaths; - } - - foreach (explode(',', $paths) as $path) { - $customModulePaths[$this->findVendorAndModuleNameFromPath(trim($path))] = $path; - } - - return $customModulePaths; + return ModuleResolverService::getInstance()->getCustomModulePaths(); } /** @@ -770,41 +638,7 @@ private function getModuleBlocklist() */ private function getRegisteredModuleList() { - if (!empty($this->registeredModuleList)) { - return $this->registeredModuleList; - } - - if (array_key_exists('MAGENTO_BP', $_ENV)) { - $autoloadPath = realpath(MAGENTO_BP . "/app/autoload.php"); - if ($autoloadPath) { - require_once($autoloadPath); - } else { - throw new TestFrameworkException("Magento app/autoload.php not found with given MAGENTO_BP:" - . MAGENTO_BP); - } - } - - try { - $allComponents = []; - if (!class_exists(self::REGISTRAR_CLASS)) { - throw new TestFrameworkException("Magento Installation not found when loading registered modules.\n"); - } - $components = new \Magento\Framework\Component\ComponentRegistrar(); - foreach (self::PATHS as $componentType) { - $allComponents = array_merge($allComponents, $components->getPaths($componentType)); - } - array_walk($allComponents, function (&$value) { - // Magento stores component paths with unix DIRECTORY_SEPARATOR, need to stay uniform and convert - $value = realpath($value); - $value .= DIRECTORY_SEPARATOR . self::TEST_MFTF_PATTERN; - }); - return $allComponents; - } catch (TestFrameworkException $e) { - LoggingUtil::getInstance()->getLogger(ModuleResolver::class)->warning( - "$e" - ); - } - return []; + return ModuleResolverService::getInstance()->getRegisteredModuleList(); } /** diff --git a/src/Magento/FunctionalTestingFramework/Util/ModuleResolver/ModuleResolverService.php b/src/Magento/FunctionalTestingFramework/Util/ModuleResolver/ModuleResolverService.php new file mode 100644 index 000000000..c3dad2118 --- /dev/null +++ b/src/Magento/FunctionalTestingFramework/Util/ModuleResolver/ModuleResolverService.php @@ -0,0 +1,322 @@ +registeredModuleList)) { + return $this->registeredModuleList; + } + + if (array_key_exists('MAGENTO_BP', $_ENV)) { + $autoloadPath = realpath(MAGENTO_BP . "/app/autoload.php"); + + if ($autoloadPath) { + require_once($autoloadPath); + } else { + throw new TestFrameworkException( + "Magento app/autoload.php not found with given MAGENTO_BP:" . MAGENTO_BP + ); + } + } + + try { + $allComponents = []; + + if (!class_exists(ModuleResolver::REGISTRAR_CLASS)) { + throw new TestFrameworkException("Magento Installation not found when loading registered modules.\n"); + } + + $components = new ComponentRegistrar(); + + foreach (ModuleResolver::PATHS as $componentType) { + $allComponents = array_merge($allComponents, $components->getPaths($componentType)); + } + + array_walk($allComponents, function (&$value) { + // Magento stores component paths with unix DIRECTORY_SEPARATOR, need to stay uniform and convert + $value = realpath($value); + $value .= DIRECTORY_SEPARATOR . ModuleResolver::TEST_MFTF_PATTERN; + }); + + return $allComponents; + } catch (TestFrameworkException $exception) { + LoggingUtil::getInstance()->getLogger(ModuleResolver::class)->warning("$exception"); + } + return []; + } + + /** + * Function which takes a code path and a pattern and determines if there are any matching subdir paths. Matches + * are returned as an associative array keyed by basename (the last dir excluding pattern) to an array containing + * the matching path. + * + * @param string $testPath + * @param string $pattern + * + * @return array + * @throws TestFrameworkException + */ + public function globRelevantPaths(string $testPath, string $pattern): array + { + $modulePaths = []; + $relevantPaths = []; + + if (file_exists($testPath)) { + $relevantPaths = $this->globRelevantWrapper($testPath, $pattern); + } + + foreach ($relevantPaths as $codePath) { + $potentialSymlink = str_replace(DIRECTORY_SEPARATOR . $pattern, "", $codePath); + + if (is_link($potentialSymlink)) { + $codePath = realpath($potentialSymlink) . DIRECTORY_SEPARATOR . $pattern; + } + + $mainModName = basename(str_replace($pattern, '', $codePath)); + $modulePaths[$codePath] = [$mainModName]; + + if (MftfApplicationConfig::getConfig()->verboseEnabled()) { + LoggingUtil::getInstance()->getLogger(ModuleResolver::class)->debug( + "including module", + ['module' => $mainModName, 'path' => $codePath] + ); + } + } + + return $modulePaths; + } + + /** + * Glob wrapper for globRelevantPaths function. + * + * @param string $testPath + * @param string $pattern + * + * @return array + */ + private static function globRelevantWrapper(string $testPath, string $pattern): array + { + if ($pattern == "") { + return glob($testPath . '*' . DIRECTORY_SEPARATOR . '*' . $pattern); + } + + $subDirectory = "*" . DIRECTORY_SEPARATOR; + $directories = glob($testPath . $subDirectory . $pattern, GLOB_ONLYDIR); + + foreach (glob($testPath . $subDirectory, GLOB_ONLYDIR) as $dir) { + $directories = array_merge_recursive($directories, self::globRelevantWrapper($dir, $pattern)); + } + + return $directories; + } + + /** + * Retrieve all module code paths that have test module composer json files. + * + * @param array $codePaths + * + * @return array + */ + public function getComposerJsonTestModulePaths(array $codePaths): array + { + if (null !== $this->composerJsonModulePaths) { + return $this->composerJsonModulePaths; + } + + try { + $this->composerJsonModulePaths = []; + $resolver = new ComposerModuleResolver(); + $this->composerJsonModulePaths = $resolver->getTestModulesFromPaths($codePaths); + } catch (TestFrameworkException $e) { + } + + return $this->composerJsonModulePaths; + } + + /** + * Retrieve composer installed test module code paths. + * + * @param string $composerFile + * + * @return array + */ + public function getComposerInstalledTestModulePaths(string $composerFile): array + { + if (null !== $this->composerInstalledModulePaths) { + return $this->composerInstalledModulePaths; + } + + try { + $this->composerInstalledModulePaths = []; + $resolver = new ComposerModuleResolver(); + $this->composerInstalledModulePaths = $resolver->getComposerInstalledTestModules($composerFile); + } catch (TestFrameworkException $e) { + } + + return $this->composerInstalledModulePaths; + } + + /** + * Retrieves all module directories which might contain pertinent test code. + * + * @return array + * @throws TestFrameworkException + */ + public function aggregateTestModulePaths(): array + { + $allModulePaths = []; + + // Define the Module paths from magento bp + $magentoBaseCodePath = FilePathFormatter::format(MAGENTO_BP, false); + + // Define the Module paths from default TESTS_MODULE_PATH + $modulePath = defined('TESTS_MODULE_PATH') ? TESTS_MODULE_PATH : TESTS_BP; + $modulePath = FilePathFormatter::format($modulePath, false); + + // If $modulePath is DEV_TESTS path, we don't need to search by pattern + if (strpos($modulePath, ModuleResolver::DEV_TESTS) === false) { + $codePathsToPattern[$modulePath] = ''; + } + + $vendorCodePath = DIRECTORY_SEPARATOR . ModuleResolver::VENDOR; + $codePathsToPattern[$magentoBaseCodePath . $vendorCodePath] = ModuleResolver::TEST_MFTF_PATTERN; + + $appCodePath = DIRECTORY_SEPARATOR . ModuleResolver::APP_CODE; + $codePathsToPattern[$magentoBaseCodePath . $appCodePath] = ModuleResolver::TEST_MFTF_PATTERN; + + foreach ($codePathsToPattern as $codePath => $pattern) { + $allModulePaths = array_merge_recursive($allModulePaths, $this->globRelevantPaths($codePath, $pattern)); + } + + return $allModulePaths; + } + + /** + * Returns an array of custom module paths defined by the user. + * + * @return string[] + */ + public function getCustomModulePaths(): array + { + $customModulePaths = []; + $paths = getenv(ModuleResolver::CUSTOM_MODULE_PATHS); + + if (!$paths) { + return $customModulePaths; + } + + foreach (explode(',', $paths) as $path) { + $customModulePaths[$this->findVendorAndModuleNameFromPath(trim($path))] = $path; + } + + return $customModulePaths; + } + + /** + * Find vendor and module name from path. + * + * @param string $path + * + * @return string + */ + private function findVendorAndModuleNameFromPath(string $path): string + { + $path = str_replace(DIRECTORY_SEPARATOR . ModuleResolver::TEST_MFTF_PATTERN, '', $path); + + return $this->findVendorNameFromPath($path) . '_' . basename($path); + } + + /** + * Find vendor name from path. + * + * @param string $path + * + * @return string + */ + private function findVendorNameFromPath(string $path): string + { + $possibleVendorName = 'UnknownVendor'; + $dirPaths = [ + ModuleResolver::VENDOR, + ModuleResolver::APP_CODE, + ModuleResolver::DEV_TESTS + ]; + + foreach ($dirPaths as $dirPath) { + $regex = "~.+\\/" . $dirPath . "\/(?<" . ModuleResolver::VENDOR . ">[^\/]+)\/.+~"; + $match = []; + preg_match($regex, $path, $match); + + if (isset($match[ModuleResolver::VENDOR])) { + $possibleVendorName = ucfirst($match[ModuleResolver::VENDOR]); + return $possibleVendorName; + } + } + + return $possibleVendorName; + } +}