Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ImageMagickTest.php
Go to the documentation of this file.
1 <?php
7 
10 
11 class ImageMagickTest extends \PHPUnit\Framework\TestCase
12 {
16  protected $filesystemMock;
17 
21  protected $loggerMock;
22 
26  protected $writeMock;
27 
31  protected $imageMagic;
32 
33  public function setup()
34  {
35  $objectManager = new ObjectManager($this);
36  $this->loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)->getMock();
37  $this->writeMock = $this->getMockBuilder(
38  \Magento\Framework\Filesystem\Directory\WriteInterface::class
39  )->getMock();
40  $this->filesystemMock = $this->createPartialMock(\Magento\Framework\Filesystem::class, ['getDirectoryWrite']);
41  $this->filesystemMock
42  ->expects($this->once())
43  ->method('getDirectoryWrite')
44  ->willReturn($this->writeMock);
45 
46  $this->imageMagic = $objectManager
47  ->getObject(
48  \Magento\Framework\Image\Adapter\ImageMagick::class,
49  ['filesystem' => $this->filesystemMock,
50  'logger' => $this->loggerMock]
51  );
52  }
53 
59  public function testWatermark($imagePath, $expectedMessage)
60  {
61  $this->expectException('LogicException');
62  $this->expectExceptionMessage($expectedMessage);
63  $this->imageMagic->watermark($imagePath);
64  }
65 
69  public function watermarkDataProvider()
70  {
71  return [
72  ['', \Magento\Framework\Image\Adapter\ImageMagick::ERROR_WATERMARK_IMAGE_ABSENT],
73  [__DIR__ . '/not_exists', \Magento\Framework\Image\Adapter\ImageMagick::ERROR_WATERMARK_IMAGE_ABSENT],
74  [
75  __DIR__ . '/_files/invalid_image.jpg',
76  \Magento\Framework\Image\Adapter\ImageMagick::ERROR_WRONG_IMAGE
77  ]
78  ];
79  }
80 
85  public function testSaveWithException()
86  {
87  $exception = new FileSystemException(
88  new \Magento\Framework\Phrase('Unable to write file into directory product/cache. Access forbidden.')
89  );
90  $this->writeMock->method('create')->will($this->throwException($exception));
91  $this->loggerMock->expects($this->once())->method('critical')->with($exception);
92  $this->imageMagic->save('product/cache', 'sample.jpg');
93  }
94 }
$objectManager
Definition: bootstrap.php:17
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60