Skip to content

Commit 785e560

Browse files
author
Gurzhyi, Andrii
committed
MAGETWO-48227: Fix API/Integration tests
- Fixed tests
1 parent f6597f9 commit 785e560

File tree

8 files changed

+143
-73
lines changed

8 files changed

+143
-73
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\ConfigurableProduct\Helper\Product\Options;
7+
8+
use Magento\Catalog\Api\Data\ProductInterface;
9+
use Magento\ConfigurableProduct\Api\Data\OptionInterface;
10+
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
11+
use Magento\ConfigurableProduct\Api\Data\OptionValueInterfaceFactory;
12+
13+
/**
14+
* Class Loader
15+
*/
16+
class Loader
17+
{
18+
/**
19+
* @var OptionValueInterfaceFactory
20+
*/
21+
private $optionValueFactory;
22+
23+
/**
24+
* ReadHandler constructor
25+
*
26+
* @param OptionValueInterfaceFactory $optionValueFactory
27+
*/
28+
public function __construct(OptionValueInterfaceFactory $optionValueFactory)
29+
{
30+
$this->optionValueFactory = $optionValueFactory;
31+
}
32+
33+
/**
34+
* @param ProductInterface $product
35+
* @return OptionInterface[]
36+
*/
37+
public function load(ProductInterface $product)
38+
{
39+
$options = [];
40+
/** @var Configurable $typeInstance */
41+
$typeInstance = $product->getTypeInstance();
42+
$attributeCollection = $typeInstance->getConfigurableAttributes($product);
43+
44+
foreach ($attributeCollection as $attribute) {
45+
$values = [];
46+
$attributeOptions = $attribute->getOptions();
47+
if (is_array($attributeOptions)) {
48+
foreach ($attributeOptions as $option) {
49+
/** @var \Magento\ConfigurableProduct\Api\Data\OptionValueInterface $value */
50+
$value = $this->optionValueFactory->create();
51+
$value->setValueIndex($option['value_index']);
52+
$values[] = $value;
53+
}
54+
}
55+
$attribute->setValues($values);
56+
$options[] = $attribute;
57+
}
58+
59+
return $options;
60+
}
61+
}

app/code/Magento/ConfigurableProduct/Model/OptionRepository.php

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Catalog\Api\Data\ProductInterface;
1010
use Magento\Catalog\Model\Product;
1111
use Magento\ConfigurableProduct\Api\Data\OptionInterface;
12+
use Magento\ConfigurableProduct\Helper\Product\Options\Loader;
1213
use Magento\Framework\Exception\NoSuchEntityException;
1314
use Magento\Framework\Exception\StateException;
1415
use Magento\Framework\Exception\InputException;
@@ -62,11 +63,17 @@ class OptionRepository implements \Magento\ConfigurableProduct\Api\OptionReposit
6263
* @var \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable
6364
*/
6465
private $configurableTypeResource;
66+
6567
/**
6668
* @var MetadataPool
6769
*/
6870
private $metadataPool;
6971

72+
/**
73+
* @var Loader
74+
*/
75+
private $optionLoader;
76+
7077
/**
7178
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
7279
* @param \Magento\ConfigurableProduct\Api\Data\OptionValueInterfaceFactory $optionValueFactory
@@ -77,6 +84,7 @@ class OptionRepository implements \Magento\ConfigurableProduct\Api\OptionReposit
7784
* @param ConfigurableType\AttributeFactory $configurableAttributeFactory
7885
* @param \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable $configurableTypeResource
7986
* @param MetadataPool $metadataPool
87+
* @param Loader $optionLoader
8088
*/
8189
public function __construct(
8290
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
@@ -87,7 +95,8 @@ public function __construct(
8795
\Magento\Catalog\Api\ProductAttributeRepositoryInterface $productAttributeRepository,
8896
\Magento\ConfigurableProduct\Model\Product\Type\Configurable\AttributeFactory $configurableAttributeFactory,
8997
\Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable $configurableTypeResource,
90-
MetadataPool $metadataPool
98+
MetadataPool $metadataPool,
99+
Loader $optionLoader
91100
) {
92101
$this->productRepository = $productRepository;
93102
$this->optionValueFactory = $optionValueFactory;
@@ -98,6 +107,7 @@ public function __construct(
98107
$this->configurableAttributeFactory = $configurableAttributeFactory;
99108
$this->configurableTypeResource = $configurableTypeResource;
100109
$this->metadataPool = $metadataPool;
110+
$this->optionLoader = $optionLoader;
101111
}
102112

103113
/**
@@ -107,15 +117,13 @@ public function get($sku, $id)
107117
{
108118
$product = $this->getProduct($sku);
109119

110-
$extensionAttribute = $product->getExtensionAttributes();
111-
if ($extensionAttribute !== null) {
112-
$options = $extensionAttribute->getConfigurableProductOptions();
113-
foreach ($options as $option) {
114-
if ($option->getId() == $id) {
115-
return $option;
116-
}
120+
$options = $this->optionLoader->load($product);
121+
foreach ($options as $option) {
122+
if ($option->getId() == $id) {
123+
return $option;
117124
}
118125
}
126+
119127
throw new NoSuchEntityException(__('Requested option doesn\'t exist: %1', $id));
120128
}
121129

@@ -124,22 +132,19 @@ public function get($sku, $id)
124132
*/
125133
public function getList($sku)
126134
{
127-
$options = [];
128135
$product = $this->getProduct($sku);
129136

130-
$extensionAttribute = $product->getExtensionAttributes();
131-
if ($extensionAttribute !== null) {
132-
$options = $extensionAttribute->getConfigurableProductOptions();
133-
}
134-
return $options;
137+
return $this->optionLoader->load($product);
135138
}
136139

137140
/**
138141
* {@inheritdoc}
139142
*/
140143
public function delete(OptionInterface $option)
141144
{
142-
$product = $this->getProductById($option->getProductId());
145+
$entityId = $this->configurableTypeResource->getEntityIdByAttribute($option);
146+
$product = $this->getProductById($entityId);
147+
143148
try {
144149
$this->configurableTypeResource->saveProducts($product, []);
145150
} catch (\Exception $exception) {
@@ -179,10 +184,12 @@ public function deleteById($sku, $id)
179184
public function save($sku, OptionInterface $option)
180185
{
181186
$metadata = $this->metadataPool->getMetadata(ProductInterface::class);
182-
/** @var Product $product */
183-
$product = $this->getProduct($sku);
184187
if ($option->getId()) {
188+
/** @var Product $product */
189+
$product = $this->getProduct($sku);
190+
$data = $option->getData();
185191
$option->load($option->getId());
192+
$option->setData(array_replace_recursive($option->getData(), $data));
186193
if (!$option->getId() || $option->getProductId() != $product->getData($metadata->getLinkField())) {
187194
throw new NoSuchEntityException(
188195
__(
@@ -192,6 +199,8 @@ public function save($sku, OptionInterface $option)
192199
);
193200
}
194201
} else {
202+
/** @var Product $product */
203+
$product = $this->productRepository->get($sku);
195204
$this->validateNewOptionData($option);
196205
$allowedTypes = [ProductType::TYPE_SIMPLE, ProductType::TYPE_VIRTUAL, ConfigurableType::TYPE_CODE];
197206
if (!in_array($product->getTypeId(), $allowedTypes)) {

app/code/Magento/ConfigurableProduct/Model/Product/ReadHandler.php

Lines changed: 11 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,27 @@
66
namespace Magento\ConfigurableProduct\Model\Product;
77

88
use Magento\Catalog\Api\Data\ProductInterface;
9-
use Magento\ConfigurableProduct\Api\Data\OptionInterface;
10-
use Magento\ConfigurableProduct\Api\Data\OptionValueInterfaceFactory;
9+
use Magento\ConfigurableProduct\Helper\Product\Options\Loader;
1110
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
12-
use Magento\Framework\Model\Entity\MetadataPool;
1311

1412
/**
1513
* Class ReadHandler
1614
*/
1715
class ReadHandler
1816
{
1917
/**
20-
* @var OptionValueInterfaceFactory
18+
* @var Loader
2119
*/
22-
private $optionValueFactory;
23-
24-
/**
25-
* @var MetadataPool
26-
*/
27-
private $metadataPool;
20+
private $optionLoader;
2821

2922
/**
3023
* ReadHandler constructor
31-
* @param OptionValueInterfaceFactory $optionValueFactory
32-
* @param MetadataPool $metadataPool
24+
*
25+
* @param Loader $optionLoader
3326
*/
34-
public function __construct(OptionValueInterfaceFactory $optionValueFactory, MetadataPool $metadataPool)
27+
public function __construct(Loader $optionLoader)
3528
{
36-
$this->optionValueFactory = $optionValueFactory;
37-
$this->metadataPool = $metadataPool;
29+
$this->optionLoader = $optionLoader;
3830
}
3931

4032
/**
@@ -50,40 +42,13 @@ public function execute($entityType, $entity)
5042
}
5143

5244
$extensionAttributes = $entity->getExtensionAttributes();
53-
$extensionAttributes->setConfigurableProductOptions($this->getOptions($entity));
45+
5446
$extensionAttributes->setConfigurableProductLinks($this->getLinkedProducts($entity));
55-
$entity->setExtensionAttributes($extensionAttributes);
56-
return $entity;
57-
}
47+
$extensionAttributes->setConfigurableProductOptions($this->optionLoader->load($entity));
5848

59-
/**
60-
* Get configurable options
61-
*
62-
* @param ProductInterface $product
63-
* @return OptionInterface[]
64-
*/
65-
private function getOptions(ProductInterface $product)
66-
{
67-
$options = [];
68-
/** @var Configurable $typeInstance */
69-
$typeInstance = $product->getTypeInstance();
70-
$attributeCollection = $typeInstance->getConfigurableAttributes($product);
49+
$entity->setExtensionAttributes($extensionAttributes);
7150

72-
foreach ($attributeCollection as $attribute) {
73-
$values = [];
74-
$attributeOptions = $attribute->getOptions();
75-
if (is_array($attributeOptions)) {
76-
foreach ($attributeOptions as $option) {
77-
/** @var \Magento\ConfigurableProduct\Api\Data\OptionValueInterface $value */
78-
$value = $this->optionValueFactory->create();
79-
$value->setValueIndex($option['value_index']);
80-
$values[] = $value;
81-
}
82-
}
83-
$attribute->setValues($values);
84-
$options[] = $attribute;
85-
}
86-
return $options;
51+
return $entity;
8752
}
8853

8954
/**
@@ -94,7 +59,6 @@ private function getOptions(ProductInterface $product)
9459
*/
9560
private function getLinkedProducts(ProductInterface $product)
9661
{
97-
$metadata = $this->metadataPool->getMetadata(ProductInterface::class);
9862
/** @var Configurable $typeInstance */
9963
$typeInstance = $product->getTypeInstance();
10064
$childrenIds = $typeInstance->getChildrenIds($product->getId());

app/code/Magento/ConfigurableProduct/Model/Product/SaveHandler.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
1010
use Magento\ConfigurableProduct\Api\OptionRepositoryInterface;
1111
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
12+
use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable as ResourceModelConfigurable;
1213
use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\ConfigurableFactory;
1314

1415
/**
@@ -31,21 +32,29 @@ class SaveHandler
3132
*/
3233
private $productAttributeRepository;
3334

35+
/**
36+
* @var ResourceModelConfigurable
37+
*/
38+
private $resourceModel;
39+
3440
/**
3541
* SaveHandler constructor
3642
*
43+
* @param ResourceModelConfigurable $resourceModel
3744
* @param OptionRepositoryInterface $optionRepository
3845
* @param ConfigurableFactory $configurableFactory
3946
* @param ProductAttributeRepositoryInterface $productAttributeRepository
4047
*/
4148
public function __construct(
49+
ResourceModelConfigurable $resourceModel,
4250
OptionRepositoryInterface $optionRepository,
4351
ConfigurableFactory $configurableFactory,
4452
ProductAttributeRepositoryInterface $productAttributeRepository
4553
) {
4654
$this->optionRepository = $optionRepository;
4755
$this->configurableFactory = $configurableFactory;
4856
$this->productAttributeRepository = $productAttributeRepository;
57+
$this->resourceModel = $resourceModel;
4958
}
5059

5160
/**
@@ -76,9 +85,7 @@ public function execute($entityType, ProductInterface $entity)
7685

7786
$configurableLinks = (array) $extensionAttributes->getConfigurableProductLinks();
7887

79-
/** @var \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable $configurable */
80-
$configurable = $this->configurableFactory->create();
81-
$configurable->saveProducts($entity, $configurableLinks);
88+
$this->resourceModel->saveProducts($entity, $configurableLinks);
8289

8390
return $entity;
8491
}
@@ -96,7 +103,11 @@ private function saveConfigurableProductAttributes(ProductInterface $product, ar
96103
/** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute $attribute */
97104
foreach ($attributes as $attribute) {
98105
$eavAttribute = $this->productAttributeRepository->get($attribute->getAttributeId());
106+
107+
$data = $attribute->getData();
99108
$attribute->loadByProductAndAttribute($product, $eavAttribute);
109+
$attribute->setData(array_replace_recursive($attribute->getData(), $data));
110+
100111
$ids[] = $this->optionRepository->save($product->getSku(), $attribute);
101112
}
102113

app/code/Magento/ConfigurableProduct/Model/ResourceModel/Product/Type/Configurable.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\ConfigurableProduct\Model\ResourceModel\Product\Type;
99

1010
use Magento\Catalog\Api\Data\ProductInterface;
11+
use Magento\ConfigurableProduct\Api\Data\OptionInterface;
1112
use Magento\Framework\Model\Entity\MetadataPool;
1213

1314
class Configurable extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
@@ -51,6 +52,27 @@ protected function _construct()
5152
$this->_init('catalog_product_super_link', 'link_id');
5253
}
5354

55+
/**
56+
* Get product entity id by product attribute
57+
*
58+
* @param OptionInterface $option
59+
* @return int
60+
*/
61+
public function getEntityIdByAttribute(OptionInterface $option)
62+
{
63+
$metadata = $this->metadataPool->getMetadata(ProductInterface::class);
64+
65+
$select = $this->getConnection()->select()->from(
66+
['e' => $this->getTable('catalog_product_entity')],
67+
['e.entity_id']
68+
)->where(
69+
'e.' . $metadata->getLinkField() . '=?',
70+
$option->getProductId()
71+
)->limit(1);
72+
73+
return (int) $this->getConnection()->fetchOne($select);
74+
}
75+
5476
/**
5577
* Save configurable product relations
5678
*

dev/tests/api-functional/testsuite/Magento/ConfigurableProduct/Api/LinkManagementTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,14 @@ public function testGetChildren()
4343
}
4444

4545
/**
46+
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_simple_77.php
4647
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
4748
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/delete_association.php
4849
*/
4950
public function testAddChild()
5051
{
5152
$productSku = 'configurable';
52-
$childSku = 'simple_10';
53+
$childSku = 'simple_77';
5354
$serviceInfo = [
5455
'rest' => [
5556
'resourcePath' => self::RESOURCE_PATH . '/' . $productSku . '/child',

0 commit comments

Comments
 (0)