Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions app/code/Magento/Catalog/Block/Product/ImageBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Magento\Catalog\Block\Product;

use Magento\Catalog\Helper\ImageFactory as HelperFactory;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Image\NotLoadInfoImageException;

class ImageBuilder
Expand All @@ -21,7 +22,7 @@ class ImageBuilder
protected $helperFactory;

/**
* @var \Magento\Catalog\Model\Product
* @var Product
*/
protected $product;

Expand Down Expand Up @@ -50,10 +51,10 @@ public function __construct(
/**
* Set product
*
* @param \Magento\Catalog\Model\Product $product
* @param Product $product
* @return $this
*/
public function setProduct(\Magento\Catalog\Model\Product $product)
public function setProduct(Product $product)
{
$this->product = $product;
return $this;
Expand All @@ -79,9 +80,7 @@ public function setImageId($imageId)
*/
public function setAttributes(array $attributes)
{
if ($attributes) {
$this->attributes = $attributes;
}
$this->attributes = $attributes;
return $this;
}

Expand Down
244 changes: 172 additions & 72 deletions app/code/Magento/Catalog/Test/Unit/Block/Product/ImageBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,43 @@
*/
namespace Magento\Catalog\Test\Unit\Block\Product;

use Magento\Catalog\Block\Product\ImageBuilder;
use Magento\Catalog\Block\Product\ImageFactory;
use Magento\Catalog\Helper\Image;
use Magento\Catalog\Model\Product;

class ImageBuilderTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Catalog\Block\Product\ImageBuilder
* @var ImageBuilder
*/
protected $model;
private $model;

/**
* @var \Magento\Catalog\Helper\ImageFactory|\PHPUnit_Framework_MockObject_MockObject
*/
protected $helperFactory;
private $helperFactory;

/**
* @var \Magento\Catalog\Block\Product\ImageFactory|\PHPUnit_Framework_MockObject_MockObject
* @var ImageFactory|\PHPUnit_Framework_MockObject_MockObject
*/
protected $imageFactory;
private $imageFactory;

protected function setUp()
{
$this->helperFactory = $this->getMockBuilder(\Magento\Catalog\Helper\ImageFactory::class)
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();

$this->imageFactory = $this->getMockBuilder(\Magento\Catalog\Block\Product\ImageFactory::class)
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();

$this->model = new \Magento\Catalog\Block\Product\ImageBuilder(
$this->helperFactory,
$this->imageFactory
);
$this->helperFactory = $this->createPartialMock(\Magento\Catalog\Helper\ImageFactory::class, ['create']);

$this->imageFactory = $this->createPartialMock(ImageFactory::class, ['create']);

$this->model = new ImageBuilder($this->helperFactory, $this->imageFactory);
}

public function testSetProduct()
{
$productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
->disableOriginalConstructor()
->getMock();
$productMock = $this->createMock(Product::class);

$this->assertInstanceOf(
\Magento\Catalog\Block\Product\ImageBuilder::class,
ImageBuilder::class,
$this->model->setProduct($productMock)
);
}
Expand All @@ -57,7 +51,7 @@ public function testSetImageId()
$imageId = 'test_image_id';

$this->assertInstanceOf(
\Magento\Catalog\Block\Product\ImageBuilder::class,
ImageBuilder::class,
$this->model->setImageId($imageId)
);
}
Expand All @@ -68,7 +62,7 @@ public function testSetAttributes()
'name' => 'value',
];
$this->assertInstanceOf(
\Magento\Catalog\Block\Product\ImageBuilder::class,
ImageBuilder::class,
$this->model->setAttributes($attributes)
);
}
Expand All @@ -81,13 +75,9 @@ public function testCreate($data, $expected)
{
$imageId = 'test_image_id';

$productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
->disableOriginalConstructor()
->getMock();
$productMock = $this->createMock(Product::class);

$helperMock = $this->getMockBuilder(\Magento\Catalog\Helper\Image::class)
->disableOriginalConstructor()
->getMock();
$helperMock = $this->createMock(Image::class);
$helperMock->expects($this->once())
->method('init')
->with($productMock, $imageId)
Expand Down Expand Up @@ -116,9 +106,7 @@ public function testCreate($data, $expected)
->method('create')
->willReturn($helperMock);

$imageMock = $this->getMockBuilder(\Magento\Catalog\Block\Product\Image::class)
->disableOriginalConstructor()
->getMock();
$imageMock = $this->createMock(\Magento\Catalog\Block\Product\Image::class);

$this->imageFactory->expects($this->once())
->method('create')
Expand All @@ -131,61 +119,173 @@ public function testCreate($data, $expected)
$this->assertInstanceOf(\Magento\Catalog\Block\Product\Image::class, $this->model->create());
}

/**
* Check if custom attributes will be overridden when builder used few times
* @param array $data
* @dataProvider createMultipleCallsDataProvider
*/
public function testCreateMultipleCalls($data)
{
list ($firstCall, $secondCall) = array_values($data);

$imageId = 'test_image_id';

$productMock = $this->createMock(Product::class);

$helperMock = $this->createMock(Image::class);
$helperMock->expects($this->exactly(2))
->method('init')
->with($productMock, $imageId)
->willReturnSelf();

$helperMock->expects($this->exactly(2))
->method('getFrame')
->willReturnOnConsecutiveCalls($firstCall['data']['frame'], $secondCall['data']['frame']);
$helperMock->expects($this->exactly(2))
->method('getUrl')
->willReturnOnConsecutiveCalls($firstCall['data']['url'], $secondCall['data']['url']);
$helperMock->expects($this->exactly(4))
->method('getWidth')
->willReturnOnConsecutiveCalls(
$firstCall['data']['width'],
$firstCall['data']['width'],
$secondCall['data']['width'],
$secondCall['data']['width']
);
$helperMock->expects($this->exactly(4))
->method('getHeight')
->willReturnOnConsecutiveCalls(
$firstCall['data']['height'],
$firstCall['data']['height'],
$secondCall['data']['height'],
$secondCall['data']['height']
);
$helperMock->expects($this->exactly(2))
->method('getLabel')
->willReturnOnConsecutiveCalls($firstCall['data']['label'], $secondCall['data']['label']);
$helperMock->expects($this->exactly(2))
->method('getResizedImageInfo')
->willReturnOnConsecutiveCalls($firstCall['data']['imagesize'], $secondCall['data']['imagesize']);
$this->helperFactory->expects($this->exactly(2))
->method('create')
->willReturn($helperMock);

$imageMock = $this->createMock(\Magento\Catalog\Block\Product\Image::class);

$this->imageFactory->expects($this->at(0))
->method('create')
->with($firstCall['expected'])
->willReturn($imageMock);

$this->imageFactory->expects($this->at(1))
->method('create')
->with($secondCall['expected'])
->willReturn($imageMock);

$this->model->setProduct($productMock);
$this->model->setImageId($imageId);
$this->model->setAttributes($firstCall['data']['custom_attributes']);

$this->assertInstanceOf(\Magento\Catalog\Block\Product\Image::class, $this->model->create());

$this->model->setProduct($productMock);
$this->model->setImageId($imageId);
$this->model->setAttributes($secondCall['data']['custom_attributes']);
$this->assertInstanceOf(\Magento\Catalog\Block\Product\Image::class, $this->model->create());
}

/**
* @return array
*/
public function createDataProvider(): array
{
return [
$this->getTestDataWithoutAttributes(),
$this->getTestDataWithAttributes(),
];
}

/**
* @return array
*/
public function createDataProvider()
public function createMultipleCallsDataProvider(): array
{
return [
[
[
'without_attributes' => $this->getTestDataWithoutAttributes(),
'with_attributes' => $this->getTestDataWithAttributes(),
],
],
[
[
'with_attributes' => $this->getTestDataWithAttributes(),
'without_attributes' => $this->getTestDataWithoutAttributes(),
],
],
];
}

/**
* @return array
*/
private function getTestDataWithoutAttributes(): array
{
return [
'data' => [
'frame' => 0,
'url' => 'test_url_1',
'width' => 100,
'height' => 100,
'label' => 'test_label',
'custom_attributes' => [],
'imagesize' => [100, 100],
],
'expected' => [
'data' => [
'frame' => 0,
'url' => 'test_url_1',
'template' => 'Magento_Catalog::product/image_with_borders.phtml',
'image_url' => 'test_url_1',
'width' => 100,
'height' => 100,
'label' => 'test_label',
'custom_attributes' => [],
'imagesize' => [100, 100],
'ratio' => 1,
'custom_attributes' => '',
'resized_image_width' => 100,
'resized_image_height' => 100,
],
'expected' => [
'data' => [
'template' => 'Magento_Catalog::product/image_with_borders.phtml',
'image_url' => 'test_url_1',
'width' => 100,
'height' => 100,
'label' => 'test_label',
'ratio' => 1,
'custom_attributes' => '',
'resized_image_width' => 100,
'resized_image_height' => 100,
],
],
];
}

/**
* @return array
*/
private function getTestDataWithAttributes(): array
{
return [
'data' => [
'frame' => 1,
'url' => 'test_url_2',
'width' => 100,
'height' => 50,
'label' => 'test_label_2',
'custom_attributes' => [
'name_1' => 'value_1',
'name_2' => 'value_2',
],
'imagesize' => [120, 70],
],
[
'expected' => [
'data' => [
'frame' => 1,
'url' => 'test_url_2',
'template' => 'Magento_Catalog::product/image.phtml',
'image_url' => 'test_url_2',
'width' => 100,
'height' => 50,
'label' => 'test_label_2',
'custom_attributes' => [
'name_1' => 'value_1',
'name_2' => 'value_2',
],
'imagesize' => [120, 70],
],
'expected' => [
'data' => [
'template' => 'Magento_Catalog::product/image.phtml',
'image_url' => 'test_url_2',
'width' => 100,
'height' => 50,
'label' => 'test_label_2',
'ratio' => 0.5,
'custom_attributes' => 'name_1="value_1" name_2="value_2"',
'resized_image_width' => 120,
'resized_image_height' => 70,
],
'ratio' => 0.5,
'custom_attributes' => 'name_1="value_1" name_2="value_2"',
'resized_image_width' => 120,
'resized_image_height' => 70,
],
],
];
Expand Down