SaveHandlerTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Test\Unit\Model\QuoteRepository;
  7. use Magento\Quote\Model\QuoteRepository\SaveHandler;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  9. use Magento\Quote\Model\ResourceModel\Quote as QuoteResourceModel;
  10. use Magento\Quote\Model\Quote\Item\CartItemPersister;
  11. use Magento\Quote\Model\Quote\Address\BillingAddressPersister;
  12. use Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentPersister;
  13. use Magento\Customer\Api\AddressRepositoryInterface;
  14. use Magento\Quote\Model\Quote;
  15. use Magento\Quote\Model\Quote\Address as QuoteAddress;
  16. use Magento\Quote\Api\Data\CartExtensionInterface;
  17. use Magento\Quote\Model\Quote\Item as QuoteItem;
  18. use Magento\Framework\Exception\NoSuchEntityException;
  19. class SaveHandlerTest extends \PHPUnit\Framework\TestCase
  20. {
  21. /**
  22. * @var SaveHandler
  23. */
  24. private $saveHandler;
  25. /**
  26. * @var ObjectManagerHelper
  27. */
  28. private $objectManagerHelper;
  29. /**
  30. * @var QuoteResourceModel|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. private $quoteResourceModelMock;
  33. /**
  34. * @var CartItemPersister|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. private $cartItemPersisterMock;
  37. /**
  38. * @var BillingAddressPersister|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. private $billingAddressPersisterMock;
  41. /**
  42. * @var ShippingAssignmentPersister|\PHPUnit_Framework_MockObject_MockObject
  43. */
  44. private $shippingAssignmentPersisterMock;
  45. /**
  46. * @var AddressRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  47. */
  48. private $addressRepositoryMock;
  49. /**
  50. * @var Quote|\PHPUnit_Framework_MockObject_MockObject
  51. */
  52. private $quoteMock;
  53. /**
  54. * @var QuoteAddress|\PHPUnit_Framework_MockObject_MockObject
  55. */
  56. private $billingAddressMock;
  57. /**
  58. * @var CartExtensionInterface|\PHPUnit_Framework_MockObject_MockObject
  59. */
  60. private $extensionAttributesMock;
  61. protected function setUp()
  62. {
  63. $this->quoteResourceModelMock = $this->getMockBuilder(QuoteResourceModel::class)
  64. ->disableOriginalConstructor()
  65. ->getMock();
  66. $this->cartItemPersisterMock = $this->getMockBuilder(CartItemPersister::class)
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. $this->billingAddressPersisterMock = $this->getMockBuilder(BillingAddressPersister::class)
  70. ->disableOriginalConstructor()
  71. ->getMock();
  72. $this->shippingAssignmentPersisterMock = $this->getMockBuilder(ShippingAssignmentPersister::class)
  73. ->disableOriginalConstructor()
  74. ->getMock();
  75. $this->addressRepositoryMock = $this->getMockBuilder(AddressRepositoryInterface::class)
  76. ->getMockForAbstractClass();
  77. $this->quoteMock = $this->getMockBuilder(Quote::class)
  78. ->disableOriginalConstructor()
  79. ->setMethods(
  80. [
  81. 'getItems', 'setLastAddedItem', 'getBillingAddress', 'getExtensionAttributes', 'isVirtual',
  82. 'collectTotals'
  83. ]
  84. )
  85. ->getMock();
  86. $this->billingAddressMock = $this->getMockBuilder(QuoteAddress::class)
  87. ->setMethods(['getCustomerAddress', 'getCustomerAddressId', 'setCustomerAddressId'])
  88. ->disableOriginalConstructor()
  89. ->getMock();
  90. $this->extensionAttributesMock = $this->getMockBuilder(CartExtensionInterface::class)
  91. ->setMethods(['getShippingAssignments'])
  92. ->getMockForAbstractClass();
  93. $this->quoteMock->expects(static::any())
  94. ->method('getBillingAddress')
  95. ->willReturn($this->billingAddressMock);
  96. $this->quoteMock->expects(static::any())
  97. ->method('getExtensionAttributes')
  98. ->willReturn($this->extensionAttributesMock);
  99. $this->objectManagerHelper = new ObjectManagerHelper($this);
  100. $this->saveHandler = $this->objectManagerHelper->getObject(
  101. SaveHandler::class,
  102. [
  103. 'quoteResource' => $this->quoteResourceModelMock,
  104. 'cartItemPersister' => $this->cartItemPersisterMock,
  105. 'billingAddressPersister' => $this->billingAddressPersisterMock,
  106. 'shippingAssignmentPersister' => $this->shippingAssignmentPersisterMock,
  107. 'addressRepository' => $this->addressRepositoryMock
  108. ]
  109. );
  110. }
  111. public function testSaveForVirtualQuote()
  112. {
  113. $quoteItemMock = $this->createQuoteItemMock(false);
  114. $this->quoteMock->expects(static::atLeastOnce())
  115. ->method('getItems')
  116. ->willReturn([$quoteItemMock]);
  117. $this->cartItemPersisterMock->expects(static::once())
  118. ->method('save')
  119. ->with($this->quoteMock, $quoteItemMock)
  120. ->willReturn($quoteItemMock);
  121. $this->quoteMock->expects(static::once())
  122. ->method('setLastAddedItem')
  123. ->with($quoteItemMock)
  124. ->willReturnSelf();
  125. $this->billingAddressMock->expects(static::atLeastOnce())
  126. ->method('getCustomerAddressId')
  127. ->willReturn(null);
  128. $this->billingAddressMock->expects(static::never())
  129. ->method('getCustomerAddress');
  130. $this->billingAddressPersisterMock->expects(static::once())
  131. ->method('save')
  132. ->with($this->quoteMock, $this->billingAddressMock);
  133. $this->quoteMock->expects(static::atLeastOnce())
  134. ->method('isVirtual')
  135. ->willReturn(true);
  136. $this->extensionAttributesMock->expects(static::never())
  137. ->method('getShippingAssignments');
  138. $this->quoteMock->expects(static::atLeastOnce())
  139. ->method('collectTotals')
  140. ->willReturnSelf();
  141. $this->quoteResourceModelMock->expects(static::once())
  142. ->method('save')
  143. ->with($this->quoteMock)
  144. ->willReturnSelf();
  145. $this->assertSame($this->quoteMock, $this->saveHandler->save($this->quoteMock));
  146. }
  147. public function testSaveWithNotExistingCustomerAddress()
  148. {
  149. $customerAddressId = 5;
  150. $this->quoteMock->expects(static::atLeastOnce())
  151. ->method('getItems')
  152. ->willReturn([]);
  153. $this->quoteMock->expects(static::never())
  154. ->method('setLastAddedItem');
  155. $this->billingAddressMock->expects(static::atLeastOnce())
  156. ->method('getCustomerAddressId')
  157. ->willReturn($customerAddressId);
  158. $this->addressRepositoryMock->expects(static::once())
  159. ->method('getById')
  160. ->with($customerAddressId)
  161. ->willThrowException(new NoSuchEntityException());
  162. $this->billingAddressMock->expects(static::once())
  163. ->method('setCustomerAddressId')
  164. ->willReturn(null);
  165. $this->billingAddressPersisterMock->expects(static::once())
  166. ->method('save')
  167. ->with($this->quoteMock, $this->billingAddressMock);
  168. $this->quoteMock->expects(static::atLeastOnce())
  169. ->method('isVirtual')
  170. ->willReturn(true);
  171. $this->extensionAttributesMock->expects(static::never())
  172. ->method('getShippingAssignments');
  173. $this->quoteMock->expects(static::atLeastOnce())
  174. ->method('collectTotals')
  175. ->willReturnSelf();
  176. $this->quoteResourceModelMock->expects(static::once())
  177. ->method('save')
  178. ->with($this->quoteMock)
  179. ->willReturnSelf();
  180. $this->assertSame($this->quoteMock, $this->saveHandler->save($this->quoteMock));
  181. }
  182. /**
  183. * Create quote item mock
  184. *
  185. * @param bool $isDeleted
  186. * @return QuoteItem|\PHPUnit_Framework_MockObject_MockObject
  187. */
  188. private function createQuoteItemMock($isDeleted)
  189. {
  190. $quoteItemMock = $this->getMockBuilder(QuoteItem::class)
  191. ->disableOriginalConstructor()
  192. ->getMock();
  193. $quoteItemMock->expects(static::any())
  194. ->method('isDeleted')
  195. ->willReturn($isDeleted);
  196. return $quoteItemMock;
  197. }
  198. }