Skip to content

Commit

Permalink
magento-engcom/import-export-improvements#42: create an image type pr…
Browse files Browse the repository at this point in the history
…ocessor to enable more modular testing
  • Loading branch information
dmanners committed Jan 16, 2018
1 parent 8d3a237 commit 100e123
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
14 changes: 13 additions & 1 deletion 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 @@ -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 Down Expand Up @@ -1084,6 +1094,8 @@ protected function _initSkus()
*/
protected function _initImagesArrayKeys()
{
$this->_imagesArrayKeys = $this->imageTypeProcessor->getImageTypes();
return $this;
$select = $this->_connection->select()->from(
$this->getResource()->getTable('eav_attribute'),
['code' => 'attribute_code']
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CatalogImportExport\Model\Import\Product;

class ImageTypeProcessor
{
/**
* @return array
*/
public function getImageTypes()
{
return ['image', 'small_image', 'thumbnail', 'swatch_image', '_media_image'];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?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()
{
$typeProcessor = new ImageTypeProcessor();
$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

0 comments on commit 100e123

Please sign in to comment.