Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GetProductSalableQtyTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
15 use PHPUnit\Framework\TestCase;
16 
17 class GetProductSalableQtyTest extends TestCase
18 {
22  private $reservationBuilder;
23 
27  private $appendReservations;
28 
32  private $cleanupReservations;
33 
37  private $getProductSalableQty;
38 
39  protected function setUp()
40  {
41  $this->reservationBuilder = Bootstrap::getObjectManager()->get(ReservationBuilderInterface::class);
42  $this->appendReservations = Bootstrap::getObjectManager()->get(AppendReservationsInterface::class);
43  $this->cleanupReservations = Bootstrap::getObjectManager()->get(CleanupReservationsInterface::class);
44  $this->getProductSalableQty = Bootstrap::getObjectManager()->get(
45  GetProductSalableQtyInterface::class
46  );
47  }
48 
52  protected function tearDown()
53  {
54  $this->cleanupReservations->execute();
55  }
56 
73  public function testGetProductQuantity(string $sku, int $stockId, float $qty)
74  {
75  self::assertEquals($qty, $this->getProductSalableQty->execute($sku, $stockId));
76  }
77 
81  public function getProductQuantityProvider(): array
82  {
83  return [
84  ['SKU-1', 10, 8.5],
85  ['SKU-1', 20, 0],
86  ['SKU-1', 30, 8.5],
87  ['SKU-2', 10, 0],
88  ['SKU-2', 20, 5],
89  ['SKU-2', 30, 5],
90  ['SKU-3', 10, 0],
91  ['SKU-3', 20, 0],
92  ['SKU-3', 30, 0],
93  ];
94  }
95 
107  {
108  $this->appendReservations->execute([
109  // emulate order placement reserve 5 units)
110  $this->reservationBuilder->setStockId(10)->setSku('SKU-1')->setQuantity(-5)->build(),
111  // emulate partial order canceling (1.5 units)
112  $this->reservationBuilder->setStockId(10)->setSku('SKU-1')->setQuantity(1.5)->build(),
113  ]);
114  self::assertEquals(5, $this->getProductSalableQty->execute('SKU-1', 10));
115 
116  $this->appendReservations->execute([
117  // unreserved 3.5 units for cleanup
118  $this->reservationBuilder->setStockId(10)->setSku('SKU-1')->setQuantity(3.5)->build(),
119  ]);
120  }
121 }