Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StockItemDataChecker.php
Go to the documentation of this file.
1 <?php
7 
8 use Magento\Catalog\Api\Data\ProductInterfaceFactory;
12 use Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory;
16 use PHPUnit\Framework\Assert;
17 
19 {
23  private $hydrator;
24 
28  private $stockItemRepository;
29 
33  private $stockItemCriteriaFactory;
34 
38  private $productRepository;
39 
43  private $productFactory;
44 
48  private $searchCriteriaBuilder;
49 
58  public function __construct(
59  HydratorInterface $hydrator,
60  StockItemRepositoryInterface $stockItemRepository,
61  StockItemCriteriaInterfaceFactory $stockItemCriteriaFactory,
62  ProductRepositoryInterface $productRepository,
63  ProductInterfaceFactory $productFactory,
64  SearchCriteriaBuilder $searchCriteriaBuilder
65  ) {
66  $this->hydrator = $hydrator;
67  $this->stockItemRepository = $stockItemRepository;
68  $this->stockItemCriteriaFactory = $stockItemCriteriaFactory;
69  $this->productRepository = $productRepository;
70  $this->productFactory = $productFactory;
71  $this->searchCriteriaBuilder = $searchCriteriaBuilder;
72  }
73 
79  public function checkStockItemData($sku, array $expectedData)
80  {
81  $product = $this->productRepository->get($sku, false, null, true);
82  $this->doCheckStockItemData($product, $expectedData);
83 
85  $productLoadedByModel = $this->productFactory->create();
86  $productLoadedByModel->load($product->getId());
87  $this->doCheckStockItemData($product, $expectedData);
88  }
89 
94  private function doCheckStockItemData(Product $product, array $expectedData)
95  {
96  $stockItem = $product->getExtensionAttributes()->getStockItem();
97  $stockItem = $this->stockItemRepository->get($stockItem->getItemId());
98 
99  $this->assertArrayContains($expectedData, $this->hydrator->extract($stockItem));
100 
101  $criteria = $this->stockItemCriteriaFactory->create();
102  $result = $this->stockItemRepository->getList($criteria);
103  $items = $result->getItems();
104  $stockItem = current($items);
105  $this->assertArrayContains($expectedData, $this->hydrator->extract($stockItem));
106 
107  $expectedQuantityAndStockStatusData = array_intersect_key($expectedData, [
110  ]);
111  Assert::assertNotNull($product->getQuantityAndStockStatus());
112  $this->assertArrayContains($expectedQuantityAndStockStatusData, $product->getQuantityAndStockStatus());
113 
114  Assert::assertNotNull($product->getData('quantity_and_stock_status'));
115  $this->assertArrayContains($expectedQuantityAndStockStatusData, $product->getData('quantity_and_stock_status'));
116  }
117 
123  private function assertArrayContains(array $expected, array $actual)
124  {
125  foreach ($expected as $key => $value) {
126  Assert::assertArrayHasKey(
127  $key,
128  $actual,
129  "Expected value for key '{$key}' is missed"
130  );
131  if (is_array($value)) {
132  $this->assertArrayContains($value, $actual[$key]);
133  } else {
134  Assert::assertEquals(
135  $value,
136  $actual[$key],
137  "Expected value for key '{$key}' doesn't match"
138  );
139  }
140  }
141  }
142 }
$value
Definition: gender.phtml:16
__construct(HydratorInterface $hydrator, StockItemRepositoryInterface $stockItemRepository, StockItemCriteriaInterfaceFactory $stockItemCriteriaFactory, ProductRepositoryInterface $productRepository, ProductInterfaceFactory $productFactory, SearchCriteriaBuilder $searchCriteriaBuilder)
$searchCriteriaBuilder
$items