ImageMagickTest.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Image\Test\Unit\Adapter;
  7. use Magento\Framework\Exception\FileSystemException;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  9. class ImageMagickTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var \PHPUnit_Framework_MockObject_MockObject |\Magento\Framework\Filesystem
  13. */
  14. protected $filesystemMock;
  15. /**
  16. * @var \PHPUnit_Framework_MockObject_MockObject |\Psr\Log\LoggerInterface
  17. */
  18. protected $loggerMock;
  19. /**
  20. * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Filesystem\Directory\WriteInterface
  21. */
  22. protected $writeMock;
  23. /**
  24. * @var \Magento\Framework\Image\Adapter\ImageMagick
  25. */
  26. protected $imageMagic;
  27. public function setup()
  28. {
  29. $objectManager = new ObjectManager($this);
  30. $this->loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)->getMock();
  31. $this->writeMock = $this->getMockBuilder(
  32. \Magento\Framework\Filesystem\Directory\WriteInterface::class
  33. )->getMock();
  34. $this->filesystemMock = $this->createPartialMock(\Magento\Framework\Filesystem::class, ['getDirectoryWrite']);
  35. $this->filesystemMock
  36. ->expects($this->once())
  37. ->method('getDirectoryWrite')
  38. ->willReturn($this->writeMock);
  39. $this->imageMagic = $objectManager
  40. ->getObject(
  41. \Magento\Framework\Image\Adapter\ImageMagick::class,
  42. ['filesystem' => $this->filesystemMock,
  43. 'logger' => $this->loggerMock]
  44. );
  45. }
  46. /**
  47. * @param string $imagePath
  48. * @param string $expectedMessage
  49. * @dataProvider watermarkDataProvider
  50. */
  51. public function testWatermark($imagePath, $expectedMessage)
  52. {
  53. $this->expectException('LogicException');
  54. $this->expectExceptionMessage($expectedMessage);
  55. $this->imageMagic->watermark($imagePath);
  56. }
  57. /**
  58. * @return array
  59. */
  60. public function watermarkDataProvider()
  61. {
  62. return [
  63. ['', \Magento\Framework\Image\Adapter\ImageMagick::ERROR_WATERMARK_IMAGE_ABSENT],
  64. [__DIR__ . '/not_exists', \Magento\Framework\Image\Adapter\ImageMagick::ERROR_WATERMARK_IMAGE_ABSENT],
  65. [
  66. __DIR__ . '/_files/invalid_image.jpg',
  67. \Magento\Framework\Image\Adapter\ImageMagick::ERROR_WRONG_IMAGE
  68. ]
  69. ];
  70. }
  71. /**
  72. * @expectedException \Exception
  73. * @expectedExceptionMessage Unable to write file into directory product/cache. Access forbidden.
  74. */
  75. public function testSaveWithException()
  76. {
  77. $exception = new FileSystemException(
  78. new \Magento\Framework\Phrase('Unable to write file into directory product/cache. Access forbidden.')
  79. );
  80. $this->writeMock->method('create')->will($this->throwException($exception));
  81. $this->loggerMock->expects($this->once())->method('critical')->with($exception);
  82. $this->imageMagic->save('product/cache', 'sample.jpg');
  83. }
  84. }