8 use Magento\Catalog\Api\Data\ProductInterfaceFactory;
12 use Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory;
16 use PHPUnit\Framework\Assert;
28 private $stockItemRepository;
33 private $stockItemCriteriaFactory;
38 private $productRepository;
43 private $productFactory;
48 private $searchCriteriaBuilder;
61 StockItemCriteriaInterfaceFactory $stockItemCriteriaFactory,
63 ProductInterfaceFactory $productFactory,
66 $this->hydrator = $hydrator;
68 $this->stockItemCriteriaFactory = $stockItemCriteriaFactory;
79 public function checkStockItemData($sku, array $expectedData)
81 $product = $this->productRepository->get($sku,
false,
null,
true);
82 $this->doCheckStockItemData(
$product, $expectedData);
85 $productLoadedByModel = $this->productFactory->create();
86 $productLoadedByModel->load(
$product->getId());
87 $this->doCheckStockItemData(
$product, $expectedData);
94 private function doCheckStockItemData(Product
$product, array $expectedData)
99 $this->assertArrayContains($expectedData, $this->hydrator->extract(
$stockItem));
101 $criteria = $this->stockItemCriteriaFactory->create();
102 $result = $this->stockItemRepository->getList($criteria);
105 $this->assertArrayContains($expectedData, $this->hydrator->extract(
$stockItem));
107 $expectedQuantityAndStockStatusData = array_intersect_key($expectedData, [
111 Assert::assertNotNull(
$product->getQuantityAndStockStatus());
112 $this->assertArrayContains($expectedQuantityAndStockStatusData,
$product->getQuantityAndStockStatus());
114 Assert::assertNotNull(
$product->getData(
'quantity_and_stock_status'));
115 $this->assertArrayContains($expectedQuantityAndStockStatusData,
$product->getData(
'quantity_and_stock_status'));
123 private function assertArrayContains(array $expected, array $actual)
125 foreach ($expected as $key =>
$value) {
126 Assert::assertArrayHasKey(
129 "Expected value for key '{$key}' is missed" 132 $this->assertArrayContains(
$value, $actual[$key]);
134 Assert::assertEquals(
137 "Expected value for key '{$key}' doesn't match"
__construct(HydratorInterface $hydrator, StockItemRepositoryInterface $stockItemRepository, StockItemCriteriaInterfaceFactory $stockItemCriteriaFactory, ProductRepositoryInterface $productRepository, ProductInterfaceFactory $productFactory, SearchCriteriaBuilder $searchCriteriaBuilder)