Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ModuleResolverTest.php
Go to the documentation of this file.
1 <?php
8 
9 use AspectMock\Proxy\Verifier;
10 use AspectMock\Test as AspectMock;
11 
20 
22 {
27  public function setUp()
28  {
29  TestLoggingUtil::getInstance()->setMockLoggingUtil();
30  }
31 
36  public static function tearDownAfterClass()
37  {
38  TestLoggingUtil::getInstance()->clearMockLoggingUtil();
39  }
40 
45  public function testGetModulePathsAlreadySet()
46  {
47  $this->setMockResolverClass();
48  $resolver = ModuleResolver::getInstance();
49  $this->setMockResolverProperties($resolver, ["example" . DIRECTORY_SEPARATOR . "paths"]);
50  $this->assertEquals(["example" . DIRECTORY_SEPARATOR . "paths"], $resolver->getModulesPath());
51  }
52 
57  public function testGetModulePathsAggregate()
58  {
59  $this->mockForceGenerate(false);
60  $this->setMockResolverClass(false, null, null, null, ["example" => "example" . DIRECTORY_SEPARATOR . "paths"]);
61  $resolver = ModuleResolver::getInstance();
62  $this->setMockResolverProperties($resolver, null, [0 => "Magento_example"]);
63  $this->assertEquals(
64  [
65  "example" . DIRECTORY_SEPARATOR . "paths",
66  "example" . DIRECTORY_SEPARATOR . "paths",
67  "example" . DIRECTORY_SEPARATOR . "paths"
68  ],
69  $resolver->getModulesPath()
70  );
71  }
72 
77  public function testGetModulePathsLocations()
78  {
79  $this->mockForceGenerate(false);
80  $mockResolver = $this->setMockResolverClass(
81  true,
82  [0 => "magento_example"],
83  null,
84  null,
85  ["example" => "example" . DIRECTORY_SEPARATOR . "paths"]
86  );
87  $resolver = ModuleResolver::getInstance();
88  $this->setMockResolverProperties($resolver, null, null);
89  $this->assertEquals(
90  [
91  "example" . DIRECTORY_SEPARATOR . "paths",
92  "example" . DIRECTORY_SEPARATOR . "paths",
93  "example" . DIRECTORY_SEPARATOR . "paths"
94  ],
95  $resolver->getModulesPath()
96  );
97 
98  // Define the Module paths from app/code
99  $magentoBaseCodePath = MAGENTO_BP;
100 
101  // Define the Module paths from default TESTS_MODULE_PATH
102  $modulePath = defined('TESTS_MODULE_PATH') ? TESTS_MODULE_PATH : TESTS_BP;
103 
104  $mockResolver->verifyInvoked('globRelevantPaths', [$modulePath, '']);
105  $mockResolver->verifyInvoked(
106  'globRelevantPaths',
107  [$magentoBaseCodePath . DIRECTORY_SEPARATOR . "vendor" , 'Test' . DIRECTORY_SEPARATOR .'Mftf']
108  );
109  $mockResolver->verifyInvoked(
110  'globRelevantPaths',
111  [
112  $magentoBaseCodePath . DIRECTORY_SEPARATOR . "app" . DIRECTORY_SEPARATOR . "code",
113  'Test' . DIRECTORY_SEPARATOR .'Mftf'
114  ]
115  );
116  }
117 
122  public function testGetCustomModulePath()
123  {
124  $this->setMockResolverClass(false, ["Magento_TestModule"], null, null, [], ['otherPath']);
125  $resolver = ModuleResolver::getInstance();
126  $this->setMockResolverProperties($resolver, null, null, null);
127  $this->assertEquals(['otherPath'], $resolver->getModulesPath());
128  TestLoggingUtil::getInstance()->validateMockLogStatement(
129  'info',
130  'including custom module',
131  ['module' => 'otherPath']
132  );
133  }
134 
139  public function testGetModulePathsBlacklist()
140  {
141  $this->setMockResolverClass(
142  false,
143  null,
144  null,
145  null,
146  function ($arg1, $arg2) {
147  if ($arg2 === "") {
148  $mockValue = ["somePath" => "somePath"];
149  } else {
150  $mockValue = ["lastPath" => "lastPath"];
151  }
152  return $mockValue;
153  }
154  );
155  $resolver = ModuleResolver::getInstance();
156  $this->setMockResolverProperties($resolver, null, null, ["somePath"]);
157  $this->assertEquals(["lastPath", "lastPath"], $resolver->getModulesPath());
158  TestLoggingUtil::getInstance()->validateMockLogStatement(
159  'info',
160  'excluding module',
161  ['module' => 'somePath']
162  );
163  }
164 
170  {
171  // Set --force to false
172  $this->mockForceGenerate(false);
173 
174  // Mock ModuleResolver and $enabledModulesPath
175  $this->setMockResolverClass(false, null, ["example" . DIRECTORY_SEPARATOR . "paths"], []);
176  $resolver = ModuleResolver::getInstance();
177  $this->setMockResolverProperties($resolver, null, null);
178 
179  // Cannot Generate if no --force was passed in and no Admin Token is returned succesfully
180  $this->expectException(TestFrameworkException::class);
181  $resolver->getModulesPath();
182  }
183 
188  {
189  // Set --force to true
190  $this->mockForceGenerate(true);
191 
192  // Mock ModuleResolver and applyCustomModuleMethods()
193  $mockResolver = $this->setMockResolverClass();
194  $resolver = ModuleResolver::getInstance();
195  $this->setMockResolverProperties($resolver, null, null);
196  $resolver->getModulesPath();
197  $mockResolver->verifyNeverInvoked("getAdminToken");
198 
199  // verifyNeverInvoked does not add to assertion count
200  $this->addToAssertionCount(1);
201  }
202 
207  {
208  // Set --force to true
209  $this->mockForceGenerate(false);
210 
211  // Unset env
212  unset($_ENV['MAGENTO_ADMIN_USERNAME']);
213 
214  // Mock ModuleResolver and applyCustomModuleMethods()
215  $mockResolver = $this->setMockResolverClass();
216  $resolver = ModuleResolver::getInstance();
217 
218  // Expect exception
219  $this->expectException(TestFrameworkException::class);
220  $resolver->getModulesPath();
221  }
222 
227  {
228  // Set --force to true
229  $this->mockForceGenerate(false);
230 
231  // Mock ModuleResolver and applyCustomModuleMethods()
232  $mockResolver = $this->setMockResolverClass();
233  $resolver = ModuleResolver::getInstance();
234 
235  // Expect exception
236  $this->expectException(TestFrameworkException::class);
237  $resolver->getModulesPath();
238  }
239 
252  private function setMockResolverClass(
253  $mockToken = null,
254  $mockGetModules = null,
255  $mockCustomMethods = null,
256  $mockGlob = null,
257  $mockRelativePaths = null,
258  $mockCustomModules = null
259  ) {
260  $property = new \ReflectionProperty(ModuleResolver::class, 'instance');
261  $property->setAccessible(true);
262  $property->setValue(null);
263 
264  $mockMethods = [];
265  if (isset($mockToken)) {
266  $mockMethods['getAdminToken'] = $mockToken;
267  }
268  if (isset($mockGetModules)) {
269  $mockMethods['getEnabledModules'] = $mockGetModules;
270  }
271  if (isset($mockCustomMethods)) {
272  $mockMethods['applyCustomModuleMethods'] = $mockCustomMethods;
273  }
274  if (isset($mockGlob)) {
275  $mockMethods['globRelevantWrapper'] = $mockGlob;
276  }
277  if (isset($mockRelativePaths)) {
278  $mockMethods['globRelevantPaths'] = $mockRelativePaths;
279  }
280  if (isset($mockCustomModules)) {
281  $mockMethods['getCustomModulePaths'] = $mockCustomModules;
282  }
283 // $mockMethods['printMagentoVersionInfo'] = null;
284 
285  $mockResolver = AspectMock::double(
286  ModuleResolver::class,
287  $mockMethods
288  );
289  $instance = AspectMock::double(
290  ObjectManager::class,
291  ['create' => $mockResolver->make(), 'get' => null]
292  )->make();
293  // bypass the private constructor
294  AspectMock::double(ObjectManagerFactory::class, ['getObjectManager' => $instance]);
295 
296  return $mockResolver;
297  }
298 
308  private function setMockResolverProperties($instance, $mockPaths = null, $mockModules = null, $mockBlacklist = [])
309  {
310  $property = new \ReflectionProperty(ModuleResolver::class, 'enabledModulePaths');
311  $property->setAccessible(true);
312  $property->setValue($instance, $mockPaths);
313 
314  $property = new \ReflectionProperty(ModuleResolver::class, 'enabledModules');
315  $property->setAccessible(true);
316  $property->setValue($instance, $mockModules);
317 
318  $property = new \ReflectionProperty(ModuleResolver::class, 'moduleBlacklist');
319  $property->setAccessible(true);
320  $property->setValue($instance, $mockBlacklist);
321  }
322 
329  private function mockForceGenerate($forceGenerate)
330  {
331  $mockConfig = AspectMock::double(
332  MftfApplicationConfig::class,
333  ['forceGenerateEnabled' => $forceGenerate]
334  );
335  $instance = AspectMock::double(
336  ObjectManager::class,
337  ['create' => $mockConfig->make(), 'get' => null]
338  )->make();
339  AspectMock::double(ObjectManagerFactory::class, ['getObjectManager' => $instance]);
340  }
341 
346  protected function tearDown()
347  {
348  // re set env
349  if (!isset($_ENV['MAGENTO_ADMIN_USERNAME'])) {
350  $_ENV['MAGENTO_ADMIN_USERNAME'] = "admin";
351  }
352 
353  AspectMock::clean();
354  }
355 }