MediaTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Swatches\Test\Unit\Helper;
  7. /**
  8. * Helper to move images from tmp to catalog directory
  9. *
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class MediaTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Model\Product\Media\Config */
  15. protected $mediaConfigMock;
  16. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Filesystem */
  17. protected $fileSystemMock;
  18. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Filesystem\Directory\WriteInterface */
  19. protected $writeInstanceMock;
  20. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\MediaStorage\Helper\File\Storage\Database */
  21. protected $fileStorageDbMock;
  22. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Store\Model\StoreManager */
  23. protected $storeManagerMock;
  24. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Image\Factory */
  25. protected $imageFactoryMock;
  26. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\View\Config */
  27. protected $viewConfigMock;
  28. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Filesystem\Directory\Write */
  29. protected $mediaDirectoryMock;
  30. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Store\Model\Store */
  31. protected $storeMock;
  32. /** @var \Magento\Swatches\Helper\Media|\Magento\Framework\TestFramework\Unit\Helper\ObjectManager */
  33. protected $mediaHelperObject;
  34. protected function setUp()
  35. {
  36. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  37. $this->mediaConfigMock = $this->createMock(\Magento\Catalog\Model\Product\Media\Config::class);
  38. $this->writeInstanceMock = $this->createMock(\Magento\Framework\Filesystem\Directory\WriteInterface::class);
  39. $this->fileStorageDbMock = $this->createPartialMock(
  40. \Magento\MediaStorage\Helper\File\Storage\Database::class,
  41. ['checkDbUsage', 'getUniqueFilename', 'renameFile']
  42. );
  43. $this->storeManagerMock = $this->createPartialMock(\Magento\Store\Model\StoreManager::class, ['getStore']);
  44. $this->imageFactoryMock = $this->createMock(\Magento\Framework\Image\Factory::class);
  45. $this->viewConfigMock = $this->createMock(\Magento\Framework\View\Config::class);
  46. $this->storeMock = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getBaseUrl']);
  47. $this->mediaDirectoryMock = $this->createMock(\Magento\Framework\Filesystem\Directory\Write::class);
  48. $this->fileSystemMock = $this->createPartialMock(\Magento\Framework\Filesystem::class, ['getDirectoryWrite']);
  49. $this->fileSystemMock
  50. ->expects($this->any())
  51. ->method('getDirectoryWrite')
  52. ->will($this->returnValue($this->mediaDirectoryMock));
  53. $this->mediaHelperObject = $objectManager->getObject(
  54. \Magento\Swatches\Helper\Media::class,
  55. [
  56. 'mediaConfig' => $this->mediaConfigMock,
  57. 'filesystem' => $this->fileSystemMock,
  58. 'fileStorageDb' => $this->fileStorageDbMock,
  59. 'storeManager' => $this->storeManagerMock,
  60. 'imageFactory' => $this->imageFactoryMock,
  61. 'configInterface' => $this->viewConfigMock,
  62. ]
  63. );
  64. }
  65. /**
  66. * @dataProvider dataForFullPath
  67. */
  68. public function testGetSwatchAttributeImage($swatchType, $expectedResult)
  69. {
  70. $this->storeManagerMock
  71. ->expects($this->once())
  72. ->method('getStore')
  73. ->willReturn($this->storeMock);
  74. $this->storeMock
  75. ->expects($this->once())
  76. ->method('getBaseUrl')
  77. ->with('media')
  78. ->willReturn('http://url/pub/media/');
  79. $this->generateImageConfig();
  80. $this->testGenerateSwatchVariations();
  81. $result = $this->mediaHelperObject->getSwatchAttributeImage($swatchType, '/f/i/file.png');
  82. $this->assertEquals($result, $expectedResult);
  83. }
  84. /**
  85. * @return array
  86. */
  87. public function dataForFullPath()
  88. {
  89. return [
  90. [
  91. 'swatch_image',
  92. 'http://url/pub/media/attribute/swatch/swatch_image/30x20/f/i/file.png',
  93. ],
  94. [
  95. 'swatch_thumb',
  96. 'http://url/pub/media/attribute/swatch/swatch_thumb/110x90/f/i/file.png',
  97. ],
  98. ];
  99. }
  100. public function testMoveImageFromTmp()
  101. {
  102. $this->fileStorageDbMock->method('checkDbUsage')->willReturn(1);
  103. $this->fileStorageDbMock->expects($this->atLeastOnce())->method('getUniqueFilename')->willReturn('file___1');
  104. $this->fileStorageDbMock->method('renameFile')->will($this->returnSelf());
  105. $this->mediaDirectoryMock->expects($this->exactly(2))->method('delete')->will($this->returnSelf());
  106. $this->mediaHelperObject->moveImageFromTmp('file.tmp');
  107. }
  108. public function testMoveImageFromTmpNoDb()
  109. {
  110. $this->fileStorageDbMock->method('checkDbUsage')->willReturn(false);
  111. $this->fileStorageDbMock->method('renameFile')->will($this->returnSelf());
  112. $result = $this->mediaHelperObject->moveImageFromTmp('file.tmp');
  113. $this->assertNotNull($result);
  114. }
  115. public function testGenerateSwatchVariations()
  116. {
  117. $this->mediaDirectoryMock
  118. ->expects($this->atLeastOnce())
  119. ->method('getAbsolutePath')
  120. ->willReturn('attribute/swatch/e/a/earth.png');
  121. $image = $this->createPartialMock(\Magento\Framework\Image::class, [
  122. 'resize',
  123. 'save',
  124. 'keepTransparency',
  125. 'constrainOnly',
  126. 'keepFrame',
  127. 'keepAspectRatio',
  128. 'backgroundColor',
  129. 'quality'
  130. ]);
  131. $this->imageFactoryMock->expects($this->any())->method('create')->willReturn($image);
  132. $this->generateImageConfig();
  133. $image->expects($this->any())->method('resize')->will($this->returnSelf());
  134. $image->expects($this->atLeastOnce())->method('backgroundColor')->with([255, 255, 255])->willReturnSelf();
  135. $this->mediaHelperObject->generateSwatchVariations('/e/a/earth.png');
  136. }
  137. public function testGetSwatchMediaUrl()
  138. {
  139. $storeMock = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getBaseUrl']);
  140. $this->storeManagerMock
  141. ->expects($this->once())
  142. ->method('getStore')
  143. ->willReturn($storeMock);
  144. $storeMock
  145. ->expects($this->once())
  146. ->method('getBaseUrl')
  147. ->with('media')
  148. ->willReturn('http://url/pub/media/');
  149. $result = $this->mediaHelperObject->getSwatchMediaUrl();
  150. $this->assertEquals($result, 'http://url/pub/media/attribute/swatch');
  151. }
  152. /**
  153. * @dataProvider dataForFolderName
  154. */
  155. public function testGetFolderNameSize($swatchType, $imageConfig, $expectedResult)
  156. {
  157. if ($imageConfig === null) {
  158. $this->generateImageConfig();
  159. }
  160. $result = $this->mediaHelperObject->getFolderNameSize($swatchType, $imageConfig);
  161. $this->assertEquals($expectedResult, $result);
  162. }
  163. /**
  164. * @return array
  165. */
  166. public function dataForFolderName()
  167. {
  168. return [
  169. [
  170. 'swatch_image',
  171. [
  172. 'swatch_image' => [
  173. 'width' => 30,
  174. 'height' => 20,
  175. ],
  176. 'swatch_thumb' => [
  177. 'width' => 110,
  178. 'height' => 90,
  179. ],
  180. ],
  181. '30x20',
  182. ],
  183. [
  184. 'swatch_thumb',
  185. [
  186. 'swatch_image' => [
  187. 'width' => 30,
  188. 'height' => 20,
  189. ],
  190. 'swatch_thumb' => [
  191. 'width' => 110,
  192. 'height' => 90,
  193. ],
  194. ],
  195. '110x90',
  196. ],
  197. [
  198. 'swatch_thumb',
  199. null,
  200. '110x90',
  201. ],
  202. ];
  203. }
  204. public function testGetImageConfig()
  205. {
  206. $this->generateImageConfig();
  207. $this->mediaHelperObject->getImageConfig();
  208. }
  209. protected function generateImageConfig()
  210. {
  211. $configMock = $this->createMock(\Magento\Framework\Config\View::class);
  212. $this->viewConfigMock
  213. ->expects($this->atLeastOnce())
  214. ->method('getViewConfig')
  215. ->willReturn($configMock);
  216. $imageConfig = [
  217. 'swatch_image' => [
  218. 'width' => 30,
  219. 'height' => 20,
  220. ],
  221. 'swatch_thumb' => [
  222. 'width' => 110,
  223. 'height' => 90,
  224. ],
  225. ];
  226. $configMock->expects($this->any())->method('getMediaEntities')->willReturn($imageConfig);
  227. }
  228. public function testGetAttributeSwatchPath()
  229. {
  230. $result = $this->mediaHelperObject->getAttributeSwatchPath('/m/a/magento.png');
  231. $this->assertEquals($result, 'attribute/swatch/m/a/magento.png');
  232. }
  233. public function testGetSwatchMediaPath()
  234. {
  235. $this->assertEquals('attribute/swatch', $this->mediaHelperObject->getSwatchMediaPath());
  236. }
  237. /**
  238. * @dataProvider getSwatchTypes
  239. */
  240. public function testGetSwatchCachePath($swatchType, $expectedResult)
  241. {
  242. $this->assertEquals($expectedResult, $this->mediaHelperObject->getSwatchCachePath($swatchType));
  243. }
  244. /**
  245. * @return array
  246. */
  247. public function getSwatchTypes()
  248. {
  249. return [
  250. [
  251. 'swatch_image',
  252. 'attribute/swatch/swatch_image/',
  253. ],
  254. [
  255. 'swatch_thumb',
  256. 'attribute/swatch/swatch_thumb/',
  257. ],
  258. ];
  259. }
  260. }