|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace tests\unit\Magento\FunctionalTestFramework\Test\Util; |
| 8 | + |
| 9 | +use AspectMock\Test as AspectMock; |
| 10 | + |
| 11 | +use Magento\FunctionalTestingFramework\ObjectManager; |
| 12 | +use Magento\FunctionalTestingFramework\ObjectManagerFactory; |
| 13 | +use Magento\FunctionalTestingFramework\Util\ModuleResolver; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | + |
| 16 | +class ModuleResolverTest extends TestCase |
| 17 | +{ |
| 18 | + /** |
| 19 | + * Validate that Paths that are already set are returned |
| 20 | + * @throws \Exception |
| 21 | + */ |
| 22 | + public function testGetModulePathsAlreadySet() |
| 23 | + { |
| 24 | + $this->setMockResolverClass(); |
| 25 | + $resolver = ModuleResolver::getInstance(); |
| 26 | + $this->setMockResolverProperties($resolver, ["example/paths"]); |
| 27 | + $this->assertEquals(["example/paths"], $resolver->getModulesPath()); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Validate paths are aggregated correctly |
| 32 | + * @throws \Exception |
| 33 | + */ |
| 34 | + public function testGetModulePathsAggregate() |
| 35 | + { |
| 36 | + $this->setMockResolverClass(false, ["Magento_TestModule"]); |
| 37 | + $resolver = ModuleResolver::getInstance(); |
| 38 | + $this->setMockResolverProperties($resolver, null, null); |
| 39 | + $this->assertEquals( |
| 40 | + ['/git/magento2-functional-testing-framework/dev/tests/verification/TestModule'], |
| 41 | + $resolver->getModulesPath() |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Validate blacklisted modules are removed |
| 47 | + * @throws \Exception |
| 48 | + */ |
| 49 | + public function testGetModulePathsBlacklist() |
| 50 | + { |
| 51 | + $this->setMockResolverClass(false, ["Magento_TestModule"]); |
| 52 | + $resolver = ModuleResolver::getInstance(); |
| 53 | + $this->setMockResolverProperties($resolver, null, null, ["TestModule"]); |
| 54 | + $this->assertEquals([], $resolver->getModulesPath()); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Validate that getEnabledModules returns correctly with no admin token |
| 59 | + * @throws \Exception |
| 60 | + */ |
| 61 | + public function testGetModulePathsNoAdminToken() |
| 62 | + { |
| 63 | + $this->setMockResolverClass(false, null, ["example/paths"]); |
| 64 | + $resolver = ModuleResolver::getInstance(); |
| 65 | + $this->setMockResolverProperties($resolver, null, null); |
| 66 | + $this->assertEquals(["example/paths"], $resolver->getModulesPath()); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Function used to set mock for parser return and force init method to run between tests. |
| 71 | + * |
| 72 | + * @param string $mockToken |
| 73 | + * @param array $mockModules |
| 74 | + * @param string[] $mockCustomMethods |
| 75 | + * @throws \Exception |
| 76 | + */ |
| 77 | + private function setMockResolverClass($mockToken = null, $mockModules = null, $mockCustomMethods = null) |
| 78 | + { |
| 79 | + $property = new \ReflectionProperty(ModuleResolver::class, 'instance'); |
| 80 | + $property->setAccessible(true); |
| 81 | + $property->setValue(null); |
| 82 | + |
| 83 | + $mockMethods = []; |
| 84 | + if (isset($mockToken)) { |
| 85 | + $mockMethods['getAdminToken'] = $mockToken; |
| 86 | + } |
| 87 | + if (isset($mockModules)) { |
| 88 | + $mockMethods['getEnabledModules'] = $mockModules; |
| 89 | + } |
| 90 | + if (isset($mockCustomMethods)) { |
| 91 | + $mockMethods['applyCustomModuleMethods'] = $mockCustomMethods; |
| 92 | + } |
| 93 | + $mockMethods['printMagentoVersionInfo'] = null; |
| 94 | + |
| 95 | + $mockResolver = AspectMock::double( |
| 96 | + ModuleResolver::class, |
| 97 | + $mockMethods |
| 98 | + )->make(); |
| 99 | + $instance = AspectMock::double(ObjectManager::class, ['create' => $mockResolver, 'get' => null])->make(); |
| 100 | + // bypass the private constructor |
| 101 | + AspectMock::double(ObjectManagerFactory::class, ['getObjectManager' => $instance]); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Function used to set mock for Resolver properties |
| 106 | + * |
| 107 | + * @param ModuleResolver $instance |
| 108 | + * @param array $mockPaths |
| 109 | + * @param array $mockModules |
| 110 | + * @throws \Exception |
| 111 | + */ |
| 112 | + private function setMockResolverProperties($instance, $mockPaths = null, $mockModules = null, $mockBlacklist = []) |
| 113 | + { |
| 114 | + $property = new \ReflectionProperty(ModuleResolver::class, 'enabledModulePaths'); |
| 115 | + $property->setAccessible(true); |
| 116 | + $property->setValue($instance, $mockPaths); |
| 117 | + |
| 118 | + $property = new \ReflectionProperty(ModuleResolver::class, 'enabledModules'); |
| 119 | + $property->setAccessible(true); |
| 120 | + $property->setValue($instance, $mockModules); |
| 121 | + |
| 122 | + $property = new \ReflectionProperty(ModuleResolver::class, 'moduleBlacklist'); |
| 123 | + $property->setAccessible(true); |
| 124 | + $property->setValue($instance, $mockBlacklist); |
| 125 | + } |
| 126 | +} |
0 commit comments