From 4b3c4ea8eade7b9dd86d0b82343e0fe003e63095 Mon Sep 17 00:00:00 2001 From: NazarKlovanych Date: Wed, 7 Nov 2018 17:50:16 +0200 Subject: [PATCH 1/6] creating attribute option value using API returns unexpected response --- .../Entity/Attribute/OptionManagement.php | 41 +++++++++++++++---- .../Entity/Attribute/OptionManagementTest.php | 18 ++++---- 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php b/app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php index 3c3bc083fdf8f..743ac0e3a61c8 100644 --- a/app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php +++ b/app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php @@ -53,19 +53,19 @@ public function add($entityType, $attributeCode, $option) } $optionLabel = $option->getLabel(); - $optionId = $this->getOptionId($option); + $optionValue = $this->getOptionValue($option); $options = []; - $options['value'][$optionId][0] = $optionLabel; - $options['order'][$optionId] = $option->getSortOrder(); + $options['value'][$optionValue][0] = $optionLabel; + $options['order'][$optionValue] = $option->getSortOrder(); if (is_array($option->getStoreLabels())) { foreach ($option->getStoreLabels() as $label) { - $options['value'][$optionId][$label->getStoreId()] = $label->getLabel(); + $options['value'][$optionValue][$label->getStoreId()] = $label->getLabel(); } } if ($option->getIsDefault()) { - $attribute->setDefault([$optionId]); + $attribute->setDefault([$optionValue]); } $attribute->setOption($options); @@ -78,7 +78,7 @@ public function add($entityType, $attributeCode, $option) throw new StateException(__('The "%1" attribute can\'t be saved.', $attributeCode)); } - return $this->getOptionId($option); + return $this->getOptionId($option, $attribute); } /** @@ -153,14 +153,37 @@ protected function validateOption($attribute, $optionId) } /** - * Returns option id + * Returns option value * * @param \Magento\Eav\Api\Data\AttributeOptionInterface $option * @return string */ - private function getOptionId(\Magento\Eav\Api\Data\AttributeOptionInterface $option) : string + private function getOptionValue(\Magento\Eav\Api\Data\AttributeOptionInterface $option) : string { - return 'id_' . ($option->getValue() ?: 'new_option'); + return 'value_' . ($option->getValue() ?: 'new_option'); + } + + /** + * Returns option id + * + * @param \Magento\Eav\Api\Data\AttributeOptionInterface $option + * @param \Magento\Eav\Api\Data\AttributeInterface $attribute + * @return null|int + * @throws NoSuchEntityException + */ + private function getOptionId(\Magento\Eav\Api\Data\AttributeOptionInterface $option, $attribute) + { + $optionId = null; + + $attribute = $this->attributeRepository->get('catalog_product', $attribute->getAttributeCode()); + if ($attribute->getOptions()) { + foreach ($attribute->getOptions() as $optionLabel) { + if ($option->getLabel() == $optionLabel->getLabel()) { + $optionId = $attribute->getSource()->getOptionId($optionLabel->getLabel()); + } + } + } + return $optionId; } /** diff --git a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/OptionManagementTest.php b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/OptionManagementTest.php index b63a4dd2c9ae6..853103d9888b5 100644 --- a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/OptionManagementTest.php +++ b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/OptionManagementTest.php @@ -59,17 +59,19 @@ public function testAdd() $labelMock = $this->createMock(\Magento\Eav\Api\Data\AttributeOptionLabelInterface::class); $option = ['value' => [ - 'id_new_option' => [ + 'value_new_option' => [ 0 => 'optionLabel', 42 => 'labelLabel', ], ], 'order' => [ - 'id_new_option' => 'optionSortOrder', + 'value_new_option' => 'optionSortOrder', ], ]; - $this->attributeRepositoryMock->expects($this->once())->method('get')->with($entityType, $attributeCode) + $this->attributeRepositoryMock->expects($this->at(0))->method('get')->with($entityType, $attributeCode) + ->willReturn($attributeMock); + $this->attributeRepositoryMock->expects($this->at(1))->method('get')->with('catalog_product', null) ->willReturn($attributeMock); $attributeMock->expects($this->once())->method('usesSource')->willReturn(true); $optionMock->expects($this->once())->method('getLabel')->willReturn('optionLabel'); @@ -78,10 +80,10 @@ public function testAdd() $labelMock->expects($this->once())->method('getStoreId')->willReturn(42); $labelMock->expects($this->once())->method('getLabel')->willReturn('labelLabel'); $optionMock->expects($this->once())->method('getIsDefault')->willReturn(true); - $attributeMock->expects($this->once())->method('setDefault')->with(['id_new_option']); + $attributeMock->expects($this->once())->method('setDefault')->with(['value_new_option']); $attributeMock->expects($this->once())->method('setOption')->with($option); $this->resourceModelMock->expects($this->once())->method('save')->with($attributeMock); - $this->assertEquals('id_new_option', $this->model->add($entityType, $attributeCode, $optionMock)); + $this->assertEquals(null, $this->model->add($entityType, $attributeCode, $optionMock)); } /** @@ -167,13 +169,13 @@ public function testAddWithCannotSaveException() $labelMock = $this->createMock(\Magento\Eav\Api\Data\AttributeOptionLabelInterface::class); $option = ['value' => [ - 'id_new_option' => [ + 'value_new_option' => [ 0 => 'optionLabel', 42 => 'labelLabel', ], ], 'order' => [ - 'id_new_option' => 'optionSortOrder', + 'value_new_option' => 'optionSortOrder', ], ]; @@ -186,7 +188,7 @@ public function testAddWithCannotSaveException() $labelMock->expects($this->once())->method('getStoreId')->willReturn(42); $labelMock->expects($this->once())->method('getLabel')->willReturn('labelLabel'); $optionMock->expects($this->once())->method('getIsDefault')->willReturn(true); - $attributeMock->expects($this->once())->method('setDefault')->with(['id_new_option']); + $attributeMock->expects($this->once())->method('setDefault')->with(['value_new_option']); $attributeMock->expects($this->once())->method('setOption')->with($option); $this->resourceModelMock->expects($this->once())->method('save')->with($attributeMock) ->willThrowException(new \Exception()); From 2bb033277a834199c6bc300cfda4fd19e28a23d7 Mon Sep 17 00:00:00 2001 From: Pavel Bystritsky Date: Mon, 10 Dec 2018 14:28:16 +0200 Subject: [PATCH 2/6] Performance improvement. --- .../Entity/Attribute/OptionManagement.php | 52 +++++++--------- .../ResourceModel/GetAttributeOptionId.php | 62 +++++++++++++++++++ .../Entity/Attribute/OptionManagementTest.php | 24 +++++-- .../Magento/Eav/Api/OptionManagementTest.php | 57 +++++++++++++++++ 4 files changed, 158 insertions(+), 37 deletions(-) create mode 100644 app/code/Magento/Eav/Model/ResourceModel/GetAttributeOptionId.php create mode 100644 dev/tests/api-functional/testsuite/Magento/Eav/Api/OptionManagementTest.php diff --git a/app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php b/app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php index 743ac0e3a61c8..f0bf139332d98 100644 --- a/app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php +++ b/app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php @@ -6,6 +6,10 @@ namespace Magento\Eav\Model\Entity\Attribute; +use Magento\Eav\Api\AttributeOptionManagementInterface; +use Magento\Eav\Model\AttributeRepository; +use Magento\Eav\Model\ResourceModel\Entity\Attribute; +use Magento\Eav\Model\ResourceModel\GetAttributeOptionId; use Magento\Framework\Exception\InputException; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\Exception\StateException; @@ -13,29 +17,37 @@ /** * Eav Option Management */ -class OptionManagement implements \Magento\Eav\Api\AttributeOptionManagementInterface +class OptionManagement implements AttributeOptionManagementInterface { /** - * @var \Magento\Eav\Model\AttributeRepository + * @var AttributeRepository */ protected $attributeRepository; /** - * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute + * @var Attribute */ protected $resourceModel; /** - * @param \Magento\Eav\Model\AttributeRepository $attributeRepository - * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute $resourceModel + * @var GetAttributeOptionId + */ + private $getAttributeOptionId; + + /** + * @param AttributeRepository $attributeRepository + * @param Attribute $resourceModel + * @param GetAttributeOptionId $getAttributeOptionId * @codeCoverageIgnore */ public function __construct( - \Magento\Eav\Model\AttributeRepository $attributeRepository, - \Magento\Eav\Model\ResourceModel\Entity\Attribute $resourceModel + AttributeRepository $attributeRepository, + Attribute $resourceModel, + GetAttributeOptionId $getAttributeOptionId ) { $this->attributeRepository = $attributeRepository; $this->resourceModel = $resourceModel; + $this->getAttributeOptionId = $getAttributeOptionId; } /** @@ -57,6 +69,7 @@ public function add($entityType, $attributeCode, $option) $options = []; $options['value'][$optionValue][0] = $optionLabel; $options['order'][$optionValue] = $option->getSortOrder(); + $attributeId = $attribute->getAttributeId(); if (is_array($option->getStoreLabels())) { foreach ($option->getStoreLabels() as $label) { @@ -78,7 +91,7 @@ public function add($entityType, $attributeCode, $option) throw new StateException(__('The "%1" attribute can\'t be saved.', $attributeCode)); } - return $this->getOptionId($option, $attribute); + return $this->getAttributeOptionId->execute($attributeId, $optionLabel); } /** @@ -163,29 +176,6 @@ private function getOptionValue(\Magento\Eav\Api\Data\AttributeOptionInterface $ return 'value_' . ($option->getValue() ?: 'new_option'); } - /** - * Returns option id - * - * @param \Magento\Eav\Api\Data\AttributeOptionInterface $option - * @param \Magento\Eav\Api\Data\AttributeInterface $attribute - * @return null|int - * @throws NoSuchEntityException - */ - private function getOptionId(\Magento\Eav\Api\Data\AttributeOptionInterface $option, $attribute) - { - $optionId = null; - - $attribute = $this->attributeRepository->get('catalog_product', $attribute->getAttributeCode()); - if ($attribute->getOptions()) { - foreach ($attribute->getOptions() as $optionLabel) { - if ($option->getLabel() == $optionLabel->getLabel()) { - $optionId = $attribute->getSource()->getOptionId($optionLabel->getLabel()); - } - } - } - return $optionId; - } - /** * Set option value * diff --git a/app/code/Magento/Eav/Model/ResourceModel/GetAttributeOptionId.php b/app/code/Magento/Eav/Model/ResourceModel/GetAttributeOptionId.php new file mode 100644 index 0000000000000..9c7aa4af6a6ee --- /dev/null +++ b/app/code/Magento/Eav/Model/ResourceModel/GetAttributeOptionId.php @@ -0,0 +1,62 @@ +resource = $connection; + } + + /** + * Returns Attribute Option Id by Attribute Id and Option Label. + * + * @param int $attributeId + * @param string $optionLabel + * + * @return string + */ + public function execute(int $attributeId, string $optionLabel): string + { + $connection = $this->resource->getConnection(); + $optionTable = $this->resource->getTableName('eav_attribute_option'); + $optionValueTable = $this->resource->getTableName('eav_attribute_option_value'); + $select = $connection->select() + ->from( + ['ovt' => $optionValueTable], + 'option_id' + )->joinInner( + ['ot' => $optionTable], + 'ovt.option_id = ot.option_id', + [] + )->where( + 'ovt.value = ?', + $optionLabel + )->where( + 'ot.attribute_id = ?', + $attributeId + ); + + return $connection->fetchOne($select); + } +} diff --git a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/OptionManagementTest.php b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/OptionManagementTest.php index 853103d9888b5..c3d2d5bcf657d 100644 --- a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/OptionManagementTest.php +++ b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/OptionManagementTest.php @@ -23,14 +23,22 @@ class OptionManagementTest extends \PHPUnit\Framework\TestCase */ protected $resourceModelMock; + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + private $getAttributeOptionIdMock; + protected function setUp() { $this->attributeRepositoryMock = $this->createMock(\Magento\Eav\Model\AttributeRepository::class); $this->resourceModelMock = $this->createMock(\Magento\Eav\Model\ResourceModel\Entity\Attribute::class); + $this->getAttributeOptionIdMock = + $this->createMock(\Magento\Eav\Model\ResourceModel\GetAttributeOptionId::class); $this->model = new \Magento\Eav\Model\Entity\Attribute\OptionManagement( $this->attributeRepositoryMock, - $this->resourceModelMock + $this->resourceModelMock, + $this->getAttributeOptionIdMock ); } @@ -54,7 +62,7 @@ public function testAdd() false, false, true, - ['usesSource', 'setDefault', 'setOption'] + ['usesSource', 'setDefault', 'setOption', 'getAttributeId'] ); $labelMock = $this->createMock(\Magento\Eav\Api\Data\AttributeOptionLabelInterface::class); $option = @@ -69,9 +77,7 @@ public function testAdd() ], ]; - $this->attributeRepositoryMock->expects($this->at(0))->method('get')->with($entityType, $attributeCode) - ->willReturn($attributeMock); - $this->attributeRepositoryMock->expects($this->at(1))->method('get')->with('catalog_product', null) + $this->attributeRepositoryMock->expects($this->once())->method('get')->with($entityType, $attributeCode) ->willReturn($attributeMock); $attributeMock->expects($this->once())->method('usesSource')->willReturn(true); $optionMock->expects($this->once())->method('getLabel')->willReturn('optionLabel'); @@ -82,8 +88,14 @@ public function testAdd() $optionMock->expects($this->once())->method('getIsDefault')->willReturn(true); $attributeMock->expects($this->once())->method('setDefault')->with(['value_new_option']); $attributeMock->expects($this->once())->method('setOption')->with($option); + $attributeMock->expects($this->once())->method('getAttributeId')->willReturn(93); $this->resourceModelMock->expects($this->once())->method('save')->with($attributeMock); - $this->assertEquals(null, $this->model->add($entityType, $attributeCode, $optionMock)); + $this->getAttributeOptionIdMock + ->expects($this->once()) + ->method('execute') + ->with(93, 'optionLabel') + ->willReturn(42); + $this->assertEquals(42, $this->model->add($entityType, $attributeCode, $optionMock)); } /** diff --git a/dev/tests/api-functional/testsuite/Magento/Eav/Api/OptionManagementTest.php b/dev/tests/api-functional/testsuite/Magento/Eav/Api/OptionManagementTest.php new file mode 100644 index 0000000000000..ff9a89c7725e0 --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/Eav/Api/OptionManagementTest.php @@ -0,0 +1,57 @@ + 'test_option_' . md5(random_int(0, PHP_INT_MAX)), + ]; + $attributeCode = 'multiselect_attribute'; + + // Integer value should be returned - this indicates Option was created. + $this->assertInternalType( + 'numeric', + $this->addAttributeOption($attributeCode, $option) + ); + // False should be returned - this indicates Option already exists. + $this->assertFalse( + $this->addAttributeOption($attributeCode, $option) + ); + } + + /** + * @param string $attributeCode + * @param array $option + * @return array|bool|float|int|string + */ + private function addAttributeOption(string $attributeCode, array $option) + { + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => '/V1/products/attributes/' . $attributeCode . '/options', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST, + ], + 'soap' => [ + 'service' => 'catalogProductAttributeOptionManagementV1', + 'serviceVersion' => 'V1', + 'operation' => 'catalogProductAttributeOptionManagementV1getItems', + ], + ]; + + return $this->_webApiCall($serviceInfo, ['option' => $option]); + } +} From 12276cfdcc964eae7ea68e37614cb3bfeb7d59d6 Mon Sep 17 00:00:00 2001 From: nmalevanec Date: Wed, 26 Dec 2018 14:02:02 +0200 Subject: [PATCH 3/6] Fix web-api and functional tests. --- .../Eav/Model/Entity/Attribute/OptionManagement.php | 4 +++- .../Eav/Model/ResourceModel/GetAttributeOptionId.php | 10 +++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php b/app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php index f0bf139332d98..311cd22535666 100644 --- a/app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php +++ b/app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php @@ -91,7 +91,9 @@ public function add($entityType, $attributeCode, $option) throw new StateException(__('The "%1" attribute can\'t be saved.', $attributeCode)); } - return $this->getAttributeOptionId->execute($attributeId, $optionLabel); + $optionValue = is_array($option->getStoreLabels()) ? $optionValue : $optionLabel; + + return $this->getAttributeOptionId->execute($attributeId, $optionValue); } /** diff --git a/app/code/Magento/Eav/Model/ResourceModel/GetAttributeOptionId.php b/app/code/Magento/Eav/Model/ResourceModel/GetAttributeOptionId.php index 9c7aa4af6a6ee..f5491e10961e6 100644 --- a/app/code/Magento/Eav/Model/ResourceModel/GetAttributeOptionId.php +++ b/app/code/Magento/Eav/Model/ResourceModel/GetAttributeOptionId.php @@ -29,14 +29,14 @@ public function __construct( } /** - * Returns Attribute Option Id by Attribute Id and Option Label. + * Returns Attribute Option Id by Attribute Id and Option Value. * * @param int $attributeId - * @param string $optionLabel + * @param string $optionValue * * @return string */ - public function execute(int $attributeId, string $optionLabel): string + public function execute(int $attributeId, string $optionValue): string { $connection = $this->resource->getConnection(); $optionTable = $this->resource->getTableName('eav_attribute_option'); @@ -51,12 +51,12 @@ public function execute(int $attributeId, string $optionLabel): string [] )->where( 'ovt.value = ?', - $optionLabel + $optionValue )->where( 'ot.attribute_id = ?', $attributeId ); - return $connection->fetchOne($select); + return (string)$connection->fetchOne($select); } } From 3c991aa8ccc5ff3c3438d71856b3cb04e2c97aae Mon Sep 17 00:00:00 2001 From: nmalevanec Date: Wed, 26 Dec 2018 18:07:28 +0200 Subject: [PATCH 4/6] Fix unit, static and web-api tests. --- .../Entity/Attribute/OptionManagement.php | 34 ++++++++++++++----- .../Entity/Attribute/OptionManagementTest.php | 6 ++-- .../Magento/Eav/Api/OptionManagementTest.php | 9 ++--- 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php b/app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php index 311cd22535666..0dade731f3c9c 100644 --- a/app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php +++ b/app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php @@ -7,6 +7,7 @@ namespace Magento\Eav\Model\Entity\Attribute; use Magento\Eav\Api\AttributeOptionManagementInterface; +use Magento\Eav\Api\Data\AttributeOptionInterface; use Magento\Eav\Model\AttributeRepository; use Magento\Eav\Model\ResourceModel\Entity\Attribute; use Magento\Eav\Model\ResourceModel\GetAttributeOptionId; @@ -71,11 +72,7 @@ public function add($entityType, $attributeCode, $option) $options['order'][$optionValue] = $option->getSortOrder(); $attributeId = $attribute->getAttributeId(); - if (is_array($option->getStoreLabels())) { - foreach ($option->getStoreLabels() as $label) { - $options['value'][$optionValue][$label->getStoreId()] = $label->getLabel(); - } - } + $options = $this->processStoreLabels($option, $options, $optionValue); if ($option->getIsDefault()) { $attribute->setDefault([$optionValue]); @@ -170,10 +167,10 @@ protected function validateOption($attribute, $optionId) /** * Returns option value * - * @param \Magento\Eav\Api\Data\AttributeOptionInterface $option + * @param AttributeOptionInterface $option * @return string */ - private function getOptionValue(\Magento\Eav\Api\Data\AttributeOptionInterface $option) : string + private function getOptionValue(AttributeOptionInterface $option) : string { return 'value_' . ($option->getValue() ?: 'new_option'); } @@ -181,13 +178,13 @@ private function getOptionValue(\Magento\Eav\Api\Data\AttributeOptionInterface $ /** * Set option value * - * @param \Magento\Eav\Api\Data\AttributeOptionInterface $option + * @param AttributeOptionInterface $option * @param \Magento\Eav\Api\Data\AttributeInterface $attribute * @param string $optionLabel * @return void */ private function setOptionValue( - \Magento\Eav\Api\Data\AttributeOptionInterface $option, + AttributeOptionInterface $option, \Magento\Eav\Api\Data\AttributeInterface $attribute, string $optionLabel ) { @@ -203,4 +200,23 @@ private function setOptionValue( } } } + + /** + * Process option store labels. + * + * @param AttributeOptionInterface $option + * @param array $options + * @param string $optionValue + * @return array + */ + private function processStoreLabels(AttributeOptionInterface $option, array $options, string $optionValue): array + { + if (is_array($option->getStoreLabels())) { + foreach ($option->getStoreLabels() as $label) { + $options['value'][$optionValue][$label->getStoreId()] = $label->getLabel(); + } + } + + return $options; + } } diff --git a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/OptionManagementTest.php b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/OptionManagementTest.php index c3d2d5bcf657d..0f03e288379f1 100644 --- a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/OptionManagementTest.php +++ b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/OptionManagementTest.php @@ -82,7 +82,7 @@ public function testAdd() $attributeMock->expects($this->once())->method('usesSource')->willReturn(true); $optionMock->expects($this->once())->method('getLabel')->willReturn('optionLabel'); $optionMock->expects($this->once())->method('getSortOrder')->willReturn('optionSortOrder'); - $optionMock->expects($this->exactly(2))->method('getStoreLabels')->willReturn([$labelMock]); + $optionMock->expects($this->exactly(3))->method('getStoreLabels')->willReturn([$labelMock]); $labelMock->expects($this->once())->method('getStoreId')->willReturn(42); $labelMock->expects($this->once())->method('getLabel')->willReturn('labelLabel'); $optionMock->expects($this->once())->method('getIsDefault')->willReturn(true); @@ -93,7 +93,7 @@ public function testAdd() $this->getAttributeOptionIdMock ->expects($this->once()) ->method('execute') - ->with(93, 'optionLabel') + ->with(93, 'value_new_option') ->willReturn(42); $this->assertEquals(42, $this->model->add($entityType, $attributeCode, $optionMock)); } @@ -176,7 +176,7 @@ public function testAddWithCannotSaveException() false, false, true, - ['usesSource', 'setDefault', 'setOption'] + ['usesSource', 'setDefault', 'setOption', 'getAttributeCode'] ); $labelMock = $this->createMock(\Magento\Eav\Api\Data\AttributeOptionLabelInterface::class); $option = diff --git a/dev/tests/api-functional/testsuite/Magento/Eav/Api/OptionManagementTest.php b/dev/tests/api-functional/testsuite/Magento/Eav/Api/OptionManagementTest.php index ff9a89c7725e0..182ff24ea0ca3 100644 --- a/dev/tests/api-functional/testsuite/Magento/Eav/Api/OptionManagementTest.php +++ b/dev/tests/api-functional/testsuite/Magento/Eav/Api/OptionManagementTest.php @@ -19,6 +19,7 @@ public function testAttributeOptionAdding() { $option = [ 'label' => 'test_option_' . md5(random_int(0, PHP_INT_MAX)), + 'value' => 'test_option_' . md5(random_int(0, PHP_INT_MAX)), ]; $attributeCode = 'multiselect_attribute'; @@ -27,8 +28,8 @@ public function testAttributeOptionAdding() 'numeric', $this->addAttributeOption($attributeCode, $option) ); - // False should be returned - this indicates Option already exists. - $this->assertFalse( + // Empty result should be returned - this indicates Option already exists. + $this->assertEmpty( $this->addAttributeOption($attributeCode, $option) ); } @@ -48,10 +49,10 @@ private function addAttributeOption(string $attributeCode, array $option) 'soap' => [ 'service' => 'catalogProductAttributeOptionManagementV1', 'serviceVersion' => 'V1', - 'operation' => 'catalogProductAttributeOptionManagementV1getItems', + 'operation' => 'catalogProductAttributeOptionManagementV1add', ], ]; - return $this->_webApiCall($serviceInfo, ['option' => $option]); + return $this->_webApiCall($serviceInfo, ['option' => $option, 'attributeCode' => $attributeCode]); } } From 42552f088407a79e6d1110ff42ac2c95658fbf93 Mon Sep 17 00:00:00 2001 From: NazarKlovanych Date: Fri, 18 Jan 2019 12:40:55 +0200 Subject: [PATCH 5/6] Backward Compatibility To constructor --- .../Eav/Model/Entity/Attribute/OptionManagement.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php b/app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php index 0dade731f3c9c..624e10ce0d58e 100644 --- a/app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php +++ b/app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php @@ -14,6 +14,7 @@ use Magento\Framework\Exception\InputException; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\Exception\StateException; +use \Magento\Framework\App\ObjectManager; /** * Eav Option Management @@ -42,13 +43,17 @@ class OptionManagement implements AttributeOptionManagementInterface * @codeCoverageIgnore */ public function __construct( - AttributeRepository $attributeRepository, + AttributeRepository $attributeRepository = null, Attribute $resourceModel, - GetAttributeOptionId $getAttributeOptionId + GetAttributeOptionId $getAttributeOptionId = null ) { - $this->attributeRepository = $attributeRepository; + $this->attributeRepository = $attributeRepository ?: ObjectManager::getInstance()->get( + AttributeRepository::class + ); $this->resourceModel = $resourceModel; - $this->getAttributeOptionId = $getAttributeOptionId; + $this->getAttributeOptionId = $getAttributeOptionId ?: ObjectManager::getInstance()->get( + GetAttributeOptionId::class + ); } /** From acf189a57bb0d44ec8b46389035a12abcbc12f5f Mon Sep 17 00:00:00 2001 From: Nazar Date: Fri, 18 Jan 2019 16:38:00 +0200 Subject: [PATCH 6/6] Update OptionManagement.php remove "\" from use Object Manager --- .../Magento/Eav/Model/Entity/Attribute/OptionManagement.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php b/app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php index 624e10ce0d58e..bff5c7ee31df3 100644 --- a/app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php +++ b/app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php @@ -14,7 +14,7 @@ use Magento\Framework\Exception\InputException; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\Exception\StateException; -use \Magento\Framework\App\ObjectManager; +use Magento\Framework\App\ObjectManager; /** * Eav Option Management