8
8
9
9
use Magento \Customer \Api \AccountManagementInterface ;
10
10
use Magento \Customer \Api \CustomerRepositoryInterface ;
11
+ use Magento \Customer \Api \Data \CustomerInterfaceFactory ;
12
+ use Magento \Customer \Api \Data \AddressInterfaceFactory ;
13
+ use Magento \Customer \Api \Data \RegionInterfaceFactory ;
14
+ use Magento \Framework \Api \ExtensibleDataObjectConverter ;
15
+ use Magento \Framework \Api \DataObjectHelper ;
16
+ use Magento \Framework \Encryption \EncryptorInterface ;
17
+ use Magento \Customer \Api \Data \CustomerInterface ;
18
+ use Magento \Customer \Model \CustomerRegistry ;
11
19
use Magento \Framework \Api \SortOrder ;
20
+ use Magento \Framework \Config \CacheInterface ;
21
+ use Magento \Framework \ObjectManagerInterface ;
12
22
use Magento \TestFramework \Helper \Bootstrap ;
23
+ use Magento \Customer \Api \Data \AddressInterface ;
24
+ use Magento \Framework \Api \SearchCriteriaBuilder ;
25
+ use Magento \Framework \Api \FilterBuilder ;
26
+ use Magento \Framework \Api \SortOrderBuilder ;
27
+ use Magento \Framework \Exception \NoSuchEntityException ;
28
+ use Magento \Customer \Model \Customer ;
13
29
14
30
/**
15
31
* Checks Customer insert, update, search with repository
@@ -24,60 +40,65 @@ class CustomerRepositoryTest extends \PHPUnit\Framework\TestCase
24
40
/** @var CustomerRepositoryInterface */
25
41
private $ customerRepository ;
26
42
27
- /** @var \Magento\Framework\ ObjectManagerInterface */
43
+ /** @var ObjectManagerInterface */
28
44
private $ objectManager ;
29
45
30
- /** @var \Magento\Customer\Api\Data\ CustomerInterfaceFactory */
46
+ /** @var CustomerInterfaceFactory */
31
47
private $ customerFactory ;
32
48
33
- /** @var \Magento\Customer\Api\Data\ AddressInterfaceFactory */
49
+ /** @var AddressInterfaceFactory */
34
50
private $ addressFactory ;
35
51
36
- /** @var \Magento\Customer\Api\Data\ RegionInterfaceFactory */
52
+ /** @var RegionInterfaceFactory */
37
53
private $ regionFactory ;
38
54
39
- /** @var \Magento\Framework\Api\ ExtensibleDataObjectConverter */
55
+ /** @var ExtensibleDataObjectConverter */
40
56
private $ converter ;
41
57
42
- /** @var \Magento\Framework\Api\ DataObjectHelper */
58
+ /** @var DataObjectHelper */
43
59
protected $ dataObjectHelper ;
44
60
45
- /** @var \Magento\Framework\Encryption\ EncryptorInterface */
61
+ /** @var EncryptorInterface */
46
62
protected $ encryptor ;
47
63
48
- /** @var \Magento\Customer\Model\ CustomerRegistry */
64
+ /** @var CustomerRegistry */
49
65
protected $ customerRegistry ;
50
66
67
+ /**
68
+ * @inheritdoc
69
+ */
51
70
protected function setUp ()
52
71
{
53
72
$ this ->objectManager = Bootstrap::getObjectManager ();
54
- $ this ->customerRepository =
55
- $ this ->objectManager ->create (\Magento \Customer \Api \CustomerRepositoryInterface::class);
56
- $ this ->customerFactory =
57
- $ this ->objectManager ->create (\Magento \Customer \Api \Data \CustomerInterfaceFactory::class);
58
- $ this ->addressFactory = $ this ->objectManager ->create (\Magento \Customer \Api \Data \AddressInterfaceFactory::class);
59
- $ this ->regionFactory = $ this ->objectManager ->create (\Magento \Customer \Api \Data \RegionInterfaceFactory::class);
60
- $ this ->accountManagement =
61
- $ this ->objectManager ->create (\Magento \Customer \Api \AccountManagementInterface::class);
62
- $ this ->converter = $ this ->objectManager ->create (\Magento \Framework \Api \ExtensibleDataObjectConverter::class);
63
- $ this ->dataObjectHelper = $ this ->objectManager ->create (\Magento \Framework \Api \DataObjectHelper::class);
64
- $ this ->encryptor = $ this ->objectManager ->create (\Magento \Framework \Encryption \EncryptorInterface::class);
65
- $ this ->customerRegistry = $ this ->objectManager ->create (\Magento \Customer \Model \CustomerRegistry::class);
66
-
67
- /** @var \Magento\Framework\Config\CacheInterface $cache */
68
- $ cache = $ this ->objectManager ->create (\Magento \Framework \Config \CacheInterface::class);
73
+ $ this ->customerRepository = $ this ->objectManager ->create (CustomerRepositoryInterface::class);
74
+ $ this ->customerFactory = $ this ->objectManager ->create (CustomerInterfaceFactory::class);
75
+ $ this ->addressFactory = $ this ->objectManager ->create (AddressInterfaceFactory::class);
76
+ $ this ->regionFactory = $ this ->objectManager ->create (RegionInterfaceFactory::class);
77
+ $ this ->accountManagement = $ this ->objectManager ->create (AccountManagementInterface::class);
78
+ $ this ->converter = $ this ->objectManager ->create (ExtensibleDataObjectConverter::class);
79
+ $ this ->dataObjectHelper = $ this ->objectManager ->create (DataObjectHelper::class);
80
+ $ this ->encryptor = $ this ->objectManager ->create (EncryptorInterface::class);
81
+ $ this ->customerRegistry = $ this ->objectManager ->create (CustomerRegistry::class);
82
+
83
+ /** @var CacheInterface $cache */
84
+ $ cache = $ this ->objectManager ->create (CacheInterface::class);
69
85
$ cache ->remove ('extension_attributes_config ' );
70
86
}
71
87
88
+ /**
89
+ * @inheritdoc
90
+ */
72
91
protected function tearDown ()
73
92
{
74
- $ objectManager = \ Magento \ TestFramework \ Helper \ Bootstrap::getObjectManager ();
93
+ $ objectManager = Bootstrap::getObjectManager ();
75
94
/** @var \Magento\Customer\Model\CustomerRegistry $customerRegistry */
76
- $ customerRegistry = $ objectManager ->get (\ Magento \ Customer \ Model \ CustomerRegistry::class);
95
+ $ customerRegistry = $ objectManager ->get (CustomerRegistry::class);
77
96
$ customerRegistry ->remove (1 );
78
97
}
79
98
80
99
/**
100
+ * Check if first name update was successful
101
+ *
81
102
* @magentoDbIsolation enabled
82
103
*/
83
104
public function testCreateCustomerNewThenUpdateFirstName ()
@@ -99,7 +120,7 @@ public function testCreateCustomerNewThenUpdateFirstName()
99
120
$ newCustomerFirstname = 'New First Name ' ;
100
121
$ updatedCustomer = $ this ->customerFactory ->create ();
101
122
$ this ->dataObjectHelper ->mergeDataObjects (
102
- \ Magento \ Customer \ Api \ Data \ CustomerInterface::class,
123
+ CustomerInterface::class,
103
124
$ updatedCustomer ,
104
125
$ customer
105
126
);
@@ -112,6 +133,8 @@ public function testCreateCustomerNewThenUpdateFirstName()
112
133
}
113
134
114
135
/**
136
+ * Test create new customer
137
+ *
115
138
* @magentoDbIsolation enabled
116
139
*/
117
140
public function testCreateNewCustomer ()
@@ -139,6 +162,8 @@ public function testCreateNewCustomer()
139
162
}
140
163
141
164
/**
165
+ * Test update customer
166
+ *
142
167
* @dataProvider updateCustomerDataProvider
143
168
* @magentoAppArea frontend
144
169
* @magentoDataFixture Magento/Customer/_files/customer.php
@@ -168,7 +193,7 @@ public function testUpdateCustomer($defaultBilling, $defaultShipping)
168
193
$ this ->dataObjectHelper ->populateWithArray (
169
194
$ customerDetails ,
170
195
$ customerData ,
171
- \ Magento \ Customer \ Api \ Data \ CustomerInterface::class
196
+ CustomerInterface::class
172
197
);
173
198
$ this ->customerRepository ->save ($ customerDetails , $ newPasswordHash );
174
199
$ customerAfter = $ this ->customerRepository ->getById ($ existingCustomerId );
@@ -187,12 +212,12 @@ public function testUpdateCustomer($defaultBilling, $defaultShipping)
187
212
$ attributesBefore = $ this ->converter ->toFlatArray (
188
213
$ customerBefore ,
189
214
[],
190
- \ Magento \ Customer \ Api \ Data \ CustomerInterface::class
215
+ CustomerInterface::class
191
216
);
192
217
$ attributesAfter = $ this ->converter ->toFlatArray (
193
218
$ customerAfter ,
194
219
[],
195
- \ Magento \ Customer \ Api \ Data \ CustomerInterface::class
220
+ CustomerInterface::class
196
221
);
197
222
// ignore 'updated_at'
198
223
unset($ attributesBefore ['updated_at ' ]);
@@ -215,6 +240,8 @@ public function testUpdateCustomer($defaultBilling, $defaultShipping)
215
240
}
216
241
217
242
/**
243
+ * Test update customer address
244
+ *
218
245
* @magentoAppArea frontend
219
246
* @magentoDataFixture Magento/Customer/_files/customer.php
220
247
* @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
@@ -233,14 +260,14 @@ public function testUpdateCustomerAddress()
233
260
$ this ->dataObjectHelper ->populateWithArray (
234
261
$ newAddressDataObject ,
235
262
$ newAddress ,
236
- \ Magento \ Customer \ Api \ Data \ AddressInterface::class
263
+ AddressInterface::class
237
264
);
238
265
$ newAddressDataObject ->setRegion ($ addresses [0 ]->getRegion ());
239
266
$ newCustomerEntity = $ this ->customerFactory ->create ();
240
267
$ this ->dataObjectHelper ->populateWithArray (
241
268
$ newCustomerEntity ,
242
269
$ customerDetails ,
243
- \ Magento \ Customer \ Api \ Data \ CustomerInterface::class
270
+ CustomerInterface::class
244
271
);
245
272
$ newCustomerEntity ->setId ($ customerId )
246
273
->setAddresses ([$ newAddressDataObject , $ addresses [1 ]]);
@@ -256,6 +283,8 @@ public function testUpdateCustomerAddress()
256
283
}
257
284
258
285
/**
286
+ * Test preserve all addresses after customer update
287
+ *
259
288
* @magentoAppArea frontend
260
289
* @magentoDataFixture Magento/Customer/_files/customer.php
261
290
* @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
@@ -265,22 +294,37 @@ public function testUpdateCustomerPreserveAllAddresses()
265
294
$ customerId = 1 ;
266
295
$ customer = $ this ->customerRepository ->getById ($ customerId );
267
296
$ customerDetails = $ customer ->__toArray ();
297
+ $ defaultBilling = $ customerDetails ['default_billing ' ];
298
+ $ defaultShipping = $ customerDetails ['default_shipping ' ];
268
299
$ newCustomerEntity = $ this ->customerFactory ->create ();
269
300
$ this ->dataObjectHelper ->populateWithArray (
270
301
$ newCustomerEntity ,
271
302
$ customerDetails ,
272
- \ Magento \ Customer \ Api \ Data \ CustomerInterface::class
303
+ CustomerInterface::class
273
304
);
274
305
$ newCustomerEntity ->setId ($ customer ->getId ())
275
306
->setAddresses (null );
276
307
$ this ->customerRepository ->save ($ newCustomerEntity );
277
308
278
309
$ newCustomerDetails = $ this ->customerRepository ->getById ($ customerId );
310
+ $ newCustomerArray = $ newCustomerDetails ->__toArray ();
279
311
//Verify that old addresses are still present
280
312
$ this ->assertEquals (2 , count ($ newCustomerDetails ->getAddresses ()));
313
+ $ this ->assertEquals (
314
+ $ defaultBilling ,
315
+ $ newCustomerArray ['default_billing ' ],
316
+ "Default billing invalid value "
317
+ );
318
+ $ this ->assertEquals (
319
+ $ defaultShipping ,
320
+ $ newCustomerArray ['default_shipping ' ],
321
+ "Default shipping invalid value "
322
+ );
281
323
}
282
324
283
325
/**
326
+ * Test update delete all addresses with empty arrays
327
+ *
284
328
* @magentoAppArea frontend
285
329
* @magentoDataFixture Magento/Customer/_files/customer.php
286
330
* @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
@@ -294,18 +338,31 @@ public function testUpdateCustomerDeleteAllAddressesWithEmptyArray()
294
338
$ this ->dataObjectHelper ->populateWithArray (
295
339
$ newCustomerEntity ,
296
340
$ customerDetails ,
297
- \ Magento \ Customer \ Api \ Data \ CustomerInterface::class
341
+ CustomerInterface::class
298
342
);
299
343
$ newCustomerEntity ->setId ($ customer ->getId ())
300
344
->setAddresses ([]);
301
345
$ this ->customerRepository ->save ($ newCustomerEntity );
302
346
303
347
$ newCustomerDetails = $ this ->customerRepository ->getById ($ customerId );
348
+ $ newCustomerArray = $ newCustomerDetails ->__toArray ();
304
349
//Verify that old addresses are removed
305
350
$ this ->assertEquals (0 , count ($ newCustomerDetails ->getAddresses ()));
351
+ $ this ->assertEquals (
352
+ $ newCustomerArray ['default_billing ' ],
353
+ null ,
354
+ "Default billing invalid value "
355
+ );
356
+ $ this ->assertEquals (
357
+ $ newCustomerArray ['default_shipping ' ],
358
+ null ,
359
+ "Default shipping invalid value "
360
+ );
306
361
}
307
362
308
363
/**
364
+ * Test search customers
365
+ *
309
366
* @param \Magento\Framework\Api\Filter[] $filters
310
367
* @param \Magento\Framework\Api\Filter[] $filterGroup
311
368
* @param array $expectedResult array of expected results indexed by ID
@@ -317,9 +374,8 @@ public function testUpdateCustomerDeleteAllAddressesWithEmptyArray()
317
374
*/
318
375
public function testSearchCustomers ($ filters , $ filterGroup , $ expectedResult )
319
376
{
320
- /** @var \Magento\Framework\Api\SearchCriteriaBuilder $searchBuilder */
321
- $ searchBuilder = Bootstrap::getObjectManager ()
322
- ->create (\Magento \Framework \Api \SearchCriteriaBuilder::class);
377
+ /** @var SearchCriteriaBuilder $searchBuilder */
378
+ $ searchBuilder = Bootstrap::getObjectManager ()->create (SearchCriteriaBuilder::class);
323
379
foreach ($ filters as $ filter ) {
324
380
$ searchBuilder ->addFilters ([$ filter ]);
325
381
}
@@ -346,19 +402,19 @@ public function testSearchCustomers($filters, $filterGroup, $expectedResult)
346
402
*/
347
403
public function testSearchCustomersOrder ()
348
404
{
349
- /** @var \Magento\Framework\Api\ SearchCriteriaBuilder $searchBuilder */
405
+ /** @var SearchCriteriaBuilder $searchBuilder */
350
406
$ objectManager = Bootstrap::getObjectManager ();
351
- $ searchBuilder = $ objectManager ->create (\ Magento \ Framework \ Api \ SearchCriteriaBuilder::class);
407
+ $ searchBuilder = $ objectManager ->create (SearchCriteriaBuilder::class);
352
408
353
409
// Filter for 'firstname' like 'First'
354
- $ filterBuilder = $ objectManager ->create (\ Magento \ Framework \ Api \ FilterBuilder::class);
410
+ $ filterBuilder = $ objectManager ->create (FilterBuilder::class);
355
411
$ firstnameFilter = $ filterBuilder ->setField ('firstname ' )
356
412
->setConditionType ('like ' )
357
413
->setValue ('First% ' )
358
414
->create ();
359
415
$ searchBuilder ->addFilters ([$ firstnameFilter ]);
360
416
// Search ascending order
361
- $ sortOrderBuilder = $ objectManager ->create (\ Magento \ Framework \ Api \ SortOrderBuilder::class);
417
+ $ sortOrderBuilder = $ objectManager ->create (SortOrderBuilder::class);
362
418
$ sortOrder = $ sortOrderBuilder
363
419
->setField ('lastname ' )
364
420
->setDirection (SortOrder::SORT_ASC )
@@ -383,6 +439,8 @@ public function testSearchCustomersOrder()
383
439
}
384
440
385
441
/**
442
+ * Test delete
443
+ *
386
444
* @magentoAppArea adminhtml
387
445
* @magentoDataFixture Magento/Customer/_files/customer.php
388
446
* @magentoAppIsolation enabled
@@ -393,12 +451,14 @@ public function testDelete()
393
451
$ customer = $ this ->customerRepository ->get ($ fixtureCustomerEmail );
394
452
$ this ->customerRepository ->delete ($ customer );
395
453
/** Ensure that customer was deleted */
396
- $ this ->expectException (\ Magento \ Framework \ Exception \ NoSuchEntityException::class);
454
+ $ this ->expectException (NoSuchEntityException::class);
397
455
$ this ->expectExceptionMessage ('No such entity with email = customer@example.com, websiteId = 1 ' );
398
456
$ this ->customerRepository ->get ($ fixtureCustomerEmail );
399
457
}
400
458
401
459
/**
460
+ * Test delete by id
461
+ *
402
462
* @magentoAppArea adminhtml
403
463
* @magentoDataFixture Magento/Customer/_files/customer.php
404
464
* @magentoAppIsolation enabled
@@ -409,7 +469,7 @@ public function testDeleteById()
409
469
$ fixtureCustomerId = 1 ;
410
470
$ this ->customerRepository ->deleteById ($ fixtureCustomerId );
411
471
/** Ensure that customer was deleted */
412
- $ this ->expectException (\ Magento \ Framework \ Exception \ NoSuchEntityException::class);
472
+ $ this ->expectException (NoSuchEntityException::class);
413
473
$ this ->expectExceptionMessage ('No such entity with email = customer@example.com, websiteId = 1 ' );
414
474
$ this ->customerRepository ->get ($ fixtureCustomerEmail );
415
475
}
@@ -433,9 +493,14 @@ public function updateCustomerDataProvider()
433
493
];
434
494
}
435
495
496
+ /**
497
+ * Search customer data provider
498
+ *
499
+ * @return array
500
+ */
436
501
public function searchCustomersDataProvider ()
437
502
{
438
- $ builder = Bootstrap::getObjectManager ()->create (\ Magento \ Framework \ Api \ FilterBuilder::class);
503
+ $ builder = Bootstrap::getObjectManager ()->create (FilterBuilder::class);
439
504
return [
440
505
'Customer with specific email ' => [
441
506
[$ builder ->setField ('email ' )->setValue ('customer@search.example.com ' )->create ()],
@@ -485,9 +550,9 @@ protected function expectedDefaultShippingsInCustomerModelAttributes(
485
550
$ defaultShipping
486
551
) {
487
552
/**
488
- * @var \Magento\Customer\Model\ Customer $customer
553
+ * @var Customer $customer
489
554
*/
490
- $ customer = $ this ->objectManager ->create (\ Magento \ Customer \ Model \ Customer::class);
555
+ $ customer = $ this ->objectManager ->create (Customer::class);
491
556
/** @var \Magento\Customer\Model\Customer $customer */
492
557
$ customer ->load ($ customerId );
493
558
$ this ->assertEquals (
@@ -503,6 +568,8 @@ protected function expectedDefaultShippingsInCustomerModelAttributes(
503
568
}
504
569
505
570
/**
571
+ * Test update default shipping and default billing address
572
+ *
506
573
* @magentoDataFixture Magento/Customer/_files/customer.php
507
574
* @magentoDbIsolation enabled
508
575
*/
@@ -530,13 +597,13 @@ public function testUpdateDefaultShippingAndDefaultBillingTest()
530
597
$ this ->assertEquals (
531
598
$ savedCustomer ->getDefaultBilling (),
532
599
$ oldDefaultBilling ,
533
- 'Default billing shoud not be overridden '
600
+ 'Default billing should not be overridden '
534
601
);
535
602
536
603
$ this ->assertEquals (
537
604
$ savedCustomer ->getDefaultShipping (),
538
605
$ oldDefaultShipping ,
539
- 'Default shipping shoud not be overridden '
606
+ 'Default shipping should not be overridden '
540
607
);
541
608
}
542
609
}
0 commit comments