Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AssertConfigurableProductPage.php
Go to the documentation of this file.
1 <?php
8 
10 
16 {
22  private $priceFormat = 2;
23 
35  protected function verify()
36  {
37  $errors = parent::verify();
38  $errors[] = $this->verifyPriceLabel();
39  $errors[] = $this->verifyAttributes();
40 
41  return array_filter($errors);
42  }
43 
49  protected function verifyPrice()
50  {
51  $priceBlock = $this->productView->getPriceBlock();
52  if (!$priceBlock->isVisible()) {
53  return "Price block for '{$this->product->getName()}' product' is not visible.";
54  }
55  $formPrice = $priceBlock->isOldPriceVisible() ? $priceBlock->getOldPrice() : $priceBlock->getPrice();
56  $fixturePrice = $this->getLowestConfigurablePrice();
57 
58  if ($fixturePrice != number_format($formPrice, $this->priceFormat)) {
59  return "Displayed product price on product page(front-end) not equals passed from fixture. "
60  . "Actual: {$formPrice}, expected: {$fixturePrice}.";
61  }
62  return null;
63  }
64 
70  protected function verifyAttributes()
71  {
72  $attributesData = $this->product->getConfigurableAttributesData();
74  $formOptions = $this->productView->getOptions($this->product);
75 
76  foreach ($attributesData['attributes_data'] as $attributeKey => $attributeData) {
77  $optionData = [
78  'title' => $attributeData['frontend_label'],
79  'type' => $attributeData['frontend_input'],
80  'is_require' => 'Yes',
81  ];
82 
83  foreach ($attributeData['options'] as $optionKey => $option) {
84  $optionData['options'][$optionKey] = [
85  'title' => $option['label'],
86  //Mock price validation
87  'price' => 0
88  ];
89  }
90 
91  $configurableOptions[$attributeKey] = $optionData;
92  }
93 
94  // Sort data for compare
96  foreach ($configurableOptions as $key => $configurableOption) {
97  $configurableOptions[$key] = $this->sortDataByPath($configurableOption, 'options::title');
98  }
99  $configurableFormOptions = $formOptions['configurable_options'];
100  $configurableFormOptions = $this->sortDataByPath($configurableFormOptions, '::title');
101  foreach ($configurableFormOptions as $key => $formOption) {
102  $configurableFormOptions[$key] = $this->sortDataByPath($formOption, 'options::title');
103  }
104 
105  $errors = array_merge(
106  //Verify Attribute and options
107  $this->verifyData($configurableOptions, $configurableFormOptions, true, false),
108  //Verify Attribute options prices
109  $this->verifyAttributesMatrix($formOptions['matrix'], $attributesData['matrix'])
110  );
111 
112  return $errors ? null : $this->prepareErrors($errors, 'Error configurable options:');
113  }
114 
120  protected function verifyAttributesMatrix($variationsMatrix, $generatedMatrix)
121  {
122  foreach ($generatedMatrix as $key => $value) {
123  $generatedMatrix[$key] = array_intersect_key($value, ['price' => 0]);
124  }
125  return $this->verifyData($generatedMatrix, $variationsMatrix, true, false);
126  }
127 
133  protected function getLowestConfigurablePrice()
134  {
135  $price = null;
136  $priceDataConfig = $this->product->getDataFieldConfig('price');
137  if (isset($priceDataConfig['source'])) {
138  $priceData = $priceDataConfig['source']->getPriceData();
139  if (isset($priceData['price_from'])) {
140  $price = $priceData['price_from'];
141  }
142  }
143 
144  if (null === $price) {
145  $configurableOptions = $this->product->getConfigurableAttributesData();
146  foreach ($configurableOptions['matrix'] as $option) {
147  $price = $price === null ? $option['price'] : $price;
148  if ($price > $option['price']) {
149  $price = $option['price'];
150  }
151  }
152  }
153  return $price;
154  }
155 
161  protected function verifyPriceLabel()
162  {
164  $priceBlock = $this->productView->getPriceBlock($this->product);
165 
166  if (!$priceBlock->getPriceLabel()->isVisible()) {
167  return "Product price label should be displayed.";
168  } else {
169  $expectedPriceLabel = 'As low as';
170  $actualPriceLabel = $priceBlock->getPriceLabel()->getText();
171 
172  if ($expectedPriceLabel !== $actualPriceLabel) {
173  return "Displayed product price label on product page (front-end) not equals passed from fixture. "
174  . "Actual: {$actualPriceLabel}, expected: {$expectedPriceLabel}.";
175  }
176  }
177 
178  return null;
179  }
180 }
$optionData
$price
verifyData(array $fixtureData, array $formData, $isStrict=false, $isPrepareError=true)
$attributesData
$value
Definition: gender.phtml:16
prepareErrors(array $errors, $notice=null, $indent='')
$errors
Definition: overview.phtml:9