6
6
namespace Magento \Variable \Test \Unit \Model ;
7
7
8
8
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
9
+ use Magento \Variable \Model \ResourceModel \Variable \Collection ;
9
10
10
11
class VariableTest extends \PHPUnit \Framework \TestCase
11
12
{
12
- /** @var \Magento\Variable\Model\Variable */
13
+ /**
14
+ * @var \Magento\Variable\Model\Variable
15
+ */
13
16
private $ model ;
14
17
15
- /** @var \PHPUnit_Framework_MockObject_MockObject */
18
+ /**
19
+ * @var \Magento\Framework\Escaper|\PHPUnit_Framework_MockObject_MockObject
20
+ */
16
21
private $ escaperMock ;
17
22
18
- /** @var \PHPUnit_Framework_MockObject_MockObject */
23
+ /**
24
+ * @var \Magento\Variable\Model\ResourceModel\Variable|\PHPUnit_Framework_MockObject_MockObject
25
+ */
19
26
private $ resourceMock ;
20
27
21
- /** @var \Magento\Framework\Phrase */
28
+ /**
29
+ * @var \Magento\Variable\Model\ResourceModel\Variable\Collection|\PHPUnit_Framework_MockObject_MockObject
30
+ */
31
+ private $ resourceCollectionMock ;
32
+
33
+ /**
34
+ * @var \Magento\Framework\Phrase
35
+ */
22
36
private $ validationFailedPhrase ;
23
37
24
- /** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */
38
+ /**
39
+ * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
40
+ */
25
41
private $ objectManager ;
26
42
27
43
protected function setUp ()
@@ -33,11 +49,15 @@ protected function setUp()
33
49
$ this ->resourceMock = $ this ->getMockBuilder (\Magento \Variable \Model \ResourceModel \Variable::class)
34
50
->disableOriginalConstructor ()
35
51
->getMock ();
52
+ $ this ->resourceCollectionMock = $ this ->getMockBuilder (Collection::class)
53
+ ->disableOriginalConstructor ()
54
+ ->getMock ();
36
55
$ this ->model = $ this ->objectManager ->getObject (
37
56
\Magento \Variable \Model \Variable::class,
38
57
[
39
58
'escaper ' => $ this ->escaperMock ,
40
- 'resource ' => $ this ->resourceMock
59
+ 'resource ' => $ this ->resourceMock ,
60
+ 'resourceCollection ' => $ this ->resourceCollectionMock ,
41
61
]
42
62
);
43
63
$ this ->validationFailedPhrase = __ ('Validation has failed. ' );
@@ -101,58 +121,46 @@ public function testValidate($variableArray, $objectId, $expectedResult)
101
121
public function testGetVariablesOptionArrayNoGroup ()
102
122
{
103
123
$ origOptions = [
104
- ['value ' => 'VAL ' , 'label ' => 'LBL ' ]
124
+ ['value ' => 'VAL ' , 'label ' => 'LBL ' ],
105
125
];
106
126
107
127
$ transformedOptions = [
108
- ['value ' => '{{customVar code=VAL}} ' , 'label ' => __ ('%1 ' , 'LBL ' )]
128
+ ['value ' => '{{customVar code=VAL}} ' , 'label ' => __ ('%1 ' , 'LBL ' )],
109
129
];
110
130
111
- $ collectionMock = $ this ->getMockBuilder (\Magento \Variable \Model \ResourceModel \Variable \Collection::class)
112
- ->disableOriginalConstructor ()
113
- ->getMock ();
114
- $ collectionMock ->expects ($ this ->any ())
131
+ $ this ->resourceCollectionMock ->expects ($ this ->any ())
115
132
->method ('toOptionArray ' )
116
133
->willReturn ($ origOptions );
117
- $ mockVariable = $ this ->getMockBuilder (\Magento \Variable \Model \Variable::class)
118
- ->setMethods (['getCollection ' ])
119
- ->setConstructorArgs ($ this ->objectManager ->getConstructArguments (\Magento \Variable \Model \Variable::class))
120
- ->getMock ();
121
- $ mockVariable ->expects ($ this ->any ())
122
- ->method ('getCollection ' )
123
- ->willReturn ($ collectionMock );
124
- $ this ->assertEquals ($ transformedOptions , $ mockVariable ->getVariablesOptionArray ());
134
+ $ this ->escaperMock ->expects ($ this ->once ())
135
+ ->method ('escapeHtml ' )
136
+ ->with ($ origOptions [0 ]['label ' ])
137
+ ->willReturn ($ origOptions [0 ]['label ' ]);
138
+ $ this ->assertEquals ($ transformedOptions , $ this ->model ->getVariablesOptionArray ());
125
139
}
126
140
127
141
public function testGetVariablesOptionArrayWithGroup ()
128
142
{
129
143
$ origOptions = [
130
- ['value ' => 'VAL ' , 'label ' => 'LBL ' ]
144
+ ['value ' => 'VAL ' , 'label ' => 'LBL ' ],
131
145
];
132
146
133
147
$ transformedOptions = [
134
148
[
135
149
'label ' => __ ('Custom Variables ' ),
136
150
'value ' => [
137
- ['value ' => '{{customVar code=VAL}} ' , 'label ' => __ ('%1 ' , 'LBL ' )]
138
- ]
139
- ]
151
+ ['value ' => '{{customVar code=VAL}} ' , 'label ' => __ ('%1 ' , 'LBL ' )],
152
+ ],
153
+ ],
140
154
];
141
155
142
- $ collectionMock = $ this ->getMockBuilder (\Magento \Variable \Model \ResourceModel \Variable \Collection::class)
143
- ->disableOriginalConstructor ()
144
- ->getMock ();
145
- $ collectionMock ->expects ($ this ->any ())
156
+ $ this ->resourceCollectionMock ->expects ($ this ->any ())
146
157
->method ('toOptionArray ' )
147
158
->willReturn ($ origOptions );
148
- $ mockVariable = $ this ->getMockBuilder (\Magento \Variable \Model \Variable::class)
149
- ->setMethods (['getCollection ' ])
150
- ->setConstructorArgs ($ this ->objectManager ->getConstructArguments (\Magento \Variable \Model \Variable::class))
151
- ->getMock ();
152
- $ mockVariable ->expects ($ this ->any ())
153
- ->method ('getCollection ' )
154
- ->willReturn ($ collectionMock );
155
- $ this ->assertEquals ($ transformedOptions , $ mockVariable ->getVariablesOptionArray (true ));
159
+ $ this ->escaperMock ->expects ($ this ->atLeastOnce ())
160
+ ->method ('escapeHtml ' )
161
+ ->with ($ origOptions [0 ]['label ' ])
162
+ ->willReturn ($ origOptions [0 ]['label ' ]);
163
+ $ this ->assertEquals ($ transformedOptions , $ this ->model ->getVariablesOptionArray (true ));
156
164
}
157
165
158
166
public function validateDataProvider ()
@@ -163,15 +171,15 @@ public function validateDataProvider()
163
171
return [
164
172
'Empty Variable ' => [[], null , true ],
165
173
'IDs match ' => [$ variable , 'matching_id ' , true ],
166
- 'IDs do not match ' => [$ variable , 'non_matching_id ' , __ ('Variable Code must be unique. ' )]
174
+ 'IDs do not match ' => [$ variable , 'non_matching_id ' , __ ('Variable Code must be unique. ' )],
167
175
];
168
176
}
169
177
170
178
public function validateMissingInfoDataProvider ()
171
179
{
172
180
return [
173
181
'Missing code ' => ['' , 'some-name ' ],
174
- 'Missing name ' => ['some-code ' , '' ]
182
+ 'Missing name ' => ['some-code ' , '' ],
175
183
];
176
184
}
177
185
}
0 commit comments