@@ -25,6 +25,16 @@ class StockTest extends \PHPUnit_Framework_TestCase
25
25
*/
26
26
protected $ imageBuilder ;
27
27
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
+
28
38
protected function setUp ()
29
39
{
30
40
$ objectManager = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
@@ -39,12 +49,25 @@ protected function setUp()
39
49
$ this ->imageBuilder = $ this ->getMockBuilder (\Magento \Catalog \Block \Product \ImageBuilder::class)
40
50
->disableOriginalConstructor ()
41
51
->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 );
42
63
43
64
$ this ->_block = $ objectManager ->getObject (
44
65
\Magento \ProductAlert \Block \Email \Stock::class,
45
66
[
46
67
'maliciousCode ' => $ this ->_filter ,
47
68
'imageBuilder ' => $ this ->imageBuilder ,
69
+ 'context ' => $ contextMock ,
70
+ 'appEmulation ' => $ this ->appEmulationMock
48
71
]
49
72
);
50
73
}
@@ -82,6 +105,13 @@ public function testGetImage()
82
105
->disableOriginalConstructor ()
83
106
->getMock ();
84
107
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 );
85
115
$ this ->imageBuilder ->expects ($ this ->once ())
86
116
->method ('setProduct ' )
87
117
->with ($ productMock )
@@ -97,10 +127,38 @@ public function testGetImage()
97
127
$ this ->imageBuilder ->expects ($ this ->once ())
98
128
->method ('create ' )
99
129
->willReturn ($ imageMock );
130
+ $ this ->appEmulationMock ->expects ($ this ->once ())->method ('stopEnvironmentEmulation ' );
100
131
101
132
$ this ->assertInstanceOf (
102
133
\Magento \Catalog \Block \Product \Image::class,
103
134
$ this ->_block ->getImage ($ productMock , $ imageId , $ attributes )
104
135
);
105
136
}
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
+ }
106
164
}
0 commit comments