@@ -91,6 +91,89 @@ protected function setUp()
91
91
]);
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
$ productMock = $ this ->createMock (Product::class);
@@ -163,4 +246,30 @@ private function getGalleryImagesConfigItems()
163
246
])
164
247
];
165
248
}
249
+
250
+ /**
251
+ * @return \Magento\Framework\Data\Collection
252
+ */
253
+ private function getImagesCollectionWithPopulatedDataObject ($ hasLabel )
254
+ {
255
+ $ collectionMock = $ this ->getMockBuilder (\Magento \Framework \Data \Collection::class)
256
+ ->disableOriginalConstructor ()
257
+ ->getMock ();
258
+
259
+ $ items = [
260
+ new \Magento \Framework \DataObject ([
261
+ 'file ' => 'test_file ' ,
262
+ 'label ' => ($ hasLabel ? 'test_label ' : '' ),
263
+ 'position ' => '2 ' ,
264
+ 'media_type ' => 'external-test_media_type ' ,
265
+ "video_url " => 'test_video_url '
266
+ ]),
267
+ ];
268
+
269
+ $ collectionMock ->expects ($ this ->any ())
270
+ ->method ('getIterator ' )
271
+ ->willReturn (new \ArrayIterator ($ items ));
272
+
273
+ return $ collectionMock ;
274
+ }
166
275
}
0 commit comments