Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AssertConfigurableProductImages.php
Go to the documentation of this file.
1 <?php
8 
10 
15 {
21  private $displayedImages = [];
22 
28  protected function verify()
29  {
30  $errors = [];
31  $errors[] = $this->verifyBaseImage();
32  $errors[] = $this->verifyOptionsImages();
33 
34  return array_filter($errors);
35  }
36 
42  private function verifyBaseImage()
43  {
44  $message = null;
45  $data = $this->product->getData();
46 
47  $displayedImage = $this->productView->getBaseImageSource();
48  $this->displayedImages[] = $displayedImage;
49 
50  if ($this->areImagesDifferent($displayedImage, $data['image'][0]['file'])) {
51  $message = 'Product image is not correct.';
52  }
53 
54  return $message;
55  }
56 
62  protected function verifyOptionsImages()
63  {
64  $message = null;
65  $configurableAttributes = $this->product->getData('configurable_attributes_data')['attributes_data'];
66  $attribute = array_shift($configurableAttributes);
67  $customOptions = [];
68 
69  foreach ($attribute['options'] as $option) {
70  $customOptions[] = [
71  'type' => $attribute['frontend_input'],
72  'title' => $attribute['frontend_label'],
73  'value' => $option['label']
74  ];
75  }
76 
77  foreach ($customOptions as $customOption) {
78  $this->productView->getCustomOptionsBlock()->fillCustomOptions([$customOption]);
79  $displayedImage = $this->productView->getBaseImageSource();
80  if (in_array($displayedImage, $this->displayedImages)) {
81  $message = 'Option image is not correct.';
82  break;
83  }
84 
85  $this->displayedImages[] = $displayedImage;
86  }
87 
88  return $message;
89  }
90 
98  private function areImagesDifferent($compared, $toCompare)
99  {
100  preg_match('`/(\w*?)\.(\w*?)$`', $compared, $shownImage);
101  preg_match('`/(\w*?)\.(\w*?)$`', $toCompare, $expectedImage);
102 
103  return strpos($shownImage[1], $expectedImage[1]) === false || $expectedImage[2] !== $shownImage[2];
104  }
105 }
$customOption
Definition: products.php:73
$message
$errors
Definition: overview.phtml:9