8
8
use Magento \Catalog \Model \ProductRepository ;
9
9
use Magento \Customer \Api \Data \CustomerInterfaceFactory ;
10
10
use Magento \Framework \Exception \LocalizedException ;
11
+ use Magento \Quote \Api \Data \CartInterface ;
11
12
use Magento \TestFramework \Helper \Bootstrap ;
13
+ use Magento \TestFramework \ObjectManager ;
14
+ use Magento \Framework \Api \SearchCriteriaBuilder ;
15
+ use Magento \Quote \Api \CartRepositoryInterface ;
12
16
13
17
/**
14
18
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
15
19
*/
16
20
class QuoteTest extends \PHPUnit \Framework \TestCase
17
21
{
22
+ /**
23
+ * @var ObjectManager
24
+ */
25
+ private $ objectManager ;
26
+
27
+ /**
28
+ * @inheritdoc
29
+ */
30
+ protected function setUp ()
31
+ {
32
+ $ this ->objectManager = Bootstrap::getObjectManager ();
33
+ }
34
+
18
35
private function convertToArray ($ entity )
19
36
{
20
- return Bootstrap:: getObjectManager ()
37
+ return $ this -> objectManager
21
38
->create (\Magento \Framework \Api \ExtensibleDataObjectConverter::class)
22
39
->toFlatArray ($ entity );
23
40
}
@@ -28,10 +45,10 @@ private function convertToArray($entity)
28
45
*/
29
46
public function testCollectTotalsWithVirtual ()
30
47
{
31
- $ quote = Bootstrap:: getObjectManager ()-> create (\ Magento \ Quote \ Model \ Quote::class);
48
+ $ quote = $ this -> objectManager -> create (Quote::class);
32
49
$ quote ->load ('test01 ' , 'reserved_order_id ' );
33
50
34
- $ productRepository = Bootstrap:: getObjectManager () ->create (
51
+ $ productRepository = $ this -> objectManager ->create (
35
52
\Magento \Catalog \Api \ProductRepositoryInterface::class
36
53
);
37
54
$ product = $ productRepository ->get ('virtual-product ' , false , null , true );
@@ -46,14 +63,14 @@ public function testCollectTotalsWithVirtual()
46
63
47
64
public function testSetCustomerData ()
48
65
{
49
- /** @var \Magento\Quote\Model\ Quote $quote */
50
- $ quote = Bootstrap:: getObjectManager ()-> create (\ Magento \ Quote \ Model \ Quote::class);
66
+ /** @var Quote $quote */
67
+ $ quote = $ this -> objectManager -> create (Quote::class);
51
68
/** @var CustomerInterfaceFactory $customerFactory */
52
- $ customerFactory = Bootstrap:: getObjectManager () ->create (
69
+ $ customerFactory = $ this -> objectManager ->create (
53
70
CustomerInterfaceFactory::class
54
71
);
55
72
/** @var \Magento\Framework\Api\DataObjectHelper $dataObjectHelper */
56
- $ dataObjectHelper = Bootstrap:: getObjectManager () ->create (\Magento \Framework \Api \DataObjectHelper::class);
73
+ $ dataObjectHelper = $ this -> objectManager ->create (\Magento \Framework \Api \DataObjectHelper::class);
57
74
$ expected = $ this ->_getCustomerDataArray ();
58
75
$ customer = $ customerFactory ->create ();
59
76
$ dataObjectHelper ->populateWithArray (
@@ -72,13 +89,13 @@ public function testSetCustomerData()
72
89
73
90
public function testUpdateCustomerData ()
74
91
{
75
- /** @var \Magento\Quote\Model\ Quote $quote */
76
- $ quote = Bootstrap:: getObjectManager ()-> create (\ Magento \ Quote \ Model \ Quote::class);
77
- $ customerFactory = Bootstrap:: getObjectManager () ->create (
92
+ /** @var Quote $quote */
93
+ $ quote = $ this -> objectManager -> create (Quote::class);
94
+ $ customerFactory = $ this -> objectManager ->create (
78
95
CustomerInterfaceFactory::class
79
96
);
80
97
/** @var \Magento\Framework\Api\DataObjectHelper $dataObjectHelper */
81
- $ dataObjectHelper = Bootstrap:: getObjectManager () ->create (\Magento \Framework \Api \DataObjectHelper::class);
98
+ $ dataObjectHelper = $ this -> objectManager ->create (\Magento \Framework \Api \DataObjectHelper::class);
82
99
$ expected = $ this ->_getCustomerDataArray ();
83
100
//For save in repository
84
101
$ expected = $ this ->removeIdFromCustomerData ($ expected );
@@ -92,7 +109,7 @@ public function testUpdateCustomerData()
92
109
/**
93
110
* @var \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
94
111
*/
95
- $ customerRepository = Bootstrap:: getObjectManager ()
112
+ $ customerRepository = $ this -> objectManager
96
113
->create (\Magento \Customer \Api \CustomerRepositoryInterface::class);
97
114
$ customerRepository ->save ($ customerDataSet );
98
115
$ quote ->setCustomer ($ customerDataSet );
@@ -121,13 +138,13 @@ public function testGetCustomerGroupFromCustomer()
121
138
{
122
139
/** Preconditions */
123
140
/** @var CustomerInterfaceFactory $customerFactory */
124
- $ customerFactory = Bootstrap:: getObjectManager () ->create (
141
+ $ customerFactory = $ this -> objectManager ->create (
125
142
CustomerInterfaceFactory::class
126
143
);
127
144
$ customerGroupId = 3 ;
128
145
$ customerData = $ customerFactory ->create ()->setId (1 )->setGroupId ($ customerGroupId );
129
- /** @var \Magento\Quote\Model\ Quote $quote */
130
- $ quote = Bootstrap:: getObjectManager ()-> create (\ Magento \ Quote \ Model \ Quote::class);
146
+ /** @var Quote $quote */
147
+ $ quote = $ this -> objectManager -> create (Quote::class);
131
148
$ quote ->setCustomer ($ customerData );
132
149
$ quote ->unsetData ('customer_group_id ' );
133
150
@@ -146,10 +163,10 @@ public function testGetCustomerTaxClassId()
146
163
$ fixtureGroupCode = 'custom_group ' ;
147
164
$ fixtureTaxClassId = 3 ;
148
165
/** @var \Magento\Customer\Model\Group $group */
149
- $ group = Bootstrap:: getObjectManager () ->create (\Magento \Customer \Model \Group::class);
166
+ $ group = $ this -> objectManager ->create (\Magento \Customer \Model \Group::class);
150
167
$ fixtureGroupId = $ group ->load ($ fixtureGroupCode , 'customer_group_code ' )->getId ();
151
- /** @var \Magento\Quote\Model\ Quote $quote */
152
- $ quote = Bootstrap:: getObjectManager ()-> create (\ Magento \ Quote \ Model \ Quote::class);
168
+ /** @var Quote $quote */
169
+ $ quote = $ this -> objectManager -> create (Quote::class);
153
170
$ quote ->setCustomerGroupId ($ fixtureGroupId );
154
171
155
172
/** Execute SUT */
@@ -169,8 +186,8 @@ public function testAssignCustomerWithAddressChangeAddressesNotSpecified()
169
186
* Customer with two addresses created
170
187
* First address is default billing, second is default shipping.
171
188
*/
172
- /** @var \Magento\Quote\Model\ Quote $quote */
173
- $ quote = Bootstrap:: getObjectManager ()-> create (\ Magento \ Quote \ Model \ Quote::class);
189
+ /** @var Quote $quote */
190
+ $ quote = $ this -> objectManager -> create (Quote::class);
174
191
$ customerData = $ this ->_prepareQuoteForTestAssignCustomerWithAddressChange ($ quote );
175
192
176
193
/** Execute SUT */
@@ -234,9 +251,8 @@ public function testAssignCustomerWithAddressChange()
234
251
* Customer with two addresses created
235
252
* First address is default billing, second is default shipping.
236
253
*/
237
- /** @var \Magento\Quote\Model\Quote $quote */
238
- $ objectManager = Bootstrap::getObjectManager ();
239
- $ quote = $ objectManager ->create (\Magento \Quote \Model \Quote::class);
254
+ /** @var Quote $quote */
255
+ $ quote = $ this ->objectManager ->create (Quote::class);
240
256
$ customerData = $ this ->_prepareQuoteForTestAssignCustomerWithAddressChange ($ quote );
241
257
/** @var \Magento\Quote\Model\Quote\Address $quoteBillingAddress */
242
258
$ expectedBillingAddressData = [
@@ -249,7 +265,7 @@ public function testAssignCustomerWithAddressChange()
249
265
'firstname ' => 'FirstBilling ' ,
250
266
'region_id ' => 1
251
267
];
252
- $ quoteBillingAddress = $ objectManager ->create (\Magento \Quote \Model \Quote \Address::class);
268
+ $ quoteBillingAddress = $ this -> objectManager ->create (\Magento \Quote \Model \Quote \Address::class);
253
269
$ quoteBillingAddress ->setData ($ expectedBillingAddressData );
254
270
255
271
$ expectedShippingAddressData = [
@@ -262,7 +278,7 @@ public function testAssignCustomerWithAddressChange()
262
278
'firstname ' => 'FirstShipping ' ,
263
279
'region_id ' => 1
264
280
];
265
- $ quoteShippingAddress = $ objectManager ->create (\Magento \Quote \Model \Quote \Address::class);
281
+ $ quoteShippingAddress = $ this -> objectManager ->create (\Magento \Quote \Model \Quote \Address::class);
266
282
$ quoteShippingAddress ->setData ($ expectedShippingAddressData );
267
283
268
284
/** Execute SUT */
@@ -295,13 +311,13 @@ public function testAssignCustomerWithAddressChange()
295
311
*/
296
312
public function testAddProductUpdateItem ()
297
313
{
298
- /** @var \Magento\Quote\Model\ Quote $quote */
299
- $ quote = Bootstrap:: getObjectManager ()-> create (\ Magento \ Quote \ Model \ Quote::class);
314
+ /** @var Quote $quote */
315
+ $ quote = $ this -> objectManager -> create (Quote::class);
300
316
$ quote ->load ('test01 ' , 'reserved_order_id ' );
301
317
302
318
$ productStockQty = 100 ;
303
319
304
- $ productRepository = Bootstrap:: getObjectManager () ->create (
320
+ $ productRepository = $ this -> objectManager ->create (
305
321
\Magento \Catalog \Api \ProductRepositoryInterface::class
306
322
);
307
323
$ product = $ productRepository ->get ('simple-1 ' , false , null , true );
@@ -334,17 +350,16 @@ public function testAddProductUpdateItem()
334
350
*
335
351
* Customer with two addresses created. First address is default billing, second is default shipping.
336
352
*
337
- * @param \Magento\Quote\Model\ Quote $quote
353
+ * @param Quote $quote
338
354
* @return \Magento\Customer\Api\Data\CustomerInterface
339
355
*/
340
356
protected function _prepareQuoteForTestAssignCustomerWithAddressChange ($ quote )
341
357
{
342
- $ objectManager = Bootstrap::getObjectManager ();
343
358
/** @var \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository */
344
- $ customerRepository = $ objectManager ->create (\Magento \Customer \Api \CustomerRepositoryInterface::class);
359
+ $ customerRepository = $ this -> objectManager ->create (\Magento \Customer \Api \CustomerRepositoryInterface::class);
345
360
$ fixtureCustomerId = 1 ;
346
361
/** @var \Magento\Customer\Model\Customer $customer */
347
- $ customer = $ objectManager ->create (\Magento \Customer \Model \Customer::class);
362
+ $ customer = $ this -> objectManager ->create (\Magento \Customer \Model \Customer::class);
348
363
$ fixtureSecondAddressId = 2 ;
349
364
$ customer ->load ($ fixtureCustomerId )->setDefaultShipping ($ fixtureSecondAddressId )->save ();
350
365
$ customerData = $ customerRepository ->getById ($ fixtureCustomerId );
@@ -413,9 +428,8 @@ protected function _getCustomerDataArray()
413
428
*/
414
429
public function testReserveOrderId ()
415
430
{
416
- $ objectManager = Bootstrap::getObjectManager ();
417
- /** @var \Magento\Quote\Model\Quote $quote */
418
- $ quote = $ objectManager ->create (\Magento \Quote \Model \Quote::class);
431
+ /** @var Quote $quote */
432
+ $ quote = $ this ->objectManager ->create (Quote::class);
419
433
$ quote ->load ('reserved_order_id ' , 'reserved_order_id ' );
420
434
$ quote ->reserveOrderId ();
421
435
$ this ->assertEquals ('reserved_order_id ' , $ quote ->getReservedOrderId ());
@@ -431,18 +445,17 @@ public function testReserveOrderId()
431
445
public function testAddedProductToQuoteIsSalable ()
432
446
{
433
447
$ productId = 99 ;
434
- $ objectManager = Bootstrap::getObjectManager ();
435
448
436
449
/** @var ProductRepository $productRepository */
437
- $ productRepository = $ objectManager ->get (ProductRepository::class);
450
+ $ productRepository = $ this -> objectManager ->get (ProductRepository::class);
438
451
439
- /** @var \Magento\Quote\Model\ Quote $quote */
452
+ /** @var Quote $quote */
440
453
$ product = $ productRepository ->getById ($ productId , false , null , true );
441
454
442
455
$ this ->expectException (LocalizedException::class);
443
456
$ this ->expectExceptionMessage ('Product that you are trying to add is not available. ' );
444
457
445
- $ quote = $ objectManager ->create (\ Magento \ Quote \ Model \ Quote::class);
458
+ $ quote = $ this -> objectManager ->create (Quote::class);
446
459
$ quote ->addProduct ($ product );
447
460
}
448
461
@@ -452,13 +465,12 @@ public function testAddedProductToQuoteIsSalable()
452
465
*/
453
466
public function testGetItemById ()
454
467
{
455
- $ objectManager = Bootstrap::getObjectManager ();
456
- $ quote = $ objectManager ->create (\Magento \Quote \Model \Quote::class);
468
+ $ quote = $ this ->objectManager ->create (Quote::class);
457
469
$ quote ->load ('test01 ' , 'reserved_order_id ' );
458
470
459
- $ quoteItem = $ objectManager ->create (\Magento \Quote \Model \Quote \Item::class);
471
+ $ quoteItem = $ this -> objectManager ->create (\Magento \Quote \Model \Quote \Item::class);
460
472
461
- $ productRepository = $ objectManager ->create (\Magento \Catalog \Api \ProductRepositoryInterface::class);
473
+ $ productRepository = $ this -> objectManager ->create (\Magento \Catalog \Api \ProductRepositoryInterface::class);
462
474
$ product = $ productRepository ->get ('simple ' );
463
475
464
476
$ quoteItem ->setProduct ($ product );
@@ -468,4 +480,44 @@ public function testGetItemById()
468
480
$ this ->assertInstanceOf (\Magento \Quote \Model \Quote \Item::class, $ quote ->getItemById ($ quoteItem ->getId ()));
469
481
$ this ->assertEquals ($ quoteItem ->getId (), $ quote ->getItemById ($ quoteItem ->getId ())->getId ());
470
482
}
483
+
484
+ /**
485
+ * Tests of quotes merging.
486
+ *
487
+ * @magentoDataFixture Magento/Sales/_files/quote.php
488
+ */
489
+ public function testMerge ()
490
+ {
491
+ $ giftMessageId = 1 ;
492
+
493
+ /** @var Quote $quote */
494
+ $ guestQuote = $ this ->getQuote ('test01 ' );
495
+ $ guestQuote ->setGiftMessageId ($ giftMessageId );
496
+
497
+ /** @var Quote $customerQuote */
498
+ $ customerQuote = $ this ->objectManager ->create (Quote::class);
499
+ $ customerQuote ->merge ($ guestQuote );
500
+
501
+ self ::assertEquals ($ giftMessageId , $ customerQuote ->getGiftMessageId ());
502
+ }
503
+
504
+ /**
505
+ * Gets quote by reserved order id.
506
+ *
507
+ * @param string $reservedOrderId
508
+ * @return Quote
509
+ */
510
+ private function getQuote ($ reservedOrderId )
511
+ {
512
+ /** @var SearchCriteriaBuilder $searchCriteriaBuilder */
513
+ $ searchCriteriaBuilder = $ this ->objectManager ->get (SearchCriteriaBuilder::class);
514
+ $ searchCriteria = $ searchCriteriaBuilder ->addFilter ('reserved_order_id ' , $ reservedOrderId )
515
+ ->create ();
516
+
517
+ /** @var CartRepositoryInterface $quoteRepository */
518
+ $ quoteRepository = $ this ->objectManager ->get (CartRepositoryInterface::class);
519
+ $ items = $ quoteRepository ->getList ($ searchCriteria )->getItems ();
520
+
521
+ return array_pop ($ items );
522
+ }
471
523
}
0 commit comments