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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Quote\Model;
use Magento\Store\Model\StoreRepository;
use Magento\TestFramework\Helper\Bootstrap as BootstrapHelper;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Api\SearchCriteria;
use Magento\Framework\Api\SearchResults;
use Magento\Framework\Api\FilterBuilder;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Api\Data\CartSearchResultsInterface;
use Magento\Quote\Api\Data\CartExtension;
use Magento\User\Api\Data\UserInterface;
use Magento\Quote\Model\Quote\Address as QuoteAddress;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @magentoDbIsolation disabled
*/
class QuoteRepositoryTest extends \PHPUnit\Framework\TestCase
{
/**
* @var ObjectManagerInterface
*/
private $objectManager;
/**
* @var QuoteRepository
*/
private $quoteRepository;
/**
* @var SearchCriteriaBuilder
*/
private $searchCriteriaBuilder;
/**
* @var FilterBuilder
*/
private $filterBuilder;
/**
* Set up
*/
protected function setUp()
{
$this->objectManager = BootstrapHelper::getObjectManager();
$this->quoteRepository = $this->objectManager->create(QuoteRepository::class);
$this->searchCriteriaBuilder = $this->objectManager->create(SearchCriteriaBuilder::class);
$this->filterBuilder = $this->objectManager->create(FilterBuilder::class);
}
/**
* Tests that quote saved with custom store id has same store id after getting via repository.
*
* @magentoDataFixture Magento/Sales/_files/quote.php
* @magentoDataFixture Magento/Store/_files/second_store.php
*/
public function testGetQuoteWithCustomStoreId()
{
$secondStoreCode = 'fixture_second_store';
$reservedOrderId = 'test01';
$storeRepository = $this->objectManager->create(StoreRepository::class);
$secondStore = $storeRepository->get($secondStoreCode);
// Set store_id in quote to second store_id
$quote = $this->getQuote($reservedOrderId);
$quote->setStoreId($secondStore->getId());
$this->quoteRepository->save($quote);
$savedQuote = $this->quoteRepository->get($quote->getId());
$this->assertEquals(
$secondStore->getId(),
$savedQuote->getStoreId(),
'Quote store id should be equal with store id value in DB'
);
}
/**
* @magentoDataFixture Magento/Sales/_files/quote.php
*/
public function testGetList()
{
$searchCriteria = $this->getSearchCriteria('test01');
$searchResult = $this->quoteRepository->getList($searchCriteria);
$this->performAssertions($searchResult);
}
/**
* @magentoDataFixture Magento/Sales/_files/quote.php
*/
public function testGetListDoubleCall()
{
$searchCriteria1 = $this->getSearchCriteria('test01');
$searchCriteria2 = $this->getSearchCriteria('test02');
$searchResult = $this->quoteRepository->getList($searchCriteria1);
$this->performAssertions($searchResult);
$searchResult = $this->quoteRepository->getList($searchCriteria2);
$this->assertEmpty($searchResult->getItems());
}
/**
* @magentoAppIsolation enabled
*/
public function testSaveWithNotExistingCustomerAddress()
{
$addressData = include __DIR__ . '/../../Sales/_files/address_data.php';
/** @var QuoteAddress $billingAddress */
$billingAddress = $this->objectManager->create(QuoteAddress::class, ['data' => $addressData]);
$billingAddress->setAddressType(QuoteAddress::ADDRESS_TYPE_BILLING)
->setCustomerAddressId('not_existing');
/** @var QuoteAddress $shippingAddress */
$shippingAddress = $this->objectManager->create(QuoteAddress::class, ['data' => $addressData]);
$shippingAddress->setAddressType(QuoteAddress::ADDRESS_TYPE_SHIPPING)
->setCustomerAddressId('not_existing');
/** @var Shipping $shipping */
$shipping = $this->objectManager->create(Shipping::class);
$shipping->setAddress($shippingAddress);
/** @var ShippingAssignment $shippingAssignment */
$shippingAssignment = $this->objectManager->create(ShippingAssignment::class);
$shippingAssignment->setItems([]);
$shippingAssignment->setShipping($shipping);
/** @var CartExtension $extensionAttributes */
$extensionAttributes = $this->objectManager->create(CartExtension::class);
$extensionAttributes->setShippingAssignments([$shippingAssignment]);
/** @var Quote $quote */
$quote = $this->objectManager->create(Quote::class);
$quote->setStoreId(1)
->setIsActive(true)
->setIsMultiShipping(false)
->setBillingAddress($billingAddress)
->setShippingAddress($shippingAddress)
->setExtensionAttributes($extensionAttributes)
->save();
$this->quoteRepository->save($quote);
$this->assertNull($quote->getBillingAddress()->getCustomerAddressId());
$this->assertNull(
$quote->getExtensionAttributes()
->getShippingAssignments()[0]
->getShipping()
->getAddress()
->getCustomerAddressId()
);
}
/**
* Returns quote by reserved order id.
*
* @param string $reservedOrderId
* @return CartInterface
*/
private function getQuote(string $reservedOrderId)
{
$searchCriteria = $this->getSearchCriteria($reservedOrderId);
$searchResult = $this->quoteRepository->getList($searchCriteria);
$items = $searchResult->getItems();
/** @var CartInterface $quote */
$quote = array_pop($items);
return $quote;
}
/**
* Get search criteria
*
* @param string $filterValue
* @return SearchCriteria
*/
private function getSearchCriteria($filterValue)
{
$filters = [];
$filters[] = $this->filterBuilder->setField('reserved_order_id')
->setConditionType('=')
->setValue($filterValue)
->create();
$this->searchCriteriaBuilder->addFilters($filters);
return $this->searchCriteriaBuilder->create();
}
/**
* Perform assertions
*
* @param SearchResults|CartSearchResultsInterface $searchResult
*/
private function performAssertions($searchResult)
{
$expectedExtensionAttributes = [
'firstname' => 'firstname',
'lastname' => 'lastname',
'email' => 'admin@example.com'
];
$items = $searchResult->getItems();
/** @var CartInterface $actualQuote */
$actualQuote = array_pop($items);
/** @var UserInterface $testAttribute */
$testAttribute = $actualQuote->getExtensionAttributes()->getQuoteTestAttribute();
$this->assertInstanceOf(CartInterface::class, $actualQuote);
$this->assertEquals('test01', $actualQuote->getReservedOrderId());
$this->assertEquals($expectedExtensionAttributes['firstname'], $testAttribute->getFirstName());
$this->assertEquals($expectedExtensionAttributes['lastname'], $testAttribute->getLastName());
$this->assertEquals($expectedExtensionAttributes['email'], $testAttribute->getEmail());
}
}