99use AspectMock \Proxy \Verifier ;
1010use AspectMock \Test as AspectMock ;
1111
12+ use Magento \FunctionalTestingFramework \Config \MftfApplicationConfig ;
13+ use Magento \FunctionalTestingFramework \Exceptions \TestFrameworkException ;
1214use Magento \FunctionalTestingFramework \ObjectManager ;
1315use Magento \FunctionalTestingFramework \ObjectManagerFactory ;
1416use Magento \FunctionalTestingFramework \Util \Logger \LoggingUtil ;
@@ -54,9 +56,10 @@ public function testGetModulePathsAlreadySet()
5456 */
5557 public function testGetModulePathsAggregate ()
5658 {
57- $ this ->setMockResolverClass (false , null , null , null , ["example " . DIRECTORY_SEPARATOR . "paths " ]);
59+ $ this ->mockForceGenerate (false );
60+ $ this ->setMockResolverClass (false , null , null , null , ["example " => "example " . DIRECTORY_SEPARATOR . "paths " ]);
5861 $ resolver = ModuleResolver::getInstance ();
59- $ this ->setMockResolverProperties ($ resolver , null , null );
62+ $ this ->setMockResolverProperties ($ resolver , null , [ 0 => " Magento_example " ] );
6063 $ this ->assertEquals (
6164 [
6265 "example " . DIRECTORY_SEPARATOR . "paths " ,
@@ -73,12 +76,13 @@ public function testGetModulePathsAggregate()
7376 */
7477 public function testGetModulePathsLocations ()
7578 {
79+ $ this ->mockForceGenerate (false );
7680 $ mockResolver = $ this ->setMockResolverClass (
77- false ,
78- null ,
81+ true ,
82+ [ 0 => " magento_example " ] ,
7983 null ,
8084 null ,
81- ["example " . DIRECTORY_SEPARATOR . "paths " ]
85+ ["example " => " example " . DIRECTORY_SEPARATOR . "paths " ]
8286 );
8387 $ resolver = ModuleResolver::getInstance ();
8488 $ this ->setMockResolverProperties ($ resolver , null , null );
@@ -166,15 +170,78 @@ function ($arg1, $arg2) {
166170 }
167171
168172 /**
169- * Validate that getEnabledModules returns correctly with no admin token
173+ * Validate that getEnabledModules errors out when no Admin Token is returned and --force is false
170174 * @throws \Exception
171175 */
172176 public function testGetModulePathsNoAdminToken ()
173177 {
178+ // Set --force to false
179+ $ this ->mockForceGenerate (false );
180+
181+ // Mock ModuleResolver and $enabledModulesPath
174182 $ this ->setMockResolverClass (false , null , ["example " . DIRECTORY_SEPARATOR . "paths " ], []);
175183 $ resolver = ModuleResolver::getInstance ();
176184 $ this ->setMockResolverProperties ($ resolver , null , null );
177- $ this ->assertEquals (["example " . DIRECTORY_SEPARATOR . "paths " ], $ resolver ->getModulesPath ());
185+
186+ // Cannot Generate if no --force was passed in and no Admin Token is returned succesfully
187+ $ this ->expectException (TestFrameworkException::class);
188+ $ resolver ->getModulesPath ();
189+ }
190+
191+ /**
192+ * Validates that getAdminToken is not called when --force is enabled
193+ */
194+ public function testGetAdminTokenNotCalledWhenForce ()
195+ {
196+ // Set --force to true
197+ $ this ->mockForceGenerate (true );
198+
199+ // Mock ModuleResolver and applyCustomModuleMethods()
200+ $ mockResolver = $ this ->setMockResolverClass ();
201+ $ resolver = ModuleResolver::getInstance ();
202+ $ this ->setMockResolverProperties ($ resolver , null , null );
203+ $ resolver ->getModulesPath ();
204+ $ mockResolver ->verifyNeverInvoked ("getAdminToken " );
205+
206+ // verifyNeverInvoked does not add to assertion count
207+ $ this ->addToAssertionCount (1 );
208+ }
209+
210+ /**
211+ * Verify the getAdminToken method returns throws an exception if ENV is not fully loaded.
212+ */
213+ public function testGetAdminTokenWithMissingEnv ()
214+ {
215+ // Set --force to true
216+ $ this ->mockForceGenerate (false );
217+
218+ // Unset env
219+ unset($ _ENV ['MAGENTO_ADMIN_USERNAME ' ]);
220+
221+ // Mock ModuleResolver and applyCustomModuleMethods()
222+ $ mockResolver = $ this ->setMockResolverClass ();
223+ $ resolver = ModuleResolver::getInstance ();
224+
225+ // Expect exception
226+ $ this ->expectException (TestFrameworkException::class);
227+ $ resolver ->getModulesPath ();
228+ }
229+
230+ /**
231+ * Verify the getAdminToken method returns throws an exception if Token was bad.
232+ */
233+ public function testGetAdminTokenWithBadResponse ()
234+ {
235+ // Set --force to true
236+ $ this ->mockForceGenerate (false );
237+
238+ // Mock ModuleResolver and applyCustomModuleMethods()
239+ $ mockResolver = $ this ->setMockResolverClass ();
240+ $ resolver = ModuleResolver::getInstance ();
241+
242+ // Expect exception
243+ $ this ->expectException (TestFrameworkException::class);
244+ $ resolver ->getModulesPath ();
178245 }
179246
180247 /**
@@ -205,7 +272,7 @@ private function setMockResolverClass(
205272 if (isset ($ mockToken )) {
206273 $ mockMethods ['getAdminToken ' ] = $ mockToken ;
207274 }
208- if (isset ($ mockModules )) {
275+ if (isset ($ mockGetModules )) {
209276 $ mockMethods ['getEnabledModules ' ] = $ mockGetModules ;
210277 }
211278 if (isset ($ mockCustomMethods )) {
@@ -220,7 +287,7 @@ private function setMockResolverClass(
220287 if (isset ($ mockCustomModules )) {
221288 $ mockMethods ['getCustomModulePaths ' ] = $ mockCustomModules ;
222289 }
223- $ mockMethods ['printMagentoVersionInfo ' ] = null ;
290+ // $mockMethods['printMagentoVersionInfo'] = null;
224291
225292 $ mockResolver = AspectMock::double (
226293 ModuleResolver::class,
@@ -260,12 +327,36 @@ private function setMockResolverProperties($instance, $mockPaths = null, $mockMo
260327 $ property ->setValue ($ instance , $ mockBlacklist );
261328 }
262329
330+ /**
331+ * Mocks MftfApplicationConfig->forceGenerateEnabled()
332+ * @param $forceGenerate
333+ * @throws \Exception
334+ * @return void
335+ */
336+ private function mockForceGenerate ($ forceGenerate )
337+ {
338+ $ mockConfig = AspectMock::double (
339+ MftfApplicationConfig::class,
340+ ['forceGenerateEnabled ' => $ forceGenerate ]
341+ );
342+ $ instance = AspectMock::double (
343+ ObjectManager::class,
344+ ['create ' => $ mockConfig ->make (), 'get ' => null ]
345+ )->make ();
346+ AspectMock::double (ObjectManagerFactory::class, ['getObjectManager ' => $ instance ]);
347+ }
348+
263349 /**
264350 * After method functionality
265351 * @return void
266352 */
267353 protected function tearDown ()
268354 {
355+ // re set env
356+ if (!isset ($ _ENV ['MAGENTO_ADMIN_USERNAME ' ])) {
357+ $ _ENV ['MAGENTO_ADMIN_USERNAME ' ] = "admin " ;
358+ }
359+
269360 AspectMock::clean ();
270361 }
271362}
0 commit comments