ReservationPlacingDuringBackItemQtyTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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\StockManagement;
  8. use Magento\Catalog\Api\ProductRepositoryInterface;
  9. use Magento\CatalogInventory\Model\StockManagement;
  10. use Magento\InventoryApi\Api\StockRepositoryInterface;
  11. use Magento\InventoryReservationsApi\Model\CleanupReservationsInterface;
  12. use Magento\InventoryReservationsApi\Model\AppendReservationsInterface;
  13. use Magento\InventoryReservationsApi\Model\ReservationBuilderInterface;
  14. use Magento\InventorySalesApi\Api\GetProductSalableQtyInterface;
  15. use Magento\Store\Api\WebsiteRepositoryInterface;
  16. use Magento\TestFramework\Helper\Bootstrap;
  17. use PHPUnit\Framework\TestCase;
  18. class ReservationPlacingDuringBackItemQtyTest extends TestCase
  19. {
  20. /**
  21. * @var GetProductSalableQtyInterface
  22. */
  23. private $getProductSalableQty;
  24. /**
  25. * @var ProductRepositoryInterface
  26. */
  27. private $productRepository;
  28. /**
  29. * @var StockRepositoryInterface
  30. */
  31. private $stockRepository;
  32. /**
  33. * @var WebsiteRepositoryInterface
  34. */
  35. private $websiteRepository;
  36. /**
  37. * @var ReservationBuilderInterface
  38. */
  39. private $reservationBuilder;
  40. /**
  41. * @var AppendReservationsInterface
  42. */
  43. private $appendReservations;
  44. /**
  45. * @var CleanupReservationsInterface
  46. */
  47. private $cleanupReservations;
  48. /**
  49. * @var StockManagement
  50. */
  51. private $stockManagement;
  52. protected function setUp()
  53. {
  54. $this->getProductSalableQty = Bootstrap::getObjectManager()->get(GetProductSalableQtyInterface::class);
  55. $this->productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class);
  56. $this->stockRepository = Bootstrap::getObjectManager()->get(StockRepositoryInterface::class);
  57. $this->websiteRepository = Bootstrap::getObjectManager()->get(WebsiteRepositoryInterface::class);
  58. $this->reservationBuilder = Bootstrap::getObjectManager()->get(ReservationBuilderInterface::class);
  59. $this->appendReservations = Bootstrap::getObjectManager()->get(AppendReservationsInterface::class);
  60. $this->cleanupReservations = Bootstrap::getObjectManager()->get(CleanupReservationsInterface::class);
  61. $this->stockManagement = Bootstrap::getObjectManager()->get(StockManagement::class);
  62. }
  63. /**
  64. * We broke transaction during indexation so we need to clean db state manually
  65. */
  66. protected function tearDown()
  67. {
  68. $this->cleanupReservations->execute();
  69. }
  70. /**
  71. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
  72. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
  73. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
  74. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
  75. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
  76. * @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
  77. * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/websites_with_stores.php
  78. * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/stock_website_sales_channels.php
  79. *
  80. * @magentoDbIsolation disabled
  81. */
  82. public function testRevertProductsSale()
  83. {
  84. self::assertEquals(8.5, $this->getProductSalableQty->execute('SKU-1', 10));
  85. $product = $this->productRepository->get('SKU-1');
  86. $website = $this->websiteRepository->get('eu_website');
  87. $this->stockManagement->backItemQty($product->getId(), 3.5, $website->getId());
  88. self::assertEquals(12, $this->getProductSalableQty->execute('SKU-1', 10));
  89. $this->appendReservations->execute([
  90. // reserved 3.5 units for cleanup
  91. $this->reservationBuilder->setStockId(10)->setSku('SKU-1')->setQuantity(-3.5)->build(),
  92. ]);
  93. }
  94. }