Skip to content

Commit

Permalink
magento-engcom/import-export-improvements#42: refactor the ImageTypeP…
Browse files Browse the repository at this point in the history
…rocessor so that it will actually use the database to load the image information
  • Loading branch information
dmanners committed Jan 16, 2018
1 parent 100e123 commit 36a053f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 14 deletions.
12 changes: 0 additions & 12 deletions app/code/Magento/CatalogImportExport/Model/Import/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -1096,18 +1096,6 @@ protected function _initImagesArrayKeys()
{
$this->_imagesArrayKeys = $this->imageTypeProcessor->getImageTypes();
return $this;
$select = $this->_connection->select()->from(
$this->getResource()->getTable('eav_attribute'),
['code' => 'attribute_code']
)->where(
'frontend_input = :frontend_input'
);
$bind = [':frontend_input' => 'media_image'];

$this->_imagesArrayKeys = $this->_connection->fetchCol($select, $bind);
$this->_imagesArrayKeys[] = '_media_image';

return $this;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,47 @@
*/
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()
{
return ['image', 'small_image', 'thumbnail', 'swatch_image', '_media_image'];
$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
Expand Up @@ -11,7 +11,46 @@ class ImageTypeProcessorTest extends \PHPUnit\Framework\TestCase
{
public function testGetImageTypes()
{
$typeProcessor = new ImageTypeProcessor();
$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()
Expand Down

0 comments on commit 36a053f

Please sign in to comment.