6
6
7
7
namespace Magento \Eav \Test \Unit \Model \Entity \Attribute ;
8
8
9
+ use Magento \Eav \Api \Data \AttributeOptionInterface as EavAttributeOptionInterface ;
10
+ use Magento \Eav \Api \Data \AttributeOptionLabelInterface as EavAttributeOptionLabelInterface ;
11
+ use Magento \Eav \Model \Entity \Attribute \AbstractAttribute as EavAbstractAttribute ;
12
+ use Magento \Eav \Model \Entity \Attribute \Source \Table as EavAttributeSource ;
13
+ use PHPUnit \Framework \MockObject \MockObject as MockObject ;
14
+
9
15
class OptionManagementTest extends \PHPUnit \Framework \TestCase
10
16
{
11
17
/**
@@ -38,25 +44,9 @@ public function testAdd()
38
44
{
39
45
$ entityType = 42 ;
40
46
$ attributeCode = 'atrCde ' ;
41
- $ optionMock = $ this ->getMockForAbstractClass (
42
- \Magento \Eav \Api \Data \AttributeOptionInterface::class,
43
- [],
44
- '' ,
45
- false ,
46
- false ,
47
- true ,
48
- ['getSourceLabels ' ]
49
- );
50
- $ attributeMock = $ this ->getMockForAbstractClass (
51
- \Magento \Framework \Model \AbstractModel::class,
52
- [],
53
- '' ,
54
- false ,
55
- false ,
56
- true ,
57
- ['usesSource ' , 'setDefault ' , 'setOption ' ]
58
- );
59
- $ labelMock = $ this ->createMock (\Magento \Eav \Api \Data \AttributeOptionLabelInterface::class);
47
+ $ attributeMock = $ this ->getAttribute ();
48
+ $ optionMock = $ this ->getAttributeOption ();
49
+ $ labelMock = $ this ->getAttributeOptionLabel ();
60
50
$ option =
61
51
['value ' => [
62
52
'id_new_option ' => [
@@ -92,15 +82,7 @@ public function testAddWithEmptyAttributeCode()
92
82
{
93
83
$ entityType = 42 ;
94
84
$ attributeCode = '' ;
95
- $ optionMock = $ this ->getMockForAbstractClass (
96
- \Magento \Eav \Api \Data \AttributeOptionInterface::class,
97
- [],
98
- '' ,
99
- false ,
100
- false ,
101
- true ,
102
- ['getSourceLabels ' ]
103
- );
85
+ $ optionMock = $ this ->getAttributeOption ();
104
86
$ this ->resourceModelMock ->expects ($ this ->never ())->method ('save ' );
105
87
$ this ->model ->add ($ entityType , $ attributeCode , $ optionMock );
106
88
}
@@ -113,24 +95,8 @@ public function testAddWithWrongOptions()
113
95
{
114
96
$ entityType = 42 ;
115
97
$ attributeCode = 'testAttribute ' ;
116
- $ optionMock = $ this ->getMockForAbstractClass (
117
- \Magento \Eav \Api \Data \AttributeOptionInterface::class,
118
- [],
119
- '' ,
120
- false ,
121
- false ,
122
- true ,
123
- ['getSourceLabels ' ]
124
- );
125
- $ attributeMock = $ this ->getMockForAbstractClass (
126
- \Magento \Framework \Model \AbstractModel::class,
127
- [],
128
- '' ,
129
- false ,
130
- false ,
131
- true ,
132
- ['usesSource ' , 'setDefault ' , 'setOption ' ]
133
- );
98
+ $ attributeMock = $ this ->getAttribute ();
99
+ $ optionMock = $ this ->getAttributeOption ();
134
100
$ this ->attributeRepositoryMock ->expects ($ this ->once ())->method ('get ' )->with ($ entityType , $ attributeCode )
135
101
->willReturn ($ attributeMock );
136
102
$ attributeMock ->expects ($ this ->once ())->method ('usesSource ' )->willReturn (false );
@@ -146,25 +112,9 @@ public function testAddWithCannotSaveException()
146
112
{
147
113
$ entityType = 42 ;
148
114
$ attributeCode = 'atrCde ' ;
149
- $ optionMock = $ this ->getMockForAbstractClass (
150
- \Magento \Eav \Api \Data \AttributeOptionInterface::class,
151
- [],
152
- '' ,
153
- false ,
154
- false ,
155
- true ,
156
- ['getSourceLabels ' ]
157
- );
158
- $ attributeMock = $ this ->getMockForAbstractClass (
159
- \Magento \Framework \Model \AbstractModel::class,
160
- [],
161
- '' ,
162
- false ,
163
- false ,
164
- true ,
165
- ['usesSource ' , 'setDefault ' , 'setOption ' ]
166
- );
167
- $ labelMock = $ this ->createMock (\Magento \Eav \Api \Data \AttributeOptionLabelInterface::class);
115
+ $ optionMock = $ this ->getAttributeOption ();
116
+ $ attributeMock = $ this ->getAttribute ();
117
+ $ labelMock = $ this ->getAttributeOptionLabel ();
168
118
$ option =
169
119
['value ' => [
170
120
'id_new_option ' => [
@@ -340,7 +290,7 @@ public function testGetItems()
340
290
true ,
341
291
['getOptions ' ]
342
292
);
343
- $ optionsMock = [$ this ->createMock (\ Magento \ Eav \ Api \ Data \AttributeOptionInterface ::class)];
293
+ $ optionsMock = [$ this ->createMock (EavAttributeOptionInterface ::class)];
344
294
$ this ->attributeRepositoryMock ->expects ($ this ->once ())->method ('get ' )->with ($ entityType , $ attributeCode )
345
295
->willReturn ($ attributeMock );
346
296
$ attributeMock ->expects ($ this ->once ())->method ('getOptions ' )->willReturn ($ optionsMock );
@@ -380,4 +330,55 @@ public function testGetItemsWithEmptyAttributeCode()
380
330
$ attributeCode = '' ;
381
331
$ this ->model ->getItems ($ entityType , $ attributeCode );
382
332
}
333
+
334
+ /**
335
+ * Returns attribute entity mock.
336
+ *
337
+ * @param array $attributeOptions attribute options for return
338
+ * @return MockObject|EavAbstractAttribute
339
+ */
340
+ private function getAttribute (array $ attributeOptions = [])
341
+ {
342
+ $ attribute = $ this ->getMockBuilder (EavAbstractAttribute::class)
343
+ ->disableOriginalConstructor ()
344
+ ->setMethods (
345
+ [
346
+ 'usesSource ' ,
347
+ 'setDefault ' ,
348
+ 'setOption ' ,
349
+ 'setStoreId ' ,
350
+ 'getSource ' ,
351
+ ]
352
+ )
353
+ ->getMock ();
354
+ $ source = $ this ->getMockBuilder (EavAttributeSource::class)
355
+ ->disableOriginalConstructor ()
356
+ ->getMock ();
357
+
358
+ $ attribute ->method ('getSource ' )->willReturn ($ source );
359
+ $ source ->method ('toOptionArray ' )->willReturn ($ attributeOptions );
360
+
361
+ return $ attribute ;
362
+ }
363
+
364
+ /**
365
+ * Return attribute option entity mock.
366
+ *
367
+ * @return MockObject|EavAttributeOptionInterface
368
+ */
369
+ private function getAttributeOption ()
370
+ {
371
+ return $ this ->getMockBuilder (EavAttributeOptionInterface::class)
372
+ ->setMethods (['getSourceLabels ' ])
373
+ ->getMockForAbstractClass ();
374
+ }
375
+
376
+ /**
377
+ * @return MockObject|EavAttributeOptionLabelInterface
378
+ */
379
+ private function getAttributeOptionLabel ()
380
+ {
381
+ return $ this ->getMockBuilder (EavAttributeOptionLabelInterface::class)
382
+ ->getMockForAbstractClass ();
383
+ }
383
384
}
0 commit comments