Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConfigurableProductsAssert.php
Go to the documentation of this file.
1 <?php
7 
15 {
19  private $productRepository;
20 
24  private $optionRepository;
25 
29  private $productAssert;
30 
36  public function __construct(
37  \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
38  \Magento\ConfigurableProduct\Api\OptionRepositoryInterface $optionRepository,
39  \Magento\Setup\Fixtures\FixturesAsserts\ProductAssert $productAssert
40  ) {
41  $this->productRepository = $productRepository;
42  $this->optionRepository = $optionRepository;
43  $this->productAssert = $productAssert;
44  }
45 
54  public function assert()
55  {
56  $productsMap = [
57  'Configurable Product - Default %s' => [
58  'attributes' => 1,
59  'options' => 3,
60  'amount' => 2,
61  ],
62  'Configurable Product - Color-Size %s' => [
63  'attributes' => 2,
64  'options' => 3,
65  'amount' => 2,
66  ],
67  'Configurable Product 2-2 %s' => [
68  'attributes' => 2,
69  'options' => 2,
70  'amount' => 2,
71  ],
72  ];
73 
74  foreach ($productsMap as $skuPattern => $expectedData) {
75  $configurableSku = sprintf($skuPattern, 1);
76  $product = $this->productRepository->get($configurableSku);
77  $this->productAssert->assertProductsCount($skuPattern, $expectedData['amount']);
78  $this->productAssert->assertProductType('configurable', $product);
79  $options = $this->optionRepository->getList($configurableSku);
80 
81  if ($expectedData['attributes'] !== count($options)) {
82  throw new \AssertionError('Configurable options amount is wrong');
83  }
84 
85  if ($expectedData['options'] !== count($options[0]->getValues())) {
86  throw new \AssertionError('Configurable option values amount is wrong');
87  }
88  }
89 
90  return true;
91  }
92 }
__construct(\Magento\Catalog\Api\ProductRepositoryInterface $productRepository, \Magento\ConfigurableProduct\Api\OptionRepositoryInterface $optionRepository, \Magento\Setup\Fixtures\FixturesAsserts\ProductAssert $productAssert)