@@ -91,6 +91,89 @@ protected function mockContext()
91
91
->willReturn ($ this ->registry );
92
92
}
93
93
94
+ public function testGetGalleryImagesJsonWithLabel ()
95
+ {
96
+ $ this ->prepareGetGalleryImagesJsonMocks ();
97
+ $ json = $ this ->model ->getGalleryImagesJson ();
98
+ $ decodedJson = json_decode ($ json , true );
99
+ $ this ->assertEquals ('product_page_image_small_url ' , $ decodedJson [0 ]['thumb ' ]);
100
+ $ this ->assertEquals ('product_page_image_medium_url ' , $ decodedJson [0 ]['img ' ]);
101
+ $ this ->assertEquals ('product_page_image_large_url ' , $ decodedJson [0 ]['full ' ]);
102
+ $ this ->assertEquals ('test_label ' , $ decodedJson [0 ]['caption ' ]);
103
+ $ this ->assertEquals ('2 ' , $ decodedJson [0 ]['position ' ]);
104
+ $ this ->assertEquals (false , $ decodedJson [0 ]['isMain ' ]);
105
+ $ this ->assertEquals ('test_media_type ' , $ decodedJson [0 ]['type ' ]);
106
+ $ this ->assertEquals ('test_video_url ' , $ decodedJson [0 ]['videoUrl ' ]);
107
+ }
108
+
109
+ public function testGetGalleryImagesJsonWithoutLabel ()
110
+ {
111
+ $ this ->prepareGetGalleryImagesJsonMocks (false );
112
+ $ json = $ this ->model ->getGalleryImagesJson ();
113
+ $ decodedJson = json_decode ($ json , true );
114
+ $ this ->assertEquals ('test_product_name ' , $ decodedJson [0 ]['caption ' ]);
115
+ }
116
+
117
+ private function prepareGetGalleryImagesJsonMocks ($ hasLabel = true )
118
+ {
119
+ $ storeMock = $ this ->getMockBuilder (\Magento \Store \Model \Store::class)
120
+ ->disableOriginalConstructor ()
121
+ ->getMock ();
122
+
123
+ $ productMock = $ this ->getMockBuilder (\Magento \Catalog \Model \Product::class)
124
+ ->disableOriginalConstructor ()
125
+ ->getMock ();
126
+
127
+ $ productTypeMock = $ this ->getMockBuilder (\Magento \Catalog \Model \Product \Type \AbstractType::class)
128
+ ->disableOriginalConstructor ()
129
+ ->getMock ();
130
+ $ productTypeMock ->expects ($ this ->any ())
131
+ ->method ('getStoreFilter ' )
132
+ ->with ($ productMock )
133
+ ->willReturn ($ storeMock );
134
+
135
+ $ productMock ->expects ($ this ->any ())
136
+ ->method ('getTypeInstance ' )
137
+ ->willReturn ($ productTypeMock );
138
+ $ productMock ->expects ($ this ->any ())
139
+ ->method ('getMediaGalleryImages ' )
140
+ ->willReturn ($ this ->getImagesCollectionWithPopulatedDataObject ($ hasLabel ));
141
+ $ productMock ->expects ($ this ->any ())
142
+ ->method ('getName ' )
143
+ ->willReturn ('test_product_name ' );
144
+
145
+ $ this ->registry ->expects ($ this ->any ())
146
+ ->method ('registry ' )
147
+ ->with ('product ' )
148
+ ->willReturn ($ productMock );
149
+
150
+ $ this ->imageHelper ->expects ($ this ->any ())
151
+ ->method ('init ' )
152
+ ->willReturnMap ([
153
+ [$ productMock , 'product_page_image_small ' , [], $ this ->imageHelper ],
154
+ [$ productMock , 'product_page_image_medium_no_frame ' , [], $ this ->imageHelper ],
155
+ [$ productMock , 'product_page_image_large_no_frame ' , [], $ this ->imageHelper ],
156
+ ])
157
+ ->willReturnSelf ();
158
+ $ this ->imageHelper ->expects ($ this ->any ())
159
+ ->method ('setImageFile ' )
160
+ ->with ('test_file ' )
161
+ ->willReturnSelf ();
162
+ $ this ->imageHelper ->expects ($ this ->at (2 ))
163
+ ->method ('getUrl ' )
164
+ ->willReturn ('product_page_image_small_url ' );
165
+ $ this ->imageHelper ->expects ($ this ->at (5 ))
166
+ ->method ('getUrl ' )
167
+ ->willReturn ('product_page_image_medium_url ' );
168
+ $ this ->imageHelper ->expects ($ this ->at (8 ))
169
+ ->method ('getUrl ' )
170
+ ->willReturn ('product_page_image_large_url ' );
171
+
172
+ $ this ->galleryImagesConfigMock ->expects ($ this ->exactly (2 ))
173
+ ->method ('getItems ' )
174
+ ->willReturn ($ this ->getGalleryImagesConfigItems ());
175
+ }
176
+
94
177
public function testGetGalleryImages ()
95
178
{
96
179
$ storeMock = $ this ->getMockBuilder (\Magento \Store \Model \Store::class)
@@ -225,4 +308,30 @@ private function getGalleryImagesConfigItems()
225
308
])
226
309
];
227
310
}
311
+
312
+ /**
313
+ * @return \Magento\Framework\Data\Collection
314
+ */
315
+ private function getImagesCollectionWithPopulatedDataObject ($ hasLabel )
316
+ {
317
+ $ collectionMock = $ this ->getMockBuilder (\Magento \Framework \Data \Collection::class)
318
+ ->disableOriginalConstructor ()
319
+ ->getMock ();
320
+
321
+ $ items = [
322
+ new \Magento \Framework \DataObject ([
323
+ 'file ' => 'test_file ' ,
324
+ 'label ' => ($ hasLabel ? 'test_label ' : '' ),
325
+ 'position ' => '2 ' ,
326
+ 'media_type ' => 'external-test_media_type ' ,
327
+ "video_url " => 'test_video_url '
328
+ ]),
329
+ ];
330
+
331
+ $ collectionMock ->expects ($ this ->any ())
332
+ ->method ('getIterator ' )
333
+ ->willReturn (new \ArrayIterator ($ items ));
334
+
335
+ return $ collectionMock ;
336
+ }
228
337
}
0 commit comments