123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\ConfigurableProduct\Test\Unit\Helper;
- use Magento\Catalog\Model\Product\Image\UrlBuilder;
- use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
- class DataTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\ConfigurableProduct\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $_model;
- /**
- * @var \Magento\Catalog\Helper\Image|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $_imageHelperMock;
- /**
- * @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $_productMock;
- /**
- * @var UrlBuilder|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $imageUrlBuilder;
- protected function setUp()
- {
- $objectManager = new ObjectManager($this);
- $this->imageUrlBuilder = $this->getMockBuilder(UrlBuilder::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->_imageHelperMock = $this->createMock(\Magento\Catalog\Helper\Image::class);
- $this->_productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
- $this->_model = $objectManager->getObject(
- \Magento\ConfigurableProduct\Helper\Data::class,
- [
- '_imageHelper' => $this->_imageHelperMock
- ]
- );
- $objectManager->setBackwardCompatibleProperty($this->_model, 'imageUrlBuilder', $this->imageUrlBuilder);
- }
- public function testGetAllowAttributes()
- {
- $typeInstanceMock = $this->createMock(\Magento\ConfigurableProduct\Model\Product\Type\Configurable::class);
- $typeInstanceMock->expects($this->once())
- ->method('getConfigurableAttributes')
- ->with($this->_productMock);
- $this->_productMock->expects($this->once())
- ->method('getTypeInstance')
- ->will($this->returnValue($typeInstanceMock));
- $this->_model->getAllowAttributes($this->_productMock);
- }
- /**
- * @param array $expected
- * @param array $data
- * @dataProvider getOptionsDataProvider
- */
- public function testGetOptions(array $expected, array $data)
- {
- if (count($data['allowed_products'])) {
- $imageHelper1 = $this->getMockBuilder(\Magento\Catalog\Helper\Image::class)
- ->disableOriginalConstructor()
- ->getMock();
- $imageHelper1->expects($this->any())
- ->method('getUrl')
- ->willReturn('http://example.com/base_img_url');
- $imageHelper2 = $this->getMockBuilder(\Magento\Catalog\Helper\Image::class)
- ->disableOriginalConstructor()
- ->getMock();
- $imageHelper2->expects($this->any())
- ->method('getUrl')
- ->willReturn('http://example.com/base_img_url_2');
- $this->_imageHelperMock->expects($this->any())
- ->method('init')
- ->willReturnMap([
- [$data['current_product_mock'], 'product_page_image_large', [], $imageHelper1],
- [$data['allowed_products'][0], 'product_page_image_large', [], $imageHelper1],
- [$data['allowed_products'][1], 'product_page_image_large', [], $imageHelper2],
- ]);
- }
- $this->assertEquals(
- $expected,
- $this->_model->getOptions($data['current_product_mock'], $data['allowed_products'])
- );
- }
- /**
- * @return array
- */
- public function getOptionsDataProvider()
- {
- $currentProductMock = $this->createPartialMock(
- \Magento\Catalog\Model\Product::class,
- ['getTypeInstance', '__wakeup']
- );
- $provider = [];
- $provider[] = [
- [],
- [
- 'allowed_products' => [],
- 'current_product_mock' => $currentProductMock,
- ],
- ];
- $attributesCount = 3;
- $attributes = [];
- for ($i = 1; $i < $attributesCount; $i++) {
- $attribute = $this->createPartialMock(\Magento\Framework\DataObject::class, ['getProductAttribute']);
- $productAttribute = $this->createPartialMock(
- \Magento\Framework\DataObject::class,
- ['getId', 'getAttributeCode']
- );
- $productAttribute->expects($this->any())
- ->method('getId')
- ->will($this->returnValue('attribute_id_' . $i));
- $productAttribute->expects($this->any())
- ->method('getAttributeCode')
- ->will($this->returnValue('attribute_code_' . $i));
- $attribute->expects($this->any())
- ->method('getProductAttribute')
- ->will($this->returnValue($productAttribute));
- $attributes[] = $attribute;
- }
- $typeInstanceMock = $this->createMock(\Magento\ConfigurableProduct\Model\Product\Type\Configurable::class);
- $typeInstanceMock->expects($this->any())
- ->method('getConfigurableAttributes')
- ->will($this->returnValue($attributes));
- $currentProductMock->expects($this->any())
- ->method('getTypeInstance')
- ->will($this->returnValue($typeInstanceMock));
- $allowedProducts = [];
- for ($i = 1; $i <= 2; $i++) {
- $productMock = $this->createPartialMock(
- \Magento\Catalog\Model\Product::class,
- ['getData', 'getImage', 'getId', '__wakeup', 'getMediaGalleryImages']
- );
- $productMock->expects($this->any())
- ->method('getData')
- ->will($this->returnCallback([$this, 'getDataCallback']));
- $productMock->expects($this->any())
- ->method('getId')
- ->will($this->returnValue('product_id_' . $i));
- if ($i == 2) {
- $productMock->expects($this->any())
- ->method('getImage')
- ->will($this->returnValue(true));
- }
- $allowedProducts[] = $productMock;
- }
- $provider[] = [
- [
- 'attribute_id_1' => [
- 'attribute_code_value_1' => ['product_id_1', 'product_id_2'],
- ],
- 'index' => [
- 'product_id_1' => [
- 'attribute_id_1' => 'attribute_code_value_1',
- 'attribute_id_2' => 'attribute_code_value_2'
- ],
- 'product_id_2' => [
- 'attribute_id_1' => 'attribute_code_value_1',
- 'attribute_id_2' => 'attribute_code_value_2'
- ]
- ],
- 'attribute_id_2' => [
- 'attribute_code_value_2' => ['product_id_1', 'product_id_2'],
- ],
- ],
- [
- 'allowed_products' => $allowedProducts,
- 'current_product_mock' => $currentProductMock,
- ],
- ];
- return $provider;
- }
- /**
- * @param string $key
- * @return string
- */
- public function getDataCallback($key)
- {
- $map = [];
- for ($k = 1; $k < 3; $k++) {
- $map['attribute_code_' . $k] = 'attribute_code_value_' . $k;
- }
- return $map[$key];
- }
- public function testGetGalleryImages()
- {
- $productMock = $this->getMockBuilder(\Magento\Catalog\Api\Data\ProductInterface::class)
- ->setMethods(['getMediaGalleryImages'])
- ->getMockForAbstractClass();
- $productMock->expects($this->once())
- ->method('getMediaGalleryImages')
- ->willReturn($this->getImagesCollection());
- $this->imageUrlBuilder->expects($this->exactly(3))
- ->method('getUrl')
- ->withConsecutive(
- [
- self::identicalTo('test_file'),
- self::identicalTo('product_page_image_small')
- ],
- [
- self::identicalTo('test_file'),
- self::identicalTo('product_page_image_medium')
- ],
- [
- self::identicalTo('test_file'),
- self::identicalTo('product_page_image_large')
- ]
- )
- ->will(self::onConsecutiveCalls(
- 'testSmallImageUrl',
- 'testMediumImageUrl',
- 'testLargeImageUrl'
- ));
- $this->_imageHelperMock->expects(self::never())
- ->method('setImageFile')
- ->with('test_file')
- ->willReturnSelf();
- $this->_imageHelperMock->expects(self::never())
- ->method('getUrl')
- ->willReturn('product_page_image_small_url');
- $this->_imageHelperMock->expects(self::never())
- ->method('getUrl')
- ->willReturn('product_page_image_medium_url');
- $this->_imageHelperMock->expects(self::never())
- ->method('getUrl')
- ->willReturn('product_page_image_large_url');
- $this->assertInstanceOf(
- \Magento\Framework\Data\Collection::class,
- $this->_model->getGalleryImages($productMock)
- );
- }
- /**
- * @return \Magento\Framework\Data\Collection
- */
- private function getImagesCollection()
- {
- $collectionMock = $this->getMockBuilder(\Magento\Framework\Data\Collection::class)
- ->disableOriginalConstructor()
- ->getMock();
- $items = [
- new \Magento\Framework\DataObject([
- 'file' => 'test_file'
- ]),
- ];
- $collectionMock->expects($this->any())
- ->method('getIterator')
- ->willReturn(new \ArrayIterator($items));
- return $collectionMock;
- }
- }
|