Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
QuoteTest.php
Go to the documentation of this file.
1 <?php
8 
14 
21 class QuoteTest extends \PHPUnit\Framework\TestCase
22 {
27 
31  protected $quoteAddressMock;
32 
37 
41  protected $storeManagerMock;
42 
46  protected $resourceMock;
47 
51  protected $contextMock;
52 
57 
61  protected $eventManagerMock;
62 
67 
71  protected $quote;
72 
76  protected $productMock;
77 
81  protected $objectFactoryMock;
82 
87 
92 
97 
101  protected $scopeConfig;
102 
107 
112 
117 
122 
127 
132 
136  private $extensionAttributesJoinProcessorMock;
137 
141  private $customerDataFactoryMock;
142 
146  private $itemProcessor;
147 
151  private $orderIncrementIdChecker;
152 
156  protected function setUp()
157  {
158  $this->quoteAddressFactoryMock = $this->createPartialMock(
159  \Magento\Quote\Model\Quote\AddressFactory::class,
160  ['create']
161  );
162  $this->quoteAddressMock = $this->createPartialMock(\Magento\Quote\Model\Quote\Address::class, [
163  'isDeleted', 'getCollection', 'getId', 'getCustomerAddressId',
164  '__wakeup', 'getAddressType', 'getDeleteImmediately', 'validateMinimumAmount', 'setData'
165  ]);
166  $this->quoteAddressCollectionMock = $this->createMock(
167  \Magento\Quote\Model\ResourceModel\Quote\Address\Collection::class
168  );
169  $this->extensibleDataObjectConverterMock = $this->createPartialMock(
170  \Magento\Framework\Api\ExtensibleDataObjectConverter::class,
171  ['toFlatArray']
172  );
173  $this->customerRepositoryMock = $this->getMockForAbstractClass(
174  \Magento\Customer\Api\CustomerRepositoryInterface::class,
175  [],
176  '',
177  false,
178  true,
179  true,
180  ['getById', 'save']
181  );
182  $this->objectCopyServiceMock = $this->createPartialMock(
183  \Magento\Framework\DataObject\Copy::class,
184  ['copyFieldsetToTarget']
185  );
186  $this->productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
187  $this->objectFactoryMock = $this->createPartialMock(\Magento\Framework\DataObject\Factory::class, ['create']);
188  $this->quoteAddressFactoryMock->expects(
189  $this->any()
190  )->method(
191  'create'
192  )->will(
193  $this->returnValue($this->quoteAddressMock)
194  );
195  $this->quoteAddressMock->expects(
196  $this->any()
197  )->method(
198  'getCollection'
199  )->will(
200  $this->returnValue($this->quoteAddressCollectionMock)
201  );
202  $this->eventManagerMock = $this->getMockBuilder(\Magento\Framework\Event\Manager::class)
203  ->disableOriginalConstructor()
204  ->getMock();
205  $this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManager::class)
206  ->disableOriginalConstructor()
207  ->getMock();
208  $this->resourceMock = $this->getMockBuilder(\Magento\Quote\Model\ResourceModel\Quote::class)
209  ->disableOriginalConstructor()
210  ->getMock();
211  $this->contextMock = $this->getMockBuilder(\Magento\Framework\Model\Context::class)
212  ->disableOriginalConstructor()
213  ->getMock();
214  $this->customerFactoryMock = $this->getMockBuilder(\Magento\Customer\Model\CustomerFactory::class)
215  ->disableOriginalConstructor()
216  ->setMethods(['create'])
217  ->getMock();
218  $this->groupRepositoryMock = $this->getMockBuilder(\Magento\Customer\Api\GroupRepositoryInterface::class)
219  ->disableOriginalConstructor()
220  ->getMock();
221  $this->contextMock->expects($this->any())
222  ->method('getEventDispatcher')
223  ->will($this->returnValue($this->eventManagerMock));
224  $this->quoteItemCollectionFactoryMock = $this->createPartialMock(
225  \Magento\Quote\Model\ResourceModel\Quote\Item\CollectionFactory::class,
226  ['create']
227  );
228  $this->quotePaymentCollectionFactoryMock = $this->createPartialMock(
229  \Magento\Quote\Model\ResourceModel\Quote\Payment\CollectionFactory::class,
230  ['create']
231  );
232  $this->paymentFactoryMock = $this->createPartialMock(
233  \Magento\Quote\Model\Quote\PaymentFactory::class,
234  ['create']
235  );
236  $this->scopeConfig = $this->getMockBuilder(\Magento\Framework\App\Config::class)
237  ->disableOriginalConstructor()
238  ->getMock();
239 
240  $this->addressRepositoryMock = $this->getMockForAbstractClass(
241  \Magento\Customer\Api\AddressRepositoryInterface::class,
242  [],
243  '',
244  false
245  );
246 
247  $this->criteriaBuilderMock = $this->getMockBuilder(\Magento\Framework\Api\SearchCriteriaBuilder::class)
248  ->disableOriginalConstructor()
249  ->getMock();
250 
251  $this->filterBuilderMock = $this->getMockBuilder(\Magento\Framework\Api\FilterBuilder::class)
252  ->disableOriginalConstructor()
253  ->getMock();
254  $this->itemProcessor = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item\Processor::class)
255  ->disableOriginalConstructor()
256  ->getMock();
257  $this->extensionAttributesJoinProcessorMock = $this->createMock(
258  \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface::class
259  );
260  $this->customerDataFactoryMock = $this->createPartialMock(
261  \Magento\Customer\Api\Data\CustomerInterfaceFactory::class,
262  ['create']
263  );
264  $this->orderIncrementIdChecker = $this->createMock(\Magento\Sales\Model\OrderIncrementIdChecker::class);
265  $this->quote = (new ObjectManager($this))
266  ->getObject(
267  \Magento\Quote\Model\Quote::class,
268  [
269  'quoteAddressFactory' => $this->quoteAddressFactoryMock,
270  'storeManager' => $this->storeManagerMock,
271  'resource' => $this->resourceMock,
272  'context' => $this->contextMock,
273  'customerFactory' => $this->customerFactoryMock,
274  'groupRepository' => $this->groupRepositoryMock,
275  'objectFactory' => $this->objectFactoryMock,
276  'addressRepository' => $this->addressRepositoryMock,
277  'criteriaBuilder' => $this->criteriaBuilderMock,
278  'filterBuilder' => $this->filterBuilderMock,
279  'quoteItemCollectionFactory' => $this->quoteItemCollectionFactoryMock,
280  'quotePaymentCollectionFactory' => $this->quotePaymentCollectionFactoryMock,
281  'quotePaymentFactory' => $this->paymentFactoryMock,
282  'scopeConfig' => $this->scopeConfig,
283  'extensibleDataObjectConverter' => $this->extensibleDataObjectConverterMock,
284  'customerRepository' => $this->customerRepositoryMock,
285  'objectCopyService' => $this->objectCopyServiceMock,
286  'extensionAttributesJoinProcessor' => $this->extensionAttributesJoinProcessorMock,
287  'customerDataFactory' => $this->customerDataFactoryMock,
288  'itemProcessor' => $this->itemProcessor,
289  'orderIncrementIdChecker' => $this->orderIncrementIdChecker,
290  'data' => [
291  'reserved_order_id' => 1000001,
292  ],
293  ]
294  );
295  }
296 
302  public function testIsMultipleShippingAddresses($addresses, $expected)
303  {
304  $this->quoteAddressCollectionMock->expects(
305  $this->any()
306  )->method(
307  'setQuoteFilter'
308  )->will(
309  $this->returnValue($this->quoteAddressCollectionMock)
310  );
311  $this->quoteAddressCollectionMock->expects(
312  $this->once()
313  )->method(
314  'getIterator'
315  )->will(
316  $this->returnValue(new \ArrayIterator($addresses))
317  );
318 
319  $this->assertEquals($expected, $this->quote->isMultipleShippingAddresses());
320  }
321 
326  {
327  $this->assertEquals(
328  \Magento\Customer\Model\GroupManagement::NOT_LOGGED_IN_ID,
329  $this->quote->getCustomerGroupId(),
330  "Customer group ID is invalid"
331  );
332  }
333 
337  public function testGetCustomerGroupId()
338  {
340  $customerGroupId = 33;
341  $this->quote->setCustomerGroupId($customerGroupId);
342 
344  $this->assertEquals($customerGroupId, $this->quote->getCustomerGroupId(), "Customer group ID is invalid");
345  }
346 
351  {
352  return [
353  [
355  true,
356  ],
357  [
359  false
360  ]
361  ];
362  }
363 
368  protected function getAddressMock($type)
369  {
370  $shippingAddressMock = $this->createPartialMock(
371  \Magento\Quote\Model\Quote\Address::class,
372  ['getAddressType', '__wakeup', 'isDeleted']
373  );
374 
375  $shippingAddressMock->expects($this->any())->method('getAddressType')->will($this->returnValue($type));
376  $shippingAddressMock->expects($this->any())->method('isDeleted')->will($this->returnValue(false));
377  return $shippingAddressMock;
378  }
379 
380  public function testGetStoreIdNoId()
381  {
382  $storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
383  ->disableOriginalConstructor()
384  ->getMock();
385  $storeMock->expects($this->once())
386  ->method('getId')
387  ->will($this->returnValue(null));
388  $this->storeManagerMock->expects($this->once())
389  ->method('getStore')
390  ->will($this->returnValue($storeMock));
391 
392  $result = $this->quote->getStoreId();
393  $this->assertNull($result);
394  }
395 
396  public function testGetStoreId()
397  {
398  $storeId = 1;
399 
400  $result = $this->quote->setStoreId($storeId)->getStoreId();
401  $this->assertEquals($storeId, $result);
402  }
403 
404  public function testGetStore()
405  {
406  $storeId = 1;
407 
408  $storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
409  ->disableOriginalConstructor()
410  ->getMock();
411  $this->storeManagerMock->expects($this->once())
412  ->method('getStore')
413  ->with($storeId)
414  ->will($this->returnValue($storeMock));
415 
416  $this->quote->setStoreId($storeId);
417  $result = $this->quote->getStore();
418  $this->assertInstanceOf(\Magento\Store\Model\Store::class, $result);
419  }
420 
421  public function testSetStore()
422  {
423  $storeId = 1;
424 
425  $storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
426  ->disableOriginalConstructor()
427  ->getMock();
428  $storeMock->expects($this->once())
429  ->method('getId')
430  ->will($this->returnValue($storeId));
431 
432  $result = $this->quote->setStore($storeMock);
433  $this->assertInstanceOf(\Magento\Quote\Model\Quote::class, $result);
434  }
435 
437  {
438  $sharedIds = null;
439  $storeIds = [1, 2, 3];
440 
441  $websiteMock = $this->getMockBuilder(\Magento\Store\Model\Website::class)
442  ->disableOriginalConstructor()
443  ->getMock();
444  $websiteMock->expects($this->once())
445  ->method('getStoreIds')
446  ->will($this->returnValue($storeIds));
447 
448  $this->quote->setData('shared_store_ids', $sharedIds);
449  $this->quote->setWebsite($websiteMock);
450  $result = $this->quote->getSharedStoreIds();
451  $this->assertEquals($storeIds, $result);
452  }
453 
454  public function testGetSharedStoreIds()
455  {
456  $sharedIds = null;
457  $storeIds = [1, 2, 3];
458  $storeId = 1;
459 
460  $websiteMock = $this->getMockBuilder(\Magento\Store\Model\Website::class)
461  ->disableOriginalConstructor()
462  ->getMock();
463  $websiteMock->expects($this->once())
464  ->method('getStoreIds')
465  ->will($this->returnValue($storeIds));
466 
467  $storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
468  ->disableOriginalConstructor()
469  ->getMock();
470  $storeMock->expects($this->once())
471  ->method('getWebsite')
472  ->will($this->returnValue($websiteMock));
473 
474  $this->storeManagerMock->expects($this->once())
475  ->method('getStore')
476  ->with($storeId)
477  ->will($this->returnValue($storeMock));
478 
479  $this->quote->setData('shared_store_ids', $sharedIds);
480  $this->quote->setStoreId($storeId);
481  $result = $this->quote->getSharedStoreIds();
482  $this->assertEquals($storeIds, $result);
483  }
484 
485  public function testLoadActive()
486  {
487  $quoteId = 1;
488 
489  $this->resourceMock->expects($this->once())
490  ->method('loadActive')
491  ->with($this->quote, $quoteId);
492 
493  $this->eventManagerMock->expects($this->any())
494  ->method('dispatch');
495 
496  $result = $this->quote->loadActive($quoteId);
497  $this->assertInstanceOf(\Magento\Quote\Model\Quote::class, $result);
498  }
499 
500  public function testloadByIdWithoutStore()
501  {
502  $quoteId = 1;
503 
504  $this->resourceMock->expects($this->once())
505  ->method('loadByIdWithoutStore')
506  ->with($this->quote, $quoteId);
507 
508  $this->eventManagerMock->expects($this->any())
509  ->method('dispatch');
510 
511  $result = $this->quote->loadByIdWithoutStore($quoteId);
512  $this->assertInstanceOf(\Magento\Quote\Model\Quote::class, $result);
513  }
514 
518  public function testSetCustomerAddressData()
519  {
520  $customerId = 1;
521  $addressMock = $this->getMockForAbstractClass(
522  \Magento\Customer\Api\Data\AddressInterface::class,
523  [],
524  '',
525  false,
526  true,
527  true,
528  ['getId']
529  );
530  $addressMock->expects($this->any())
531  ->method('getId')
532  ->will($this->returnValue(null));
533 
534  $addresses = [$addressMock];
535 
536  $customerMock = $this->getMockForAbstractClass(
537  \Magento\Customer\Api\Data\CustomerInterface::class,
538  [],
539  '',
540  false
541  );
542  $customerResultMock = $this->getMockForAbstractClass(
543  \Magento\Customer\Api\Data\CustomerInterface::class,
544  [],
545  '',
546  false
547  );
548  $requestMock = $this->createMock(
549  \Magento\Framework\DataObject::class
550  );
551 
552  $this->extensibleDataObjectConverterMock->expects($this->any())
553  ->method('toFlatArray')
554  ->will($this->returnValue(['customer_id' => $customerId]));
555 
556  $this->customerRepositoryMock->expects($this->any())
557  ->method('getById')
558  ->will($this->returnValue($customerMock));
559  $this->customerDataFactoryMock->expects($this->any())
560  ->method('create')
561  ->will($this->returnValue($customerMock));
562  $this->customerRepositoryMock->expects($this->never())
563  ->method('save')
564  ->will($this->returnValue($customerMock));
565  $customerMock->expects($this->any())
566  ->method('getAddresses')
567  ->will($this->returnValue($addresses));
568  $this->objectFactoryMock->expects($this->once())
569  ->method('create')
570  ->with($this->equalTo(['customer_id' => $customerId]))
571  ->will($this->returnValue($requestMock));
572  $result = $this->quote->setCustomerAddressData([$addressMock]);
573  $this->assertInstanceOf(\Magento\Quote\Model\Quote::class, $result);
574  $this->assertEquals($customerResultMock, $this->quote->getCustomer());
575  }
576 
577  public function testGetCustomerTaxClassId()
578  {
579  $groupId = 1;
580  $taxClassId = 1;
581  $groupMock = $this->getMockForAbstractClass(\Magento\Customer\Api\Data\GroupInterface::class, [], '', false);
582  $groupMock->expects($this->once())
583  ->method('getTaxClassId')
584  ->willReturn($taxClassId);
585  $this->groupRepositoryMock->expects($this->once())
586  ->method('getById')
587  ->with($groupId)
588  ->will($this->returnValue($groupMock));
589  $this->quote->setData('customer_group_id', $groupId);
590  $result = $this->quote->getCustomerTaxClassId();
591  $this->assertEquals($taxClassId, $result);
592  }
593 
594  public function testGetAllAddresses()
595  {
596  $id = 1;
597  $this->quoteAddressCollectionMock->expects($this->once())
598  ->method('setQuoteFilter')
599  ->with($id)
600  ->will($this->returnSelf());
601 
602  $this->quoteAddressMock->expects($this->once())
603  ->method('isDeleted')
604  ->will($this->returnValue(false));
605 
606  $iterator = new \ArrayIterator([$this->quoteAddressMock]);
607  $this->quoteAddressCollectionMock->expects($this->any())
608  ->method('getIterator')
609  ->will($this->returnValue($iterator));
610 
611  $this->quote->setId($id);
612  $result = $this->quote->getAllAddresses();
613  $this->assertEquals([$this->quoteAddressMock], $result);
614  }
615 
619  public function testGetAddressById($addressId, $expected)
620  {
621  $id = 1;
622  $this->quoteAddressCollectionMock->expects($this->once())
623  ->method('setQuoteFilter')
624  ->with($id)
625  ->will($this->returnSelf());
626 
627  $this->quoteAddressMock->expects($this->once())
628  ->method('getId')
629  ->will($this->returnValue($id));
630 
631  $iterator = new \ArrayIterator([$this->quoteAddressMock]);
632  $this->quoteAddressCollectionMock->expects($this->any())
633  ->method('getIterator')
634  ->will($this->returnValue($iterator));
635 
636  $this->quote->setId($id);
637  $result = $this->quote->getAddressById($addressId);
638 
639  $this->assertEquals((bool)$expected, (bool)$result);
640  }
641 
645  public static function dataProviderGetAddress()
646  {
647  return [
648  [1, true],
649  [2, false]
650  ];
651  }
652 
660  public function testGetAddressByCustomerAddressId($isDeleted, $customerAddressId, $expected)
661  {
662  $id = 1;
663  $this->quoteAddressCollectionMock->expects($this->once())
664  ->method('setQuoteFilter')
665  ->with($id)
666  ->will($this->returnSelf());
667 
668  $this->quoteAddressMock->expects($this->once())
669  ->method('isDeleted')
670  ->will($this->returnValue($isDeleted));
671  $this->quoteAddressMock->expects($this->once())
672  ->method('getCustomerAddressId')
673  ->will($this->returnValue($customerAddressId));
674 
675  $iterator = new \ArrayIterator([$this->quoteAddressMock]);
676  $this->quoteAddressCollectionMock->expects($this->any())
677  ->method('getIterator')
678  ->will($this->returnValue($iterator));
679 
680  $this->quote->setId($id);
681  $result = $this->quote->getAddressByCustomerAddressId($id);
682 
683  $this->assertEquals((bool)$expected, (bool)$result);
684  }
685 
689  public static function dataProviderGetAddressByCustomer()
690  {
691  return [
692  [false, 1, true],
693  [false, 2, false]
694  ];
695  }
696 
705  public function testGetShippingAddressByCustomerAddressId($isDeleted, $addressType, $customerAddressId, $expected)
706  {
707  $id = 1;
708 
709  $this->quoteAddressCollectionMock->expects($this->once())
710  ->method('setQuoteFilter')
711  ->with($id)
712  ->will($this->returnSelf());
713 
714  $this->quoteAddressMock->expects($this->once())
715  ->method('isDeleted')
716  ->will($this->returnValue($isDeleted));
717  $this->quoteAddressMock->expects($this->once())
718  ->method('getCustomerAddressId')
719  ->will($this->returnValue($customerAddressId));
720  $this->quoteAddressMock->expects($this->once())
721  ->method('getAddressType')
722  ->will($this->returnValue($addressType));
723 
724  $iterator = new \ArrayIterator([$this->quoteAddressMock]);
725  $this->quoteAddressCollectionMock->expects($this->any())
726  ->method('getIterator')
727  ->will($this->returnValue($iterator));
728 
729  $this->quote->setId($id);
730 
731  $result = $this->quote->getShippingAddressByCustomerAddressId($id);
732  $this->assertEquals($expected, (bool)$result);
733  }
734 
738  public static function dataProviderShippingAddress()
739  {
740  return [
741  [false, \Magento\Customer\Model\Address\AbstractAddress::TYPE_SHIPPING, 1, true],
742  [false, \Magento\Customer\Model\Address\AbstractAddress::TYPE_SHIPPING, 2, false],
743  ];
744  }
745 
746  public function testRemoveAddress()
747  {
748  $id = 1;
749 
750  $this->quoteAddressCollectionMock->expects($this->once())
751  ->method('setQuoteFilter')
752  ->with($id)
753  ->will($this->returnSelf());
754 
755  $this->quoteAddressMock->expects($this->once())
756  ->method('isDeleted')
757  ->with(true);
758  $this->quoteAddressMock->expects($this->once())
759  ->method('getId')
760  ->will($this->returnValue($id));
761 
762  $iterator = new \ArrayIterator([$this->quoteAddressMock]);
763  $this->quoteAddressCollectionMock->expects($this->any())
764  ->method('getIterator')
765  ->will($this->returnValue($iterator));
766 
767  $this->quote->setId($id);
768 
769  $result = $this->quote->removeAddress($id);
770  $this->assertInstanceOf(\Magento\Quote\Model\Quote::class, $result);
771  }
772 
773  public function testRemoveAllAddresses()
774  {
775  $id = 1;
776 
777  $this->quoteAddressCollectionMock->expects($this->once())
778  ->method('setQuoteFilter')
779  ->with($id)
780  ->will($this->returnSelf());
781 
782  $this->quoteAddressMock->expects($this->any())
783  ->method('getAddressType')
784  ->will($this->returnValue(\Magento\Customer\Model\Address\AbstractAddress::TYPE_SHIPPING));
785  $this->quoteAddressMock->expects($this->any())
786  ->method('getAddressType')
787  ->will($this->returnValue(\Magento\Customer\Model\Address\AbstractAddress::TYPE_SHIPPING));
788  $this->quoteAddressMock->expects($this->any())
789  ->method('isDeleted')
790  ->will($this->returnValue(false));
791  $this->quoteAddressMock->expects($this->any())
792  ->method('setData')
793  ->will($this->returnSelf());
794  $this->quoteAddressMock->expects($this->once())
795  ->method('getId')
796  ->will($this->returnValue($id));
797  $this->quoteAddressMock->expects($this->once())
798  ->method('getDeleteImmediately')
799  ->will($this->returnValue(true));
800 
801  $iterator = new \ArrayIterator([$id => $this->quoteAddressMock]);
802  $this->quoteAddressCollectionMock->expects($this->any())
803  ->method('getIterator')
804  ->will($this->returnValue($iterator));
805  $this->quoteAddressCollectionMock->expects($this->once())
806  ->method('removeItemByKey')
807  ->with($id)
808  ->will($this->returnValue($iterator));
809 
810  $this->quote->setId($id);
811 
812  $result = $this->quote->removeAllAddresses();
813  $this->assertInstanceOf(\Magento\Quote\Model\Quote::class, $result);
814  }
815 
819  public function testAddProductException()
820  {
821  $this->quote->addProduct($this->productMock, 'test');
822  }
823 
824  public function testAddProductNoCandidates()
825  {
826  $expectedResult = 'test_string';
827  $requestMock = $this->createMock(
828  \Magento\Framework\DataObject::class
829  );
830  $this->objectFactoryMock->expects($this->once())
831  ->method('create')
832  ->with($this->equalTo(['qty' => 1]))
833  ->will($this->returnValue($requestMock));
834 
835  $this->productMock->expects($this->once())
836  ->method('isSalable')
837  ->willReturn(true);
838 
839  $typeInstanceMock = $this->createPartialMock(\Magento\Catalog\Model\Product\Type\Simple::class, [
840  'prepareForCartAdvanced'
841  ]);
842  $typeInstanceMock->expects($this->once())
843  ->method('prepareForCartAdvanced')
844  ->will($this->returnValue($expectedResult));
845  $this->productMock->expects($this->once())
846  ->method('getTypeInstance')
847  ->will($this->returnValue($typeInstanceMock));
848 
849  $result = $this->quote->addProduct($this->productMock, null);
850  $this->assertEquals($expectedResult, $result);
851  }
852 
854  {
855  $itemMock = $this->createMock(\Magento\Quote\Model\Quote\Item::class);
856 
857  $expectedResult = $itemMock;
858  $requestMock = $this->createMock(
859  \Magento\Framework\DataObject::class
860  );
861  $this->objectFactoryMock->expects($this->once())
862  ->method('create')
863  ->with($this->equalTo(['qty' => 1]))
864  ->will($this->returnValue($requestMock));
865 
866  $typeInstanceMock = $this->createPartialMock(\Magento\Catalog\Model\Product\Type\Simple::class, [
867  'prepareForCartAdvanced'
868  ]);
869 
870  $productMock = $this->createPartialMock(\Magento\Catalog\Model\Product::class, [
871  'getParentProductId',
872  'setStickWithinParent',
873  '__wakeup'
874  ]);
875 
876  $collectionMock = $this->createMock(\Magento\Quote\Model\ResourceModel\Quote\Item\Collection::class);
877 
878  $itemMock->expects($this->any())
879  ->method('representProduct')
880  ->will($this->returnValue(true));
881 
882  $iterator = new \ArrayIterator([$itemMock]);
883  $collectionMock->expects($this->any())
884  ->method('getIterator')
885  ->will($this->returnValue($iterator));
886 
887  $this->quoteItemCollectionFactoryMock->expects($this->once())
888  ->method('create')
889  ->will($this->returnValue($collectionMock));
890 
891  $this->productMock->expects($this->once())
892  ->method('isSalable')
893  ->willReturn(true);
894 
895  $typeInstanceMock->expects($this->once())
896  ->method('prepareForCartAdvanced')
897  ->will($this->returnValue([$productMock]));
898  $this->productMock->expects($this->once())
899  ->method('getTypeInstance')
900  ->will($this->returnValue($typeInstanceMock));
901 
902  $result = $this->quote->addProduct($this->productMock, null);
903  $this->assertEquals($expectedResult, $result);
904  }
905 
906  public function testAddProductItemNew()
907  {
908  $itemMock = $this->createMock(\Magento\Quote\Model\Quote\Item::class);
909 
910  $expectedResult = $itemMock;
911  $requestMock = $this->createMock(
912  \Magento\Framework\DataObject::class
913  );
914  $this->objectFactoryMock->expects($this->once())
915  ->method('create')
916  ->with($this->equalTo(['qty' => 1]))
917  ->will($this->returnValue($requestMock));
918 
919  $typeInstanceMock = $this->createPartialMock(\Magento\Catalog\Model\Product\Type\Simple::class, [
920  'prepareForCartAdvanced'
921  ]);
922 
923  $productMock = $this->createPartialMock(\Magento\Catalog\Model\Product::class, [
924  'getParentProductId',
925  'setStickWithinParent',
926  '__wakeup'
927  ]);
928 
929  $collectionMock = $this->createMock(\Magento\Quote\Model\ResourceModel\Quote\Item\Collection::class);
930 
931  $itemMock->expects($this->any())
932  ->method('representProduct')
933  ->will($this->returnValue(false));
934 
935  $iterator = new \ArrayIterator([$itemMock]);
936  $collectionMock->expects($this->any())
937  ->method('getIterator')
938  ->will($this->returnValue($iterator));
939 
940  $this->quoteItemCollectionFactoryMock->expects($this->once())
941  ->method('create')
942  ->will($this->returnValue($collectionMock));
943 
944  $this->productMock->expects($this->once())
945  ->method('isSalable')
946  ->willReturn(true);
947  $this->itemProcessor
948  ->expects($this->once())
949  ->method('init')
950  ->willReturn($itemMock);
951  $itemMock->expects($this->once())
952  ->method('setProduct');
953  $itemMock->expects($this->once())
954  ->method('setOptions');
955  $itemMock->expects($this->any())
956  ->method('setQuote')
957  ->with($this->quote);
958  $typeInstanceMock->expects($this->once())
959  ->method('prepareForCartAdvanced')
960  ->will($this->returnValue([$productMock]));
961  $this->productMock->expects($this->once())
962  ->method('getTypeInstance')
963  ->will($this->returnValue($typeInstanceMock));
964 
965  $result = $this->quote->addProduct($this->productMock, null);
966  $this->assertEquals($expectedResult, $result);
967  }
968 
969  public function testValidateMinimumAmount()
970  {
971  $storeId = 1;
972  $this->quote->setStoreId($storeId);
973 
974  $valueMap = [
975  ['sales/minimum_order/active', ScopeInterface::SCOPE_STORE, $storeId, true],
976  ['sales/minimum_order/multi_address', ScopeInterface::SCOPE_STORE, $storeId, true],
977  ['sales/minimum_order/amount', ScopeInterface::SCOPE_STORE, $storeId, 20],
978  ['sales/minimum_order/tax_including', ScopeInterface::SCOPE_STORE, $storeId, true],
979  ];
980  $this->scopeConfig->expects($this->any())
981  ->method('isSetFlag')
982  ->will($this->returnValueMap($valueMap));
983 
984  $this->quoteAddressMock->expects($this->once())
985  ->method('validateMinimumAmount')
986  ->willReturn(true);
987 
988  $this->quoteAddressCollectionMock->expects($this->once())
989  ->method('setQuoteFilter')
990  ->willReturn([$this->quoteAddressMock]);
991 
992  $this->assertTrue($this->quote->validateMinimumAmount());
993  }
994 
996  {
997  $storeId = 1;
998  $this->quote->setStoreId($storeId);
999 
1000  $valueMap = [
1001  ['sales/minimum_order/active', ScopeInterface::SCOPE_STORE, $storeId, true],
1002  ['sales/minimum_order/multi_address', ScopeInterface::SCOPE_STORE, $storeId, true],
1003  ['sales/minimum_order/amount', ScopeInterface::SCOPE_STORE, $storeId, 20],
1004  ['sales/minimum_order/tax_including', ScopeInterface::SCOPE_STORE, $storeId, true],
1005  ];
1006  $this->scopeConfig->expects($this->any())
1007  ->method('isSetFlag')
1008  ->will($this->returnValueMap($valueMap));
1009 
1010  $this->quoteAddressMock->expects($this->once())
1011  ->method('validateMinimumAmount')
1012  ->willReturn(false);
1013 
1014  $this->quoteAddressCollectionMock->expects($this->once())
1015  ->method('setQuoteFilter')
1016  ->willReturn([$this->quoteAddressMock]);
1017 
1018  $this->assertFalse($this->quote->validateMinimumAmount());
1019  }
1020 
1021  public function testGetPaymentIsNotDeleted()
1022  {
1023  $this->quote->setId(1);
1024  $payment = $this->createPartialMock(
1025  \Magento\Quote\Model\Quote\Payment::class,
1026  ['setQuote', 'isDeleted', '__wakeup']
1027  );
1028  $payment->expects($this->once())
1029  ->method('setQuote');
1030  $payment->expects($this->once())
1031  ->method('isDeleted')
1032  ->willReturn(false);
1033  $quotePaymentCollectionMock = $this->createPartialMock(
1034  \Magento\Quote\Model\ResourceModel\Quote\Payment\Collection::class,
1035  ['setQuoteFilter', 'getFirstItem']
1036  );
1037  $quotePaymentCollectionMock->expects($this->once())
1038  ->method('setQuoteFilter')
1039  ->with(1)
1040  ->will($this->returnSelf());
1041  $quotePaymentCollectionMock->expects($this->once())
1042  ->method('getFirstItem')
1043  ->willReturn($payment);
1044  $this->quotePaymentCollectionFactoryMock->expects($this->once())
1045  ->method('create')
1046  ->willReturn($quotePaymentCollectionMock);
1047 
1048  $this->assertInstanceOf(\Magento\Quote\Model\Quote\Payment::class, $this->quote->getPayment());
1049  }
1050 
1051  public function testGetPaymentIsDeleted()
1052  {
1053  $this->quote->setId(1);
1054  $payment = $this->createPartialMock(
1055  \Magento\Quote\Model\Quote\Payment::class,
1056  ['setQuote', 'isDeleted', 'getId', '__wakeup']
1057  );
1058  $payment->expects($this->exactly(2))
1059  ->method('setQuote');
1060  $payment->expects($this->once())
1061  ->method('isDeleted')
1062  ->willReturn(true);
1063  $payment->expects($this->once())
1064  ->method('getId')
1065  ->willReturn(1);
1066  $quotePaymentCollectionMock = $this->createPartialMock(
1067  \Magento\Quote\Model\ResourceModel\Quote\Payment\Collection::class,
1068  ['setQuoteFilter', 'getFirstItem']
1069  );
1070  $quotePaymentCollectionMock->expects($this->once())
1071  ->method('setQuoteFilter')
1072  ->with(1)
1073  ->will($this->returnSelf());
1074  $quotePaymentCollectionMock->expects($this->once())
1075  ->method('getFirstItem')
1076  ->willReturn($payment);
1077  $this->quotePaymentCollectionFactoryMock->expects($this->once())
1078  ->method('create')
1079  ->willReturn($quotePaymentCollectionMock);
1080 
1081  $this->paymentFactoryMock->expects($this->once())
1082  ->method('create')
1083  ->willReturn($payment);
1084 
1085  $this->assertInstanceOf(\Magento\Quote\Model\Quote\Payment::class, $this->quote->getPayment());
1086  }
1087 
1088  public function testAddItem()
1089  {
1090  $item = $this->createPartialMock(\Magento\Quote\Model\Quote\Item::class, ['setQuote', 'getId']);
1091  $item->expects($this->once())
1092  ->method('setQuote');
1093  $item->expects($this->once())
1094  ->method('getId')
1095  ->willReturn(false);
1096  $itemsMock = $this->createPartialMock(
1097  \Magento\Eav\Model\Entity\Collection\AbstractCollection::class,
1098  ['setQuote', 'addItem']
1099  );
1100  $itemsMock->expects($this->once())
1101  ->method('setQuote');
1102  $itemsMock->expects($this->once())
1103  ->method('addItem')
1104  ->with($item);
1105  $this->quoteItemCollectionFactoryMock->expects($this->once())
1106  ->method('create')
1107  ->willReturn($itemsMock);
1108  $this->eventManagerMock->expects($this->once())
1109  ->method('dispatch');
1110 
1111  $this->quote->addItem($item);
1112  }
1113 
1119  public function testBeforeSaveIsVirtualQuote(array $productTypes, $expected)
1120  {
1121  $storeId = 1;
1122  $currencyMock = $this->getMockBuilder(\Magento\Directory\Model\Currency::class)
1123  ->disableOriginalConstructor()
1124  ->getMock();
1125  $currencyMock->expects($this->any())
1126  ->method('getCode')
1127  ->will($this->returnValue('test_code'));
1128  $currencyMock->expects($this->any())
1129  ->method('getRate')
1130  ->will($this->returnValue('test_rate'));
1131  $storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
1132  ->disableOriginalConstructor()
1133  ->getMock();
1134  $storeMock->expects($this->once())
1135  ->method('getBaseCurrency')
1136  ->will($this->returnValue($currencyMock));
1137  $storeMock->expects($this->once())
1138  ->method('getCurrentCurrency')
1139  ->will($this->returnValue($currencyMock));
1140 
1141  $this->storeManagerMock->expects($this->any())
1142  ->method('getStore')
1143  ->with($storeId)
1144  ->will($this->returnValue($storeMock));
1145  $this->quote->setStoreId($storeId);
1146 
1147  $collectionMock = $this->createMock(\Magento\Quote\Model\ResourceModel\Quote\Item\Collection::class);
1148  $items = [];
1149  foreach ($productTypes as $type) {
1150  $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
1151  $productMock->expects($this->any())->method('getIsVirtual')->willReturn($type);
1152 
1153  $itemMock = $this->createPartialMock(
1154  \Magento\Quote\Model\Quote\Item::class,
1155  ['isDeleted', 'getParentItemId', 'getProduct']
1156  );
1157  $itemMock->expects($this->any())
1158  ->method('isDeleted')
1159  ->willReturn(false);
1160  $itemMock->expects($this->any())
1161  ->method('getParentItemId')
1162  ->willReturn(false);
1163  $itemMock->expects($this->any())
1164  ->method('getProduct')
1165  ->willReturn($productMock);
1166  $items[] = $itemMock;
1167  }
1168  $iterator = new \ArrayIterator($items);
1169  $collectionMock->expects($this->any())
1170  ->method('getIterator')
1171  ->will($this->returnValue($iterator));
1172  $this->quoteItemCollectionFactoryMock->expects($this->once())
1173  ->method('create')
1174  ->will($this->returnValue($collectionMock));
1175 
1176  $this->quote->beforeSave();
1177  $this->assertEquals($expected, $this->quote->getDataByKey(CartInterface::KEY_IS_VIRTUAL));
1178  $this->assertNull($this->quote->getUpdatedAt());
1179  }
1180 
1185  {
1186  return [
1187  [[true], 1],
1188  [[true, true], 1],
1189  [[false], 0],
1190  [[true, false], 0],
1191  [[false, false], 0]
1192  ];
1193  }
1194 
1195  public function testGetItemsCollection()
1196  {
1197  $itemCollectionMock = $this->getMockBuilder(\Magento\Quote\Model\ResourceModel\Quote\Collection::class)
1198  ->disableOriginalConstructor()
1199  ->setMethods(['setQuote'])
1200  ->getMock();
1201  $this->quoteItemCollectionFactoryMock->expects($this->once())
1202  ->method('create')
1203  ->willReturn($itemCollectionMock);
1204 
1205  $this->extensionAttributesJoinProcessorMock->expects($this->once())
1206  ->method('process')
1207  ->with(
1208  $this->isInstanceOf(\Magento\Quote\Model\ResourceModel\Quote\Collection::class)
1209  );
1210  $itemCollectionMock->expects($this->once())->method('setQuote')->with($this->quote);
1211 
1212  $this->quote->getItemsCollection();
1213  }
1214 
1215  public function testGetAllItems()
1216  {
1217  $itemOneMock = $this->getMockBuilder(\Magento\Quote\Model\ResourceModel\Quote\Item::class)
1218  ->setMethods(['isDeleted'])
1219  ->disableOriginalConstructor()
1220  ->getMock();
1221  $itemOneMock->expects($this->once())
1222  ->method('isDeleted')
1223  ->willReturn(false);
1224 
1225  $itemTwoMock = $this->getMockBuilder(\Magento\Quote\Model\ResourceModel\Quote\Item::class)
1226  ->setMethods(['isDeleted'])
1227  ->disableOriginalConstructor()
1228  ->getMock();
1229  $itemTwoMock->expects($this->once())
1230  ->method('isDeleted')
1231  ->willReturn(true);
1232 
1233  $items = [$itemOneMock, $itemTwoMock];
1234  $itemResult = [$itemOneMock];
1235  $this->quote->setData('items_collection', $items);
1236 
1237  $this->assertEquals($itemResult, $this->quote->getAllItems());
1238  }
1239 
1248  public function testReserveOrderId(bool $isReservedOrderIdExist, int $reservedOrderId): void
1249  {
1250  $this->orderIncrementIdChecker
1251  ->expects($this->once())
1252  ->method('isIncrementIdUsed')
1253  ->with(1000001)->willReturn($isReservedOrderIdExist);
1254  $this->resourceMock->expects($this->any())->method('getReservedOrderId')->willReturn($reservedOrderId);
1255  $this->quote->reserveOrderId();
1256  $this->assertEquals($reservedOrderId, $this->quote->getReservedOrderId());
1257  }
1258 
1262  public function reservedOrderIdDataProvider(): array
1263  {
1264  return [
1265  'id_already_in_use' => [true, 100002],
1266  'id_not_in_use' => [false, 1000001],
1267  ];
1268  }
1269 }
testReserveOrderId(bool $isReservedOrderIdExist, int $reservedOrderId)
Definition: QuoteTest.php:1248
return false
Definition: gallery.phtml:36
$id
Definition: fieldset.phtml:14
$addresses
Definition: address_list.php:7
testGetShippingAddressByCustomerAddressId($isDeleted, $addressType, $customerAddressId, $expected)
Definition: QuoteTest.php:705
$payment
Definition: order.php:17
$type
Definition: item.phtml:13
testGetAddressById($addressId, $expected)
Definition: QuoteTest.php:619
testBeforeSaveIsVirtualQuote(array $productTypes, $expected)
Definition: QuoteTest.php:1119
testIsMultipleShippingAddresses($addresses, $expected)
Definition: QuoteTest.php:302
$items
testGetAddressByCustomerAddressId($isDeleted, $customerAddressId, $expected)
Definition: QuoteTest.php:660