@@ -276,42 +276,37 @@ class AccountManagementTest extends TestCase
276
276
/**
277
277
* @var int
278
278
*/
279
- private $ getStoreIdCounter ;
279
+ private $ getWebsiteIdCounter ;
280
280
281
281
/**
282
- * @var int
282
+ * @var int|null
283
283
*/
284
- private $ getWebsiteIdCounter ;
284
+ private $ customerStoreId ;
285
285
286
286
/**
287
287
* @inheritDoc
288
288
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
289
289
*/
290
290
protected function setUp (): void
291
291
{
292
-
293
- $ this ->customerFactory = $ this ->createPartialMock (CustomerFactory::class, ['create ' ]);
294
- $ this ->manager = $ this ->getMockForAbstractClass (ManagerInterface::class);
295
- $ this ->store = $ this ->getMockBuilder (Store::class)
296
- ->disableOriginalConstructor ()
297
- ->getMock ();
298
- $ this ->storeManager = $ this ->getMockForAbstractClass (StoreManagerInterface::class);
292
+ $ this ->customerFactory = $ this ->createMock (CustomerFactory::class);
293
+ $ this ->manager = $ this ->createMock (ManagerInterface::class);
294
+ $ this ->store = $ this ->createMock (Store::class);
295
+ $ this ->storeManager = $ this ->createMock (StoreManagerInterface::class);
299
296
$ this ->random = $ this ->createMock (Random::class);
300
297
$ this ->validator = $ this ->createMock (Validator::class);
301
298
$ this ->validationResultsInterfaceFactory = $ this ->createMock (
302
299
ValidationResultsInterfaceFactory::class
303
300
);
304
- $ this ->addressRepository = $ this ->getMockForAbstractClass (AddressRepositoryInterface::class);
305
- $ this ->customerMetadata = $ this ->getMockForAbstractClass (CustomerMetadataInterface::class);
301
+ $ this ->addressRepository = $ this ->createMock (AddressRepositoryInterface::class);
302
+ $ this ->customerMetadata = $ this ->createMock (CustomerMetadataInterface::class);
306
303
$ this ->customerRegistry = $ this ->createMock (CustomerRegistry::class);
307
- $ this ->logger = $ this ->getMockForAbstractClass (LoggerInterface::class);
308
- $ this ->encryptor = $ this ->getMockForAbstractClass (EncryptorInterface::class);
304
+ $ this ->logger = $ this ->createMock (LoggerInterface::class);
305
+ $ this ->encryptor = $ this ->createMock (EncryptorInterface::class);
309
306
$ this ->share = $ this ->createMock (Share::class);
310
307
$ this ->string = $ this ->createMock (StringUtils::class);
311
- $ this ->customerRepository = $ this ->getMockForAbstractClass (CustomerRepositoryInterface::class);
312
- $ this ->scopeConfig = $ this ->getMockBuilder (ScopeConfigInterface::class)
313
- ->disableOriginalConstructor ()
314
- ->getMockForAbstractClass ();
308
+ $ this ->customerRepository = $ this ->createMock (CustomerRepositoryInterface::class);
309
+ $ this ->scopeConfig = $ this ->createMock (ScopeConfigInterface::class);
315
310
$ this ->transportBuilder = $ this ->createMock (TransportBuilder::class);
316
311
$ this ->dataObjectProcessor = $ this ->createMock (DataObjectProcessor::class);
317
312
$ this ->registry = $ this ->createMock (Registry::class);
@@ -324,12 +319,8 @@ protected function setUp(): void
324
319
ExtensibleDataObjectConverter::class
325
320
);
326
321
$ this ->allowedCountriesReader = $ this ->createMock (AllowedCountries::class);
327
- $ this ->authenticationMock = $ this ->getMockBuilder (AuthenticationInterface::class)
328
- ->disableOriginalConstructor ()
329
- ->getMockForAbstractClass ();
330
- $ this ->emailNotificationMock = $ this ->getMockBuilder (EmailNotificationInterface::class)
331
- ->disableOriginalConstructor ()
332
- ->getMockForAbstractClass ();
322
+ $ this ->authenticationMock = $ this ->createMock (AuthenticationInterface::class);
323
+ $ this ->emailNotificationMock = $ this ->createMock (EmailNotificationInterface::class);
333
324
334
325
$ this ->customerSecure = $ this ->getMockBuilder (CustomerSecure::class)
335
326
->onlyMethods (['addData ' , 'setData ' ])
@@ -341,16 +332,9 @@ protected function setUp(): void
341
332
$ this ->accountConfirmation = $ this ->createMock (AccountConfirmation::class);
342
333
$ this ->searchCriteriaBuilderMock = $ this ->createMock (SearchCriteriaBuilder::class);
343
334
344
- $ this ->visitorCollectionFactory = $ this ->getMockBuilder (CollectionFactory::class)
345
- ->disableOriginalConstructor ()
346
- ->onlyMethods (['create ' ])
347
- ->getMock ();
348
- $ this ->sessionManager = $ this ->getMockBuilder (SessionManagerInterface::class)
349
- ->disableOriginalConstructor ()
350
- ->getMockForAbstractClass ();
351
- $ this ->saveHandler = $ this ->getMockBuilder (SaveHandlerInterface::class)
352
- ->disableOriginalConstructor ()
353
- ->getMockForAbstractClass ();
335
+ $ this ->visitorCollectionFactory = $ this ->createMock (CollectionFactory::class);
336
+ $ this ->sessionManager = $ this ->createMock (SessionManagerInterface::class);
337
+ $ this ->saveHandler = $ this ->createMock (SaveHandlerInterface::class);
354
338
$ this ->addressFactory = $ this ->createMock (AddressFactory::class);
355
339
356
340
$ this ->objectManagerHelper = new ObjectManagerHelper ($ this );
@@ -448,9 +432,11 @@ protected function setUp(): void
448
432
'emailNotification ' ,
449
433
$ this ->emailNotificationMock
450
434
);
435
+ $ this ->allowedCountriesReader ->method ('getAllowedCountries ' )->willReturn (['US ' => 'US ' ]);
436
+
451
437
$ this ->getIdCounter = 0 ;
452
- $ this ->getStoreIdCounter = 0 ;
453
438
$ this ->getWebsiteIdCounter = 0 ;
439
+ $ this ->customerStoreId = null ;
454
440
}
455
441
456
442
/**
@@ -545,12 +531,17 @@ public function testCreateAccountWithPasswordHashWithCustomerWithoutStoreId(): v
545
531
->method ('getWebsiteId ' )
546
532
->willReturn ($ websiteId );
547
533
$ customer ->method ('getStoreId ' )
548
- ->willReturnOnConsecutiveCalls ( null , null , 1 );
534
+ ->willReturnCallback ( fn () => $ this -> customerStoreId );
549
535
$ customer ->expects ($ this ->once ())
550
536
->method ('setStoreId ' )
551
- ->with ($ defaultStoreId );
537
+ ->with ($ defaultStoreId )
538
+ ->willReturnCallback (function ($ storeId ) use ($ customer ) {
539
+ $ this ->customerStoreId = $ storeId ;
540
+ return $ customer ;
541
+ });
552
542
553
543
$ address = $ this ->createMock (AddressInterface::class);
544
+ $ address ->expects ($ this ->atLeastOnce ())->method ('getCountryId ' )->willReturn ('US ' );
554
545
$ customer ->expects ($ this ->once ())->method ('getAddresses ' )->willReturn ([$ address ]);
555
546
$ customer ->expects ($ this ->once ())->method ('setAddresses ' )->with (null );
556
547
$ addressModel = $ this ->createMock (Address::class);
@@ -622,12 +613,17 @@ public function testCreateAccountWithPasswordHashWithLocalizedException(): void
622
613
->method ('getWebsiteId ' )
623
614
->willReturn ($ websiteId );
624
615
$ customer ->method ('getStoreId ' )
625
- ->willReturnOnConsecutiveCalls ( null , null , 1 );
616
+ ->willReturnCallback ( fn () => $ this -> customerStoreId );
626
617
$ customer ->expects ($ this ->once ())
627
618
->method ('setStoreId ' )
628
- ->with ($ defaultStoreId );
619
+ ->with ($ defaultStoreId )
620
+ ->willReturnCallback (function ($ storeId ) use ($ customer ) {
621
+ $ this ->customerStoreId = $ storeId ;
622
+ return $ customer ;
623
+ });
629
624
630
625
$ address = $ this ->createMock (AddressInterface::class);
626
+ $ address ->expects ($ this ->atLeastOnce ())->method ('getCountryId ' )->willReturn ('US ' );
631
627
$ customer ->expects ($ this ->once ())->method ('getAddresses ' )->willReturn ([$ address ]);
632
628
$ customer ->expects ($ this ->once ())->method ('setAddresses ' )->with (null );
633
629
$ addressModel = $ this ->createMock (Address::class);
@@ -698,17 +694,18 @@ public function testCreateAccountWithPasswordHashWithAddressException(): void
698
694
$ customer ->expects ($ this ->atLeastOnce ())
699
695
->method ('getWebsiteId ' )
700
696
->willReturn ($ websiteId );
701
- $ callCount = 0 ;
702
- $ customer
703
- ->method ('getStoreId ' )
704
- ->willReturnCallback (function () use (&$ callCount ) {
705
- return $ callCount ++ < 2 ? null : 1 ;
706
- });
697
+ $ customer ->method ('getStoreId ' )
698
+ ->willReturnCallback (fn () => $ this ->customerStoreId );
707
699
$ customer ->expects ($ this ->once ())
708
700
->method ('setStoreId ' )
709
- ->with ($ defaultStoreId );
701
+ ->with ($ defaultStoreId )
702
+ ->willReturnCallback (function ($ storeId ) use ($ customer ) {
703
+ $ this ->customerStoreId = $ storeId ;
704
+ return $ customer ;
705
+ });
710
706
711
707
$ address = $ this ->createMock (AddressInterface::class);
708
+ $ address ->expects ($ this ->atLeastOnce ())->method ('getCountryId ' )->willReturn ('US ' );
712
709
$ customer ->expects ($ this ->once ())->method ('getAddresses ' )->willReturn ([$ address ]);
713
710
$ customer ->expects ($ this ->once ())->method ('setAddresses ' )->with (null );
714
711
$ addressModel = $ this ->createMock (Address::class);
@@ -871,19 +868,15 @@ public function testCreateAccountWithoutPassword(): void
871
868
$ customer ->expects ($ this ->once ())
872
869
->method ('setWebsiteId ' )
873
870
->with ($ websiteId );
874
- $ customer ->expects ($ this ->any ())
875
- ->method ('getStoreId ' )
876
- ->will ($ this ->returnCallback (function () use ($ testCase , $ defaultStoreId ) {
877
- if ($ testCase ->getStoreIdCounter > 0 ) {
878
- return $ defaultStoreId ;
879
- } else {
880
- $ testCase ->getStoreIdCounter += 1 ;
881
- return null ;
882
- }
883
- }));
871
+ $ customer ->method ('getStoreId ' )
872
+ ->willReturnCallback (fn () => $ this ->customerStoreId );
884
873
$ customer ->expects ($ this ->once ())
885
874
->method ('setStoreId ' )
886
- ->with ($ defaultStoreId );
875
+ ->with ($ defaultStoreId )
876
+ ->willReturnCallback (function ($ storeId ) use ($ customer ) {
877
+ $ this ->customerStoreId = $ storeId ;
878
+ return $ customer ;
879
+ });
887
880
888
881
$ address = $ this ->createMock (AddressInterface::class);
889
882
$ address ->expects ($ this ->once ())->method ('setCustomerId ' )->with ($ customerId );
@@ -936,10 +929,6 @@ public function testCreateAccountWithoutPassword(): void
936
929
$ this ->emailNotificationMock ->expects ($ this ->once ())
937
930
->method ('newAccount ' )
938
931
->willReturnSelf ();
939
- $ this ->allowedCountriesReader
940
- ->expects ($ this ->atLeastOnce ())
941
- ->method ('getAllowedCountries ' )
942
- ->willReturn (['US ' => 'US ' ]);
943
932
$ address
944
933
->expects ($ this ->atLeastOnce ())
945
934
->method ('getCountryId ' )
@@ -1165,19 +1154,15 @@ public function testCreateAccountWithPassword(): void
1165
1154
$ customer ->expects ($ this ->once ())
1166
1155
->method ('setWebsiteId ' )
1167
1156
->with ($ websiteId );
1168
- $ customer ->expects ($ this ->any ())
1169
- ->method ('getStoreId ' )
1170
- ->will ($ this ->returnCallback (function () use ($ testCase , $ defaultStoreId ) {
1171
- if ($ testCase ->getStoreIdCounter > 0 ) {
1172
- return $ defaultStoreId ;
1173
- } else {
1174
- $ testCase ->getStoreIdCounter += 1 ;
1175
- return null ;
1176
- }
1177
- }));
1157
+ $ customer ->method ('getStoreId ' )
1158
+ ->willReturnCallback (fn () => $ this ->customerStoreId );
1178
1159
$ customer ->expects ($ this ->once ())
1179
1160
->method ('setStoreId ' )
1180
- ->with ($ defaultStoreId );
1161
+ ->with ($ defaultStoreId )
1162
+ ->willReturnCallback (function ($ storeId ) use ($ customer ) {
1163
+ $ this ->customerStoreId = $ storeId ;
1164
+ return $ customer ;
1165
+ });
1181
1166
1182
1167
$ address = $ this ->createMock (AddressInterface::class);
1183
1168
$ address ->expects ($ this ->once ())->method ('setCustomerId ' )->with ($ customerId );
@@ -1230,10 +1215,6 @@ public function testCreateAccountWithPassword(): void
1230
1215
$ this ->emailNotificationMock ->expects ($ this ->once ())
1231
1216
->method ('newAccount ' )
1232
1217
->willReturnSelf ();
1233
- $ this ->allowedCountriesReader
1234
- ->expects ($ this ->atLeastOnce ())
1235
- ->method ('getAllowedCountries ' )
1236
- ->willReturn (['US ' => 'US ' ]);
1237
1218
$ address
1238
1219
->expects ($ this ->atLeastOnce ())
1239
1220
->method ('getCountryId ' )
@@ -1347,19 +1328,15 @@ public function testCreateAccountWithGroupId(): void
1347
1328
$ customer ->expects ($ this ->once ())
1348
1329
->method ('setWebsiteId ' )
1349
1330
->with ($ websiteId );
1350
- $ customer ->expects ($ this ->any ())
1351
- ->method ('getStoreId ' )
1352
- ->will ($ this ->returnCallback (function () use ($ testCase , $ defaultStoreId ) {
1353
- if ($ testCase ->getStoreIdCounter > 0 ) {
1354
- return $ defaultStoreId ;
1355
- } else {
1356
- $ testCase ->getStoreIdCounter += 1 ;
1357
- return null ;
1358
- }
1359
- }));
1331
+ $ customer ->method ('getStoreId ' )
1332
+ ->willReturnCallback (fn () => $ this ->customerStoreId );
1360
1333
$ customer ->expects ($ this ->once ())
1361
1334
->method ('setStoreId ' )
1362
- ->with ($ defaultStoreId );
1335
+ ->with ($ defaultStoreId )
1336
+ ->willReturnCallback (function ($ storeId ) use ($ customer ) {
1337
+ $ this ->customerStoreId = $ storeId ;
1338
+ return $ customer ;
1339
+ });
1363
1340
1364
1341
$ address = $ this ->createMock (AddressInterface::class);
1365
1342
$ address ->expects ($ this ->once ())->method ('setCustomerId ' )->with ($ customerId );
@@ -1412,10 +1389,6 @@ public function testCreateAccountWithGroupId(): void
1412
1389
$ this ->emailNotificationMock ->expects ($ this ->once ())
1413
1390
->method ('newAccount ' )
1414
1391
->willReturnSelf ();
1415
- $ this ->allowedCountriesReader
1416
- ->expects ($ this ->atLeastOnce ())
1417
- ->method ('getAllowedCountries ' )
1418
- ->willReturn (['US ' => 'US ' ]);
1419
1392
$ address
1420
1393
->expects ($ this ->atLeastOnce ())
1421
1394
->method ('getCountryId ' )
@@ -2241,18 +2214,20 @@ public function testCreateAccountWithPasswordHashWithCustomerAddresses(): void
2241
2214
$ store ->expects ($ this ->any ())
2242
2215
->method ('getWebsiteId ' )
2243
2216
->willReturn ($ websiteId );
2217
+
2244
2218
//Handle address - existing and non-existing. Non-Existing should return null when call getId method
2245
2219
$ existingAddress = $ this ->createMock (AddressInterface::class);
2220
+ $ existingAddress ->expects ($ this ->atLeastOnce ())
2221
+ ->method ('getCountryId ' )
2222
+ ->willReturn ('US ' );
2246
2223
$ nonExistingAddress = $ this ->createMock (AddressInterface::class);
2224
+ $ nonExistingAddress ->expects ($ this ->atLeastOnce ())
2225
+ ->method ('getCountryId ' )
2226
+ ->willReturn ('US ' );
2247
2227
//Ensure that existing address is not in use
2248
- $ this ->addressRepository
2249
- ->expects ($ this ->atLeastOnce ())
2250
- ->method ("save " )
2251
- ->willReturnCallback (function ($ arg1 ) use ($ existingAddress , $ nonExistingAddress ) {
2252
- if ($ arg1 == $ existingAddress || $ arg1 == $ nonExistingAddress ) {
2253
- return null ;
2254
- }
2255
- });
2228
+ $ this ->addressRepository ->expects ($ this ->exactly (2 ))
2229
+ ->method ('save ' )
2230
+ ->willReturnArgument (0 );
2256
2231
2257
2232
$ existingAddress
2258
2233
->expects ($ this ->any ())
@@ -2332,7 +2307,7 @@ public function testCreateAccountWithPasswordHashWithCustomerAddresses(): void
2332
2307
->method ('getStore ' )
2333
2308
->willReturn ($ store );
2334
2309
$ this ->share
2335
- ->expects ($ this ->once ())
2310
+ ->expects ($ this ->atLeastOnce ())
2336
2311
->method ('isWebsiteScope ' )
2337
2312
->willReturn (true );
2338
2313
$ website = $ this ->getMockBuilder (Website::class)
@@ -2346,14 +2321,6 @@ public function testCreateAccountWithPasswordHashWithCustomerAddresses(): void
2346
2321
->method ('getWebsite ' )
2347
2322
->with ($ websiteId )
2348
2323
->willReturn ($ website );
2349
- $ this ->allowedCountriesReader
2350
- ->expects ($ this ->atLeastOnce ())
2351
- ->method ('getAllowedCountries ' )
2352
- ->willReturn (['US ' => 'US ' ]);
2353
- $ existingAddress
2354
- ->expects ($ this ->atLeastOnce ())
2355
- ->method ('getCountryId ' )
2356
- ->willReturn ('US ' );
2357
2324
2358
2325
$ this ->assertSame ($ customer , $ this ->accountManagement ->createAccountWithPasswordHash ($ customer , $ hash ));
2359
2326
}
@@ -2438,19 +2405,15 @@ public function testCreateAccountUnexpectedValueException(): void
2438
2405
$ customer ->expects ($ this ->once ())
2439
2406
->method ('setWebsiteId ' )
2440
2407
->with ($ websiteId );
2441
- $ customer ->expects ($ this ->any ())
2442
- ->method ('getStoreId ' )
2443
- ->will ($ this ->returnCallback (function () use ($ testCase , $ defaultStoreId ) {
2444
- if ($ testCase ->getStoreIdCounter > 0 ) {
2445
- return $ defaultStoreId ;
2446
- } else {
2447
- $ testCase ->getStoreIdCounter += 1 ;
2448
- return null ;
2449
- }
2450
- }));
2408
+ $ customer ->method ('getStoreId ' )
2409
+ ->willReturnCallback (fn () => $ this ->customerStoreId );
2451
2410
$ customer ->expects ($ this ->once ())
2452
2411
->method ('setStoreId ' )
2453
- ->with ($ defaultStoreId );
2412
+ ->with ($ defaultStoreId )
2413
+ ->willReturnCallback (function ($ storeId ) use ($ customer ) {
2414
+ $ this ->customerStoreId = $ storeId ;
2415
+ return $ customer ;
2416
+ });
2454
2417
2455
2418
$ address = $ this ->createMock (AddressInterface::class);
2456
2419
$ address ->expects ($ this ->once ())->method ('setCustomerId ' )->with ($ customerId );
@@ -2504,8 +2467,6 @@ public function testCreateAccountUnexpectedValueException(): void
2504
2467
->method ('newAccount ' )
2505
2468
->willThrowException ($ exception );
2506
2469
$ this ->logger ->expects ($ this ->once ())->method ('error ' )->with ($ exception );
2507
- $ this ->allowedCountriesReader ->expects ($ this ->atLeastOnce ())
2508
- ->method ('getAllowedCountries ' )->willReturn (['US ' => 'US ' ]);
2509
2470
$ address ->expects ($ this ->atLeastOnce ())->method ('getCountryId ' )->willReturn ('US ' );
2510
2471
$ this ->accountManagement ->createAccount ($ customer );
2511
2472
}
0 commit comments