AddSalesQuoteItemOnDefaultStockTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\InventorySales\Test\Integration\SalesQuoteItem;
  8. use Magento\Catalog\Api\Data\ProductInterface;
  9. use Magento\Catalog\Api\ProductRepositoryInterface;
  10. use Magento\Framework\Exception\LocalizedException;
  11. use Magento\Framework\Exception\NoSuchEntityException;
  12. use Magento\InventoryCatalogApi\Api\DefaultStockProviderInterface;
  13. use Magento\InventoryReservationsApi\Model\CleanupReservationsInterface;
  14. use Magento\Quote\Api\Data\CartItemInterface;
  15. use Magento\Quote\Model\Quote;
  16. use Magento\TestFramework\Helper\Bootstrap;
  17. use PHPUnit\Framework\TestCase;
  18. /**
  19. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  20. */
  21. class AddSalesQuoteItemOnDefaultStockTest extends TestCase
  22. {
  23. /**
  24. * @var CleanupReservationsInterface
  25. */
  26. private $cleanupReservations;
  27. /**
  28. * @var ProductRepositoryInterface
  29. */
  30. private $productRepository;
  31. /**
  32. * @var DefaultStockProviderInterface
  33. */
  34. private $defaultStockProvider;
  35. /**
  36. * @inheritdoc
  37. */
  38. protected function setUp()
  39. {
  40. parent::setUp();
  41. $this->defaultStockProvider = Bootstrap::getObjectManager()->get(DefaultStockProviderInterface::class);
  42. $this->cleanupReservations = Bootstrap::getObjectManager()->get(CleanupReservationsInterface::class);
  43. $this->productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class);
  44. $this->cleanupReservations->execute();
  45. }
  46. /**
  47. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
  48. * @magentoDataFixture ../../../../app/code/Magento/InventoryCatalog/Test/_files/source_items_on_default_source.php
  49. * @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
  50. */
  51. public function testAddOutOfStockProductToQuote()
  52. {
  53. $productSku = 'SKU-1';
  54. $productQty = 6;
  55. $product = $this->getProductBySku($productSku);
  56. $quote = $this->getQuote();
  57. self::expectException(LocalizedException::class);
  58. $quote->addProduct($product, $productQty);
  59. $quoteItemCount = count($quote->getAllItems());
  60. self::assertEquals(0, $quoteItemCount);
  61. }
  62. /**
  63. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
  64. * @magentoDataFixture ../../../../app/code/Magento/InventoryCatalog/Test/_files/source_items_on_default_source.php
  65. * @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
  66. */
  67. public function testAddInStockProductToQuote()
  68. {
  69. $productSku = 'SKU-1';
  70. $productQty = 4;
  71. $expectedQtyInCart = 4;
  72. $product = $this->getProductBySku($productSku);
  73. $quote = $this->getQuote();
  74. $quote->addProduct($product, $productQty);
  75. /** @var CartItemInterface $quoteItem */
  76. $quoteItem = current($quote->getAllItems());
  77. self::assertEquals($expectedQtyInCart, $quoteItem->getQty());
  78. }
  79. /**
  80. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
  81. * @magentoDataFixture ../../../../app/code/Magento/InventoryCatalog/Test/_files/source_items_on_default_source.php
  82. * @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
  83. */
  84. public function testAddProductToQuoteMultipleTimes()
  85. {
  86. $productSku = 'SKU-1';
  87. $productQty1 = 3;
  88. $productQty2 = 2;
  89. $productQty3 = 3;
  90. $expectedQtyInCart1 = 3;
  91. $expectedQtyInCart2 = 5;
  92. $expectedQtyInCart3 = 5;
  93. $product = $this->getProductBySku($productSku);
  94. $quote = $this->getQuote();
  95. //(5.5 - 3) 2.5 in stock
  96. $quote->addProduct($product, $productQty1);
  97. /** @var CartItemInterface $quoteItem */
  98. $quoteItem = current($quote->getAllItems());
  99. self::assertEquals($expectedQtyInCart1, $quoteItem->getQty());
  100. //(2.5 - 2) 0.5 in stock
  101. $quote->addProduct($product, $productQty2);
  102. /** @var CartItemInterface $quoteItem */
  103. $quoteItem = current($quote->getAllItems());
  104. self::assertEquals($expectedQtyInCart2, $quoteItem->getQty());
  105. //(0.5 - 3) -2.5 out of stock
  106. self::expectException(LocalizedException::class);
  107. $quote->addProduct($product, $productQty3);
  108. /** @var CartItemInterface $quoteItem */
  109. $quoteItem = current($quote->getAllItems());
  110. self::assertEquals($expectedQtyInCart3, $quoteItem->getQty());
  111. }
  112. /**
  113. * @param string $sku
  114. * @return ProductInterface
  115. * @throws NoSuchEntityException
  116. */
  117. private function getProductBySku(string $sku): ProductInterface
  118. {
  119. return $this->productRepository->get($sku);
  120. }
  121. /**
  122. * @return Quote
  123. */
  124. private function getQuote(): Quote
  125. {
  126. return Bootstrap::getObjectManager()->create(
  127. Quote::class,
  128. [
  129. 'data' => [
  130. 'store_id' => 1,
  131. 'is_active' => 0,
  132. 'is_multi_shipping' => 0,
  133. 'id' => 1
  134. ]
  135. ]
  136. );
  137. }
  138. protected function tearDown()
  139. {
  140. $this->cleanupReservations->execute();
  141. }
  142. }