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