Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CheckItemsTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
18 use Magento\Quote\Api\CartRepositoryInterface as QuoteRepository;
20 
29 {
33  private $quote;
34 
38  private $checkoutSession;
39 
43  private $productRepository;
44 
48  private $json;
49 
53  public function setUp()
54  {
55  parent::setUp();
56 
57  $this->checkoutSession = $this->_objectManager->get(Session::class);
58  $this->productRepository = $this->_objectManager->get(ProductRepositoryInterface::class);
59  $this->json = $this->_objectManager->get(Json::class);
60  $this->quote = $this->getQuote('test01');
61  $this->checkoutSession->setQuoteId($this->quote->getId());
62  $this->checkoutSession->setCartWasUpdated(false);
63  }
64 
75  public function testExecute($requestQuantity, $expectedResponse)
76  {
77  $this->loginCustomer();
78 
79  try {
81  $product = $this->productRepository->get('simple');
82  } catch (\Exception $e) {
83  $this->fail('No such product entity');
84  }
85 
86  $quoteItem = $this->quote->getItemByProduct($product);
87  $this->assertNotFalse($quoteItem, 'Cannot get quote item for simple product');
88 
89  $request = [];
90  if (!empty($requestQuantity) && is_array($requestQuantity)) {
91  $request= [
92  'ship' => [
93  [$quoteItem->getId() => $requestQuantity],
94  ]
95  ];
96  }
97 
98  $this->getRequest()->setPostValue($request);
99  $this->dispatch('multishipping/checkout/checkItems');
100  $response = $this->getResponse()->getBody();
101 
102  $this->assertEquals($expectedResponse, $this->json->unserialize($response));
103  }
104 
108  private function loginCustomer()
109  {
110  $logger = $this->createMock(\Psr\Log\LoggerInterface::class);
112  $service = $this->_objectManager->create(AccountManagementInterface::class);
113  try {
114  $customer = $service->authenticate('[email protected]', 'password');
115  } catch (LocalizedException $e) {
116  $this->fail($e->getMessage());
117  }
119  $customerSession = $this->_objectManager->create(CustomerSession::class, [$logger]);
120  $customerSession->setCustomerDataAsLoggedIn($customer);
121  }
122 
129  private function getQuote($reservedOrderId)
130  {
132  $searchCriteriaBuilder = $this->_objectManager->create(SearchCriteriaBuilder::class);
133  $searchCriteria = $searchCriteriaBuilder->addFilter('reserved_order_id', $reservedOrderId)
134  ->create();
136  $quoteRepository = $this->_objectManager->get(QuoteRepository::class);
138  ->getItems();
139  return array_pop($items);
140  }
141 
146  public function requestDataProvider(): array
147  {
148  return [
149  [
150  'request' => [],
151  'response' => [
152  'success' => false,
153  'error_message' => 'We are unable to process your request. Please, try again later.'
154  ]
155  ],
156  [
157  'request' => ['qty' => 2],
158  'response' => [
159  'success' => true,
160  ]
161  ],
162  [
163  'request' => ['qty' => 101],
164  'response' => [
165  'success' => false,
166  'error_message' => 'The requested qty is not available']
167  ],
168  [
169  'request' => ['qty' => 230],
170  'response' => [
171  'success' => false,
172  'error_message' => 'Maximum qty allowed for Shipping to multiple addresses is 200']
173  ],
174  ];
175  }
176 }
$response
Definition: 404.php:11
$customer
Definition: customers.php:11
$logger
$searchCriteria
$quoteItem
Definition: quote.php:38
dispatch(RequestInterface $request)
Definition: Checkout.php:92
$searchCriteriaBuilder
$quoteRepository
$items