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 );
@@ -336,17 +352,16 @@ public function testAddProductUpdateItem()
336
352
*
337
353
* Customer with two addresses created. First address is default billing, second is default shipping.
338
354
*
339
- * @param \Magento\Quote\Model\ Quote $quote
355
+ * @param Quote $quote
340
356
* @return \Magento\Customer\Api\Data\CustomerInterface
341
357
*/
342
358
protected function _prepareQuoteForTestAssignCustomerWithAddressChange ($ quote )
343
359
{
344
- $ objectManager = Bootstrap::getObjectManager ();
345
360
/** @var \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository */
346
- $ customerRepository = $ objectManager ->create (\Magento \Customer \Api \CustomerRepositoryInterface::class);
361
+ $ customerRepository = $ this -> objectManager ->create (\Magento \Customer \Api \CustomerRepositoryInterface::class);
347
362
$ fixtureCustomerId = 1 ;
348
363
/** @var \Magento\Customer\Model\Customer $customer */
349
- $ customer = $ objectManager ->create (\Magento \Customer \Model \Customer::class);
364
+ $ customer = $ this -> objectManager ->create (\Magento \Customer \Model \Customer::class);
350
365
$ fixtureSecondAddressId = 2 ;
351
366
$ customer ->load ($ fixtureCustomerId )->setDefaultShipping ($ fixtureSecondAddressId )->save ();
352
367
$ customerData = $ customerRepository ->getById ($ fixtureCustomerId );
@@ -415,9 +430,8 @@ protected function _getCustomerDataArray()
415
430
*/
416
431
public function testReserveOrderId ()
417
432
{
418
- $ objectManager = Bootstrap::getObjectManager ();
419
- /** @var \Magento\Quote\Model\Quote $quote */
420
- $ quote = $ objectManager ->create (\Magento \Quote \Model \Quote::class);
433
+ /** @var Quote $quote */
434
+ $ quote = $ this ->objectManager ->create (Quote::class);
421
435
$ quote ->load ('reserved_order_id ' , 'reserved_order_id ' );
422
436
$ quote ->reserveOrderId ();
423
437
$ this ->assertEquals ('reserved_order_id ' , $ quote ->getReservedOrderId ());
@@ -433,20 +447,19 @@ public function testReserveOrderId()
433
447
public function testAddedProductToQuoteIsSalable ()
434
448
{
435
449
$ productId = 99 ;
436
- $ objectManager = Bootstrap::getObjectManager ();
437
450
438
451
/** @var ProductRepository $productRepository */
439
- $ productRepository = $ objectManager ->get (ProductRepository::class);
452
+ $ productRepository = $ this -> objectManager ->get (ProductRepository::class);
440
453
441
- /** @var \Magento\Quote\Model\ Quote $quote */
454
+ /** @var Quote $quote */
442
455
$ product = $ productRepository ->getById ($ productId , false , null , true );
443
456
444
457
$ this ->expectException (
445
458
LocalizedException::class,
446
459
'Product that you are trying to add is not available. '
447
460
);
448
461
449
- $ quote = $ objectManager ->create (\ Magento \ Quote \ Model \ Quote::class);
462
+ $ quote = $ this -> objectManager ->create (Quote::class);
450
463
$ quote ->addProduct ($ product );
451
464
}
452
465
@@ -456,13 +469,12 @@ public function testAddedProductToQuoteIsSalable()
456
469
*/
457
470
public function testGetItemById ()
458
471
{
459
- $ objectManager = Bootstrap::getObjectManager ();
460
- $ quote = $ objectManager ->create (\Magento \Quote \Model \Quote::class);
472
+ $ quote = $ this ->objectManager ->create (Quote::class);
461
473
$ quote ->load ('test01 ' , 'reserved_order_id ' );
462
474
463
- $ quoteItem = $ objectManager ->create (\Magento \Quote \Model \Quote \Item::class);
475
+ $ quoteItem = $ this -> objectManager ->create (\Magento \Quote \Model \Quote \Item::class);
464
476
465
- $ productRepository = $ objectManager ->create (\Magento \Catalog \Api \ProductRepositoryInterface::class);
477
+ $ productRepository = $ this -> objectManager ->create (\Magento \Catalog \Api \ProductRepositoryInterface::class);
466
478
$ product = $ productRepository ->get ('simple ' );
467
479
468
480
$ quoteItem ->setProduct ($ product );
@@ -472,4 +484,44 @@ public function testGetItemById()
472
484
$ this ->assertInstanceOf (\Magento \Quote \Model \Quote \Item::class, $ quote ->getItemById ($ quoteItem ->getId ()));
473
485
$ this ->assertEquals ($ quoteItem ->getId (), $ quote ->getItemById ($ quoteItem ->getId ())->getId ());
474
486
}
487
+
488
+ /**
489
+ * Tests of quotes merging.
490
+ *
491
+ * @magentoDataFixture Magento/Sales/_files/quote.php
492
+ */
493
+ public function testMerge ()
494
+ {
495
+ $ giftMessageId = 1 ;
496
+
497
+ /** @var Quote $quote */
498
+ $ guestQuote = $ this ->getQuote ('test01 ' );
499
+ $ guestQuote ->setGiftMessageId ($ giftMessageId );
500
+
501
+ /** @var Quote $customerQuote */
502
+ $ customerQuote = $ this ->objectManager ->create (Quote::class);
503
+ $ customerQuote ->merge ($ guestQuote );
504
+
505
+ self ::assertEquals ($ giftMessageId , $ customerQuote ->getGiftMessageId ());
506
+ }
507
+
508
+ /**
509
+ * Gets quote by reserved order id.
510
+ *
511
+ * @param string $reservedOrderId
512
+ * @return Quote
513
+ */
514
+ private function getQuote ($ reservedOrderId )
515
+ {
516
+ /** @var SearchCriteriaBuilder $searchCriteriaBuilder */
517
+ $ searchCriteriaBuilder = $ this ->objectManager ->get (SearchCriteriaBuilder::class);
518
+ $ searchCriteria = $ searchCriteriaBuilder ->addFilter ('reserved_order_id ' , $ reservedOrderId )
519
+ ->create ();
520
+
521
+ /** @var CartRepositoryInterface $quoteRepository */
522
+ $ quoteRepository = $ this ->objectManager ->get (CartRepositoryInterface::class);
523
+ $ items = $ quoteRepository ->getList ($ searchCriteria )->getItems ();
524
+
525
+ return array_pop ($ items );
526
+ }
475
527
}
0 commit comments