CheckItemsTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Multishipping\Controller\Checkout;
  8. use Magento\Catalog\Model\Product;
  9. use Magento\Catalog\Api\ProductRepositoryInterface;
  10. use Magento\Checkout\Model\Session;
  11. use Magento\Customer\Api\AccountManagementInterface;
  12. use Magento\Customer\Model\Session as CustomerSession;
  13. use Magento\Framework\Exception\LocalizedException;
  14. use Magento\Quote\Model\Quote;
  15. use Magento\Framework\Api\SearchCriteriaBuilder;
  16. use Magento\Quote\Api\CartRepositoryInterface as QuoteRepository;
  17. use Magento\Framework\Serialize\Serializer\Json;
  18. /**
  19. * Test class for \Magento\Multishipping\Controller\Checkout
  20. *
  21. * @magentoAppArea frontend
  22. * @magentoDataFixture Magento/Sales/_files/quote.php
  23. * @magentoDataFixture Magento/Customer/_files/customer.php
  24. */
  25. class CheckItemsTest extends \Magento\TestFramework\TestCase\AbstractController
  26. {
  27. /**
  28. * @var Quote
  29. */
  30. private $quote;
  31. /**
  32. * @var Session
  33. */
  34. private $checkoutSession;
  35. /**
  36. * @var ProductRepositoryInterface
  37. */
  38. private $productRepository;
  39. /**
  40. * @var Json
  41. */
  42. private $json;
  43. /**
  44. * @inheritdoc
  45. */
  46. public function setUp()
  47. {
  48. parent::setUp();
  49. $this->checkoutSession = $this->_objectManager->get(Session::class);
  50. $this->productRepository = $this->_objectManager->get(ProductRepositoryInterface::class);
  51. $this->json = $this->_objectManager->get(Json::class);
  52. $this->quote = $this->getQuote('test01');
  53. $this->checkoutSession->setQuoteId($this->quote->getId());
  54. $this->checkoutSession->setCartWasUpdated(false);
  55. }
  56. /**
  57. * Validator of quote items.
  58. *
  59. * @param array $requestQuantity
  60. * @param array $expectedResponse
  61. *
  62. * @magentoConfigFixture current_store multishipping/options/checkout_multiple 1
  63. * @magentoConfigFixture current_store multishipping/options/checkout_multiple_maximum_qty 200
  64. * @dataProvider requestDataProvider
  65. */
  66. public function testExecute($requestQuantity, $expectedResponse)
  67. {
  68. $this->loginCustomer();
  69. try {
  70. /** @var $product Product */
  71. $product = $this->productRepository->get('simple');
  72. } catch (\Exception $e) {
  73. $this->fail('No such product entity');
  74. }
  75. $quoteItem = $this->quote->getItemByProduct($product);
  76. $this->assertNotFalse($quoteItem, 'Cannot get quote item for simple product');
  77. $request = [];
  78. if (!empty($requestQuantity) && is_array($requestQuantity)) {
  79. $request= [
  80. 'ship' => [
  81. [$quoteItem->getId() => $requestQuantity],
  82. ]
  83. ];
  84. }
  85. $this->getRequest()->setPostValue($request);
  86. $this->dispatch('multishipping/checkout/checkItems');
  87. $response = $this->getResponse()->getBody();
  88. $this->assertEquals($expectedResponse, $this->json->unserialize($response));
  89. }
  90. /**
  91. * Authenticates customer and creates customer session.
  92. */
  93. private function loginCustomer()
  94. {
  95. $logger = $this->createMock(\Psr\Log\LoggerInterface::class);
  96. /** @var AccountManagementInterface $service */
  97. $service = $this->_objectManager->create(AccountManagementInterface::class);
  98. try {
  99. $customer = $service->authenticate('customer@example.com', 'password');
  100. } catch (LocalizedException $e) {
  101. $this->fail($e->getMessage());
  102. }
  103. /** @var CustomerSession $customerSession */
  104. $customerSession = $this->_objectManager->create(CustomerSession::class, [$logger]);
  105. $customerSession->setCustomerDataAsLoggedIn($customer);
  106. }
  107. /**
  108. * Gets quote by reserved order id.
  109. *
  110. * @param string $reservedOrderId
  111. * @return Quote
  112. */
  113. private function getQuote($reservedOrderId)
  114. {
  115. /** @var SearchCriteriaBuilder $searchCriteriaBuilder */
  116. $searchCriteriaBuilder = $this->_objectManager->create(SearchCriteriaBuilder::class);
  117. $searchCriteria = $searchCriteriaBuilder->addFilter('reserved_order_id', $reservedOrderId)
  118. ->create();
  119. /** @var QuoteRepository $quoteRepository */
  120. $quoteRepository = $this->_objectManager->get(QuoteRepository::class);
  121. $items = $quoteRepository->getList($searchCriteria)
  122. ->getItems();
  123. return array_pop($items);
  124. }
  125. /**
  126. * Variations of request data.
  127. * @returns array
  128. */
  129. public function requestDataProvider(): array
  130. {
  131. return [
  132. [
  133. 'request' => [],
  134. 'response' => [
  135. 'success' => false,
  136. 'error_message' => 'We are unable to process your request. Please, try again later.'
  137. ]
  138. ],
  139. [
  140. 'request' => ['qty' => 2],
  141. 'response' => [
  142. 'success' => true,
  143. ]
  144. ],
  145. [
  146. 'request' => ['qty' => 101],
  147. 'response' => [
  148. 'success' => false,
  149. 'error_message' => 'The requested qty is not available']
  150. ],
  151. [
  152. 'request' => ['qty' => 230],
  153. 'response' => [
  154. 'success' => false,
  155. 'error_message' => 'Maximum qty allowed for Shipping to multiple addresses is 200']
  156. ],
  157. ];
  158. }
  159. }