Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AddStockStatusToProductsTest.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 AddStockStatusToProductsTest extends TestCase
18 {
22  private $stockHelper;
23 
27  private $storeManager;
28 
32  private $storeCodeBefore;
33 
37  protected function setUp()
38  {
39  parent::setUp();
40 
41  $this->stockHelper = Bootstrap::getObjectManager()->get(Stock::class);
42  $this->storeManager = Bootstrap::getObjectManager()->get(StoreManagerInterface::class);
43  $this->storeCodeBefore = $this->storeManager->getStore()->getCode();
44  }
45 
61  public function testAddStockStatusToProducts(string $storeCode, array $productsData)
62  {
63  $this->storeManager->setCurrentStore($storeCode);
64 
66  $collection = Bootstrap::getObjectManager()->create(Collection::class);
67  $collection->addFieldToFilter(ProductInterface::SKU, ['in' => array_keys($productsData)]);
68  $collection->load();
69  self::assertCount(3, $collection->getItems());
70 
71  $this->stockHelper->addStockStatusToProducts($collection);
72 
74  foreach ($collection as $product) {
75  self::assertEquals($productsData[$product->getSku()], $product->isSalable());
76  }
77  }
78 
82  public function addStockStatusToProductsDataProvider(): array
83  {
84  return [
85  'eu_website' => [
86  'store_for_eu_website',
87  [
88  'SKU-1' => 1,
89  'SKU-2' => 0,
90  'SKU-3' => 0,
91  ],
92  ],
93  'us_website' => [
94  'store_for_us_website',
95  [
96  'SKU-1' => 0,
97  'SKU-2' => 1,
98  'SKU-3' => 0,
99  ],
100  ],
101  'global_website' => [
102  'store_for_global_website',
103  [
104  'SKU-1' => 1,
105  'SKU-2' => 1,
106  'SKU-3' => 0,
107  ],
108  ],
109  ];
110  }
111 
115  protected function tearDown()
116  {
117  $this->storeManager->setCurrentStore($this->storeCodeBefore);
118 
119  parent::tearDown();
120  }
121 }
$productsData
Definition: products.php:19
$storeCode
Definition: indexer.php:15