3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
-
7
6
namespace Magento \Quote \Test \Unit \Model \Quote \Item ;
8
7
8
+ use Magento \Framework \Serialize \JsonValidator ;
9
+ use Magento \Framework \Serialize \Serializer \Json ;
10
+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
11
+ use Magento \Quote \Model \Quote \Item ;
12
+ use Magento \Quote \Model \Quote \Item \Compare ;
13
+ use Magento \Quote \Model \Quote \Item \Option ;
14
+ use PHPUnit \Framework \MockObject \MockObject ;
15
+ use PHPUnit \Framework \TestCase ;
16
+
9
17
/**
10
- * Class CompareTest
18
+ * Tests the class that is used to compare Quote Item Options
11
19
*/
12
- class CompareTest extends \ PHPUnit \ Framework \ TestCase
20
+ class CompareTest extends TestCase
13
21
{
14
22
/**
15
- * @var \Magento\Quote\Model\Quote\Item\ Compare
23
+ * @var Compare
16
24
*/
17
25
private $ helper ;
18
26
19
27
/**
20
- * @var \Magento\Quote\Model\Quote\ Item|\PHPUnit_Framework_MockObject_MockObject
28
+ * @var Item|MockObject
21
29
*/
22
30
private $ itemMock ;
23
31
24
32
/**
25
- * @var \Magento\Quote\Model\Quote\ Item|\PHPUnit_Framework_MockObject_MockObject
33
+ * @var Item|MockObject
26
34
*/
27
35
private $ comparedMock ;
28
36
29
37
/**
30
- * @var \Magento\Quote\Model\Quote\Item\ Option|\PHPUnit_Framework_MockObject_MockObject
38
+ * @var Option|MockObject
31
39
*/
32
40
private $ optionMock ;
33
41
34
42
/**
35
- * @var \Magento\Framework\Serialize\ JsonValidator|\PHPUnit_Framework_MockObject_MockObject
43
+ * @var JsonValidator|MockObject
36
44
*/
37
45
private $ jsonValidatorMock ;
38
46
39
47
/**
40
- * test setUp
48
+ * @inheritdoc
41
49
*/
42
50
protected function setUp ()
43
51
{
44
52
$ this ->itemMock = $ this ->createPartialMock (
45
- \ Magento \ Quote \ Model \ Quote \ Item::class,
46
- ['__wakeup ' , 'getProductId ' , 'getOptions ' ]
53
+ Item::class,
54
+ ['__wakeup ' , 'getProductId ' , 'getOptions ' , ' getOptionsByCode ' ]
47
55
);
48
56
$ this ->comparedMock = $ this ->createPartialMock (
49
- \ Magento \ Quote \ Model \ Quote \ Item::class,
50
- ['__wakeup ' , 'getProductId ' , 'getOptions ' ]
57
+ Item::class,
58
+ ['__wakeup ' , 'getProductId ' , 'getOptions ' , ' getOptionsByCode ' ]
51
59
);
52
60
$ this ->optionMock = $ this ->createPartialMock (
53
- \ Magento \ Quote \ Model \ Quote \ Item \ Option::class,
61
+ Option::class,
54
62
['__wakeup ' , 'getCode ' , 'getValue ' ]
55
63
);
56
- $ serializer = $ this ->getMockBuilder (\ Magento \ Framework \ Serialize \ Serializer \ Json::class)
64
+ $ serializer = $ this ->getMockBuilder (Json::class)
57
65
->setMethods (['unserialize ' ])
58
66
->disableOriginalConstructor ()
59
67
->getMockForAbstractClass ();
@@ -65,13 +73,13 @@ function ($value) {
65
73
}
66
74
);
67
75
68
- $ this ->jsonValidatorMock = $ this ->getMockBuilder (\ Magento \ Framework \ Serialize \ JsonValidator::class)
76
+ $ this ->jsonValidatorMock = $ this ->getMockBuilder (JsonValidator::class)
69
77
->disableOriginalConstructor ()
70
78
->getMock ();
71
79
72
- $ objectManagerHelper = new \ Magento \ Framework \ TestFramework \ Unit \ Helper \ ObjectManager ($ this );
80
+ $ objectManagerHelper = new ObjectManager ($ this );
73
81
$ this ->helper = $ objectManagerHelper ->getObject (
74
- \ Magento \ Quote \ Model \ Quote \ Item \ Compare::class,
82
+ Compare::class,
75
83
[
76
84
'serializer ' => $ serializer ,
77
85
'jsonValidator ' => $ this ->jsonValidatorMock
@@ -82,17 +90,17 @@ function ($value) {
82
90
/**
83
91
* @param string $code
84
92
* @param mixed $value
85
- * @return \PHPUnit_Framework_MockObject_MockObject
93
+ * @return MockObject
86
94
*/
87
95
protected function getOptionMock ($ code , $ value )
88
96
{
89
97
$ optionMock = clone $ this ->optionMock ;
90
- $ optionMock ->expects ($ this ->once ())
98
+ $ optionMock ->expects ($ this ->any ())
91
99
->method ('getCode ' )
92
- ->will ( $ this -> returnValue ( $ code) );
93
- $ optionMock ->expects ($ this ->once ())
100
+ ->willReturn ( $ code );
101
+ $ optionMock ->expects ($ this ->any ())
94
102
->method ('getValue ' )
95
- ->will ( $ this -> returnValue ( $ value) );
103
+ ->willReturn ( $ value );
96
104
return $ optionMock ;
97
105
}
98
106
@@ -103,10 +111,10 @@ public function testCompareDifferentProduct()
103
111
{
104
112
$ this ->itemMock ->expects ($ this ->once ())
105
113
->method ('getProductId ' )
106
- ->will ( $ this -> returnValue ( 1 ) );
114
+ ->willReturn ( 1 );
107
115
$ this ->itemMock ->expects ($ this ->once ())
108
116
->method ('getProductId ' )
109
- ->will ( $ this -> returnValue ( 2 ) );
117
+ ->willReturn ( 2 );
110
118
111
119
$ this ->assertFalse ($ this ->helper ->compare ($ this ->itemMock , $ this ->comparedMock ));
112
120
}
@@ -116,36 +124,47 @@ public function testCompareDifferentProduct()
116
124
*/
117
125
public function testCompareProductWithDifferentOptions ()
118
126
{
127
+ // Identical Product Ids
119
128
$ this ->itemMock ->expects ($ this ->any ())
120
129
->method ('getProductId ' )
121
- ->will ( $ this -> returnValue ( 1 ) );
130
+ ->willReturn ( 1 );
122
131
$ this ->comparedMock ->expects ($ this ->any ())
123
132
->method ('getProductId ' )
124
- ->will ( $ this -> returnValue ( 1 ) );
133
+ ->willReturn ( 1 );
125
134
135
+ // Identical Option Keys
126
136
$ this ->itemMock ->expects ($ this ->any ())
127
137
->method ('getOptions ' )
128
- ->will (
129
- $ this ->returnValue (
130
- [
131
- $ this ->getOptionMock ('option-1 ' , 1 ),
132
- $ this ->getOptionMock ('option-2 ' , 'option-value ' ),
133
- $ this ->getOptionMock ('option-3 ' , json_encode (['value ' => 'value-1 ' , 'qty ' => 2 ]))
134
- ]
135
- )
136
- );
138
+ ->willReturn ([$ this ->getOptionMock ('identical ' , 'value ' )]);
137
139
$ this ->comparedMock ->expects ($ this ->any ())
138
140
->method ('getOptions ' )
139
- ->will ($ this ->returnValue (
141
+ ->willReturn ([$ this ->getOptionMock ('identical ' , 'value ' )]);
142
+
143
+ // Different Option Values
144
+ $ this ->itemMock ->expects ($ this ->once ())
145
+ ->method ('getOptionsByCode ' )
146
+ ->willReturn (
140
147
[
141
- $ this ->getOptionMock ('option-4 ' , 1 ),
142
- $ this ->getOptionMock ('option-2 ' , 'option-value ' ),
143
- $ this ->getOptionMock ('option-3 ' , json_encode ([
144
- 'value ' => 'value-1 ' ,
145
- 'qty ' => 2 ,
146
- ])),
148
+ 'info_buyRequest ' => $ this ->getOptionMock ('info_buyRequest ' , ['value-1 ' ]),
149
+ 'option ' => $ this ->getOptionMock ('option ' , 1 ),
150
+ 'simple_product ' => $ this ->getOptionMock ('simple_product ' , 3 ),
151
+ 'product_qty_2 ' => $ this ->getOptionMock ('product_qty_2 ' , 10 ),
152
+ 'attributes ' => $ this ->getOptionMock ('attributes ' , 93 ),
147
153
]
148
- ));
154
+ );
155
+
156
+ $ this ->comparedMock ->expects ($ this ->once ())
157
+ ->method ('getOptionsByCode ' )
158
+ ->willReturn (
159
+ [
160
+ 'info_buyRequest ' => $ this ->getOptionMock ('info_buyRequest ' , ['value-2 ' ]),
161
+ 'option ' => $ this ->getOptionMock ('option ' , 1 ),
162
+ 'simple_product ' => $ this ->getOptionMock ('simple_product ' , 3 ),
163
+ 'product_qty_2 ' => $ this ->getOptionMock ('product_qty_2 ' , 10 ),
164
+ 'attributes ' => $ this ->getOptionMock ('attributes ' , 94 ),
165
+ ]
166
+ );
167
+
149
168
$ this ->assertFalse ($ this ->helper ->compare ($ this ->itemMock , $ this ->comparedMock ));
150
169
}
151
170
@@ -156,24 +175,24 @@ public function testCompareItemWithComparedWithoutOption()
156
175
{
157
176
$ this ->itemMock ->expects ($ this ->any ())
158
177
->method ('getProductId ' )
159
- ->will ( $ this -> returnValue ( 1 ) );
178
+ ->willReturn ( 1 );
160
179
$ this ->comparedMock ->expects ($ this ->any ())
161
180
->method ('getProductId ' )
162
- ->will ( $ this -> returnValue ( 1 ) );
163
- $ this ->itemMock ->expects ($ this ->any ())
164
- ->method ('getOptions ' )
165
- ->will (
166
- $ this -> returnValue (
167
- [
168
- $ this ->getOptionMock ('option-1 ' , 1 ),
169
- $ this ->getOptionMock ('option-2 ' , ' option-value ' ),
170
- $ this ->getOptionMock ('option-3 ' , json_encode ([ ' value ' => ' value-1 ' , ' qty ' => 2 ]) ),
171
- ]
172
- )
181
+ ->willReturn ( 1 );
182
+ $ this ->itemMock ->expects ($ this ->once ())
183
+ ->method ('getOptionsByCode ' )
184
+ ->willReturn (
185
+ [
186
+ ' info_buyRequest ' => $ this -> getOptionMock ( ' info_buyRequest ' , [ ' value-1 ' ]),
187
+ ' option ' => $ this ->getOptionMock ('option ' , 1 ),
188
+ ' simple_product ' => $ this ->getOptionMock ('simple_product ' , 3 ),
189
+ ' product_qty_2 ' => $ this ->getOptionMock ('product_qty_2 ' , 10 ),
190
+ ' attributes ' => $ this -> getOptionMock ( ' attributes ' , 93 ),
191
+ ]
173
192
);
174
193
$ this ->comparedMock ->expects ($ this ->any ())
175
- ->method ('getOptions ' )
176
- ->will ( $ this -> returnValue ([]) );
194
+ ->method ('getOptionsByCode ' )
195
+ ->willReturn ([] );
177
196
$ this ->assertFalse ($ this ->helper ->compare ($ this ->itemMock , $ this ->comparedMock ));
178
197
}
179
198
@@ -184,62 +203,24 @@ public function testCompareItemWithoutOptionWithCompared()
184
203
{
185
204
$ this ->itemMock ->expects ($ this ->any ())
186
205
->method ('getProductId ' )
187
- ->will ( $ this -> returnValue ( 1 ) );
206
+ ->willReturn ( 1 );
188
207
$ this ->comparedMock ->expects ($ this ->any ())
189
208
->method ('getProductId ' )
190
- ->will ($ this ->returnValue (1 ));
191
- $ this ->comparedMock ->expects ($ this ->any ())
192
- ->method ('getOptions ' )
193
- ->will ($ this ->returnValue (
194
- [
195
- $ this ->getOptionMock ('option-1 ' , 1 ),
196
- $ this ->getOptionMock ('option-2 ' , 'option-value ' ),
197
- $ this ->getOptionMock (
198
- 'option-3 ' ,
199
- json_encode (['value ' => 'value-1 ' , 'qty ' => 2 ])
200
- ),
201
- ]
202
- ));
203
- $ this ->itemMock ->expects ($ this ->any ())
204
- ->method ('getOptions ' )
205
- ->will ($ this ->returnValue ([]));
206
- $ this ->assertFalse ($ this ->helper ->compare ($ this ->itemMock , $ this ->comparedMock ));
207
- }
208
-
209
- /**
210
- * Verify that compare ignores empty options.
211
- */
212
- public function testCompareWithEmptyValues ()
213
- {
214
- $ itemOptionValue = '{"non-empty-option":"test","empty_option":""} ' ;
215
- $ comparedOptionValue = '{"non-empty-option":"test"} ' ;
216
-
217
- $ this ->jsonValidatorMock ->expects ($ this ->any ())
218
- ->method ('isValid ' )
219
- ->willReturn (true );
220
-
221
- $ this ->itemMock ->expects ($ this ->any ())
222
- ->method ('getProductId ' )
223
- ->will ($ this ->returnValue (1 ));
224
- $ this ->comparedMock ->expects ($ this ->any ())
225
- ->method ('getProductId ' )
226
- ->will ($ this ->returnValue (1 ));
227
-
228
- $ this ->itemMock ->expects ($ this ->once ())
229
- ->method ('getOptions ' )
230
- ->willReturn (
231
- [
232
- $ this ->getOptionMock ('option-1 ' , $ itemOptionValue )
233
- ]
234
- );
209
+ ->willReturn (1 );
235
210
$ this ->comparedMock ->expects ($ this ->once ())
236
- ->method ('getOptions ' )
211
+ ->method ('getOptionsByCode ' )
237
212
->willReturn (
238
213
[
239
- $ this ->getOptionMock ('option-1 ' , $ comparedOptionValue )
214
+ 'info_buyRequest ' => $ this ->getOptionMock ('info_buyRequest ' , ['value-2 ' ]),
215
+ 'option ' => $ this ->getOptionMock ('option ' , 1 ),
216
+ 'simple_product ' => $ this ->getOptionMock ('simple_product ' , 3 ),
217
+ 'product_qty_2 ' => $ this ->getOptionMock ('product_qty_2 ' , 10 ),
218
+ 'attributes ' => $ this ->getOptionMock ('attributes ' , 94 ),
240
219
]
241
220
);
242
-
243
- $ this ->assertTrue ($ this ->helper ->compare ($ this ->itemMock , $ this ->comparedMock ));
221
+ $ this ->itemMock ->expects ($ this ->any ())
222
+ ->method ('getOptionsByCode ' )
223
+ ->willReturn ([]);
224
+ $ this ->assertFalse ($ this ->helper ->compare ($ this ->itemMock , $ this ->comparedMock ));
244
225
}
245
226
}
0 commit comments