@@ -77,6 +77,85 @@ protected function mockContext()
77
77
->willReturn ($ this ->registry );
78
78
}
79
79
80
+ public function testGetGalleryImagesJsonWithLabel ()
81
+ {
82
+ $ this ->prepareGetGalleryImagesJsonMocks ();
83
+ $ json = $ this ->model ->getGalleryImagesJson ();
84
+ $ decodedJson = json_decode ($ json , true );
85
+ $ this ->assertEquals ('product_page_image_small_url ' , $ decodedJson [0 ]['thumb ' ]);
86
+ $ this ->assertEquals ('product_page_image_medium_url ' , $ decodedJson [0 ]['img ' ]);
87
+ $ this ->assertEquals ('product_page_image_large_url ' , $ decodedJson [0 ]['full ' ]);
88
+ $ this ->assertEquals ('test_label ' , $ decodedJson [0 ]['caption ' ]);
89
+ $ this ->assertEquals ('2 ' , $ decodedJson [0 ]['position ' ]);
90
+ $ this ->assertEquals (false , $ decodedJson [0 ]['isMain ' ]);
91
+ $ this ->assertEquals ('test_media_type ' , $ decodedJson [0 ]['type ' ]);
92
+ $ this ->assertEquals ('test_video_url ' , $ decodedJson [0 ]['videoUrl ' ]);
93
+ }
94
+
95
+ public function testGetGalleryImagesJsonWithoutLabel ()
96
+ {
97
+ $ this ->prepareGetGalleryImagesJsonMocks (false );
98
+ $ json = $ this ->model ->getGalleryImagesJson ();
99
+ $ decodedJson = json_decode ($ json , true );
100
+ $ this ->assertEquals ('test_product_name ' , $ decodedJson [0 ]['caption ' ]);
101
+ }
102
+
103
+ private function prepareGetGalleryImagesJsonMocks ($ hasLabel = true )
104
+ {
105
+ $ storeMock = $ this ->getMockBuilder (\Magento \Store \Model \Store::class)
106
+ ->disableOriginalConstructor ()
107
+ ->getMock ();
108
+
109
+ $ productMock = $ this ->getMockBuilder (\Magento \Catalog \Model \Product::class)
110
+ ->disableOriginalConstructor ()
111
+ ->getMock ();
112
+
113
+ $ productTypeMock = $ this ->getMockBuilder (\Magento \Catalog \Model \Product \Type \AbstractType::class)
114
+ ->disableOriginalConstructor ()
115
+ ->getMock ();
116
+ $ productTypeMock ->expects ($ this ->any ())
117
+ ->method ('getStoreFilter ' )
118
+ ->with ($ productMock )
119
+ ->willReturn ($ storeMock );
120
+
121
+ $ productMock ->expects ($ this ->any ())
122
+ ->method ('getTypeInstance ' )
123
+ ->willReturn ($ productTypeMock );
124
+ $ productMock ->expects ($ this ->any ())
125
+ ->method ('getMediaGalleryImages ' )
126
+ ->willReturn ($ this ->getImagesCollectionWithPopulatedDataObject ($ hasLabel ));
127
+ $ productMock ->expects ($ this ->any ())
128
+ ->method ('getName ' )
129
+ ->willReturn ('test_product_name ' );
130
+
131
+ $ this ->registry ->expects ($ this ->any ())
132
+ ->method ('registry ' )
133
+ ->with ('product ' )
134
+ ->willReturn ($ productMock );
135
+
136
+ $ this ->imageHelper ->expects ($ this ->any ())
137
+ ->method ('init ' )
138
+ ->willReturnMap ([
139
+ [$ productMock , 'product_page_image_small ' , [], $ this ->imageHelper ],
140
+ [$ productMock , 'product_page_image_medium_no_frame ' , [], $ this ->imageHelper ],
141
+ [$ productMock , 'product_page_image_large_no_frame ' , [], $ this ->imageHelper ],
142
+ ])
143
+ ->willReturnSelf ();
144
+ $ this ->imageHelper ->expects ($ this ->any ())
145
+ ->method ('setImageFile ' )
146
+ ->with ('test_file ' )
147
+ ->willReturnSelf ();
148
+ $ this ->imageHelper ->expects ($ this ->at (2 ))
149
+ ->method ('getUrl ' )
150
+ ->willReturn ('product_page_image_small_url ' );
151
+ $ this ->imageHelper ->expects ($ this ->at (5 ))
152
+ ->method ('getUrl ' )
153
+ ->willReturn ('product_page_image_medium_url ' );
154
+ $ this ->imageHelper ->expects ($ this ->at (8 ))
155
+ ->method ('getUrl ' )
156
+ ->willReturn ('product_page_image_large_url ' );
157
+ }
158
+
80
159
public function testGetGalleryImages ()
81
160
{
82
161
$ storeMock = $ this ->getMockBuilder (\Magento \Store \Model \Store::class)
@@ -154,4 +233,30 @@ private function getImagesCollection()
154
233
155
234
return $ collectionMock ;
156
235
}
236
+
237
+ /**
238
+ * @return \Magento\Framework\Data\Collection
239
+ */
240
+ private function getImagesCollectionWithPopulatedDataObject ($ hasLabel )
241
+ {
242
+ $ collectionMock = $ this ->getMockBuilder (\Magento \Framework \Data \Collection::class)
243
+ ->disableOriginalConstructor ()
244
+ ->getMock ();
245
+
246
+ $ items = [
247
+ new \Magento \Framework \DataObject ([
248
+ 'file ' => 'test_file ' ,
249
+ 'label ' => ($ hasLabel ? 'test_label ' : '' ),
250
+ 'position ' => '2 ' ,
251
+ 'media_type ' => 'external-test_media_type ' ,
252
+ "video_url " => 'test_video_url '
253
+ ]),
254
+ ];
255
+
256
+ $ collectionMock ->expects ($ this ->any ())
257
+ ->method ('getIterator ' )
258
+ ->willReturn (new \ArrayIterator ($ items ));
259
+
260
+ return $ collectionMock ;
261
+ }
157
262
}
0 commit comments