Skip to content

Commit cb05e58

Browse files
author
Bibu Mathew
committed
MAGETWO-31968: PHP Fatal error: Call to a member function addStreamLog() on a non-object
- Fixed Fatal error - added unit test method
1 parent a437b12 commit cb05e58

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed

dev/tests/unit/testsuite/Magento/Framework/Image/Adapter/ImageMagickTest.php

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,63 @@
44
*/
55
namespace Magento\Framework\Image\Adapter;
66

7+
use Magento\Framework\Filesystem\FilesystemException;
8+
use Magento\TestFramework\Helper\ObjectManager;
9+
710
class ImageMagickTest extends \PHPUnit_Framework_TestCase
811
{
912
/**
13+
* @var \PHPUnit_Framework_MockObject_MockObject |\Magento\Framework\Filesystem
14+
*/
15+
protected $filesystemMock;
16+
17+
/**
18+
* @var \PHPUnit_Framework_MockObject_MockObject |\Psr\Log\LoggerInterface
19+
*/
20+
protected $loggerMock;
21+
22+
/**
23+
* @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Filesystem\Directory\WriteInterface
24+
*/
25+
protected $writeMock;
26+
/**
27+
* @var \Magento\Framework\Image\Adapter\ImageMagick
28+
*/
29+
protected $imageMagic;
30+
31+
public function setup()
32+
{
33+
$objectManager = new ObjectManager($this);
34+
$this->loggerMock = $this->getMockBuilder( 'Psr\Log\LoggerInterface')->getMock();
35+
$this->writeMock = $this->getMockBuilder('Magento\Framework\Filesystem\Directory\WriteInterface')->getMock();
36+
$this->filesystemMock = $this->getMock(
37+
'Magento\Framework\Filesystem',
38+
['getDirectoryWrite'],
39+
[],
40+
'',
41+
false
42+
);
43+
$this->filesystemMock
44+
->expects($this->once())
45+
->method('getDirectoryWrite')
46+
->will($this->returnValue( $this->writeMock));
47+
48+
$this->imageMagic = $objectManager
49+
->getObject(
50+
'Magento\Framework\Image\Adapter\ImageMagick',
51+
['filesystem' => $this->filesystemMock,
52+
'logger' => $this->loggerMock]
53+
);
54+
}
55+
/**
56+
* @param string $imagePath
57+
* @param string $expectedMessage
1058
* @dataProvider watermarkDataProvider
1159
*/
1260
public function testWatermark($imagePath, $expectedMessage)
1361
{
14-
$filesystem =
15-
$this->getMockBuilder('Magento\Framework\Filesystem')->disableOriginalConstructor()->getMock();
1662
$this->setExpectedException('LogicException', $expectedMessage);
17-
$object = new \Magento\Framework\Image\Adapter\ImageMagick($filesystem);
18-
$object->watermark($imagePath);
63+
$this->imageMagic->watermark($imagePath);
1964
}
2065

2166
/**
@@ -32,4 +77,16 @@ public function watermarkDataProvider()
3277
]
3378
];
3479
}
80+
81+
/**
82+
* @expectedException \Exception
83+
* @expectedExceptionMessage Unable to write file into directory product/cache. Access forbidden.
84+
*/
85+
public function testSaveWithException()
86+
{
87+
$exception = new FilesystemException();
88+
$this->writeMock->method('create')->will($this->throwException($exception));
89+
$this->loggerMock->expects($this->once())->method('critical')->with($exception);
90+
$this->imageMagic->save('product/cache','sample.jpg');
91+
}
3592
}

lib/internal/Magento/Framework/Image/Adapter/AbstractAdapter.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,13 @@ abstract public function getColorAt($x, $y);
260260
* Initialize default values
261261
*
262262
* @param \Magento\Framework\Filesystem $filesystem
263+
* @param \Psr\Log\LoggerInterface $logger
263264
* @param array $data
264265
*/
265-
public function __construct(\Magento\Framework\Filesystem $filesystem, array $data = [])
266+
public function __construct(\Magento\Framework\Filesystem $filesystem, $logger, array $data = [])
266267
{
267268
$this->_filesystem = $filesystem;
269+
$this->logger = $logger;
268270
$this->directoryWrite = $this->_filesystem->getDirectoryWrite(DirectoryList::ROOT);
269271
}
270272

0 commit comments

Comments
 (0)