7
7
8
8
use Magento \Framework \Filesystem ;
9
9
use Magento \Catalog \Block \Adminhtml \Product \Helper \Form \Gallery \Content ;
10
+ use Magento \Framework \Phrase ;
10
11
11
12
class ContentTest extends \PHPUnit_Framework_TestCase
12
13
{
@@ -40,14 +41,30 @@ class ContentTest extends \PHPUnit_Framework_TestCase
40
41
*/
41
42
protected $ galleryMock ;
42
43
44
+ /**
45
+ * @var \Magento\Catalog\Helper\Image|\PHPUnit_Framework_MockObject_MockObject
46
+ */
47
+ protected $ imageHelper ;
48
+
49
+ /**
50
+ * @var \Magento\Framework\View\Asset\Repository|\PHPUnit_Framework_MockObject_MockObject
51
+ */
52
+ protected $ assetRepo ;
53
+
43
54
/**
44
55
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
45
56
*/
46
57
protected $ objectManager ;
47
58
48
59
public function setUp ()
49
60
{
50
- $ this ->fileSystemMock = $ this ->getMock ('Magento\Framework\Filesystem ' , [], [], '' , false );
61
+ $ this ->fileSystemMock = $ this ->getMock (
62
+ 'Magento\Framework\Filesystem ' ,
63
+ ['stat ' , 'getDirectoryRead ' ],
64
+ [],
65
+ '' ,
66
+ false
67
+ );
51
68
$ this ->readMock = $ this ->getMock ('Magento\Framework\Filesystem\Directory\ReadInterface ' );
52
69
$ this ->galleryMock = $ this ->getMock (
53
70
'Magento\Catalog\Block\Adminhtml\Product\Helper\Form\Gallery ' ,
@@ -56,7 +73,13 @@ public function setUp()
56
73
'' ,
57
74
false
58
75
);
59
- $ this ->mediaConfigMock = $ this ->getMock ('Magento\Catalog\Model\Product\Media\Config ' , [], [], '' , false );
76
+ $ this ->mediaConfigMock = $ this ->getMock (
77
+ 'Magento\Catalog\Model\Product\Media\Config ' ,
78
+ ['getMediaUrl ' , 'getMediaPath ' ],
79
+ [],
80
+ '' ,
81
+ false
82
+ );
60
83
$ this ->jsonEncoderMock = $ this ->getMockBuilder ('Magento\Framework\Json\EncoderInterface ' )
61
84
->disableOriginalConstructor ()
62
85
->getMock ();
@@ -126,11 +149,10 @@ public function testGetImagesJson()
126
149
127
150
$ this ->content ->setElement ($ this ->galleryMock );
128
151
$ this ->galleryMock ->expects ($ this ->once ())->method ('getImages ' )->willReturn ($ images );
129
- $ this ->fileSystemMock ->expects ($ this ->exactly ( 2 ))->method ('getDirectoryRead ' )->willReturn ($ this ->readMock );
152
+ $ this ->fileSystemMock ->expects ($ this ->once ( ))->method ('getDirectoryRead ' )->willReturn ($ this ->readMock );
130
153
131
154
$ this ->mediaConfigMock ->expects ($ this ->any ())->method ('getMediaUrl ' )->willReturnMap ($ url );
132
155
$ this ->mediaConfigMock ->expects ($ this ->any ())->method ('getMediaPath ' )->willReturnMap ($ mediaPath );
133
-
134
156
$ this ->readMock ->expects ($ this ->any ())->method ('stat ' )->willReturnMap ($ sizeMap );
135
157
$ this ->jsonEncoderMock ->expects ($ this ->once ())->method ('encode ' )->willReturnCallback ('json_encode ' );
136
158
@@ -144,4 +166,92 @@ public function testGetImagesJsonWithoutImages()
144
166
145
167
$ this ->assertSame ('[] ' , $ this ->content ->getImagesJson ());
146
168
}
169
+
170
+ public function testGetImagesJsonWithException ()
171
+ {
172
+ $ this ->imageHelper = $ this ->getMockBuilder ('Magento\Catalog\Helper\Image ' )
173
+ ->disableOriginalConstructor ()
174
+ ->setMethods (['getDefaultPlaceholderUrl ' , 'getPlaceholder ' ])
175
+ ->getMock ();
176
+
177
+ $ this ->assetRepo = $ this ->getMockBuilder ('Magento\Framework\View\Asset\Repository ' )
178
+ ->disableOriginalConstructor ()
179
+ ->setMethods (['createAsset ' , 'getPath ' ])
180
+ ->getMock ();
181
+
182
+ $ this ->objectManager ->setBackwardCompatibleProperty (
183
+ $ this ->content ,
184
+ 'imageHelper ' ,
185
+ $ this ->imageHelper
186
+ );
187
+
188
+ $ this ->objectManager ->setBackwardCompatibleProperty (
189
+ $ this ->content ,
190
+ 'assetRepo ' ,
191
+ $ this ->assetRepo
192
+ );
193
+
194
+ $ placeholderUrl = 'url_to_the_placeholder/placeholder.jpg ' ;
195
+
196
+ $ sizePlaceholder = ['size ' => 399659 ];
197
+
198
+ $ imagesResult = [
199
+ [
200
+ 'value_id ' => '2 ' ,
201
+ 'file ' => 'file_2.jpg ' ,
202
+ 'media_type ' => 'image ' ,
203
+ 'position ' => '0 ' ,
204
+ 'url ' => 'url_to_the_placeholder/placeholder.jpg ' ,
205
+ 'size ' => 399659
206
+ ],
207
+ [
208
+ 'value_id ' => '1 ' ,
209
+ 'file ' => 'file_1.jpg ' ,
210
+ 'media_type ' => 'image ' ,
211
+ 'position ' => '1 ' ,
212
+ 'url ' => 'url_to_the_placeholder/placeholder.jpg ' ,
213
+ 'size ' => 399659
214
+ ]
215
+ ];
216
+
217
+ $ images = [
218
+ 'images ' => [
219
+ [
220
+ 'value_id ' => '1 ' ,
221
+ 'file ' => 'file_1.jpg ' ,
222
+ 'media_type ' => 'image ' ,
223
+ 'position ' => '1 '
224
+ ],
225
+ [
226
+ 'value_id ' => '2 ' ,
227
+ 'file ' => 'file_2.jpg ' ,
228
+ 'media_type ' => 'image ' ,
229
+ 'position ' => '0 '
230
+ ]
231
+ ]
232
+ ];
233
+
234
+ $ this ->content ->setElement ($ this ->galleryMock );
235
+ $ this ->galleryMock ->expects ($ this ->once ())->method ('getImages ' )->willReturn ($ images );
236
+ $ this ->fileSystemMock ->expects ($ this ->any ())->method ('getDirectoryRead ' )->willReturn ($ this ->readMock );
237
+ $ this ->mediaConfigMock ->expects ($ this ->any ())->method ('getMediaUrl ' );
238
+ $ this ->mediaConfigMock ->expects ($ this ->any ())->method ('getMediaPath ' );
239
+ $ this ->readMock ->expects ($ this ->any ())->method ('stat ' )->willReturnOnConsecutiveCalls (
240
+ $ this ->throwException (
241
+ new \Magento \Framework \Exception \FileSystemException (new \Magento \Framework \Phrase ('test ' ))
242
+ ),
243
+ $ sizePlaceholder ,
244
+ $ this ->throwException (
245
+ new \Magento \Framework \Exception \FileSystemException (new \Magento \Framework \Phrase ('test ' ))
246
+ ),
247
+ $ sizePlaceholder
248
+ );
249
+ $ this ->imageHelper ->expects ($ this ->any ())->method ('getDefaultPlaceholderUrl ' )->willReturn ($ placeholderUrl );
250
+ $ this ->imageHelper ->expects ($ this ->any ())->method ('getPlaceholder ' );
251
+ $ this ->assetRepo ->expects ($ this ->any ())->method ('createAsset ' )->willReturnSelf ();
252
+ $ this ->assetRepo ->expects ($ this ->any ())->method ('getPath ' );
253
+ $ this ->jsonEncoderMock ->expects ($ this ->once ())->method ('encode ' )->willReturnCallback ('json_encode ' );
254
+
255
+ $ this ->assertSame (json_encode ($ imagesResult ), $ this ->content ->getImagesJson ());
256
+ }
147
257
}
0 commit comments