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