Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CancelOrderItemObserverTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
14 use Magento\Framework\DataObject\Factory as DataObjectFactory;
30 
34 class CancelOrderItemObserverTest extends \PHPUnit\Framework\TestCase
35 {
39  private $searchCriteriaBuilder;
40 
44  private $cartRepository;
45 
49  private $stockRepository;
50 
54  private $storeRepository;
55 
59  private $storeManager;
60 
64  private $productRepository;
65 
69  private $cartManagement;
70 
74  private $dataObjectFactory;
75 
79  private $cleanupReservations;
80 
84  private $registry;
85 
89  private $orderRepository;
90 
94  private $orderManagement;
95 
99  private $getSalableQuantity;
100 
104  protected function setUp()
105  {
106  $this->searchCriteriaBuilder = Bootstrap::getObjectManager()->get(SearchCriteriaBuilder::class);
107  $this->cartRepository = Bootstrap::getObjectManager()->get(CartRepositoryInterface::class);
108  $this->stockRepository = Bootstrap::getObjectManager()->get(StockRepositoryInterface::class);
109  $this->storeRepository = Bootstrap::getObjectManager()->get(StoreRepositoryInterface::class);
110  $this->storeManager = Bootstrap::getObjectManager()->get(StoreManagerInterface::class);
111  $this->productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class);
112  $this->cartManagement = Bootstrap::getObjectManager()->get(CartManagementInterface::class);
113  $this->dataObjectFactory = Bootstrap::getObjectManager()->get(DataObjectFactory::class);
114  $this->cleanupReservations = Bootstrap::getObjectManager()->get(CleanupReservationsInterface::class);
115  $this->registry = Bootstrap::getObjectManager()->get(Registry::class);
116  $this->orderRepository = Bootstrap::getObjectManager()->get(OrderRepositoryInterface::class);
117  $this->orderManagement = Bootstrap::getObjectManager()->get(OrderManagementInterface::class);
118  $this->getSalableQuantity = Bootstrap::getObjectManager()->get(GetProductSalableQtyInterface::class);
119  }
120 
135  {
136  // Place an order of configurable product with two simple products assigned to not default source and qty > 1
137  $stockId = 20;
138  $product = $this->productRepository->get('configurable');
139  $quote = $this->getCartByStockId($stockId);
140  $quote->addProduct($product, $this->getByRequest($product, 2));
141  $this->cartRepository->save($quote);
142  $salableQtyBeforeOrder = $this->getSalableQuantity->execute('simple_10', $stockId);
143  $orderId = $this->cartManagement->placeOrder($quote->getId());
144 
145  // Cancel order
146  $this->deleteOrderById((int)$orderId);
147  $salableQtyAfterOrderCancel = $this->getSalableQuantity->execute('simple_10', $stockId);
148 
149  // Expected result after cancel: reservation is compensated, salable qty is the same
150  $this->assertEquals($salableQtyBeforeOrder, $salableQtyAfterOrderCancel);
151  }
152 
159  private function getByRequest(ProductInterface $product, float $productQty): DataObject
160  {
161  $configurableOptions = $product->getTypeInstance()->getConfigurableOptions($product);
162  $option = current(current($configurableOptions));
163  return $this->dataObjectFactory->create(
164  [
165  'product' => $option['product_id'],
166  'super_attribute' => [key($configurableOptions) => $option['value_index']],
167  'qty' => $productQty
168  ]
169  );
170  }
171 
176  private function getCartByStockId(int $stockId): CartInterface
177  {
178  $searchCriteria = $this->searchCriteriaBuilder
179  ->addFilter('reserved_order_id', 'test_order_1')
180  ->create();
182  $cart = current($this->cartRepository->getList($searchCriteria)->getItems());
184  $stock = $this->stockRepository->get($stockId);
186  $salesChannels = $stock->getExtensionAttributes()->getSalesChannels();
187  $storeCode = 'store_for_';
188  foreach ($salesChannels as $salesChannel) {
189  if ($salesChannel->getType() == SalesChannelInterface::TYPE_WEBSITE) {
190  $storeCode .= $salesChannel->getCode();
191  break;
192  }
193  }
195  $store = $this->storeRepository->get($storeCode);
196  $this->storeManager->setCurrentStore($storeCode);
197  $cart->setStoreId($store->getId());
198 
199  return $cart;
200  }
201 
205  private function deleteOrderById(int $orderId)
206  {
207  $this->registry->unregister('isSecureArea');
208  $this->registry->register('isSecureArea', true);
209  $this->orderManagement->cancel($orderId);
210  $this->orderRepository->delete($this->orderRepository->get($orderId));
211  $this->registry->unregister('isSecureArea');
212  $this->registry->register('isSecureArea', false);
213  }
214 
215  protected function tearDown()
216  {
217  $this->cleanupReservations->execute();
218  }
219 }
$quote
$searchCriteria
$storeCode
Definition: indexer.php:15
$stock