Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AssignStatusToProductTest.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 AssignStatusToProductTest extends TestCase
18 {
22  private $stockHelper;
23 
27  private $productRepository;
28 
32  private $storeManager;
33 
37  private $storeCodeBefore;
38 
42  protected function setUp()
43  {
44  parent::setUp();
45 
46  $this->stockHelper = Bootstrap::getObjectManager()->get(Stock::class);
47  $this->productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class);
48  $this->storeManager = Bootstrap::getObjectManager()->get(StoreManagerInterface::class);
49  $this->storeCodeBefore = $this->storeManager->getStore()->getCode();
50  }
51 
67  public function testAssignStatusToProductIfStatusParameterIsNotPassed(string $storeCode, array $productsData)
68  {
69  $this->storeManager->setCurrentStore($storeCode);
70 
71  foreach ($productsData as $sku => $expectedStatus) {
72  $product = $this->productRepository->get($sku);
74  $this->stockHelper->assignStatusToProduct($product);
75 
76  self::assertEquals($expectedStatus, $product->isSalable());
77  }
78  }
79 
95  public function testAssignStatusToProductIfStatusParameterIsPassed(string $storeCode, array $productsData)
96  {
97  $expectedStatus = 1;
98  $this->storeManager->setCurrentStore($storeCode);
99 
100  foreach (array_keys($productsData) as $sku) {
101  $product = $this->productRepository->get($sku);
103  $this->stockHelper->assignStatusToProduct($product, $expectedStatus);
104 
105  self::assertEquals($expectedStatus, $product->isSalable());
106  }
107  }
108 
112  public function assignStatusToProductDataProvider(): array
113  {
114  return [
115  'eu_website' => [
116  'store_for_eu_website',
117  [
118  'SKU-1' => 1,
119  'SKU-2' => 0,
120  'SKU-3' => 0,
121  ],
122  ],
123  'us_website' => [
124  'store_for_us_website',
125  [
126  'SKU-1' => 0,
127  'SKU-2' => 1,
128  'SKU-3' => 0,
129  ],
130  ],
131  'global_website' => [
132  'store_for_global_website',
133  [
134  'SKU-1' => 1,
135  'SKU-2' => 1,
136  'SKU-3' => 0,
137  ],
138  ],
139  ];
140  }
141 
145  protected function tearDown()
146  {
147  $this->storeManager->setCurrentStore($this->storeCodeBefore);
148 
149  parent::tearDown();
150  }
151 }
$productsData
Definition: products.php:19
$storeCode
Definition: indexer.php:15