Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
IsSalableWithReservationsConditionTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
18 use PHPUnit\Framework\TestCase;
19 
21 {
25  private $reservationBuilder;
26 
30  private $appendReservations;
31 
35  private $cleanupReservations;
36 
40  private $isProductSalableForRequestedQty;
41 
45  private $getStockItemConfiguration;
46 
50  private $saveStockItemConfiguration;
51 
55  protected function setUp()
56  {
57  parent::setUp();
58 
59  $this->reservationBuilder = Bootstrap::getObjectManager()->get(ReservationBuilderInterface::class);
60  $this->appendReservations = Bootstrap::getObjectManager()->get(AppendReservationsInterface::class);
61  $this->cleanupReservations = Bootstrap::getObjectManager()->get(CleanupReservationsInterface::class);
62  $this->isProductSalableForRequestedQty
63  = Bootstrap::getObjectManager()->get(IsProductSalableForRequestedQtyInterface::class);
64  $this->getStockItemConfiguration = Bootstrap::getObjectManager()->get(
65  GetStockItemConfigurationInterface::class
66  );
67  $this->saveStockItemConfiguration = Bootstrap::getObjectManager()->get(
68  SaveStockItemConfigurationInterface::class
69  );
70  }
71 
88  public function testProductIsSalable(string $sku, int $stockId, float $qty, bool $isSalable)
89  {
90  self::assertEquals(
91  $isSalable,
92  $this->isProductSalableForRequestedQty->execute($sku, $stockId, $qty)->isSalable()
93  );
94  }
95 
99  public function productIsSalableDataProvider(): array
100  {
101  return [
102  ['SKU-1', 10, 1, true],
103  ['SKU-1', 20, 1, false],
104  ['SKU-1', 30, 1, true],
105  ['SKU-2', 10, 1, false],
106  ['SKU-2', 20, 1, true],
107  ['SKU-2', 30, 1, true],
108  ['SKU-3', 10, 1, false],
109  ['SKU-3', 20, 1, false],
110  ['SKU-3', 30, 1, false],
111  ];
112  }
113 
132  public function testProductIsSalableWithUseConfigMinQty(string $sku, int $stockId, float $qty, bool $isSalable)
133  {
135  $stockItemConfiguration = $this->getStockItemConfiguration->execute($sku, $stockId);
136  $stockItemConfiguration->setUseConfigMinQty(true);
137  $this->saveStockItemConfiguration->execute($sku, $stockId, $stockItemConfiguration);
138 
139  self::assertEquals(
140  $isSalable,
141  $this->isProductSalableForRequestedQty->execute($sku, $stockId, $qty)->isSalable()
142  );
143  }
144 
149  {
150  return [
151  ['SKU-1', 10, 3, true],
152  ['SKU-1', 10, 4, false],
153  ['SKU-1', 30, 3, true],
154  ['SKU-1', 30, 4, false],
155  ['SKU-2', 20, 1, false],
156  ['SKU-2', 30, 1, false],
157  ['SKU-3', 10, 1, false],
158  ['SKU-3', 30, 1, false],
159  ];
160  }
161 
178  public function testProductIsSalableWithMinQty(string $sku, int $stockId, float $qty, bool $isSalable)
179  {
181  $stockItemConfiguration = $this->getStockItemConfiguration->execute($sku, $stockId);
182  $stockItemConfiguration->setUseConfigMinQty(false);
183  $stockItemConfiguration->setMinQty(5);
184  $this->saveStockItemConfiguration->execute($sku, $stockId, $stockItemConfiguration);
185 
186  self::assertEquals(
187  $isSalable,
188  $this->isProductSalableForRequestedQty->execute($sku, $stockId, $qty)->isSalable()
189  );
190  }
191 
195  public function productIsSalableWithMinQtyDataProvider(): array
196  {
197  return [
198  ['SKU-1', 10, 3, true],
199  ['SKU-1', 10, 4, false],
200  ['SKU-1', 30, 3, true],
201  ['SKU-1', 30, 4, false],
202  ['SKU-2', 20, 1, false],
203  ['SKU-2', 30, 1, false],
204  ['SKU-3', 10, 1, false],
205  ['SKU-3', 30, 1, false],
206  ];
207  }
208 
220  {
221  // emulate order placement (reserve -8.5 units)
222  $this->appendReservations->execute([
223  $this->reservationBuilder->setStockId(10)->setSku('SKU-1')->setQuantity(-8.5)->build(),
224  ]);
225  self::assertFalse($this->isProductSalableForRequestedQty->execute('SKU-1', 10, 1)->isSalable());
226 
227  $this->appendReservations->execute([
228  // unreserve 8.5 units for cleanup
229  $this->reservationBuilder->setStockId(10)->setSku('SKU-1')->setQuantity(8.5)->build(),
230  ]);
231  $this->cleanupReservations->execute();
232  }
233 }