|
7 | 7 |
|
8 | 8 | namespace Magento\Eav\Test\Unit\Model\Entity\Attribute;
|
9 | 9 |
|
| 10 | +use Magento\Catalog\Model\Product; |
10 | 11 | use Magento\Eav\Api\Data\AttributeOptionInterface as EavAttributeOptionInterface;
|
11 | 12 | use Magento\Eav\Api\Data\AttributeOptionLabelInterface as EavAttributeOptionLabelInterface;
|
12 | 13 | use Magento\Eav\Model\AttributeRepository;
|
|
18 | 19 | use Magento\Framework\Exception\InputException;
|
19 | 20 | use Magento\Framework\Exception\NoSuchEntityException;
|
20 | 21 | use Magento\Framework\Exception\StateException;
|
21 |
| -use PHPUnit\Framework\MockObject\MockObject as MockObject; |
| 22 | +use PHPUnit\Framework\MockObject\MockObject; |
22 | 23 | use PHPUnit\Framework\TestCase;
|
23 | 24 |
|
24 | 25 | /**
|
@@ -59,13 +60,15 @@ protected function setUp(): void
|
59 | 60 |
|
60 | 61 | /**
|
61 | 62 | * Test to add attribute option
|
| 63 | + * |
| 64 | + * @param string $label |
| 65 | + * @dataProvider optionLabelDataProvider |
62 | 66 | */
|
63 |
| - public function testAdd() |
| 67 | + public function testAdd(string $label): void |
64 | 68 | {
|
65 | 69 | $entityType = 42;
|
66 | 70 | $storeId = 4;
|
67 | 71 | $attributeCode = 'atrCde';
|
68 |
| - $label = 'optionLabel'; |
69 | 72 | $storeLabel = 'labelLabel';
|
70 | 73 | $sortOder = 'optionSortOrder';
|
71 | 74 | $option = [
|
@@ -121,6 +124,17 @@ public function testAdd()
|
121 | 124 | );
|
122 | 125 | }
|
123 | 126 |
|
| 127 | + /** |
| 128 | + * @return array |
| 129 | + */ |
| 130 | + public function optionLabelDataProvider(): array |
| 131 | + { |
| 132 | + return [ |
| 133 | + ['optionLabel'], |
| 134 | + ['0'] |
| 135 | + ]; |
| 136 | + } |
| 137 | + |
124 | 138 | /**
|
125 | 139 | * Test to add attribute option with empty attribute code
|
126 | 140 | */
|
@@ -216,6 +230,75 @@ public function testAddWithCannotSaveException()
|
216 | 230 | $this->model->add($entityType, $attributeCode, $optionMock);
|
217 | 231 | }
|
218 | 232 |
|
| 233 | + /** |
| 234 | + * Test to update attribute option |
| 235 | + * |
| 236 | + * @param string $label |
| 237 | + * @dataProvider optionLabelDataProvider |
| 238 | + */ |
| 239 | + public function testUpdate(string $label): void |
| 240 | + { |
| 241 | + $entityType = Product::ENTITY; |
| 242 | + $storeId = 4; |
| 243 | + $attributeCode = 'atrCde'; |
| 244 | + $storeLabel = 'labelLabel'; |
| 245 | + $sortOder = 'optionSortOrder'; |
| 246 | + $optionId = 10; |
| 247 | + $option = [ |
| 248 | + 'value' => [ |
| 249 | + $optionId => [ |
| 250 | + 0 => $label, |
| 251 | + $storeId => $storeLabel, |
| 252 | + ], |
| 253 | + ], |
| 254 | + 'order' => [ |
| 255 | + $optionId => $sortOder, |
| 256 | + ] |
| 257 | + ]; |
| 258 | + |
| 259 | + $optionMock = $this->getAttributeOption(); |
| 260 | + $labelMock = $this->getAttributeOptionLabel(); |
| 261 | + /** @var SourceInterface|MockObject $sourceMock */ |
| 262 | + $sourceMock = $this->createMock(EavAttributeSource::class); |
| 263 | + |
| 264 | + $sourceMock->expects($this->once()) |
| 265 | + ->method('getOptionText') |
| 266 | + ->with($optionId) |
| 267 | + ->willReturn($label); |
| 268 | + |
| 269 | + $sourceMock->expects($this->once()) |
| 270 | + ->method('getOptionId') |
| 271 | + ->with($label) |
| 272 | + ->willReturn($optionId); |
| 273 | + |
| 274 | + /** @var EavAbstractAttribute|MockObject $attributeMock */ |
| 275 | + $attributeMock = $this->getMockBuilder(EavAbstractAttribute::class) |
| 276 | + ->disableOriginalConstructor() |
| 277 | + ->addMethods(['setOption']) |
| 278 | + ->onlyMethods(['usesSource', 'getSource']) |
| 279 | + ->getMock(); |
| 280 | + $attributeMock->method('usesSource')->willReturn(true); |
| 281 | + $attributeMock->expects($this->once())->method('setOption')->with($option); |
| 282 | + $attributeMock->method('getSource')->willReturn($sourceMock); |
| 283 | + |
| 284 | + $this->attributeRepositoryMock->expects($this->once()) |
| 285 | + ->method('get') |
| 286 | + ->with($entityType, $attributeCode) |
| 287 | + ->willReturn($attributeMock); |
| 288 | + $optionMock->method('getLabel')->willReturn($label); |
| 289 | + $optionMock->method('getSortOrder')->willReturn($sortOder); |
| 290 | + $optionMock->method('getIsDefault')->willReturn(true); |
| 291 | + $optionMock->method('getStoreLabels')->willReturn([$labelMock]); |
| 292 | + $labelMock->method('getStoreId')->willReturn($storeId); |
| 293 | + $labelMock->method('getLabel')->willReturn($storeLabel); |
| 294 | + $this->resourceModelMock->expects($this->once())->method('save')->with($attributeMock); |
| 295 | + |
| 296 | + $this->assertEquals( |
| 297 | + true, |
| 298 | + $this->model->update($entityType, $attributeCode, $optionId, $optionMock) |
| 299 | + ); |
| 300 | + } |
| 301 | + |
219 | 302 | /**
|
220 | 303 | * Test to delete attribute option
|
221 | 304 | */
|
|
0 commit comments