Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AssertSwatchOptionsOnProductPage.php
Go to the documentation of this file.
1 <?php
7 
10 use Magento\Mtf\Constraint\AbstractConstraint;
11 use Magento\Mtf\Fixture\FixtureInterface;
12 use Magento\Catalog\Test\Page\Product\CatalogProductView;
13 use Magento\Mtf\TestStep\TestStepFactory;
14 
18 class AssertSwatchOptionsOnProductPage extends AbstractConstraint
19 {
25  public function processAssert(
26  TestStepFactory $stepFactory,
27  CatalogProductView $catalogProductView,
28  FixtureInterface $product
29  ) {
30  $stepFactory->create(OpenProductOnFrontendStep::class, ['product' => $product])
31  ->run();
32 
33  $actualData = $catalogProductView->getProductViewWithSwatchesBlock()
34  ->getSwatchAttributesData();
35  $expectedData = $product->getConfigurableAttributesData()['attributes_data'];
36 
37  foreach ($expectedData as $expectedAttributeData) {
38  \PHPUnit\Framework\Assert::assertArrayHasKey(
39  $expectedAttributeData['attribute_code'],
40  $actualData,
41  'Attribute with code ' . $expectedAttributeData['attribute_code'] . ' is absent on Product page'
42  );
43  $actualAttributeData = $actualData[$expectedAttributeData['attribute_code']];
44  $this->verifyAttribute($expectedAttributeData, $actualAttributeData);
45  $this->verifyAttributeOptions($expectedAttributeData, $actualAttributeData);
46  }
47  }
48 
55  private function verifyAttribute(array $expectedAttributeData, array $actualAttributeData)
56  {
57  \PHPUnit\Framework\Assert::assertEquals(
58  $expectedAttributeData['attribute_code'],
59  $actualAttributeData['attribute_code'],
60  sprintf(
61  'Attribute code "%s" is not equal to expected "%s"',
62  $actualAttributeData['attribute_code'],
63  $expectedAttributeData['attribute_code']
64  )
65  );
66  \PHPUnit\Framework\Assert::assertEquals(
67  $expectedAttributeData['attribute_id'],
68  $actualAttributeData['attribute_id'],
69  sprintf(
70  'Attribute id "%s" is not equal to expected "%s"',
71  $actualAttributeData['attribute_id'],
72  $expectedAttributeData['attribute_id']
73  )
74  );
75  \PHPUnit\Framework\Assert::assertEquals(
76  $expectedAttributeData['label'],
77  $actualAttributeData['label'],
78  sprintf(
79  'Attribute label "%s" is not equal to expected "%s"',
80  $actualAttributeData['label'],
81  $expectedAttributeData['label']
82  )
83  );
84  }
85 
92  private function verifyAttributeOptions(array $expectedAttributeData, array $actualAttributeData)
93  {
94  if (isset($expectedAttributeData['options'])) {
95  \PHPUnit\Framework\Assert::assertArrayHasKey(
96  'options',
97  $actualAttributeData,
98  'Swatch attribute options are missed on Product page'
99  );
100 
101  $expectedOptionsCount = count($expectedAttributeData['options']);
102  $actualOptionsCount = count($actualAttributeData['options']);
103  \PHPUnit\Framework\Assert::assertEquals(
104  $expectedOptionsCount,
105  $actualOptionsCount,
106  sprintf(
107  'Attribute options count "%d" is not equal to expected "%d"',
108  $actualOptionsCount,
109  $expectedOptionsCount
110  )
111  );
112  } else {
113  \PHPUnit\Framework\Assert::assertArrayNotHasKey(
114  'options',
115  $actualAttributeData,
116  'Product page must be without swatch attribute options'
117  );
118  }
119  }
120 
126  public function toString()
127  {
128  return 'Swatch attributes are displayed on product page';
129  }
130 }
processAssert(TestStepFactory $stepFactory, CatalogProductView $catalogProductView, FixtureInterface $product)