|
3 | 3 | * Copyright © Magento, Inc. All rights reserved.
|
4 | 4 | * See COPYING.txt for license details.
|
5 | 5 | */
|
| 6 | +declare(strict_types=1); |
| 7 | + |
6 | 8 | namespace Magento\Catalog\Test\Unit\Controller\Adminhtml\Product\Attribute;
|
7 | 9 |
|
8 | 10 | use Magento\Catalog\Controller\Adminhtml\Product\Attribute\Validate;
|
9 | 11 | use Magento\Eav\Model\Validator\Attribute\Code as AttributeCodeValidator;
|
| 12 | +use Magento\Framework\Exception\NotFoundException; |
10 | 13 | use Magento\Framework\Serialize\Serializer\FormData;
|
11 | 14 | use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
|
12 | 15 | use Magento\Catalog\Test\Unit\Controller\Adminhtml\Product\AttributeTest;
|
|
17 | 20 | use Magento\Framework\ObjectManagerInterface;
|
18 | 21 | use Magento\Framework\View\LayoutFactory;
|
19 | 22 | use Magento\Framework\View\LayoutInterface;
|
| 23 | +use PHPUnit\Framework\MockObject\MockObject; |
20 | 24 |
|
21 | 25 | /**
|
22 | 26 | * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
23 | 27 | */
|
24 | 28 | class ValidateTest extends AttributeTest
|
25 | 29 | {
|
26 | 30 | /**
|
27 |
| - * @var ResultJsonFactory|\PHPUnit_Framework_MockObject_MockObject |
| 31 | + * @var ResultJsonFactory|MockObject |
28 | 32 | */
|
29 | 33 | protected $resultJsonFactoryMock;
|
30 | 34 |
|
31 | 35 | /**
|
32 |
| - * @var ResultJson|\PHPUnit_Framework_MockObject_MockObject |
| 36 | + * @var ResultJson|MockObject |
33 | 37 | */
|
34 | 38 | protected $resultJson;
|
35 | 39 |
|
36 | 40 | /**
|
37 |
| - * @var LayoutFactory|\PHPUnit_Framework_MockObject_MockObject |
| 41 | + * @var LayoutFactory|MockObject |
38 | 42 | */
|
39 | 43 | protected $layoutFactoryMock;
|
40 | 44 |
|
41 | 45 | /**
|
42 |
| - * @var ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject |
| 46 | + * @var ObjectManagerInterface|MockObject |
43 | 47 | */
|
44 | 48 | protected $objectManagerMock;
|
45 | 49 |
|
46 | 50 | /**
|
47 |
| - * @var Attribute|\PHPUnit_Framework_MockObject_MockObject |
| 51 | + * @var Attribute|MockObject |
48 | 52 | */
|
49 | 53 | protected $attributeMock;
|
50 | 54 |
|
51 | 55 | /**
|
52 |
| - * @var AttributeSet|\PHPUnit_Framework_MockObject_MockObject |
| 56 | + * @var AttributeSet|MockObject |
53 | 57 | */
|
54 | 58 | protected $attributeSetMock;
|
55 | 59 |
|
56 | 60 | /**
|
57 |
| - * @var Escaper|\PHPUnit_Framework_MockObject_MockObject |
| 61 | + * @var Escaper|MockObject |
58 | 62 | */
|
59 | 63 | protected $escaperMock;
|
60 | 64 |
|
61 | 65 | /**
|
62 |
| - * @var LayoutInterface|\PHPUnit_Framework_MockObject_MockObject |
| 66 | + * @var LayoutInterface|MockObject |
63 | 67 | */
|
64 | 68 | protected $layoutMock;
|
65 | 69 |
|
66 | 70 | /**
|
67 |
| - * @var FormData|\PHPUnit_Framework_MockObject_MockObject |
| 71 | + * @var FormData|MockObject |
68 | 72 | */
|
69 | 73 | private $formDataSerializerMock;
|
70 | 74 |
|
71 | 75 | /**
|
72 |
| - * @var AttributeCodeValidator|\PHPUnit_Framework_MockObject_MockObject |
| 76 | + * @var AttributeCodeValidator|MockObject |
73 | 77 | */
|
74 | 78 | private $attributeCodeValidatorMock;
|
75 | 79 |
|
@@ -148,8 +152,8 @@ public function testExecute()
|
148 | 152 | ->method('create')
|
149 | 153 | ->willReturnMap(
|
150 | 154 | [
|
151 |
| - [\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class, [], $this->attributeMock], |
152 |
| - [\Magento\Eav\Model\Entity\Attribute\Set::class, [], $this->attributeSetMock] |
| 155 | + [Attribute::class, [], $this->attributeMock], |
| 156 | + [AttributeSet::class, [], $this->attributeSetMock] |
153 | 157 | ]
|
154 | 158 | );
|
155 | 159 | $this->attributeMock->expects($this->once())
|
@@ -184,11 +188,74 @@ public function testExecute()
|
184 | 188 | $this->assertInstanceOf(ResultJson::class, $this->getModel()->execute());
|
185 | 189 | }
|
186 | 190 |
|
| 191 | + /** |
| 192 | + * Test that editing existing attribute loads attribute by id |
| 193 | + * |
| 194 | + * @return void |
| 195 | + * @throws NotFoundException |
| 196 | + */ |
| 197 | + public function testExecuteEditExisting(): void |
| 198 | + { |
| 199 | + $serializedOptions = '{"key":"value"}'; |
| 200 | + $this->requestMock->expects($this->any()) |
| 201 | + ->method('getParam') |
| 202 | + ->willReturnMap( |
| 203 | + [ |
| 204 | + ['frontend_label', null, 'test_frontend_label'], |
| 205 | + ['attribute_id', null, 10], |
| 206 | + ['attribute_code', null, 'test_attribute_code'], |
| 207 | + ['new_attribute_set_name', null, 'test_attribute_set_name'], |
| 208 | + ['serialized_options', '[]', $serializedOptions], |
| 209 | + ] |
| 210 | + ); |
| 211 | + $this->objectManagerMock->expects($this->exactly(2)) |
| 212 | + ->method('create') |
| 213 | + ->willReturnMap( |
| 214 | + [ |
| 215 | + [Attribute::class, [], $this->attributeMock], |
| 216 | + [AttributeSet::class, [], $this->attributeSetMock] |
| 217 | + ] |
| 218 | + ); |
| 219 | + $this->attributeMock->expects($this->once()) |
| 220 | + ->method('load') |
| 221 | + ->willReturnSelf(); |
| 222 | + $this->attributeMock->expects($this->once()) |
| 223 | + ->method('getAttributeCode') |
| 224 | + ->willReturn('test_attribute_code'); |
| 225 | + |
| 226 | + $this->attributeCodeValidatorMock->expects($this->once()) |
| 227 | + ->method('isValid') |
| 228 | + ->with('test_attribute_code') |
| 229 | + ->willReturn(true); |
| 230 | + |
| 231 | + $this->requestMock->expects($this->once()) |
| 232 | + ->method('has') |
| 233 | + ->with('new_attribute_set_name') |
| 234 | + ->willReturn(true); |
| 235 | + $this->attributeSetMock->expects($this->once()) |
| 236 | + ->method('setEntityTypeId') |
| 237 | + ->willReturnSelf(); |
| 238 | + $this->attributeSetMock->expects($this->once()) |
| 239 | + ->method('load') |
| 240 | + ->willReturnSelf(); |
| 241 | + $this->attributeSetMock->expects($this->once()) |
| 242 | + ->method('getId') |
| 243 | + ->willReturn(false); |
| 244 | + $this->resultJsonFactoryMock->expects($this->once()) |
| 245 | + ->method('create') |
| 246 | + ->willReturn($this->resultJson); |
| 247 | + $this->resultJson->expects($this->once()) |
| 248 | + ->method('setJsonData') |
| 249 | + ->willReturnSelf(); |
| 250 | + |
| 251 | + $this->assertInstanceOf(ResultJson::class, $this->getModel()->execute()); |
| 252 | + } |
| 253 | + |
187 | 254 | /**
|
188 | 255 | * @dataProvider provideUniqueData
|
189 | 256 | * @param array $options
|
190 | 257 | * @param boolean $isError
|
191 |
| - * @throws \Magento\Framework\Exception\NotFoundException |
| 258 | + * @throws NotFoundException |
192 | 259 | */
|
193 | 260 | public function testUniqueValidation(array $options, $isError)
|
194 | 261 | {
|
@@ -330,7 +397,7 @@ public function provideUniqueData()
|
330 | 397 | *
|
331 | 398 | * @dataProvider provideEmptyOption
|
332 | 399 | * @param array $options
|
333 |
| - * @throws \Magento\Framework\Exception\NotFoundException |
| 400 | + * @throws NotFoundException |
334 | 401 | */
|
335 | 402 | public function testEmptyOption(array $options, $result)
|
336 | 403 | {
|
@@ -453,7 +520,7 @@ public function provideEmptyOption()
|
453 | 520 | * @dataProvider provideWhitespaceOption
|
454 | 521 | * @param array $options
|
455 | 522 | * @param $result
|
456 |
| - * @throws \Magento\Framework\Exception\NotFoundException |
| 523 | + * @throws NotFoundException |
457 | 524 | */
|
458 | 525 | public function testWhitespaceOption(array $options, $result)
|
459 | 526 | {
|
@@ -571,7 +638,7 @@ public function provideWhitespaceOption()
|
571 | 638 | }
|
572 | 639 |
|
573 | 640 | /**
|
574 |
| - * @throws \Magento\Framework\Exception\NotFoundException |
| 641 | + * @throws NotFoundException |
575 | 642 | */
|
576 | 643 | public function testExecuteWithOptionsDataError()
|
577 | 644 | {
|
@@ -600,8 +667,8 @@ public function testExecuteWithOptionsDataError()
|
600 | 667 | ->method('create')
|
601 | 668 | ->willReturnMap(
|
602 | 669 | [
|
603 |
| - [\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class, [], $this->attributeMock], |
604 |
| - [\Magento\Eav\Model\Entity\Attribute\Set::class, [], $this->attributeSetMock] |
| 670 | + [Attribute::class, [], $this->attributeMock], |
| 671 | + [AttributeSet::class, [], $this->attributeSetMock] |
605 | 672 | ]
|
606 | 673 | );
|
607 | 674 |
|
@@ -639,7 +706,7 @@ public function testExecuteWithOptionsDataError()
|
639 | 706 | * @dataProvider provideInvalidAttributeCodes
|
640 | 707 | * @param string $attributeCode
|
641 | 708 | * @param $result
|
642 |
| - * @throws \Magento\Framework\Exception\NotFoundException |
| 709 | + * @throws NotFoundException |
643 | 710 | */
|
644 | 711 | public function testExecuteWithInvalidAttributeCode($attributeCode, $result)
|
645 | 712 | {
|
|
0 commit comments