Skip to content

Commit 36a053f

Browse files
committed
magento-engcom/import-export-improvements#42: refactor the ImageTypeProcessor so that it will actually use the database to load the image information
1 parent 100e123 commit 36a053f

File tree

3 files changed

+75
-14
lines changed

3 files changed

+75
-14
lines changed

app/code/Magento/CatalogImportExport/Model/Import/Product.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,18 +1096,6 @@ protected function _initImagesArrayKeys()
10961096
{
10971097
$this->_imagesArrayKeys = $this->imageTypeProcessor->getImageTypes();
10981098
return $this;
1099-
$select = $this->_connection->select()->from(
1100-
$this->getResource()->getTable('eav_attribute'),
1101-
['code' => 'attribute_code']
1102-
)->where(
1103-
'frontend_input = :frontend_input'
1104-
);
1105-
$bind = [':frontend_input' => 'media_image'];
1106-
1107-
$this->_imagesArrayKeys = $this->_connection->fetchCol($select, $bind);
1108-
$this->_imagesArrayKeys[] = '_media_image';
1109-
1110-
return $this;
11111099
}
11121100

11131101
/**

app/code/Magento/CatalogImportExport/Model/Import/Product/ImageTypeProcessor.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,47 @@
55
*/
66
namespace Magento\CatalogImportExport\Model\Import\Product;
77

8+
use Magento\CatalogImportExport\Model\Import\Proxy\Product\ResourceModel;
9+
810
class ImageTypeProcessor
911
{
12+
/**
13+
* @var \Magento\CatalogImportExport\Model\Import\Proxy\Product\ResourceModelFactory
14+
*/
15+
private $resourceFactory;
16+
17+
/**
18+
* @param \Magento\CatalogImportExport\Model\Import\Proxy\Product\ResourceModelFactory $resourceFactory
19+
*/
20+
public function __construct(
21+
\Magento\CatalogImportExport\Model\Import\Proxy\Product\ResourceModelFactory $resourceFactory
22+
)
23+
{
24+
$this->resourceFactory = $resourceFactory;
25+
}
26+
1027
/**
1128
* @return array
1229
*/
1330
public function getImageTypes()
1431
{
15-
return ['image', 'small_image', 'thumbnail', 'swatch_image', '_media_image'];
32+
$imageKeys = [];
33+
/** @var ResourceModel $resource */
34+
$resource = $this->resourceFactory->create();
35+
$connection = $resource->getConnection();
36+
$select = $connection->select();
37+
$select->from(
38+
$resource->getTable('eav_attribute'),
39+
['code' => 'attribute_code']
40+
);
41+
$select->where(
42+
'frontend_input = :frontend_input'
43+
);
44+
$bind = [':frontend_input' => 'media_image'];
45+
46+
$imageKeys = $connection->fetchCol($select, $bind);
47+
$imageKeys[] = '_media_image';
48+
49+
return $imageKeys;
1650
}
1751
}

app/code/Magento/CatalogImportExport/Test/Unit/Model/Import/Product/ImageTypeProcessorTest.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,46 @@ class ImageTypeProcessorTest extends \PHPUnit\Framework\TestCase
1111
{
1212
public function testGetImageTypes()
1313
{
14-
$typeProcessor = new ImageTypeProcessor();
14+
$resourceFactory = $this->createPartialMock(
15+
\Magento\CatalogImportExport\Model\Import\Proxy\Product\ResourceModelFactory::class,
16+
['create']
17+
);
18+
19+
$resource = $this->getMockBuilder(\Magento\CatalogImportExport\Model\Import\Proxy\Product\ResourceModel::class)
20+
->disableOriginalConstructor()
21+
->setMethods(['getTable', 'getConnection'])
22+
->getMock();
23+
$resource->expects($this->once())
24+
->method('getTable')
25+
->with('eav_attribute')
26+
->willReturnArgument(0);
27+
$connection = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class);
28+
$resource->expects($this->any())
29+
->method('getConnection')
30+
->willReturn($connection);
31+
$resourceFactory->expects($this->once())
32+
->method('create')
33+
->willReturn($resource);
34+
35+
$selectMock = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
36+
->disableOriginalConstructor()
37+
->getMock();
38+
$selectMock->expects($this->once())
39+
->method('from')
40+
->with('eav_attribute', ['code' => 'attribute_code'], null)
41+
->willReturnSelf();
42+
$selectMock->expects($this->once())
43+
->method('where')
44+
->with('frontend_input = :frontend_input')
45+
->willReturnSelf();
46+
$connection->expects($this->any())
47+
->method('fetchCol')
48+
->willReturn(['image', 'small_image', 'thumbnail', 'swatch_image']);
49+
$connection->expects($this->any())
50+
->method('select')
51+
->willReturn($selectMock);
52+
53+
$typeProcessor = new ImageTypeProcessor($resourceFactory);
1554
$this->assertEquals(
1655
['image', 'small_image', 'thumbnail', 'swatch_image', '_media_image'],
1756
$typeProcessor->getImageTypes()

0 commit comments

Comments
 (0)