Skip to content

Commit e3d09f7

Browse files
author
Mike Bijnsdorp
committed
Add test for update method in OptionManagement
Also removed an unnecessary alias
1 parent 02f7fa0 commit e3d09f7

File tree

1 file changed

+70
-1
lines changed

1 file changed

+70
-1
lines changed

app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/OptionManagementTest.php

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\Eav\Test\Unit\Model\Entity\Attribute;
99

10+
use Magento\Catalog\Model\Product;
1011
use Magento\Eav\Api\Data\AttributeOptionInterface as EavAttributeOptionInterface;
1112
use Magento\Eav\Api\Data\AttributeOptionLabelInterface as EavAttributeOptionLabelInterface;
1213
use Magento\Eav\Model\AttributeRepository;
@@ -18,7 +19,7 @@
1819
use Magento\Framework\Exception\InputException;
1920
use Magento\Framework\Exception\NoSuchEntityException;
2021
use Magento\Framework\Exception\StateException;
21-
use PHPUnit\Framework\MockObject\MockObject as MockObject;
22+
use PHPUnit\Framework\MockObject\MockObject;
2223
use PHPUnit\Framework\TestCase;
2324

2425
/**
@@ -228,6 +229,74 @@ public function testAddWithCannotSaveException()
228229
$this->model->add($entityType, $attributeCode, $optionMock);
229230
}
230231

232+
/**
233+
* Test to update attribute option
234+
*
235+
* @dataProvider optionLabelDataProvider
236+
*/
237+
public function testUpdate($label)
238+
{
239+
$entityType = Product::ENTITY;
240+
$storeId = 4;
241+
$attributeCode = 'atrCde';
242+
$storeLabel = 'labelLabel';
243+
$sortOder = 'optionSortOrder';
244+
$optionId = 10;
245+
$option = [
246+
'value' => [
247+
$optionId => [
248+
0 => $label,
249+
$storeId => $storeLabel,
250+
],
251+
],
252+
'order' => [
253+
$optionId => $sortOder,
254+
]
255+
];
256+
257+
$optionMock = $this->getAttributeOption();
258+
$labelMock = $this->getAttributeOptionLabel();
259+
/** @var SourceInterface|MockObject $sourceMock */
260+
$sourceMock = $this->createMock(EavAttributeSource::class);
261+
262+
$sourceMock->expects($this->once())
263+
->method('getOptionText')
264+
->with($optionId)
265+
->willReturn($label);
266+
267+
$sourceMock->expects($this->once())
268+
->method('getOptionId')
269+
->with($label)
270+
->willReturn($optionId);
271+
272+
/** @var EavAbstractAttribute|MockObject $attributeMock */
273+
$attributeMock = $this->getMockBuilder(EavAbstractAttribute::class)
274+
->disableOriginalConstructor()
275+
->addMethods(['setOption'])
276+
->onlyMethods(['usesSource', 'getSource'])
277+
->getMock();
278+
$attributeMock->method('usesSource')->willReturn(true);
279+
$attributeMock->expects($this->once())->method('setOption')->with($option);
280+
$attributeMock->method('getSource')->willReturn($sourceMock);
281+
282+
$this->attributeRepositoryMock->expects($this->once())
283+
->method('get')
284+
->with($entityType, $attributeCode)
285+
->willReturn($attributeMock);
286+
$optionMock->method('getLabel')->willReturn($label);
287+
$optionMock->method('getSortOrder')->willReturn($sortOder);
288+
$optionMock->method('getIsDefault')->willReturn(true);
289+
$optionMock->method('getStoreLabels')->willReturn([$labelMock]);
290+
$labelMock->method('getStoreId')->willReturn($storeId);
291+
$labelMock->method('getLabel')->willReturn($storeLabel);
292+
$this->resourceModelMock->expects($this->once())->method('save')->with($attributeMock);
293+
294+
$this->assertEquals(
295+
true,
296+
$this->model->update($entityType, $attributeCode, $optionId, $optionMock)
297+
);
298+
}
299+
231300
/**
232301
* Test to delete attribute option
233302
*/

0 commit comments

Comments
 (0)