Skip to content

Commit 3381ec0

Browse files
author
Ben Robie
committed
Adding unit test coverage for Catalog/Block/Product/View/Gallery's getGalleryImagesJson() function
magento#9931
1 parent 836cb30 commit 3381ec0

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

app/code/Magento/Catalog/Test/Unit/Block/Product/View/GalleryTest.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,83 @@ protected function mockContext()
7777
->willReturn($this->registry);
7878
}
7979

80+
public function testGetGalleryImagesJsonWithLabel(){
81+
$this->prepareGetGalleryImagesJsonMocks();
82+
$json = $this->model->getGalleryImagesJson();
83+
$decodedJson = json_decode($json, true);
84+
$this->assertEquals('product_page_image_small_url', $decodedJson[0]['thumb']);
85+
$this->assertEquals('product_page_image_medium_url', $decodedJson[0]['img']);
86+
$this->assertEquals('product_page_image_large_url', $decodedJson[0]['full']);
87+
$this->assertEquals('test_label', $decodedJson[0]['caption']);
88+
$this->assertEquals('2', $decodedJson[0]['position']);
89+
$this->assertEquals(false, $decodedJson[0]['isMain']);
90+
$this->assertEquals('test_media_type', $decodedJson[0]['type']);
91+
$this->assertEquals('test_video_url', $decodedJson[0]['videoUrl']);
92+
}
93+
94+
public function testGetGalleryImagesJsonWithoutLabel(){
95+
$this->prepareGetGalleryImagesJsonMocks(false);
96+
$json = $this->model->getGalleryImagesJson();
97+
$decodedJson = json_decode($json, true);
98+
$this->assertEquals('test_product_name', $decodedJson[0]['caption']);
99+
100+
}
101+
102+
private function prepareGetGalleryImagesJsonMocks($hasLabel = true){
103+
$storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
104+
->disableOriginalConstructor()
105+
->getMock();
106+
107+
$productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
108+
->disableOriginalConstructor()
109+
->getMock();
110+
111+
$productTypeMock = $this->getMockBuilder(\Magento\Catalog\Model\Product\Type\AbstractType::class)
112+
->disableOriginalConstructor()
113+
->getMock();
114+
$productTypeMock->expects($this->any())
115+
->method('getStoreFilter')
116+
->with($productMock)
117+
->willReturn($storeMock);
118+
119+
$productMock->expects($this->any())
120+
->method('getTypeInstance')
121+
->willReturn($productTypeMock);
122+
$productMock->expects($this->any())
123+
->method('getMediaGalleryImages')
124+
->willReturn($this->getImagesCollectionWithPopulatedDataObject($hasLabel));
125+
$productMock->expects($this->any())
126+
->method('getName')
127+
->willReturn('test_product_name');
128+
129+
$this->registry->expects($this->any())
130+
->method('registry')
131+
->with('product')
132+
->willReturn($productMock);
133+
134+
$this->imageHelper->expects($this->any())
135+
->method('init')
136+
->willReturnMap([
137+
[$productMock, 'product_page_image_small', [], $this->imageHelper],
138+
[$productMock, 'product_page_image_medium_no_frame', [], $this->imageHelper],
139+
[$productMock, 'product_page_image_large_no_frame', [], $this->imageHelper],
140+
])
141+
->willReturnSelf();
142+
$this->imageHelper->expects($this->any())
143+
->method('setImageFile')
144+
->with('test_file')
145+
->willReturnSelf();
146+
$this->imageHelper->expects($this->at(2))
147+
->method('getUrl')
148+
->willReturn('product_page_image_small_url');
149+
$this->imageHelper->expects($this->at(5))
150+
->method('getUrl')
151+
->willReturn('product_page_image_medium_url');
152+
$this->imageHelper->expects($this->at(8))
153+
->method('getUrl')
154+
->willReturn('product_page_image_large_url');
155+
}
156+
80157
public function testGetGalleryImages()
81158
{
82159
$storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
@@ -154,4 +231,30 @@ private function getImagesCollection()
154231

155232
return $collectionMock;
156233
}
234+
235+
/**
236+
* @return \Magento\Framework\Data\Collection
237+
*/
238+
private function getImagesCollectionWithPopulatedDataObject($hasLabel)
239+
{
240+
$collectionMock = $this->getMockBuilder(\Magento\Framework\Data\Collection::class)
241+
->disableOriginalConstructor()
242+
->getMock();
243+
244+
$items = [
245+
new \Magento\Framework\DataObject([
246+
'file' => 'test_file',
247+
'label' => ($hasLabel ? 'test_label' : ''),
248+
'position' => '2',
249+
'media_type' => 'external-test_media_type',
250+
"video_url" => 'test_video_url'
251+
]),
252+
];
253+
254+
$collectionMock->expects($this->any())
255+
->method('getIterator')
256+
->willReturn(new \ArrayIterator($items));
257+
258+
return $collectionMock;
259+
}
157260
}

0 commit comments

Comments
 (0)