ImageTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Test\Unit\Helper;
  7. use Magento\Catalog\Helper\Image;
  8. class ImageTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var Image
  12. */
  13. protected $helper;
  14. /**
  15. * @var \Magento\Framework\App\Helper\Context|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $context;
  18. /**
  19. * @var \Magento\Catalog\Block\Product\ImageFactory|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $imageFactory;
  22. /**
  23. * @var \Magento\Framework\View\Asset\Repository|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $assetRepository;
  26. /**
  27. * @var \Magento\Framework\View\ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $viewConfig;
  30. /**
  31. * @var \Magento\Catalog\Model\Product\Image|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $image;
  34. /**
  35. * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $scopeConfig;
  38. /**
  39. * @var \Magento\Catalog\Model\View\Asset\PlaceholderFactory|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $placeholderFactory;
  42. protected function setUp()
  43. {
  44. $this->mockContext();
  45. $this->mockImage();
  46. $this->assetRepository = $this->getMockBuilder(\Magento\Framework\View\Asset\Repository::class)
  47. ->disableOriginalConstructor()
  48. ->getMock();
  49. $this->viewConfig = $this->getMockBuilder(\Magento\Framework\View\ConfigInterface::class)
  50. ->getMockForAbstractClass();
  51. $this->placeholderFactory = $this->getMockBuilder(\Magento\Catalog\Model\View\Asset\PlaceholderFactory::class)
  52. ->disableOriginalConstructor()
  53. ->getMock();
  54. $this->helper = new Image(
  55. $this->context,
  56. $this->imageFactory,
  57. $this->assetRepository,
  58. $this->viewConfig,
  59. $this->placeholderFactory
  60. );
  61. }
  62. protected function mockContext()
  63. {
  64. $this->context = $this->getMockBuilder(\Magento\Framework\App\Helper\Context::class)
  65. ->disableOriginalConstructor()
  66. ->getMock();
  67. $this->scopeConfig = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
  68. ->getMockForAbstractClass();
  69. $this->context->expects($this->any())
  70. ->method('getScopeConfig')
  71. ->willReturn($this->scopeConfig);
  72. }
  73. protected function mockImage()
  74. {
  75. $this->imageFactory = $this->getMockBuilder(\Magento\Catalog\Model\Product\ImageFactory::class)
  76. ->disableOriginalConstructor()
  77. ->setMethods(['create'])
  78. ->getMock();
  79. $this->image = $this->getMockBuilder(\Magento\Catalog\Model\Product\Image::class)
  80. ->disableOriginalConstructor()
  81. ->getMock();
  82. $this->imageFactory->expects($this->any())
  83. ->method('create')
  84. ->willReturn($this->image);
  85. }
  86. /**
  87. * @param array $data
  88. * @dataProvider initDataProvider
  89. */
  90. public function testInit($data)
  91. {
  92. $imageId = 'test_image_id';
  93. $attributes = [];
  94. $productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
  95. ->disableOriginalConstructor()
  96. ->getMock();
  97. $this->prepareAttributes($data, $imageId);
  98. $this->prepareImageProperties($data);
  99. $this->prepareWatermarkProperties($data);
  100. $this->assertInstanceOf(
  101. Image::class,
  102. $this->helper->init($productMock, $imageId, $attributes)
  103. );
  104. }
  105. /**
  106. * @return array
  107. */
  108. public function initDataProvider()
  109. {
  110. return [
  111. [
  112. 'data' => [
  113. 'type' => 'image',
  114. 'width' => 100,
  115. 'height' => 100,
  116. 'frame' => 1,
  117. 'constrain' => 1,
  118. 'aspect_ratio' => 1,
  119. 'transparency' => 0,
  120. 'background' => [255, 255, 255],
  121. 'watermark' => 'watermark_file',
  122. 'watermark_opacity' => 100,
  123. 'watermark_position' => 1,
  124. 'watermark_size' => '100x100',
  125. 'watermark_size_array' => ['width' => 100, 'height' => 100],
  126. ],
  127. ],
  128. ];
  129. }
  130. /**
  131. * @param $data
  132. * @param $imageId
  133. */
  134. protected function prepareAttributes($data, $imageId)
  135. {
  136. $configViewMock = $this->getMockBuilder(\Magento\Framework\Config\View::class)
  137. ->disableOriginalConstructor()
  138. ->getMock();
  139. $configViewMock->expects($this->once())
  140. ->method('getMediaAttributes')
  141. ->with('Magento_Catalog', Image::MEDIA_TYPE_CONFIG_NODE, $imageId)
  142. ->willReturn($data);
  143. $this->viewConfig->expects($this->once())
  144. ->method('getViewConfig')
  145. ->willReturn($configViewMock);
  146. }
  147. /**
  148. * @param $data
  149. */
  150. protected function prepareImageProperties($data)
  151. {
  152. $this->image->expects($this->once())
  153. ->method('setDestinationSubdir')
  154. ->with($data['type'])
  155. ->willReturnSelf();
  156. $this->image->expects($this->any())
  157. ->method('getDestinationSubdir')
  158. ->willReturn($data['type']);
  159. $this->image->expects($this->once())
  160. ->method('setWidth')
  161. ->with($data['width'])
  162. ->willReturnSelf();
  163. $this->image->expects($this->once())
  164. ->method('setHeight')
  165. ->with($data['height'])
  166. ->willReturnSelf();
  167. $this->image->expects($this->any())
  168. ->method('setKeepFrame')
  169. ->with($data['frame'])
  170. ->willReturnSelf();
  171. $this->image->expects($this->any())
  172. ->method('setConstrainOnly')
  173. ->with($data['constrain'])
  174. ->willReturnSelf();
  175. $this->image->expects($this->any())
  176. ->method('setKeepAspectRatio')
  177. ->with($data['aspect_ratio'])
  178. ->willReturnSelf();
  179. $this->image->expects($this->any())
  180. ->method('setKeepTransparency')
  181. ->with($data['transparency'])
  182. ->willReturnSelf();
  183. $this->image->expects($this->any())
  184. ->method('setBackgroundColor')
  185. ->with($data['background'])
  186. ->willReturnSelf();
  187. }
  188. /**
  189. * @param $data
  190. */
  191. protected function prepareWatermarkProperties($data)
  192. {
  193. $this->scopeConfig->expects($this->any())
  194. ->method('getValue')
  195. ->willReturnMap([
  196. [
  197. 'design/watermark/' . $data['type'] . '_image',
  198. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  199. null,
  200. $data['watermark']
  201. ],
  202. [
  203. 'design/watermark/' . $data['type'] . '_imageOpacity',
  204. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  205. null,
  206. $data['watermark_opacity']
  207. ],
  208. [
  209. 'design/watermark/' . $data['type'] . '_position',
  210. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  211. null,
  212. $data['watermark_position']
  213. ],
  214. [
  215. 'design/watermark/' . $data['type'] . '_size',
  216. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  217. null,
  218. $data['watermark_size']
  219. ],
  220. ]);
  221. $this->image->expects($this->any())
  222. ->method('setWatermarkFile')
  223. ->with($data['watermark'])
  224. ->willReturnSelf();
  225. $this->image->expects($this->any())
  226. ->method('setWatermarkImageOpacity')
  227. ->with($data['watermark_opacity'])
  228. ->willReturnSelf();
  229. $this->image->expects($this->any())
  230. ->method('setWatermarkPosition')
  231. ->with($data['watermark_position'])
  232. ->willReturnSelf();
  233. $this->image->expects($this->any())
  234. ->method('setWatermarkSize')
  235. ->with($data['watermark_size_array'])
  236. ->willReturnSelf();
  237. }
  238. public function testGetType()
  239. {
  240. $imageId = 'test_image_id';
  241. $attributes = [];
  242. $data = [
  243. 'type' => 'image',
  244. ];
  245. $productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
  246. ->disableOriginalConstructor()
  247. ->getMock();
  248. $this->prepareAttributes($data, $imageId);
  249. $this->helper->init($productMock, $imageId, $attributes);
  250. $this->assertEquals($data['type'], $this->helper->getType());
  251. }
  252. public function testGetWidth()
  253. {
  254. $imageId = 'test_image_id';
  255. $attributes = [];
  256. $data = [
  257. 'width' => 100,
  258. ];
  259. $productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
  260. ->disableOriginalConstructor()
  261. ->getMock();
  262. $this->prepareAttributes($data, $imageId);
  263. $this->helper->init($productMock, $imageId, $attributes);
  264. $this->assertEquals($data['width'], $this->helper->getWidth());
  265. }
  266. /**
  267. * @param array $data
  268. * @dataProvider getHeightDataProvider
  269. */
  270. public function testGetHeight($data)
  271. {
  272. $imageId = 'test_image_id';
  273. $attributes = [];
  274. $productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
  275. ->disableOriginalConstructor()
  276. ->getMock();
  277. $this->prepareAttributes($data, $imageId);
  278. $height = isset($data['height']) ? $data['height'] : $data['width'];
  279. $this->helper->init($productMock, $imageId, $attributes);
  280. $this->assertEquals($height, $this->helper->getHeight());
  281. }
  282. /**
  283. * @return array
  284. */
  285. public function getHeightDataProvider()
  286. {
  287. return [
  288. 'data' => [
  289. [
  290. 'height' => 100,
  291. ],
  292. [
  293. 'width' => 100,
  294. 'height' => 100,
  295. ],
  296. [
  297. 'width' => 100,
  298. ],
  299. ],
  300. ];
  301. }
  302. /**
  303. * @param array $data
  304. * @dataProvider getFrameDataProvider
  305. */
  306. public function testGetFrame($data)
  307. {
  308. $imageId = 'test_image_id';
  309. $attributes = [];
  310. $productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
  311. ->disableOriginalConstructor()
  312. ->getMock();
  313. $this->prepareAttributes($data, $imageId);
  314. $this->helper->init($productMock, $imageId, $attributes);
  315. $this->assertEquals($data['frame'], $this->helper->getFrame());
  316. }
  317. /**
  318. * @return array
  319. */
  320. public function getFrameDataProvider()
  321. {
  322. return [
  323. 'data' => [
  324. [
  325. 'frame' => 0,
  326. ],
  327. [
  328. 'frame' => 1,
  329. ],
  330. ],
  331. ];
  332. }
  333. /**
  334. * @param array $data
  335. * @param string $expected
  336. * @dataProvider getLabelDataProvider
  337. */
  338. public function testGetLabel($data, $expected)
  339. {
  340. $imageId = 'test_image_id';
  341. $attributes = [];
  342. $productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
  343. ->disableOriginalConstructor()
  344. ->getMock();
  345. $productMock->expects($this->once())
  346. ->method('getData')
  347. ->with($data['type'] . '_' . 'label')
  348. ->willReturn($data['label']);
  349. $productMock->expects($this->any())
  350. ->method('getName')
  351. ->willReturn($expected);
  352. $this->prepareAttributes($data, $imageId);
  353. $this->helper->init($productMock, $imageId, $attributes);
  354. $this->assertEquals($expected, $this->helper->getLabel());
  355. }
  356. /**
  357. * @return array
  358. */
  359. public function getLabelDataProvider()
  360. {
  361. return [
  362. [
  363. 'data' => [
  364. 'type' => 'image',
  365. 'label' => 'test_label',
  366. ],
  367. 'test_label',
  368. ],
  369. [
  370. 'data' => [
  371. 'type' => 'image',
  372. 'label' => null,
  373. ],
  374. 'test_label',
  375. ],
  376. ];
  377. }
  378. /**
  379. * @param string $imageId
  380. * @param string $imageFile
  381. * @param string $baseFile
  382. * @param string $destination
  383. * @param boolean $setImageFile
  384. * @param boolean $isCached
  385. * @param boolean $isBaseFilePlaceholder
  386. * @param array $resizedImageInfo
  387. * @dataProvider getResizedImageInfoDataProvider
  388. */
  389. public function testGetResizedImageInfo(
  390. $imageId,
  391. $imageFile,
  392. $baseFile,
  393. $destination,
  394. $setImageFile,
  395. $isCached,
  396. $isBaseFilePlaceholder,
  397. $resizedImageInfo
  398. ) {
  399. $productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
  400. ->disableOriginalConstructor()
  401. ->getMock();
  402. $productMock->expects($this->any())
  403. ->method('getData')
  404. ->with($destination)
  405. ->willReturn($imageFile);
  406. $this->image->expects($this->any())
  407. ->method('setBaseFile')
  408. ->with($imageFile)
  409. ->willReturnSelf();
  410. $this->image->expects($this->once())
  411. ->method('getBaseFile')
  412. ->willReturn($baseFile);
  413. $this->image->expects($this->any())
  414. ->method('getDestinationSubdir')
  415. ->willReturn($destination);
  416. $this->image->expects($this->any())
  417. ->method('isCached')
  418. ->willReturn($isCached);
  419. $this->image->expects($this->any())
  420. ->method('resize')
  421. ->willReturnSelf();
  422. $this->image->expects($this->any())
  423. ->method('saveFile')
  424. ->willReturnSelf();
  425. $this->image->expects($this->once())
  426. ->method('getResizedImageInfo')
  427. ->willReturn($resizedImageInfo);
  428. $this->image->expects($this->any())
  429. ->method('isBaseFilePlaceholder')
  430. ->willReturn($isBaseFilePlaceholder);
  431. $this->prepareAttributes([], $imageId);
  432. $this->helper->init($productMock, $imageId);
  433. if ($setImageFile) {
  434. $this->helper->setImageFile($imageFile);
  435. }
  436. $result = $this->helper->getResizedImageInfo();
  437. $this->assertEquals($resizedImageInfo, $result);
  438. }
  439. /**
  440. * @return array
  441. */
  442. public function getResizedImageInfoDataProvider()
  443. {
  444. return [
  445. [
  446. 'image_id' => 'test_image_id',
  447. 'image_file' => '/path/to/test_image_id.png',
  448. 'base_file' => '/path/to/base_image.png',
  449. 'destination' => 'small_image',
  450. 'set_image_file' => true,
  451. 'is_cached' => false,
  452. 'is_base_file_placeholder' => false,
  453. 'resized_image_info' => [
  454. 'x' => 100,
  455. 'y' => 100,
  456. ],
  457. ],
  458. [
  459. 'image_id' => 'test_image_id',
  460. 'image_file' => '/path/to/test_image_id.png',
  461. 'base_file' => null,
  462. 'destination' => 'small_image',
  463. 'set_image_file' => false,
  464. 'is_cached' => false,
  465. 'is_base_file_placeholder' => false,
  466. 'resized_image_info' => [
  467. 'x' => 100,
  468. 'y' => 100,
  469. ],
  470. ],
  471. [
  472. 'image_id' => 'test_image_id',
  473. 'image_file' => '/path/to/test_image_id.png',
  474. 'base_file' => null,
  475. 'destination' => 'small_image',
  476. 'set_image_file' => true,
  477. 'is_cached' => false,
  478. 'is_base_file_placeholder' => false,
  479. 'resized_image_info' => [
  480. 'x' => 100,
  481. 'y' => 100,
  482. ],
  483. ],
  484. [
  485. 'image_id' => 'test_image_id',
  486. 'image_file' => '/path/to/test_image_id.png',
  487. 'base_file' => null,
  488. 'destination' => 'small_image',
  489. 'set_image_file' => true,
  490. 'is_cached' => false,
  491. 'is_base_file_placeholder' => true,
  492. 'resized_image_info' => [
  493. 'x' => 100,
  494. 'y' => 100,
  495. ],
  496. ],
  497. [
  498. 'image_id' => 'test_image_id',
  499. 'image_file' => '/path/to/test_image_id.png',
  500. 'base_file' => null,
  501. 'destination' => 'small_image',
  502. 'set_image_file' => true,
  503. 'is_cached' => false,
  504. 'is_base_file_placeholder' => false,
  505. 'resized_image_info' => [
  506. 'x' => 100,
  507. 'y' => 100,
  508. ],
  509. ],
  510. ];
  511. }
  512. }