Skip to content

Commit 75a769b

Browse files
committed
#10208: Fix getImage method when sending product alert email
- Unit test updated
1 parent 3d3d31c commit 75a769b

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

app/code/Magento/ProductAlert/Test/Unit/Block/Email/StockTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ class StockTest extends \PHPUnit_Framework_TestCase
2525
*/
2626
protected $imageBuilder;
2727

28+
/**
29+
* @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
30+
*/
31+
private $storeManagerMock;
32+
33+
/**
34+
* @var \Magento\Store\Model\App\Emulation|\PHPUnit_Framework_MockObject_MockObject
35+
*/
36+
private $appEmulationMock;
37+
2838
protected function setUp()
2939
{
3040
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
@@ -39,12 +49,25 @@ protected function setUp()
3949
$this->imageBuilder = $this->getMockBuilder(\Magento\Catalog\Block\Product\ImageBuilder::class)
4050
->disableOriginalConstructor()
4151
->getMock();
52+
$this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
53+
->disableOriginalConstructor()
54+
->getMock();
55+
$this->appEmulationMock = $this->getMockBuilder(\Magento\Store\Model\App\Emulation::class)
56+
->disableOriginalConstructor()
57+
->getMock();
58+
59+
$contextMock = $this->getMockBuilder(\Magento\Framework\View\Element\Template\Context::class)
60+
->disableOriginalConstructor()
61+
->getMock();
62+
$contextMock->expects($this->any())->method('getStoreManager')->willReturn($this->storeManagerMock);
4263

4364
$this->_block = $objectManager->getObject(
4465
\Magento\ProductAlert\Block\Email\Stock::class,
4566
[
4667
'maliciousCode' => $this->_filter,
4768
'imageBuilder' => $this->imageBuilder,
69+
'context' => $contextMock,
70+
'appEmulation' => $this->appEmulationMock
4871
]
4972
);
5073
}
@@ -82,6 +105,13 @@ public function testGetImage()
82105
->disableOriginalConstructor()
83106
->getMock();
84107

108+
$storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
109+
->disableOriginalConstructor()
110+
->getMock();
111+
112+
$this->appEmulationMock->expects($this->once())->method('startEnvironmentEmulation');
113+
$this->storeManagerMock->expects($this->atLeastOnce())->method('getStore')->willReturn($storeMock);
114+
$storeMock->expects($this->atLeastOnce())->method('getId')->willReturn(42);
85115
$this->imageBuilder->expects($this->once())
86116
->method('setProduct')
87117
->with($productMock)
@@ -97,10 +127,38 @@ public function testGetImage()
97127
$this->imageBuilder->expects($this->once())
98128
->method('create')
99129
->willReturn($imageMock);
130+
$this->appEmulationMock->expects($this->once())->method('stopEnvironmentEmulation');
100131

101132
$this->assertInstanceOf(
102133
\Magento\Catalog\Block\Product\Image::class,
103134
$this->_block->getImage($productMock, $imageId, $attributes)
104135
);
105136
}
137+
138+
/**
139+
* Test that app emulation stops when exception occurs.
140+
*
141+
* @expectedException \Exception
142+
* @expectedExceptionMessage Image Builder Exception
143+
*/
144+
public function testGetImageThrowsAnException()
145+
{
146+
$productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
147+
->disableOriginalConstructor()
148+
->getMock();
149+
$storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
150+
->disableOriginalConstructor()
151+
->getMock();
152+
153+
$this->appEmulationMock->expects($this->once())->method('startEnvironmentEmulation');
154+
$this->storeManagerMock->expects($this->atLeastOnce())->method('getStore')->willReturn($storeMock);
155+
$storeMock->expects($this->atLeastOnce())->method('getId')->willReturn(42);
156+
157+
$this->imageBuilder->expects($this->once())
158+
->method('setProduct')
159+
->willThrowException(new \Exception("Image Builder Exception"));
160+
$this->appEmulationMock->expects($this->once())->method('stopEnvironmentEmulation');
161+
162+
$this->_block->getImage($productMock, 1, []);
163+
}
106164
}

0 commit comments

Comments
 (0)