Skip to content

Commit 3b9261b

Browse files
David VerholenDavid Verholen
authored andcommitted
Fix Product View Gallery Block Unit Tests
1 parent c4894fa commit 3b9261b

File tree

2 files changed

+75
-17
lines changed

2 files changed

+75
-17
lines changed

app/code/Magento/Catalog/Block/Product/View/Gallery.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,16 @@ public function getGalleryImages()
6565
$images = $product->getMediaGalleryImages();
6666
if ($images instanceof \Magento\Framework\Data\Collection) {
6767
foreach ($images as $image) {
68-
/** @var DataObject $image */
69-
$this->galleryImagesConfig->walk(function (DataObject $imageConfig) use ($image, $product) {
68+
foreach($this->galleryImagesConfig->getItems() as $imageConfig) {
7069
/** @var Product $product */
71-
$productImage = $this->_imageHelper->init($product,
72-
$imageConfig['image_id'])
73-
->setImageFile($image->getData('file'))
74-
->getUrl();
75-
$image->setData($imageConfig->getData('data_object_key'), $productImage);
76-
});
70+
$image->setData(
71+
$imageConfig->getData('data_object_key'),
72+
$this->_imageHelper->init($product,
73+
$imageConfig['image_id'])
74+
->setImageFile($image->getData('file'))
75+
->getUrl()
76+
);
77+
}
7778
}
7879
}
7980

@@ -115,14 +116,12 @@ public function getGalleryImagesJson()
115116
'position' => $image->getData('position'),
116117
'isMain' => $this->isMainImage($image),
117118
]);
118-
$this->galleryImagesConfig->walk(function (
119-
DataObject $imageConfig
120-
) use ($imageItem, $image) {
119+
foreach($this->galleryImagesConfig->getItems() as $imageConfig) {
121120
$imageItem->setData(
122121
$imageConfig->getData('json_object_key'),
123122
$image->getData($imageConfig->getData('data_object_key'))
124123
);
125-
});
124+
}
126125
$imagesItems[] = $imageItem->toArray();
127126
}
128127
if (empty($imagesItems)) {

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

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ class GalleryTest extends \PHPUnit_Framework_TestCase
4040
/**
4141
* @var \Magento\Catalog\Model\Product\Gallery\ImagesConfigFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
4242
*/
43-
protected $imagesConfigMock;
43+
protected $imagesConfigFactoryMock;
44+
45+
/**
46+
* @var \Magento\Framework\Data\Collection|\PHPUnit_Framework_MockObject_MockObject
47+
*/
48+
protected $galleryImagesConfigMock;
4449

4550
protected function setUp()
4651
{
@@ -54,15 +59,13 @@ protected function setUp()
5459
->disableOriginalConstructor()
5560
->getMock();
5661

57-
$this->imagesConfigMock = $this->getMockBuilder('Magento\Catalog\Model\Product\Gallery\ImagesConfigFactoryInterface')
58-
->disableOriginalConstructor()
59-
->getMock();
62+
$this->imagesConfigFactoryMock = $this->getImagesConfigFactory();
6063

6164
$this->model = new \Magento\Catalog\Block\Product\View\Gallery(
6265
$this->context,
6366
$this->arrayUtils,
6467
$this->jsonEncoderMock,
65-
$this->imagesConfigMock
68+
$this->imagesConfigFactoryMock
6669
);
6770
}
6871

@@ -117,6 +120,10 @@ public function testGetGalleryImages()
117120
->with('product')
118121
->willReturn($productMock);
119122

123+
$this->galleryImagesConfigMock->expects($this->exactly(1))
124+
->method('getItems')
125+
->willReturn($this->getGalleryImagesConfigItems());
126+
120127
$this->imageHelper->expects($this->exactly(3))
121128
->method('init')
122129
->willReturnMap([
@@ -164,4 +171,56 @@ private function getImagesCollection()
164171

165172
return $collectionMock;
166173
}
174+
175+
/**
176+
* getImagesConfigFactory
177+
*
178+
* @return \Magento\Catalog\Model\Product\Gallery\ImagesConfigFactoryInterface
179+
*/
180+
private function getImagesConfigFactory()
181+
{
182+
$this->galleryImagesConfigMock = $this->getMockBuilder('\Magento\Framework\Data\Collection')
183+
->disableOriginalConstructor()
184+
->getMock();
185+
186+
$this->galleryImagesConfigMock->expects($this->any())
187+
->method('getIterator')
188+
->willReturn(new \ArrayIterator($this->getGalleryImagesConfigItems()));
189+
190+
$galleryImagesConfigFactoryMock = $this->getMockBuilder('Magento\Catalog\Model\Product\Gallery\ImagesConfigFactoryInterface')
191+
->disableOriginalConstructor()
192+
->getMock();
193+
194+
$galleryImagesConfigFactoryMock->expects($this->any())
195+
->method('create')
196+
->willReturn($this->galleryImagesConfigMock);
197+
198+
return $galleryImagesConfigFactoryMock;
199+
}
200+
201+
/**
202+
* getGalleryImagesConfigItems
203+
*
204+
* @return array
205+
*/
206+
private function getGalleryImagesConfigItems()
207+
{
208+
return [
209+
new \Magento\Framework\DataObject([
210+
'image_id' => 'product_page_image_small',
211+
'data_object_key' => 'small_image_url',
212+
'json_object_key' => 'thumb'
213+
]),
214+
new \Magento\Framework\DataObject([
215+
'image_id' => 'product_page_image_medium',
216+
'data_object_key' => 'medium_image_url',
217+
'json_object_key' => 'img'
218+
]),
219+
new \Magento\Framework\DataObject([
220+
'image_id' => 'product_page_image_large',
221+
'data_object_key' => 'large_image_url',
222+
'json_object_key' => 'full'
223+
])
224+
];
225+
}
167226
}

0 commit comments

Comments
 (0)