QuoteRepositoryTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Model;
  7. use Magento\Store\Model\StoreRepository;
  8. use Magento\TestFramework\Helper\Bootstrap as BootstrapHelper;
  9. use Magento\Framework\ObjectManagerInterface;
  10. use Magento\Framework\Api\SearchCriteriaBuilder;
  11. use Magento\Framework\Api\SearchCriteria;
  12. use Magento\Framework\Api\SearchResults;
  13. use Magento\Framework\Api\FilterBuilder;
  14. use Magento\Quote\Api\Data\CartInterface;
  15. use Magento\Quote\Api\Data\CartSearchResultsInterface;
  16. use Magento\Quote\Api\Data\CartExtension;
  17. use Magento\User\Api\Data\UserInterface;
  18. use Magento\Quote\Model\Quote\Address as QuoteAddress;
  19. /**
  20. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  21. * @magentoDbIsolation disabled
  22. */
  23. class QuoteRepositoryTest extends \PHPUnit\Framework\TestCase
  24. {
  25. /**
  26. * @var ObjectManagerInterface
  27. */
  28. private $objectManager;
  29. /**
  30. * @var QuoteRepository
  31. */
  32. private $quoteRepository;
  33. /**
  34. * @var SearchCriteriaBuilder
  35. */
  36. private $searchCriteriaBuilder;
  37. /**
  38. * @var FilterBuilder
  39. */
  40. private $filterBuilder;
  41. /**
  42. * Set up
  43. */
  44. protected function setUp()
  45. {
  46. $this->objectManager = BootstrapHelper::getObjectManager();
  47. $this->quoteRepository = $this->objectManager->create(QuoteRepository::class);
  48. $this->searchCriteriaBuilder = $this->objectManager->create(SearchCriteriaBuilder::class);
  49. $this->filterBuilder = $this->objectManager->create(FilterBuilder::class);
  50. }
  51. /**
  52. * Tests that quote saved with custom store id has same store id after getting via repository.
  53. *
  54. * @magentoDataFixture Magento/Sales/_files/quote.php
  55. * @magentoDataFixture Magento/Store/_files/second_store.php
  56. */
  57. public function testGetQuoteWithCustomStoreId()
  58. {
  59. $secondStoreCode = 'fixture_second_store';
  60. $reservedOrderId = 'test01';
  61. $storeRepository = $this->objectManager->create(StoreRepository::class);
  62. $secondStore = $storeRepository->get($secondStoreCode);
  63. // Set store_id in quote to second store_id
  64. $quote = $this->getQuote($reservedOrderId);
  65. $quote->setStoreId($secondStore->getId());
  66. $this->quoteRepository->save($quote);
  67. $savedQuote = $this->quoteRepository->get($quote->getId());
  68. $this->assertEquals(
  69. $secondStore->getId(),
  70. $savedQuote->getStoreId(),
  71. 'Quote store id should be equal with store id value in DB'
  72. );
  73. }
  74. /**
  75. * @magentoDataFixture Magento/Sales/_files/quote.php
  76. */
  77. public function testGetList()
  78. {
  79. $searchCriteria = $this->getSearchCriteria('test01');
  80. $searchResult = $this->quoteRepository->getList($searchCriteria);
  81. $this->performAssertions($searchResult);
  82. }
  83. /**
  84. * @magentoDataFixture Magento/Sales/_files/quote.php
  85. */
  86. public function testGetListDoubleCall()
  87. {
  88. $searchCriteria1 = $this->getSearchCriteria('test01');
  89. $searchCriteria2 = $this->getSearchCriteria('test02');
  90. $searchResult = $this->quoteRepository->getList($searchCriteria1);
  91. $this->performAssertions($searchResult);
  92. $searchResult = $this->quoteRepository->getList($searchCriteria2);
  93. $this->assertEmpty($searchResult->getItems());
  94. }
  95. /**
  96. * @magentoAppIsolation enabled
  97. */
  98. public function testSaveWithNotExistingCustomerAddress()
  99. {
  100. $addressData = include __DIR__ . '/../../Sales/_files/address_data.php';
  101. /** @var QuoteAddress $billingAddress */
  102. $billingAddress = $this->objectManager->create(QuoteAddress::class, ['data' => $addressData]);
  103. $billingAddress->setAddressType(QuoteAddress::ADDRESS_TYPE_BILLING)
  104. ->setCustomerAddressId('not_existing');
  105. /** @var QuoteAddress $shippingAddress */
  106. $shippingAddress = $this->objectManager->create(QuoteAddress::class, ['data' => $addressData]);
  107. $shippingAddress->setAddressType(QuoteAddress::ADDRESS_TYPE_SHIPPING)
  108. ->setCustomerAddressId('not_existing');
  109. /** @var Shipping $shipping */
  110. $shipping = $this->objectManager->create(Shipping::class);
  111. $shipping->setAddress($shippingAddress);
  112. /** @var ShippingAssignment $shippingAssignment */
  113. $shippingAssignment = $this->objectManager->create(ShippingAssignment::class);
  114. $shippingAssignment->setItems([]);
  115. $shippingAssignment->setShipping($shipping);
  116. /** @var CartExtension $extensionAttributes */
  117. $extensionAttributes = $this->objectManager->create(CartExtension::class);
  118. $extensionAttributes->setShippingAssignments([$shippingAssignment]);
  119. /** @var Quote $quote */
  120. $quote = $this->objectManager->create(Quote::class);
  121. $quote->setStoreId(1)
  122. ->setIsActive(true)
  123. ->setIsMultiShipping(false)
  124. ->setBillingAddress($billingAddress)
  125. ->setShippingAddress($shippingAddress)
  126. ->setExtensionAttributes($extensionAttributes)
  127. ->save();
  128. $this->quoteRepository->save($quote);
  129. $this->assertNull($quote->getBillingAddress()->getCustomerAddressId());
  130. $this->assertNull(
  131. $quote->getExtensionAttributes()
  132. ->getShippingAssignments()[0]
  133. ->getShipping()
  134. ->getAddress()
  135. ->getCustomerAddressId()
  136. );
  137. }
  138. /**
  139. * Returns quote by reserved order id.
  140. *
  141. * @param string $reservedOrderId
  142. * @return CartInterface
  143. */
  144. private function getQuote(string $reservedOrderId)
  145. {
  146. $searchCriteria = $this->getSearchCriteria($reservedOrderId);
  147. $searchResult = $this->quoteRepository->getList($searchCriteria);
  148. $items = $searchResult->getItems();
  149. /** @var CartInterface $quote */
  150. $quote = array_pop($items);
  151. return $quote;
  152. }
  153. /**
  154. * Get search criteria
  155. *
  156. * @param string $filterValue
  157. * @return SearchCriteria
  158. */
  159. private function getSearchCriteria($filterValue)
  160. {
  161. $filters = [];
  162. $filters[] = $this->filterBuilder->setField('reserved_order_id')
  163. ->setConditionType('=')
  164. ->setValue($filterValue)
  165. ->create();
  166. $this->searchCriteriaBuilder->addFilters($filters);
  167. return $this->searchCriteriaBuilder->create();
  168. }
  169. /**
  170. * Perform assertions
  171. *
  172. * @param SearchResults|CartSearchResultsInterface $searchResult
  173. */
  174. private function performAssertions($searchResult)
  175. {
  176. $expectedExtensionAttributes = [
  177. 'firstname' => 'firstname',
  178. 'lastname' => 'lastname',
  179. 'email' => 'admin@example.com'
  180. ];
  181. $items = $searchResult->getItems();
  182. /** @var CartInterface $actualQuote */
  183. $actualQuote = array_pop($items);
  184. /** @var UserInterface $testAttribute */
  185. $testAttribute = $actualQuote->getExtensionAttributes()->getQuoteTestAttribute();
  186. $this->assertInstanceOf(CartInterface::class, $actualQuote);
  187. $this->assertEquals('test01', $actualQuote->getReservedOrderId());
  188. $this->assertEquals($expectedExtensionAttributes['firstname'], $testAttribute->getFirstName());
  189. $this->assertEquals($expectedExtensionAttributes['lastname'], $testAttribute->getLastName());
  190. $this->assertEquals($expectedExtensionAttributes['email'], $testAttribute->getEmail());
  191. }
  192. }