Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ImageFactoryTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
15 use Magento\Catalog\Model\View\Asset\ImageFactory as ViewAssetImageFactory;
19 
20 class ImageFactoryTest extends \PHPUnit\Framework\TestCase
21 {
23  private $paramsBuilder;
24 
26  private $viewConfig;
27 
29  private $objectManager;
30 
34  private $model;
35 
39  private $viewAssetImageFactory;
40 
41  protected function setUp()
42  {
43  $this->viewConfig = $this->createMock(View::class);
44  $configInterface = $this->createMock(ConfigInterface::class);
45  $configInterface->method('getViewConfig')->willReturn($this->viewConfig);
46  $this->viewAssetImageFactory = $this->createMock(ViewAssetImageFactory::class);
47  $this->paramsBuilder = $this->createMock(ParamsBuilder::class);
48  $this->objectManager = $this->createMock(ObjectManager::class);
49  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
50  $this->model = $objectManager->getObject(
51  ImageFactory::class,
52  [
53  'objectManager' => $this->objectManager,
54  'presentationConfig' => $configInterface,
55  'viewAssetImageFactory' => $this->viewAssetImageFactory,
56  'imageParamsBuilder' => $this->paramsBuilder
57  ]
58  );
59  }
60 
65  public function testCreate($data, $expected)
66  {
67  $product = $this->createMock(Product::class);
68  $product->method('getName')->willReturn($data['product']['name']);
69  $product->method('getData')->willReturnOnConsecutiveCalls(
70  $data['product']['image_type'],
71  $data['product']['image_type_label']
72  );
73  $imageBlock = $this->createMock(Image::class);
74  $this->viewConfig->method('getMediaAttributes')->willReturn($data['viewImageConfig']);
75  $this->viewConfig->method('getVarValue')->willReturn($data['frame']);
76  $this->viewAssetImageFactory->method('create')->willReturn(
77  $viewAssetImage = $this->createMock(ViewAssetImage::class)
78  );
79  $this->paramsBuilder->method('build')->willReturn($data['imageParamsBuilder']);
80  $viewAssetImage->method('getUrl')->willReturn($data['url']);
81 
82  $this->objectManager->expects(self::once())
83  ->method('create')
84  ->with(Image::class, $expected)
85  ->willReturn($imageBlock);
86  $actual = $this->model->create($product, 'image_id', $data['custom_attributes']);
87  self::assertInstanceOf(Image::class, $actual);
88  }
89 
93  public function createDataProvider(): array
94  {
95  return [
96  $this->getTestDataWithoutAttributes(),
97  $this->getTestDataWithAttributes(),
98  ];
99  }
100 
104  private function getTestDataWithoutAttributes(): array
105  {
106  return [
107  'data' => [
108  'viewImageConfig' => [
109  'width' => 100,
110  'height' => 100,
111  'constrain_only' => false,
112  'aspect_ratio' => false,
113  'frame' => false,
114  'transparency' => false,
115  'background' => '255,255,255',
116  'type' => 'image_type' //thumbnail,small_image,image,swatch_image,swatch_thumb
117  ],
118  'imageParamsBuilder' => [
119  'image_width' => 100,
120  'image_height' => 100,
121  'constrain_only' => false,
122  'keep_aspect_ratio' => false,
123  'keep_frame' => false,
124  'keep_transparency' => false,
125  'background' => '255,255,255',
126  'image_type' => 'image_type', //thumbnail,small_image,image,swatch_image,swatch_thumb
127  'quality' => 80, // <===
128  'angle' => null // <===
129  ],
130  'product' => [
131  'image_type_label' => 'test_image_label',
132  'name' => 'test_product_name',
133  'image_type' => 'test_image_path'
134  ],
135  'url' => 'test_url_1',
136  'frame' => 'test_frame',
137  'custom_attributes' => [],
138  ],
139  'expected' => [
140  'data' => [
141  'template' => 'Magento_Catalog::product/image_with_borders.phtml',
142  'image_url' => 'test_url_1',
143  'width' => 100,
144  'height' => 100,
145  'label' => 'test_image_label',
146  'ratio' => 1,
147  'custom_attributes' => '',
148  'product_id' => null
149  ],
150  ],
151  ];
152  }
153 
157  private function getTestDataWithAttributes(): array
158  {
159  return [
160  'data' => [
161  'viewImageConfig' => [
162  'width' => 100,
163  'height' => 50, // <===
164  'constrain_only' => false,
165  'aspect_ratio' => false,
166  'frame' => true, // <===
167  'transparency' => false,
168  'background' => '255,255,255',
169  'type' => 'image_type' //thumbnail,small_image,image,swatch_image,swatch_thumb
170  ],
171  'imageParamsBuilder' => [
172  'image_width' => 100,
173  'image_height' => 50,
174  'constrain_only' => false,
175  'keep_aspect_ratio' => false,
176  'keep_frame' => true,
177  'keep_transparency' => false,
178  'background' => '255,255,255',
179  'image_type' => 'image_type', //thumbnail,small_image,image,swatch_image,swatch_thumb
180  'quality' => 80,
181  'angle' => null
182  ],
183  'product' => [
184  'image_type_label' => null, // <==
185  'name' => 'test_product_name',
186  'image_type' => 'test_image_path'
187  ],
188  'url' => 'test_url_2',
189  'frame' => 'test_frame',
190  'custom_attributes' => [
191  'name_1' => 'value_1',
192  'name_2' => 'value_2',
193  ],
194  ],
195  'expected' => [
196  'data' => [
197  'template' => 'Magento_Catalog::product/image_with_borders.phtml',
198  'image_url' => 'test_url_2',
199  'width' => 100,
200  'height' => 50,
201  'label' => 'test_product_name',
202  'ratio' => 0.5, // <==
203  'custom_attributes' => 'name_1="value_1" name_2="value_2"',
204  'product_id' => null
205  ],
206  ],
207  ];
208  }
209 }