9
9
use AspectMock \Proxy \Verifier ;
10
10
use AspectMock \Test as AspectMock ;
11
11
12
+ use Magento \FunctionalTestingFramework \Config \MftfApplicationConfig ;
13
+ use Magento \FunctionalTestingFramework \Exceptions \TestFrameworkException ;
12
14
use Magento \FunctionalTestingFramework \ObjectManager ;
13
15
use Magento \FunctionalTestingFramework \ObjectManagerFactory ;
14
16
use Magento \FunctionalTestingFramework \Util \Logger \LoggingUtil ;
@@ -54,9 +56,10 @@ public function testGetModulePathsAlreadySet()
54
56
*/
55
57
public function testGetModulePathsAggregate ()
56
58
{
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 " ]);
58
61
$ resolver = ModuleResolver::getInstance ();
59
- $ this ->setMockResolverProperties ($ resolver , null , null );
62
+ $ this ->setMockResolverProperties ($ resolver , null , [ 0 => " Magento_example " ] );
60
63
$ this ->assertEquals (
61
64
[
62
65
"example " . DIRECTORY_SEPARATOR . "paths " ,
@@ -73,12 +76,13 @@ public function testGetModulePathsAggregate()
73
76
*/
74
77
public function testGetModulePathsLocations ()
75
78
{
79
+ $ this ->mockForceGenerate (false );
76
80
$ mockResolver = $ this ->setMockResolverClass (
77
- false ,
78
- null ,
81
+ true ,
82
+ [ 0 => " magento_example " ] ,
79
83
null ,
80
84
null ,
81
- ["example " . DIRECTORY_SEPARATOR . "paths " ]
85
+ ["example " => " example " . DIRECTORY_SEPARATOR . "paths " ]
82
86
);
83
87
$ resolver = ModuleResolver::getInstance ();
84
88
$ this ->setMockResolverProperties ($ resolver , null , null );
@@ -166,15 +170,78 @@ function ($arg1, $arg2) {
166
170
}
167
171
168
172
/**
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
170
174
* @throws \Exception
171
175
*/
172
176
public function testGetModulePathsNoAdminToken ()
173
177
{
178
+ // Set --force to false
179
+ $ this ->mockForceGenerate (false );
180
+
181
+ // Mock ModuleResolver and $enabledModulesPath
174
182
$ this ->setMockResolverClass (false , null , ["example " . DIRECTORY_SEPARATOR . "paths " ], []);
175
183
$ resolver = ModuleResolver::getInstance ();
176
184
$ 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 ();
178
245
}
179
246
180
247
/**
@@ -205,7 +272,7 @@ private function setMockResolverClass(
205
272
if (isset ($ mockToken )) {
206
273
$ mockMethods ['getAdminToken ' ] = $ mockToken ;
207
274
}
208
- if (isset ($ mockModules )) {
275
+ if (isset ($ mockGetModules )) {
209
276
$ mockMethods ['getEnabledModules ' ] = $ mockGetModules ;
210
277
}
211
278
if (isset ($ mockCustomMethods )) {
@@ -220,7 +287,7 @@ private function setMockResolverClass(
220
287
if (isset ($ mockCustomModules )) {
221
288
$ mockMethods ['getCustomModulePaths ' ] = $ mockCustomModules ;
222
289
}
223
- $ mockMethods ['printMagentoVersionInfo ' ] = null ;
290
+ // $mockMethods['printMagentoVersionInfo'] = null;
224
291
225
292
$ mockResolver = AspectMock::double (
226
293
ModuleResolver::class,
@@ -260,12 +327,36 @@ private function setMockResolverProperties($instance, $mockPaths = null, $mockMo
260
327
$ property ->setValue ($ instance , $ mockBlacklist );
261
328
}
262
329
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
+
263
349
/**
264
350
* After method functionality
265
351
* @return void
266
352
*/
267
353
protected function tearDown ()
268
354
{
355
+ // re set env
356
+ if (!isset ($ _ENV ['MAGENTO_ADMIN_USERNAME ' ])) {
357
+ $ _ENV ['MAGENTO_ADMIN_USERNAME ' ] = "admin " ;
358
+ }
359
+
269
360
AspectMock::clean ();
270
361
}
271
362
}
0 commit comments