Skip to content

Commit

Permalink
ENGCOM-1389: MAGETWO-84124: Add bundle parent/child relationship duri…
Browse files Browse the repository at this point in the history
…ng import #104
  • Loading branch information
Stanislav Idolov committed May 29, 2018
2 parents 6c3eb8c + 0bcdcec commit 46f8fcf
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 5 deletions.
Expand Up @@ -411,6 +411,7 @@ public function saveData()
$this->populateExistingOptions();
$this->insertOptions();
$this->insertSelections();
$this->insertParentChildRelations();
$this->clear();
}
}
Expand Down Expand Up @@ -659,6 +660,32 @@ protected function insertSelections()
return $this;
}

/**
* Insert parent/child product relations
*
* @return \Magento\CatalogImportExport\Model\Import\Product\Type\AbstractType
*/
private function insertParentChildRelations()
{
foreach ($this->_cachedOptions as $productId => $options) {
$childIds = [];
foreach ($options as $option) {
foreach ($option['selections'] as $selection) {
if (!isset($selection['parent_product_id'])) {
if (!isset($this->_cachedSkuToProducts[$selection['sku']])) {
continue;
}
$childIds[] = $this->_cachedSkuToProducts[$selection['sku']];
}
}

$this->relationsDataSaver->saveProductRelations($productId, $childIds);
}
}

return $this;
}

/**
* Initialize attributes parameters for all attributes' sets.
*
Expand Down
Expand Up @@ -5,6 +5,9 @@
*/
namespace Magento\BundleImportExport\Model\Import\Product\Type\Bundle;

use Magento\Catalog\Model\ResourceModel\Product\Relation;
use Magento\Framework\App\ObjectManager;

/**
* A bundle product relations (options, selections, etc.) data saver.
*
Expand All @@ -17,13 +20,22 @@ class RelationsDataSaver
*/
private $resource;

/**
* @var Relation
*/
private $productRelation;

/**
* @param \Magento\Framework\App\ResourceConnection $resource
* @param Relation $productRelation
*/
public function __construct(
\Magento\Framework\App\ResourceConnection $resource
\Magento\Framework\App\ResourceConnection $resource,
Relation $productRelation = null
) {
$this->resource = $resource;
$this->resource = $resource;
$this->productRelation = $productRelation
?: ObjectManager::getInstance()->get(Relation::class);
}

/**
Expand Down Expand Up @@ -92,4 +104,17 @@ public function saveSelections(array $selections)
);
}
}

/**
* Saves given parent/child relations.
*
* @param int $parentId
* @param array $childIds
*
* @return void
*/
public function saveProductRelations($parentId, $childIds)
{
$this->productRelation->processRelations($parentId, $childIds);
}
}
Expand Up @@ -7,6 +7,7 @@
namespace Magento\BundleImportExport\Test\Unit\Model\Import\Product\Type\Bundle;

use Magento\BundleImportExport\Model\Import\Product\Type\Bundle\RelationsDataSaver;
use Magento\Catalog\Model\ResourceModel\Product\Relation;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Adapter\AdapterInterface;

Expand All @@ -30,6 +31,11 @@ class RelationsDataSaverTest extends \PHPUnit\Framework\TestCase
*/
private $connectionMock;

/**
* @var Relation|\PHPUnit_Framework_MockObject_MockObject
*/
private $productRelationMock;

protected function setUp()
{
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
Expand All @@ -39,12 +45,16 @@ protected function setUp()
$this->connectionMock = $this->getMockBuilder(AdapterInterface::class)
->disableOriginalConstructor()
->getMock();
$this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->connectionMock);

$this->productRelationMock = $this->getMockBuilder(Relation::class)
->disableOriginalConstructor()
->getMock();

$this->relationsDataSaver = $helper->getObject(
RelationsDataSaver::class,
[
'resource' => $this->resourceMock
'resource' => $this->resourceMock,
'productRelation' => $this->productRelationMock
]
);
}
Expand All @@ -53,7 +63,7 @@ public function testSaveOptions()
{
$options = [1, 2];
$table_name= 'catalog_product_bundle_option';

$this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->connectionMock);
$this->resourceMock->expects($this->once())
->method('getTableName')
->with('catalog_product_bundle_option')
Expand All @@ -78,6 +88,7 @@ public function testSaveOptionValues()
$optionsValues = [1, 2];
$table_name= 'catalog_product_bundle_option_value';

$this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->connectionMock);
$this->resourceMock->expects($this->once())
->method('getTableName')
->with('catalog_product_bundle_option_value')
Expand All @@ -98,6 +109,7 @@ public function testSaveSelections()
$selections = [1, 2];
$table_name= 'catalog_product_bundle_selection';

$this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->connectionMock);
$this->resourceMock->expects($this->once())
->method('getTableName')
->with('catalog_product_bundle_selection')
Expand All @@ -121,4 +133,16 @@ public function testSaveSelections()

$this->relationsDataSaver->saveSelections($selections);
}

public function testSaveProductRelations()
{
$parentId = 1;
$children = [2, 3];

$this->productRelationMock->expects($this->once())
->method('processRelations')
->with($parentId, $children);

$this->relationsDataSaver->saveProductRelations($parentId, $children);
}
}

0 comments on commit 46f8fcf

Please sign in to comment.