Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ImageProcessorTest.php
Go to the documentation of this file.
1 <?php
8 
12 class ImageProcessorTest extends \PHPUnit\Framework\TestCase
13 {
17  protected $imageProcessor;
18 
22  protected $objectManager;
23 
27  protected $fileSystemMock;
28 
33 
38 
42  protected $loggerMock;
43 
47  protected $uploaderMock;
48 
53 
54  protected function setUp()
55  {
56  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
57 
58  $this->directoryWriteMock = $this->getMockForAbstractClass(
59  \Magento\Framework\Filesystem\Directory\WriteInterface::class
60  );
61  $this->fileSystemMock = $this->getMockBuilder(\Magento\Framework\Filesystem::class)
62  ->disableOriginalConstructor()
63  ->getMock();
64  $this->fileSystemMock->expects($this->any())
65  ->method('getDirectoryWrite')
66  ->willReturn($this->directoryWriteMock);
67  $this->contentValidatorMock = $this->getMockBuilder(
68  \Magento\Framework\Api\ImageContentValidatorInterface::class
69  )
70  ->disableOriginalConstructor()
71  ->getMock();
72  $this->dataObjectHelperMock = $this->getMockBuilder(\Magento\Framework\Api\DataObjectHelper::class)
73  ->disableOriginalConstructor()
74  ->getMock();
75  $this->loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
76  ->disableOriginalConstructor()
77  ->getMock();
78  $this->uploaderMock = $this->getMockBuilder(\Magento\Framework\Api\Uploader::class)
79  ->setMethods(
80  [
81  'processFileAttributes',
82  'setFilesDispersion',
83  'setFilenamesCaseSensitivity',
84  'setAllowRenameFiles',
85  'save',
86  'getUploadedFileName'
87  ]
88  )
89  ->disableOriginalConstructor()
90  ->getMock();
91 
92  $this->imageProcessor = $this->objectManager->getObject(
93  \Magento\Framework\Api\ImageProcessor::class,
94  [
95  'fileSystem' => $this->fileSystemMock,
96  'contentValidator' => $this->contentValidatorMock,
97  'dataObjectHelper' => $this->dataObjectHelperMock,
98  'logger' => $this->loggerMock,
99  'uploader' => $this->uploaderMock
100  ]
101  );
102  }
103 
104  public function testSaveWithNoImageData()
105  {
106  $imageDataMock = $this->getMockBuilder(\Magento\Framework\Api\CustomAttributesDataInterface::class)
107  ->disableOriginalConstructor()
108  ->getMock();
109  $imageDataMock->expects($this->once())
110  ->method('getCustomAttributes')
111  ->willReturn([]);
112 
113  $this->dataObjectHelperMock->expects($this->once())
114  ->method('getCustomAttributeValueByType')
115  ->willReturn([]);
116 
117  $this->assertEquals($imageDataMock, $this->imageProcessor->save($imageDataMock, 'testEntityType'));
118  }
119 
124  public function testSaveInputException()
125  {
126  $imageContent = $this->getMockBuilder(\Magento\Framework\Api\Data\ImageContentInterface::class)
127  ->disableOriginalConstructor()
128  ->getMock();
129  $imageDataObject = $this->getMockBuilder(\Magento\Framework\Api\AttributeValue::class)
130  ->disableOriginalConstructor()
131  ->getMock();
132  $imageDataObject->expects($this->once())
133  ->method('getValue')
134  ->willReturn($imageContent);
135 
136  $imageDataMock = $this->getMockBuilder(\Magento\Framework\Api\CustomAttributesDataInterface::class)
137  ->disableOriginalConstructor()
138  ->getMock();
139  $imageDataMock->expects($this->once())
140  ->method('getCustomAttributes')
141  ->willReturn([]);
142 
143  $this->dataObjectHelperMock->expects($this->once())
144  ->method('getCustomAttributeValueByType')
145  ->willReturn([$imageDataObject]);
146 
147  $this->contentValidatorMock->expects($this->once())
148  ->method('isValid')
149  ->willReturn(false);
150 
151  $this->imageProcessor->save($imageDataMock, 'testEntityType');
152  }
153 
154  public function testSaveWithNoPreviousData()
155  {
156  $imageContent = $this->getMockBuilder(\Magento\Framework\Api\Data\ImageContentInterface::class)
157  ->disableOriginalConstructor()
158  ->getMock();
159  $imageContent->expects($this->any())
160  ->method('getBase64EncodedData')
161  ->willReturn('testImageData');
162  $imageContent->expects($this->any())
163  ->method('getName')
164  ->willReturn('testFileName');
165  $imageContent->expects($this->any())
166  ->method('getType')
167  ->willReturn('image/jpg');
168 
169  $imageDataObject = $this->getMockBuilder(\Magento\Framework\Api\AttributeValue::class)
170  ->disableOriginalConstructor()
171  ->getMock();
172  $imageDataObject->expects($this->once())
173  ->method('getValue')
174  ->willReturn($imageContent);
175 
176  $imageData = $this->getMockForAbstractClass(\Magento\Framework\Api\CustomAttributesDataInterface::class);
177  $imageData->expects($this->once())
178  ->method('getCustomAttributes')
179  ->willReturn([]);
180 
181  $this->dataObjectHelperMock->expects($this->once())
182  ->method('getCustomAttributeValueByType')
183  ->willReturn([$imageDataObject]);
184 
185  $this->contentValidatorMock->expects($this->once())
186  ->method('isValid')
187  ->willReturn(true);
188 
189  $this->directoryWriteMock->expects($this->any())
190  ->method('getAbsolutePath')
191  ->willReturn('testPath');
192 
193  $this->assertEquals($imageData, $this->imageProcessor->save($imageData, 'testEntityType'));
194  }
195 
196  public function testSaveWithPreviousData()
197  {
198  $imageContent = $this->getMockBuilder(\Magento\Framework\Api\Data\ImageContentInterface::class)
199  ->disableOriginalConstructor()
200  ->getMock();
201  $imageContent->expects($this->any())
202  ->method('getBase64EncodedData')
203  ->willReturn('testImageData');
204  $imageContent->expects($this->any())
205  ->method('getName')
206  ->willReturn('testFileName.png');
207 
208  $imageDataObject = $this->getMockBuilder(\Magento\Framework\Api\AttributeValue::class)
209  ->disableOriginalConstructor()
210  ->getMock();
211  $imageDataObject->expects($this->once())
212  ->method('getValue')
213  ->willReturn($imageContent);
214 
215  $imageData = $this->getMockForAbstractClass(\Magento\Framework\Api\CustomAttributesDataInterface::class);
216  $imageData->expects($this->once())
217  ->method('getCustomAttributes')
218  ->willReturn([]);
219 
220  $this->dataObjectHelperMock->expects($this->once())
221  ->method('getCustomAttributeValueByType')
222  ->willReturn([$imageDataObject]);
223 
224  $this->contentValidatorMock->expects($this->once())
225  ->method('isValid')
226  ->willReturn(true);
227 
228  $this->directoryWriteMock->expects($this->any())
229  ->method('getAbsolutePath')
230  ->willReturn('testPath');
231 
232  $prevImageAttribute = $this->getMockForAbstractClass(\Magento\Framework\Api\AttributeInterface::class);
233  $prevImageAttribute->expects($this->once())
234  ->method('getValue')
235  ->willReturn('testImagePath');
236 
237  $prevImageData = $this->getMockForAbstractClass(\Magento\Framework\Api\CustomAttributesDataInterface::class);
238  $prevImageData->expects($this->once())
239  ->method('getCustomAttribute')
240  ->willReturn($prevImageAttribute);
241 
242  $this->assertEquals($imageData, $this->imageProcessor->save($imageData, 'testEntityType', $prevImageData));
243  }
244 
250  {
251  $imageContent = $this->getMockBuilder(\Magento\Framework\Api\Data\ImageContentInterface::class)
252  ->disableOriginalConstructor()
253  ->getMock();
254  $imageContent->expects($this->once())
255  ->method('getBase64EncodedData')
256  ->willReturn('testImageData');
257  $imageContent->expects($this->once())
258  ->method('getName')
259  ->willReturn('testFileName');
260 
261  $imageDataObject = $this->getMockBuilder(\Magento\Framework\Api\AttributeValue::class)
262  ->disableOriginalConstructor()
263  ->getMock();
264  $imageDataObject->expects($this->once())
265  ->method('getValue')
266  ->willReturn($imageContent);
267 
268  $imageData = $this->getMockForAbstractClass(\Magento\Framework\Api\CustomAttributesDataInterface::class);
269  $imageData->expects($this->once())
270  ->method('getCustomAttributes')
271  ->willReturn([]);
272 
273  $this->dataObjectHelperMock->expects($this->once())
274  ->method('getCustomAttributeValueByType')
275  ->willReturn([$imageDataObject]);
276 
277  $this->contentValidatorMock->expects($this->once())
278  ->method('isValid')
279  ->willReturn(true);
280 
281  $this->assertEquals($imageData, $this->imageProcessor->save($imageData, 'testEntityType'));
282  }
283 }