Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AssertProductCustomOptionsOnProductPage.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Catalog\Test\Page\Product\CatalogProductView;
10 use Magento\Mtf\Client\BrowserInterface;
12 use Magento\Mtf\Fixture\FixtureInterface;
13 
18 {
24  protected $skippedFieldOptions = [
25  'Text/Field' => [
26  'price_type',
27  'sku',
28  ],
29  'Text/Area' => [
30  'price_type',
31  'sku',
32  ],
33  'Select/Drop-down' => [
34  'price_type',
35  'sku',
36  ],
37  'File/File' => [
38  'price_type',
39  'sku',
40  ],
41  'Select/Radio Buttons' => [
42  'price_type',
43  'sku',
44  ],
45  'Select/Checkbox' => [
46  'price_type',
47  'sku',
48  ],
49  'Select/Multiple Select' => [
50  'price_type',
51  'sku',
52  ],
53  'Date/Date' => [
54  'price_type',
55  'sku',
56  ],
57  'Date/Date & Time' => [
58  'price_type',
59  'sku',
60  ],
61  'Date/Time' => [
62  'price_type',
63  'sku',
64  ],
65  ];
66 
72  protected $isPrice = true;
73 
82  public function processAssert(
83  CatalogProductView $catalogProductView,
84  FixtureInterface $product,
85  BrowserInterface $browser
86  ) {
87  $browser->open($_ENV['app_frontend_url'] . $product->getUrlKey() . '.html');
88  $actualPrice = null;
89  if ($this->isPrice) {
90  $priceBlock = $catalogProductView->getViewBlock()->getPriceBlock();
91  $specialPrice = $priceBlock->getSpecialPrice();
92  $price = $priceBlock->getPrice();
93  $actualPrice = $specialPrice ? $specialPrice : $price;
94  }
95  $fixtureCustomOptions = $this->prepareOptions($product, $actualPrice);
96  $formCustomOptions = $catalogProductView->getViewBlock()->getOptions($product)['custom_options'];
97  $error = $this->verifyData($fixtureCustomOptions, $formCustomOptions);
98  \PHPUnit\Framework\Assert::assertEmpty($error, $error);
99  }
100 
108  protected function prepareOptions(FixtureInterface $product, $actualPrice = null)
109  {
110  $result = [];
111  $customOptions = $product->hasData('custom_options')
112  ? $product->getDataFieldConfig('custom_options')['source']->getCustomOptions()
113  : [];
114  $actualPrice = $actualPrice ?: $product->getPrice();
115  foreach ($customOptions as $customOption) {
116  $result = $this->prepareEachCustomOption($actualPrice, $customOption, $result);
117  }
118  return $result;
119  }
120 
130  protected function verifyData(array $fixtureData, array $formData, $isStrict = false, $isPrepareError = true)
131  {
132  $errors = [];
133  foreach ($fixtureData as $key => $value) {
134  if (in_array($key, $this->skippedFields, true)) {
135  continue;
136  }
137  $formValue = isset($formData[$key]) ? $formData[$key] : null;
138  $errors = $this->verifyDataForErrors($formValue, $key, $errors, $value);
139  }
140  return $this->prepareErrorsForOutput($fixtureData, $formData, $isStrict, $isPrepareError, $errors);
141  }
142 
151  private function checkNotEqualValuesErrors($value, $formValue, $key)
152  {
157  if ((int)$value === (int)$formValue + 1) {
158  return '';
159  }
160  if (is_array($value)) {
161  $value = $this->arrayToString($value);
162  }
163  if (is_array($formValue)) {
164  $formValue = $this->arrayToString($formValue);
165  }
166  return sprintf('- %s: "%s" instead of "%s"', $key, $formValue, $value);
167  }
168 
179  private function prepareErrorsForOutput(array $fixtureData, array $formData, $isStrict, $isPrepareError, $errors)
180  {
181  if ($isStrict) {
182  $diffData = array_diff(array_keys($formData), array_keys($fixtureData));
183  if ($diffData) {
184  $errors[] = '- fields ' . implode(', ', $diffData) . ' is absent in fixture';
185  }
186  }
187  if ($isPrepareError) {
188  return $this->prepareErrors($errors);
189  }
190  return $errors;
191  }
192 
202  private function verifyDataForErrors($formValue, $key, $errors, $value)
203  {
204  if (is_numeric($formValue)) {
205  $formValue = (float)$formValue;
206  }
207  if (null === $formValue) {
208  $errors[] = '- field "' . $key . '" is absent in form';
209  } elseif (is_array($value) && is_array($formValue)) {
210  $valueErrors = $this->verifyData($value, $formValue, true, false);
211  if (!empty($valueErrors)) {
212  $errors[$key] = $valueErrors;
213  }
214  } elseif ($value != $formValue) {
215  $notEqualValuesErrors = $this->checkNotEqualValuesErrors($value, $formValue, $key);
216  if ($notEqualValuesErrors) {
217  $errors[] = $notEqualValuesErrors;
218  }
219  }
220  return $errors;
221  }
222 
229  private function prepareEachCustomOption($actualPrice, $customOption, $result)
230  {
231  $skippedField = isset($this->skippedFieldOptions[$customOption['type']])
232  ? $this->skippedFieldOptions[$customOption['type']]
233  : [];
234  foreach ($customOption['options'] as &$option) {
235  // recalculate percent price
236  if ('Percent' == $option['price_type']) {
237  $option['price'] = ($actualPrice * $option['price']) / 100;
238  $option['price'] = round($option['price'], 2);
239  }
240 
241  $option = array_diff_key($option, array_flip($skippedField));
242  }
243  $result[$customOption['title']] = $customOption;
244  return $result;
245  }
246 
252  public function toString()
253  {
254  return 'Value of custom option on the page is correct.';
255  }
256 }
$customOption
Definition: products.php:73
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$price
verifyData(array $fixtureData, array $formData, $isStrict=false, $isPrepareError=true)
$value
Definition: gender.phtml:16
processAssert(CatalogProductView $catalogProductView, FixtureInterface $product, BrowserInterface $browser)
prepareErrors(array $errors, $notice=null, $indent='')
$errors
Definition: overview.phtml:9