Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

#42 - Dynamically fill the image keys #94

Merged
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
28 changes: 25 additions & 3 deletions app/code/Magento/CatalogImportExport/Model/Import/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Magento\CatalogImportExport\Model\Import;

use Magento\Catalog\Model\Product\Visibility;
use Magento\CatalogImportExport\Model\Import\Product\ImageTypeProcessor;
use Magento\CatalogImportExport\Model\Import\Product\RowValidatorInterface as ValidatorInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\ObjectManager;
Expand Down Expand Up @@ -425,7 +426,7 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
*
* @var string[]
*/
protected $_imagesArrayKeys = ['image', 'small_image', 'thumbnail', 'swatch_image', '_media_image'];
protected $_imagesArrayKeys = [];

/**
* Permanent entity columns.
Expand Down Expand Up @@ -699,6 +700,11 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
*/
private $catalogConfig;

/**
* @var ImageTypeProcessor
*/
private $imageTypeProcessor;

/**
* @param \Magento\Framework\Json\Helper\Data $jsonHelper
* @param \Magento\ImportExport\Helper\Data $importExportData
Expand Down Expand Up @@ -738,6 +744,7 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
* @param array $data
* @param array $dateAttrCodes
* @param CatalogConfig $catalogConfig
* @param ImageTypeProcessor $imageTypeProcessor
* @throws \Magento\Framework\Exception\LocalizedException
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
Expand Down Expand Up @@ -781,7 +788,8 @@ public function __construct(
\Magento\Catalog\Model\Product\Url $productUrl,
array $data = [],
array $dateAttrCodes = [],
CatalogConfig $catalogConfig = null
CatalogConfig $catalogConfig = null,
ImageTypeProcessor $imageTypeProcessor = null
) {
$this->_eventManager = $eventManager;
$this->stockRegistry = $stockRegistry;
Expand Down Expand Up @@ -814,6 +822,8 @@ public function __construct(
$this->dateAttrCodes = array_merge($this->dateAttrCodes, $dateAttrCodes);
$this->catalogConfig = $catalogConfig ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(CatalogConfig::class);
$this->imageTypeProcessor = $imageTypeProcessor ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(ImageTypeProcessor::class);

parent::__construct(
$jsonHelper,
Expand All @@ -833,7 +843,8 @@ public function __construct(

$this->_initAttributeSets()
->_initTypeModels()
->_initSkus();
->_initSkus()
->initImagesArrayKeys();
$this->validator->init($this);
}

Expand Down Expand Up @@ -1076,6 +1087,17 @@ protected function _initSkus()
return $this;
}

/**
* Initialize image array keys.
*
* @return $this
*/
private function initImagesArrayKeys()
{
$this->_imagesArrayKeys = $this->imageTypeProcessor->getImageTypes();
return $this;
}

/**
* Initialize product type models.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CatalogImportExport\Model\Import\Product;

use Magento\CatalogImportExport\Model\Import\Proxy\Product\ResourceModel;

class ImageTypeProcessor
{
/**
* @var \Magento\CatalogImportExport\Model\Import\Proxy\Product\ResourceModelFactory
*/
private $resourceFactory;

/**
* @param \Magento\CatalogImportExport\Model\Import\Proxy\Product\ResourceModelFactory $resourceFactory
*/
public function __construct(
\Magento\CatalogImportExport\Model\Import\Proxy\Product\ResourceModelFactory $resourceFactory
) {
$this->resourceFactory = $resourceFactory;
}

/**
* @return array
*/
public function getImageTypes()
{
$imageKeys = [];
/** @var ResourceModel $resource */
$resource = $this->resourceFactory->create();
$connection = $resource->getConnection();
$select = $connection->select();
$select->from(
$resource->getTable('eav_attribute'),
['code' => 'attribute_code']
);
$select->where(
'frontend_input = :frontend_input'
);
$bind = [':frontend_input' => 'media_image'];

$imageKeys = $connection->fetchCol($select, $bind);
$imageKeys[] = '_media_image';

return $imageKeys;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CatalogImportExport\Test\Unit\Model\Import\Product;

use Magento\CatalogImportExport\Model\Import\Product\ImageTypeProcessor;

class ImageTypeProcessorTest extends \PHPUnit\Framework\TestCase
{
public function testGetImageTypes()
{
$resourceFactory = $this->createPartialMock(
\Magento\CatalogImportExport\Model\Import\Proxy\Product\ResourceModelFactory::class,
['create']
);

$resource = $this->getMockBuilder(\Magento\CatalogImportExport\Model\Import\Proxy\Product\ResourceModel::class)
->disableOriginalConstructor()
->setMethods(['getTable', 'getConnection'])
->getMock();
$resource->expects($this->once())
->method('getTable')
->with('eav_attribute')
->willReturnArgument(0);
$connection = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class);
$resource->expects($this->any())
->method('getConnection')
->willReturn($connection);
$resourceFactory->expects($this->once())
->method('create')
->willReturn($resource);

$selectMock = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
->disableOriginalConstructor()
->getMock();
$selectMock->expects($this->once())
->method('from')
->with('eav_attribute', ['code' => 'attribute_code'], null)
->willReturnSelf();
$selectMock->expects($this->once())
->method('where')
->with('frontend_input = :frontend_input')
->willReturnSelf();
$connection->expects($this->any())
->method('fetchCol')
->willReturn(['image', 'small_image', 'thumbnail', 'swatch_image']);
$connection->expects($this->any())
->method('select')
->willReturn($selectMock);

$typeProcessor = new ImageTypeProcessor($resourceFactory);
$this->assertEquals(
['image', 'small_image', 'thumbnail', 'swatch_image', '_media_image'],
$typeProcessor->getImageTypes()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace Magento\CatalogImportExport\Test\Unit\Model\Import;

use Magento\CatalogImportExport\Model\Import\Product\ImageTypeProcessor;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Stdlib\DateTime;
use Magento\ImportExport\Model\Import;
Expand Down Expand Up @@ -159,6 +160,9 @@ class ProductTest extends \Magento\ImportExport\Test\Unit\Model\Import\AbstractI
/** @var \Magento\Catalog\Model\Product\Url|\PHPUnit_Framework_MockObject_MockObject*/
protected $productUrl;

/** @var ImageTypeProcessor|\PHPUnit_Framework_MockObject_MockObject */
protected $imageTypeProcessor;

/**
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
Expand Down Expand Up @@ -326,11 +330,16 @@ protected function setUp()

$this->data = [];

$this->imageTypeProcessor = $this->getMockBuilder(ImageTypeProcessor::class)
->disableOriginalConstructor()
->getMock();

$this->_objectConstructor()
->_parentObjectConstructor()
->_initAttributeSets()
->_initTypeModels()
->_initSkus();
->_initSkus()
->_initImagesArrayKeys();

$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);

Expand Down Expand Up @@ -373,7 +382,8 @@ protected function setUp()
'taxClassProcessor' => $this->taxClassProcessor,
'scopeConfig' => $this->scopeConfig,
'productUrl' => $this->productUrl,
'data' => $this->data
'data' => $this->data,
'imageTypeProcessor' => $this->imageTypeProcessor
]
);
$reflection = new \ReflectionClass(\Magento\CatalogImportExport\Model\Import\Product::class);
Expand Down Expand Up @@ -496,6 +506,14 @@ protected function _initSkus()
return $this;
}

protected function _initImagesArrayKeys()
{
$this->imageTypeProcessor->expects($this->once())->method('getImageTypes')->willReturn(
['image', 'small_image', 'thumbnail', 'swatch_image', '_media_image']
);
return $this;
}

public function testSaveProductAttributes()
{
$testTable = 'test_table';
Expand Down