Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MediaTest.php
Go to the documentation of this file.
1 <?php
7 
13 class MediaTest extends \PHPUnit\Framework\TestCase
14 {
16  protected $mediaConfigMock;
17 
19  protected $fileSystemMock;
20 
22  protected $writeInstanceMock;
23 
25  protected $fileStorageDbMock;
26 
28  protected $storeManagerMock;
29 
31  protected $imageFactoryMock;
32 
34  protected $viewConfigMock;
35 
38 
40  protected $storeMock;
41 
43  protected $mediaHelperObject;
44 
45  protected function setUp()
46  {
47  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
48 
49  $this->mediaConfigMock = $this->createMock(\Magento\Catalog\Model\Product\Media\Config::class);
50  $this->writeInstanceMock = $this->createMock(\Magento\Framework\Filesystem\Directory\WriteInterface::class);
51  $this->fileStorageDbMock = $this->createPartialMock(
52  \Magento\MediaStorage\Helper\File\Storage\Database::class,
53  ['checkDbUsage', 'getUniqueFilename', 'renameFile']
54  );
55 
56  $this->storeManagerMock = $this->createPartialMock(\Magento\Store\Model\StoreManager::class, ['getStore']);
57 
58  $this->imageFactoryMock = $this->createMock(\Magento\Framework\Image\Factory::class);
59 
60  $this->viewConfigMock = $this->createMock(\Magento\Framework\View\Config::class);
61 
62  $this->storeMock = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getBaseUrl']);
63 
64  $this->mediaDirectoryMock = $this->createMock(\Magento\Framework\Filesystem\Directory\Write::class);
65  $this->fileSystemMock = $this->createPartialMock(\Magento\Framework\Filesystem::class, ['getDirectoryWrite']);
66  $this->fileSystemMock
67  ->expects($this->any())
68  ->method('getDirectoryWrite')
69  ->will($this->returnValue($this->mediaDirectoryMock));
70 
71  $this->mediaHelperObject = $objectManager->getObject(
72  \Magento\Swatches\Helper\Media::class,
73  [
74  'mediaConfig' => $this->mediaConfigMock,
75  'filesystem' => $this->fileSystemMock,
76  'fileStorageDb' => $this->fileStorageDbMock,
77  'storeManager' => $this->storeManagerMock,
78  'imageFactory' => $this->imageFactoryMock,
79  'configInterface' => $this->viewConfigMock,
80  ]
81  );
82  }
83 
87  public function testGetSwatchAttributeImage($swatchType, $expectedResult)
88  {
89  $this->storeManagerMock
90  ->expects($this->once())
91  ->method('getStore')
92  ->willReturn($this->storeMock);
93 
94  $this->storeMock
95  ->expects($this->once())
96  ->method('getBaseUrl')
97  ->with('media')
98  ->willReturn('http://url/pub/media/');
99 
100  $this->generateImageConfig();
101 
103 
104  $result = $this->mediaHelperObject->getSwatchAttributeImage($swatchType, '/f/i/file.png');
105 
106  $this->assertEquals($result, $expectedResult);
107  }
108 
112  public function dataForFullPath()
113  {
114  return [
115  [
116  'swatch_image',
117  'http://url/pub/media/attribute/swatch/swatch_image/30x20/f/i/file.png',
118  ],
119  [
120  'swatch_thumb',
121  'http://url/pub/media/attribute/swatch/swatch_thumb/110x90/f/i/file.png',
122  ],
123  ];
124  }
125 
126  public function testMoveImageFromTmp()
127  {
128  $this->fileStorageDbMock->method('checkDbUsage')->willReturn(1);
129  $this->fileStorageDbMock->expects($this->atLeastOnce())->method('getUniqueFilename')->willReturn('file___1');
130  $this->fileStorageDbMock->method('renameFile')->will($this->returnSelf());
131  $this->mediaDirectoryMock->expects($this->exactly(2))->method('delete')->will($this->returnSelf());
132  $this->mediaHelperObject->moveImageFromTmp('file.tmp');
133  }
134 
135  public function testMoveImageFromTmpNoDb()
136  {
137  $this->fileStorageDbMock->method('checkDbUsage')->willReturn(false);
138  $this->fileStorageDbMock->method('renameFile')->will($this->returnSelf());
139  $result = $this->mediaHelperObject->moveImageFromTmp('file.tmp');
140  $this->assertNotNull($result);
141  }
142 
144  {
145  $this->mediaDirectoryMock
146  ->expects($this->atLeastOnce())
147  ->method('getAbsolutePath')
148  ->willReturn('attribute/swatch/e/a/earth.png');
149 
150  $image = $this->createPartialMock(\Magento\Framework\Image::class, [
151  'resize',
152  'save',
153  'keepTransparency',
154  'constrainOnly',
155  'keepFrame',
156  'keepAspectRatio',
157  'backgroundColor',
158  'quality'
159  ]);
160 
161  $this->imageFactoryMock->expects($this->any())->method('create')->willReturn($image);
162  $this->generateImageConfig();
163  $image->expects($this->any())->method('resize')->will($this->returnSelf());
164  $image->expects($this->atLeastOnce())->method('backgroundColor')->with([255, 255, 255])->willReturnSelf();
165  $this->mediaHelperObject->generateSwatchVariations('/e/a/earth.png');
166  }
167 
168  public function testGetSwatchMediaUrl()
169  {
170  $storeMock = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getBaseUrl']);
171 
172  $this->storeManagerMock
173  ->expects($this->once())
174  ->method('getStore')
175  ->willReturn($storeMock);
176 
177  $storeMock
178  ->expects($this->once())
179  ->method('getBaseUrl')
180  ->with('media')
181  ->willReturn('http://url/pub/media/');
182 
183  $result = $this->mediaHelperObject->getSwatchMediaUrl();
184 
185  $this->assertEquals($result, 'http://url/pub/media/attribute/swatch');
186  }
187 
191  public function testGetFolderNameSize($swatchType, $imageConfig, $expectedResult)
192  {
193  if ($imageConfig === null) {
194  $this->generateImageConfig();
195  }
196  $result = $this->mediaHelperObject->getFolderNameSize($swatchType, $imageConfig);
197  $this->assertEquals($expectedResult, $result);
198  }
199 
203  public function dataForFolderName()
204  {
205  return [
206  [
207  'swatch_image',
208  [
209  'swatch_image' => [
210  'width' => 30,
211  'height' => 20,
212  ],
213  'swatch_thumb' => [
214  'width' => 110,
215  'height' => 90,
216  ],
217  ],
218  '30x20',
219  ],
220  [
221  'swatch_thumb',
222  [
223  'swatch_image' => [
224  'width' => 30,
225  'height' => 20,
226  ],
227  'swatch_thumb' => [
228  'width' => 110,
229  'height' => 90,
230  ],
231  ],
232  '110x90',
233  ],
234  [
235  'swatch_thumb',
236  null,
237  '110x90',
238  ],
239  ];
240  }
241 
242  public function testGetImageConfig()
243  {
244  $this->generateImageConfig();
245  $this->mediaHelperObject->getImageConfig();
246  }
247 
248  protected function generateImageConfig()
249  {
250  $configMock = $this->createMock(\Magento\Framework\Config\View::class);
251 
252  $this->viewConfigMock
253  ->expects($this->atLeastOnce())
254  ->method('getViewConfig')
255  ->willReturn($configMock);
256 
257  $imageConfig = [
258  'swatch_image' => [
259  'width' => 30,
260  'height' => 20,
261  ],
262  'swatch_thumb' => [
263  'width' => 110,
264  'height' => 90,
265  ],
266  ];
267 
268  $configMock->expects($this->any())->method('getMediaEntities')->willReturn($imageConfig);
269  }
270 
271  public function testGetAttributeSwatchPath()
272  {
273  $result = $this->mediaHelperObject->getAttributeSwatchPath('/m/a/magento.png');
274  $this->assertEquals($result, 'attribute/swatch/m/a/magento.png');
275  }
276 
277  public function testGetSwatchMediaPath()
278  {
279  $this->assertEquals('attribute/swatch', $this->mediaHelperObject->getSwatchMediaPath());
280  }
281 
285  public function testGetSwatchCachePath($swatchType, $expectedResult)
286  {
287  $this->assertEquals($expectedResult, $this->mediaHelperObject->getSwatchCachePath($swatchType));
288  }
289 
293  public function getSwatchTypes()
294  {
295  return [
296  [
297  'swatch_image',
298  'attribute/swatch/swatch_image/',
299  ],
300  [
301  'swatch_thumb',
302  'attribute/swatch/swatch_thumb/',
303  ],
304  ];
305  }
306 }
testGetSwatchAttributeImage($swatchType, $expectedResult)
Definition: MediaTest.php:87
$objectManager
Definition: bootstrap.php:17
testGetFolderNameSize($swatchType, $imageConfig, $expectedResult)
Definition: MediaTest.php:191
testGetSwatchCachePath($swatchType, $expectedResult)
Definition: MediaTest.php:285