Skip to content

Commit 7ec7737

Browse files
authored
Merge pull request magento#702 from magento-engcom/687-Fix-Integration-Test-for-placing-Order
MSI-687: Integration test for placing an order
2 parents f8f0f54 + f70c9d4 commit 7ec7737

File tree

2 files changed

+33
-106
lines changed

2 files changed

+33
-106
lines changed

app/code/Magento/InventorySales/Test/Integration/Order/PlaceOrderOnDefaultSourceTest.php renamed to app/code/Magento/InventorySales/Test/Integration/Order/PlaceOrderOnDefaultStockTest.php

Lines changed: 10 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,18 @@
1919
use Magento\Quote\Api\Data\CartInterface;
2020
use Magento\Quote\Api\Data\CartItemInterface;
2121
use Magento\Quote\Api\Data\CartItemInterfaceFactory;
22+
use Magento\Sales\Api\OrderManagementInterface;
2223
use Magento\Sales\Api\OrderRepositoryInterface;
2324
use PHPUnit\Framework\TestCase;
2425
use Magento\TestFramework\Helper\Bootstrap;
2526
use Magento\InventoryCatalog\Api\DefaultStockProviderInterface;
2627
use Magento\InventoryReservations\Model\CleanupReservationsInterface;
27-
use Magento\InventoryReservations\Model\ReservationBuilderInterface;
28-
use Magento\InventoryReservationsApi\Api\AppendReservationsInterface;
2928
use Magento\Catalog\Api\ProductRepositoryInterface;
3029

3130
/**
3231
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
3332
*/
34-
class PlaceOrderOnDefaultSourceTest extends TestCase
33+
class PlaceOrderOnDefaultStockTest extends TestCase
3534
{
3635
/**
3736
* @var DefaultStockProviderInterface
@@ -63,16 +62,6 @@ class PlaceOrderOnDefaultSourceTest extends TestCase
6362
*/
6463
private $cartItemFactory;
6564

66-
/**
67-
* @var AppendReservationsInterface
68-
*/
69-
private $appendReservations;
70-
71-
/**
72-
* @var ReservationBuilderInterface
73-
*/
74-
private $reservationBuilder;
75-
7665
/**
7766
* @var SearchCriteriaBuilder
7867
*/
@@ -88,6 +77,11 @@ class PlaceOrderOnDefaultSourceTest extends TestCase
8877
*/
8978
private $registry;
9079

80+
/**
81+
* @var OrderManagementInterface
82+
*/
83+
private $orderManagement;
84+
9185
protected function setUp()
9286
{
9387
$this->registry = Bootstrap::getObjectManager()->get(Registry::class);
@@ -98,9 +92,8 @@ protected function setUp()
9892
$this->cartItemFactory = Bootstrap::getObjectManager()->get(CartItemInterfaceFactory::class);
9993
$this->defaultStockProvider = Bootstrap::getObjectManager()->get(DefaultStockProviderInterface::class);
10094
$this->cleanupReservations = Bootstrap::getObjectManager()->get(CleanupReservationsInterface::class);
101-
$this->appendReservations = Bootstrap::getObjectManager()->get(AppendReservationsInterface::class);
102-
$this->reservationBuilder = Bootstrap::getObjectManager()->get(ReservationBuilderInterface::class);
10395
$this->orderRepository = Bootstrap::getObjectManager()->get(OrderRepositoryInterface::class);
96+
$this->orderManagement = Bootstrap::getObjectManager()->get(OrderManagementInterface::class);
10497
}
10598

10699
/**
@@ -115,12 +108,8 @@ protected function setUp()
115108
*/
116109
public function testPlaceOrderWithInStockProduct()
117110
{
118-
$this->markTestSkipped(
119-
'Fix Integration Test for placing Order https://github.com/magento-engcom/msi/issues/687'
120-
);
121111
$sku = 'SKU-1';
122112
$quoteItemQty = 3.8;
123-
$reservedDuringCheckoutQty = 1.5;
124113

125114
$cart = $this->getCart();
126115
$product = $this->productRepository->get($sku);
@@ -139,14 +128,11 @@ public function testPlaceOrderWithInStockProduct()
139128
$cart->addItem($cartItem);
140129
$this->cartRepository->save($cart);
141130

142-
$this->appendReservation($sku, -$reservedDuringCheckoutQty);
143-
144131
$orderId = $this->cartManagement->placeOrder($cart->getId());
145132

146133
self::assertNotNull($orderId);
147134

148135
//cleanup
149-
$this->appendReservation($sku, $reservedDuringCheckoutQty);
150136
$this->deleteOrderById((int)$orderId);
151137
}
152138

@@ -162,12 +148,8 @@ public function testPlaceOrderWithInStockProduct()
162148
*/
163149
public function testPlaceOrderWithOutOffStockProduct()
164150
{
165-
$this->markTestSkipped(
166-
'Fix Integration Test for placing Order https://github.com/magento-engcom/msi/issues/687'
167-
);
168151
$sku = 'SKU-1';
169-
$quoteItemQty = 5.5;
170-
$reservedDuringCheckoutQty = 4.9;
152+
$quoteItemQty = 8.7;
171153

172154
$cart = $this->getCart();
173155
$product = $this->productRepository->get($sku);
@@ -186,33 +168,10 @@ public function testPlaceOrderWithOutOffStockProduct()
186168
$cart->addItem($cartItem);
187169
$this->cartRepository->save($cart);
188170

189-
//append reservation during checkout to make laking quantity in stock
190-
$this->appendReservation($sku, -$reservedDuringCheckoutQty);
191-
192171
self::expectException(LocalizedException::class);
193172
$orderId = $this->cartManagement->placeOrder($cart->getId());
194173

195174
self::assertNull($orderId);
196-
197-
//cleanup
198-
$this->appendReservation($sku, $reservedDuringCheckoutQty);
199-
}
200-
201-
/**
202-
* @param string $productSku
203-
* @param float $qty
204-
* @return void
205-
* @throws \Magento\Framework\Exception\CouldNotSaveException
206-
* @throws \Magento\Framework\Exception\InputException
207-
* @throws \Magento\Framework\Validation\ValidationException
208-
*/
209-
private function appendReservation(string $productSku, float $qty)
210-
{
211-
$this->appendReservations->execute([
212-
$this->reservationBuilder->setStockId(
213-
$this->defaultStockProvider->getId()
214-
)->setSku($productSku)->setQuantity($qty)->build(),
215-
]);
216175
}
217176

218177
/**
@@ -238,6 +197,7 @@ private function deleteOrderById(int $orderId)
238197
{
239198
$this->registry->unregister('isSecureArea');
240199
$this->registry->register('isSecureArea', true);
200+
$this->orderManagement->cancel($orderId);
241201
$this->orderRepository->delete($this->orderRepository->get($orderId));
242202
$this->registry->unregister('isSecureArea');
243203
$this->registry->register('isSecureArea', false);

app/code/Magento/InventorySales/Test/Integration/Order/PlaceOrderOnNotDefaultSourceTest.php renamed to app/code/Magento/InventorySales/Test/Integration/Order/PlaceOrderOnNotDefaultStockTest.php

Lines changed: 23 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,21 @@
2121
use Magento\Quote\Api\Data\CartInterface;
2222
use Magento\Quote\Api\Data\CartItemInterface;
2323
use Magento\Quote\Api\Data\CartItemInterfaceFactory;
24+
use Magento\Sales\Api\OrderManagementInterface;
2425
use Magento\Sales\Api\OrderRepositoryInterface;
2526
use Magento\Store\Api\Data\StoreInterface;
2627
use Magento\Store\Api\StoreRepositoryInterface;
28+
use Magento\Store\Model\StoreManagerInterface;
2729
use PHPUnit\Framework\TestCase;
2830
use Magento\TestFramework\Helper\Bootstrap;
2931
use Magento\InventoryReservations\Model\CleanupReservationsInterface;
30-
use Magento\InventoryReservations\Model\ReservationBuilderInterface;
31-
use Magento\InventoryReservationsApi\Api\AppendReservationsInterface;
3232
use Magento\Catalog\Api\ProductRepositoryInterface;
3333
use Magento\InventoryApi\Api\StockRepositoryInterface;
3434

3535
/**
3636
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
3737
*/
38-
class PlaceOrderOnNotDefaultSourceTest extends TestCase
38+
class PlaceOrderOnNotDefaultStockTest extends TestCase
3939
{
4040
/**
4141
* @var CleanupReservationsInterface
@@ -62,16 +62,6 @@ class PlaceOrderOnNotDefaultSourceTest extends TestCase
6262
*/
6363
private $cartItemFactory;
6464

65-
/**
66-
* @var AppendReservationsInterface
67-
*/
68-
private $appendReservations;
69-
70-
/**
71-
* @var ReservationBuilderInterface
72-
*/
73-
private $reservationBuilder;
74-
7565
/**
7666
* @var StockRepositoryInterface
7767
*/
@@ -97,20 +87,30 @@ class PlaceOrderOnNotDefaultSourceTest extends TestCase
9787
*/
9888
private $registry;
9989

90+
/**
91+
* @var OrderManagementInterface
92+
*/
93+
private $orderManagement;
94+
95+
/**
96+
* @var StoreManagerInterface
97+
*/
98+
private $storeManager;
99+
100100
protected function setUp()
101101
{
102102
$this->registry = Bootstrap::getObjectManager()->get(Registry::class);
103103
$this->stockRepository = Bootstrap::getObjectManager()->get(StockRepositoryInterface::class);
104104
$this->storeRepository = Bootstrap::getObjectManager()->get(StoreRepositoryInterface::class);
105+
$this->storeManager = Bootstrap::getObjectManager()->get(StoreManagerInterface::class);
105106
$this->cartManagement = Bootstrap::getObjectManager()->get(CartManagementInterface::class);
106107
$this->cartRepository = Bootstrap::getObjectManager()->get(CartRepositoryInterface::class);
107108
$this->productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class);
108109
$this->searchCriteriaBuilder = Bootstrap::getObjectManager()->get(SearchCriteriaBuilder::class);
109110
$this->cartItemFactory = Bootstrap::getObjectManager()->get(CartItemInterfaceFactory::class);
110111
$this->cleanupReservations = Bootstrap::getObjectManager()->get(CleanupReservationsInterface::class);
111-
$this->appendReservations = Bootstrap::getObjectManager()->get(AppendReservationsInterface::class);
112-
$this->reservationBuilder = Bootstrap::getObjectManager()->get(ReservationBuilderInterface::class);
113112
$this->orderRepository = Bootstrap::getObjectManager()->get(OrderRepositoryInterface::class);
113+
$this->orderManagement = Bootstrap::getObjectManager()->get(OrderManagementInterface::class);
114114
}
115115

116116
/**
@@ -130,13 +130,9 @@ protected function setUp()
130130
*/
131131
public function testPlaceOrderWithInStockProduct()
132132
{
133-
$this->markTestSkipped(
134-
'Fix Integration Test for placing Order https://github.com/magento-engcom/msi/issues/687'
135-
);
136-
$sku = 'SKU-1';
137-
$stockId = 10;
138-
$quoteItemQty = 4;
139-
$reservedDuringCheckoutQty = 1.5;
133+
$sku = 'SKU-2';
134+
$stockId = 30;
135+
$quoteItemQty = 2.2;
140136

141137
$cart = $this->getCartByStockId($stockId);
142138
$product = $this->productRepository->get($sku);
@@ -154,15 +150,10 @@ public function testPlaceOrderWithInStockProduct()
154150
);
155151
$cart->addItem($cartItem);
156152
$this->cartRepository->save($cart);
157-
158-
$this->appendReservation($sku, -$reservedDuringCheckoutQty, $stockId);
159-
160153
$orderId = $this->cartManagement->placeOrder($cart->getId());
161154

162155
self::assertNotNull($orderId);
163156

164-
//cleanup
165-
$this->appendReservation($sku, $reservedDuringCheckoutQty, $stockId);
166157
$this->deleteOrderById((int)$orderId);
167158
}
168159

@@ -183,13 +174,9 @@ public function testPlaceOrderWithInStockProduct()
183174
*/
184175
public function testPlaceOrderWithOutOffStockProduct()
185176
{
186-
$this->markTestSkipped(
187-
'Fix Integration Test for placing Order https://github.com/magento-engcom/msi/issues/687'
188-
);
189-
$sku = 'SKU-1';
190-
$stockId = 10;
191-
$quoteItemQty = 4;
192-
$reservedDuringCheckoutQty = 3.8;
177+
$sku = 'SKU-2';
178+
$stockId = 30;
179+
$quoteItemQty = 6.2;
193180

194181
$cart = $this->getCartByStockId($stockId);
195182
$product = $this->productRepository->get($sku);
@@ -208,32 +195,10 @@ public function testPlaceOrderWithOutOffStockProduct()
208195
$cart->addItem($cartItem);
209196
$this->cartRepository->save($cart);
210197

211-
//append reservation during checkout to make laking quantity in stock
212-
$this->appendReservation($sku, -$reservedDuringCheckoutQty, $stockId);
213-
214198
self::expectException(LocalizedException::class);
215199
$orderId = $this->cartManagement->placeOrder($cart->getId());
216200

217201
self::assertNull($orderId);
218-
219-
//cleanup
220-
$this->appendReservation($sku, $reservedDuringCheckoutQty, $stockId);
221-
}
222-
223-
/**
224-
* @param string $productSku
225-
* @param float $qty
226-
* @param int $stockId
227-
* @return void
228-
* @throws CouldNotSaveException
229-
* @throws InputException
230-
* @throws ValidationException
231-
*/
232-
private function appendReservation(string $productSku, float $qty, int $stockId)
233-
{
234-
$this->appendReservations->execute([
235-
$this->reservationBuilder->setStockId($stockId)->setSku($productSku)->setQuantity($qty)->build(),
236-
]);
237202
}
238203

239204
/**
@@ -261,6 +226,7 @@ private function getCartByStockId(int $stockId): CartInterface
261226
}
262227
/** @var StoreInterface $store */
263228
$store = $this->storeRepository->get($storeCode);
229+
$this->storeManager->setCurrentStore($storeCode);
264230
$cart->setStoreId($store->getId());
265231

266232
return $cart;
@@ -273,6 +239,7 @@ private function deleteOrderById(int $orderId)
273239
{
274240
$this->registry->unregister('isSecureArea');
275241
$this->registry->register('isSecureArea', true);
242+
$this->orderManagement->cancel($orderId);
276243
$this->orderRepository->delete($this->orderRepository->get($orderId));
277244
$this->registry->unregister('isSecureArea');
278245
$this->registry->register('isSecureArea', false);

0 commit comments

Comments
 (0)