From 36a053f2b3fc6016e7089f6947c670204b46ba43 Mon Sep 17 00:00:00 2001 From: David Manners Date: Tue, 16 Jan 2018 14:56:21 +0000 Subject: [PATCH] magento-engcom/import-export-improvements#42: refactor the ImageTypeProcessor so that it will actually use the database to load the image information --- .../Model/Import/Product.php | 12 ------ .../Import/Product/ImageTypeProcessor.php | 36 +++++++++++++++- .../Import/Product/ImageTypeProcessorTest.php | 41 ++++++++++++++++++- 3 files changed, 75 insertions(+), 14 deletions(-) diff --git a/app/code/Magento/CatalogImportExport/Model/Import/Product.php b/app/code/Magento/CatalogImportExport/Model/Import/Product.php index 3b307e2db9ab4..eb909a4dfe8bd 100644 --- a/app/code/Magento/CatalogImportExport/Model/Import/Product.php +++ b/app/code/Magento/CatalogImportExport/Model/Import/Product.php @@ -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; } /** diff --git a/app/code/Magento/CatalogImportExport/Model/Import/Product/ImageTypeProcessor.php b/app/code/Magento/CatalogImportExport/Model/Import/Product/ImageTypeProcessor.php index 435592226f030..ee5e18488d55c 100644 --- a/app/code/Magento/CatalogImportExport/Model/Import/Product/ImageTypeProcessor.php +++ b/app/code/Magento/CatalogImportExport/Model/Import/Product/ImageTypeProcessor.php @@ -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; } } diff --git a/app/code/Magento/CatalogImportExport/Test/Unit/Model/Import/Product/ImageTypeProcessorTest.php b/app/code/Magento/CatalogImportExport/Test/Unit/Model/Import/Product/ImageTypeProcessorTest.php index 6d76fbccccff0..fccfedd74fad2 100644 --- a/app/code/Magento/CatalogImportExport/Test/Unit/Model/Import/Product/ImageTypeProcessorTest.php +++ b/app/code/Magento/CatalogImportExport/Test/Unit/Model/Import/Product/ImageTypeProcessorTest.php @@ -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()