Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ImageTest.php
Go to the documentation of this file.
1 <?php
7 
9 
10 class ImageTest extends \PHPUnit\Framework\TestCase
11 {
15  private $attribute;
16 
20  private $objectManager;
21 
25  private $imageUploader;
26 
30  private $logger;
31 
35  private $filesystem;
36 
40  protected function setUp()
41  {
42  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
43 
44  $this->attribute = $this->getMockForAbstractClass(
45  \Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class,
46  [],
47  'TestAttribute',
48  false,
49  false,
50  true,
51  ['getName']
52  );
53 
54  $this->attribute->expects($this->once())
55  ->method('getName')
56  ->will($this->returnValue('test_attribute'));
57 
58  $this->logger = $this->getMockForAbstractClass(
59  \Psr\Log\LoggerInterface::class,
60  [],
61  'TestLogger',
62  false,
63  false,
64  true,
65  ['critical']
66  );
67 
68  $this->imageUploader = $this->createPartialMock(
69  \Magento\Catalog\Model\ImageUploader::class,
70  ['moveFileFromTmp']
71  );
72 
73  $this->filesystem = $this->getMockBuilder(\Magento\Framework\Filesystem::class)->disableOriginalConstructor()
74  ->getMock();
75  }
76 
80  public function deletedValueDataProvider()
81  {
82  return [
83  [false],
84  [['delete' => true]]
85  ];
86  }
87 
94  {
95  $model = $this->objectManager->getObject(\Magento\Catalog\Model\Category\Attribute\Backend\Image::class);
96  $model->setAttribute($this->attribute);
97 
98  $object = new \Magento\Framework\DataObject([
99  'test_attribute' => $value
100  ]);
101 
102  $model->beforeSave($object);
103 
104  $this->assertEquals(null, $object->getTestAttribute());
105  }
106 
110  public function invalidValueDataProvider()
111  {
112  $closure = function () {
113  return false;
114  };
115 
116  return [
117  [1234],
118  [true],
119  [new \stdClass()],
120  [$closure],
121  [['a' => 1, 'b' => 2]]
122  ];
123  }
124 
131  {
132  $model = $this->objectManager->getObject(\Magento\Catalog\Model\Category\Attribute\Backend\Image::class);
133  $model->setAttribute($this->attribute);
134 
135  $object = new \Magento\Framework\DataObject([
136  'test_attribute' => $value
137  ]);
138 
139  $model->beforeSave($object);
140 
141  $this->assertEquals('', $object->getTestAttribute());
142  }
143 
148  {
149  $model = $this->objectManager->getObject(\Magento\Catalog\Model\Category\Attribute\Backend\Image::class);
150  $model->setAttribute($this->attribute);
151 
152  $object = new \Magento\Framework\DataObject([
153  'test_attribute' => [
154  ['name' => 'test123.jpg']
155  ]
156  ]);
157 
158  $model->beforeSave($object);
159 
160  $this->assertEquals('test123.jpg', $object->getTestAttribute());
161  }
162 
167  {
168  $model = $this->objectManager->getObject(\Magento\Catalog\Model\Category\Attribute\Backend\Image::class, [
169  'filesystem' => $this->filesystem
170  ]);
171 
172  $model->setAttribute($this->attribute);
173 
174  $this->filesystem
175  ->expects($this->once())
176  ->method('getUri')
177  ->with(DirectoryList::MEDIA)
178  ->willReturn('pub/media');
179 
180  $object = new \Magento\Framework\DataObject([
181  'test_attribute' => [
182  [
183  'name' => '/test123.jpg',
184  'url' => '/pub/media/wysiwyg/test123.jpg',
185  ]
186  ]
187  ]);
188 
189  $model->beforeSave($object);
190 
191  $this->assertEquals('/pub/media/wysiwyg/test123.jpg', $object->getTestAttribute());
192  $this->assertEquals(
193  [['name' => '/pub/media/wysiwyg/test123.jpg', 'url' => '/pub/media/wysiwyg/test123.jpg']],
194  $object->getData('_additional_data_test_attribute')
195  );
196  }
197 
202  {
203  $model = $this->objectManager->getObject(\Magento\Catalog\Model\Category\Attribute\Backend\Image::class);
204  $model->setAttribute($this->attribute);
205 
206  $object = new \Magento\Framework\DataObject([
207  'test_attribute' => [
208  ['name' => 'test123.jpg', 'tmp_name' => 'abc123', 'url' => 'http://www.example.com/test123.jpg']
209  ]
210  ]);
211 
212  $model->beforeSave($object);
213 
214  $this->assertEquals([
215  ['name' => 'test123.jpg', 'tmp_name' => 'abc123', 'url' => 'http://www.example.com/test123.jpg']
216  ], $object->getData('_additional_data_test_attribute'));
217  }
218 
223  {
224  $model = $this->objectManager->getObject(\Magento\Catalog\Model\Category\Attribute\Backend\Image::class);
225  $model->setAttribute($this->attribute);
226 
227  $object = new \Magento\Framework\DataObject([
228  'test_attribute' => 'test123.jpg'
229  ]);
230 
231  $model->beforeSave($object);
232 
233  $this->assertEquals('test123.jpg', $object->getTestAttribute());
234  $this->assertNull($object->getData('_additional_data_test_attribute'));
235  }
236 
240  private function setUpModelForAfterSave()
241  {
242  $objectManagerMock = $this->createPartialMock(\Magento\Framework\App\ObjectManager::class, ['get']);
243 
244  $imageUploaderMock = $this->imageUploader;
245 
246  $objectManagerMock->expects($this->any())
247  ->method('get')
248  ->will($this->returnCallback(function ($class, $params = []) use ($imageUploaderMock) {
249  if ($class == \Magento\Catalog\CategoryImageUpload::class) {
250  return $imageUploaderMock;
251  }
252 
253  return $this->objectManager->get($class, $params);
254  }));
255 
256  $model = $this->objectManager->getObject(\Magento\Catalog\Model\Category\Attribute\Backend\Image::class, [
257  'objectManager' => $objectManagerMock,
258  'logger' => $this->logger
259  ]);
260  $this->objectManager->setBackwardCompatibleProperty($model, 'imageUploader', $this->imageUploader);
261 
262  return $model->setAttribute($this->attribute);
263  }
264 
268  public function attributeValueDataProvider()
269  {
270  return [
271  [[['name' => 'test1234.jpg']]],
272  ['test1234.jpg'],
273  [''],
274  [false]
275  ];
276  }
277 
284  {
285  $model = $this->setUpModelForAfterSave();
286 
287  $this->imageUploader->expects($this->once())
288  ->method('moveFileFromTmp')
289  ->with($this->equalTo('test1234.jpg'));
290 
291  $object = new \Magento\Framework\DataObject(
292  [
293  'test_attribute' => $value,
294  '_additional_data_test_attribute' => [['name' => 'test1234.jpg', 'tmp_name' => 'test-test-1234']]
295  ]
296  );
297 
298  $model->afterSave($object);
299  }
300 
307  {
308  $model = $this->setUpModelForAfterSave();
309 
310  $this->imageUploader->expects($this->never())
311  ->method('moveFileFromTmp');
312 
313  $object = new \Magento\Framework\DataObject(
314  [
315  'test_attribute' => $value
316  ]
317  );
318 
319  $model->afterSave($object);
320  }
321 
325  public function testAfterSaveWithExceptions()
326  {
327  $model = $this->setUpModelForAfterSave();
328 
329  $exception = new \Exception();
330 
331  $this->imageUploader->expects($this->any())
332  ->method('moveFileFromTmp')
333  ->will($this->throwException($exception));
334 
335  $this->logger->expects($this->once())
336  ->method('critical')
337  ->with($this->equalTo($exception));
338 
339  $object = new \Magento\Framework\DataObject(
340  [
341  '_additional_data_test_attribute' => [['name' => 'test1234.jpg', 'tmp_name' => 'test-test-1234']]
342  ]
343  );
344 
345  $model->afterSave($object);
346  }
347 }
$_option $_optionId $class
Definition: date.phtml:13
$value
Definition: gender.phtml:16
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18