Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
IsSalableLegacyStockItemIsInStockTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
16 use PHPUnit\Framework\TestCase;
17 
19 {
23  private $isProductSalable;
24 
28  private $getProductIdsBySkus;
29 
33  private $stockRegistryProvider;
34 
38  private $stockConfiguration;
39 
43  private $stockItemRepository;
44 
45  protected function setUp()
46  {
47  $this->isProductSalable = Bootstrap::getObjectManager()->get(IsProductSalableInterface::class);
48  $this->getProductIdsBySkus = Bootstrap::getObjectManager()->get(GetProductIdsBySkus::class);
49  $this->stockRegistryProvider = Bootstrap::getObjectManager()->get(StockRegistryProviderInterface::class);
50  $this->stockConfiguration = Bootstrap::getObjectManager()->get(StockConfigurationInterface::class);
51  $this->stockItemRepository = Bootstrap::getObjectManager()->get(StockItemRepositoryInterface::class);
52  }
53 
66  {
67  $sku = 'configurable';
68  $stockId = 20;
69  $this->setLegacyStockItemIsInStock($sku, 0);
70 
71  self::assertEquals(
72  false,
73  $this->isProductSalable->execute($sku, $stockId)
74  );
75  }
76 
89  {
90  $sku = 'configurable';
91  $stockId = 20;
92  $this->setLegacyStockItemIsInStock($sku, 1);
93 
94  self::assertEquals(
95  true,
96  $this->isProductSalable->execute($sku, $stockId)
97  );
98  }
99 
104  private function setLegacyStockItemIsInStock(string $sku, int $isInStock): void
105  {
106  $scopeId = $this->stockConfiguration->getDefaultScopeId();
107  $productId = current($this->getProductIdsBySkus->execute([$sku]));
108  $stockItem = $this->stockRegistryProvider->getStockItem($productId, $scopeId);
109  $stockItem->setIsInStock($isInStock);
110  $this->stockItemRepository->save($stockItem);
111  }
112 }