7
7
8
8
use Magento \Catalog \Model \Product \Configuration \Item \Option \OptionInterface ;
9
9
use Magento \Framework \App \Filesystem \DirectoryList ;
10
+ use Magento \Framework \Exception \SerializationException ;
10
11
use Magento \Framework \Filesystem ;
11
12
use Magento \Framework \Filesystem \Directory \ReadInterface ;
12
13
use Magento \Framework \Filesystem \DriverPool ;
@@ -53,6 +54,11 @@ class FileTest extends \PHPUnit_Framework_TestCase
53
54
*/
54
55
private $ escaper ;
55
56
57
+ /**
58
+ * @var \Magento\Quote\Model\Quote\Item\OptionFactory|\PHPUnit_Framework_MockObject_MockObject
59
+ */
60
+ private $ itemOptionFactoryMock ;
61
+
56
62
protected function setUp ()
57
63
{
58
64
$ this ->objectManager = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
@@ -81,6 +87,11 @@ protected function setUp()
81
87
->disableOriginalConstructor ()
82
88
->getMock ();
83
89
90
+ $ this ->itemOptionFactoryMock = $ this ->getMockBuilder (\Magento \Quote \Model \Quote \Item \OptionFactory::class)
91
+ ->setMethods (['create ' ])
92
+ ->disableOriginalConstructor ()
93
+ ->getMock ();
94
+
84
95
$ this ->coreFileStorageDatabase = $ this ->getMock (
85
96
\Magento \MediaStorage \Helper \File \Storage \Database::class,
86
97
['copyFile ' ],
@@ -102,7 +113,8 @@ protected function getFileObject()
102
113
'coreFileStorageDatabase ' => $ this ->coreFileStorageDatabase ,
103
114
'serializer ' => $ this ->serializer ,
104
115
'urlBuilder ' => $ this ->urlBuilder ,
105
- 'escaper ' => $ this ->escaper
116
+ 'escaper ' => $ this ->escaper ,
117
+ 'itemOptionFactory ' => $ this ->itemOptionFactoryMock ,
106
118
]
107
119
);
108
120
}
@@ -222,6 +234,141 @@ public function testGetFormattedOptionValue()
222
234
$ fileObject ->getFormattedOptionValue ($ optionValue );
223
235
}
224
236
237
+ public function testGetFormattedOptionValueInvalid ()
238
+ {
239
+ $ optionValue = 'invalid json option value... ' ;
240
+ $ this ->serializer ->expects ($ this ->once ())
241
+ ->method ('unserialize ' )
242
+ ->willThrowException (new SerializationException (__ ('Invalid JSON value. ' )));
243
+ $ this ->assertEquals ($ optionValue , $ this ->getFileObject ()->getFormattedOptionValue ($ optionValue ));
244
+ }
245
+
246
+ public function testGetEditableOptionValue ()
247
+ {
248
+ $ configurationItemOption = $ this ->getMockBuilder (\Magento \Catalog \Model \Product \Configuration \Item \Option \OptionInterface::class)
249
+ ->disableOriginalConstructor ()
250
+ ->setMethods (['getId ' , 'getValue ' ])
251
+ ->getMock ();
252
+ $ configurationItemOption ->expects ($ this ->once ())
253
+ ->method ('getId ' )
254
+ ->will ($ this ->returnValue (2 ));
255
+ $ fileObject = $ this ->getFileObject ()->setData ('configuration_item_option ' , $ configurationItemOption );
256
+ $ optionTitle = 'Option Title ' ;
257
+ $ optionValue = json_encode (['title ' => $ optionTitle ]);
258
+ $ this ->serializer ->expects ($ this ->once ())
259
+ ->method ('unserialize ' )
260
+ ->with ($ optionValue )
261
+ ->willReturn (json_decode ($ optionValue , true ));
262
+ $ this ->escaper ->expects ($ this ->once ())
263
+ ->method ('escapeHtml ' )
264
+ ->with ($ optionTitle )
265
+ ->will ($ this ->returnValue ($ optionTitle ));
266
+
267
+ $ this ->assertEquals ('Option Title [2] ' , $ fileObject ->getEditableOptionValue ($ optionValue ));
268
+ }
269
+
270
+ public function testGetEditableOptionValueInvalid ()
271
+ {
272
+ $ fileObject = $ this ->getFileObject ();
273
+ $ optionTitle = 'Option Title ' ;
274
+ $ optionValue = json_encode (['title ' => $ optionTitle ]);
275
+ $ this ->serializer ->expects ($ this ->once ())
276
+ ->method ('unserialize ' )
277
+ ->with ($ optionValue )
278
+ ->willThrowException (new SerializationException (__ ('Invalid JSON value. ' )));
279
+ $ this ->escaper ->expects ($ this ->never ())
280
+ ->method ('escapeHtml ' );
281
+
282
+ $ this ->assertEquals ($ optionValue , $ fileObject ->getEditableOptionValue ($ optionValue ));
283
+ }
284
+
285
+ public function testParseOptionValue ()
286
+ {
287
+ $ optionTitle = 'Option Title ' ;
288
+ $ optionValue = json_encode (['title ' => $ optionTitle ]);
289
+
290
+ $ userInput = 'Option [2] ' ;
291
+ $ fileObject = $ this ->getFileObject ();
292
+
293
+ $ itemMock = $ this ->getMockBuilder (\Magento \Quote \Model \Quote \Item \Option::class)
294
+ ->disableOriginalConstructor ()
295
+ ->setMethods (['load ' , 'getValue ' ])
296
+ ->getMock ();
297
+
298
+ $ itemMock ->expects ($ this ->any ())
299
+ ->method ('load ' )
300
+ ->will ($ this ->returnSelf ());
301
+
302
+ $ itemMock ->expects ($ this ->any ())
303
+ ->method ('getValue ' )
304
+ ->will ($ this ->returnValue ($ optionValue ));
305
+
306
+ $ this ->itemOptionFactoryMock ->expects ($ this ->any ())
307
+ ->method ('create ' )
308
+ ->will ($ this ->returnValue ($ itemMock ));
309
+
310
+ $ this ->assertEquals ($ optionValue , $ fileObject ->parseOptionValue ($ userInput , []));
311
+ }
312
+
313
+ public function testParseOptionValueNoId ()
314
+ {
315
+ $ optionValue = 'value ' ;
316
+
317
+ $ userInput = 'Option [xx] ' ;
318
+ $ fileObject = $ this ->getFileObject ();
319
+
320
+ $ itemMock = $ this ->getMockBuilder (\Magento \Quote \Model \Quote \Item \Option::class)
321
+ ->disableOriginalConstructor ()
322
+ ->setMethods (['load ' , 'getValue ' ])
323
+ ->getMock ();
324
+
325
+ $ itemMock ->expects ($ this ->any ())
326
+ ->method ('load ' )
327
+ ->will ($ this ->returnSelf ());
328
+
329
+ $ itemMock ->expects ($ this ->any ())
330
+ ->method ('getValue ' )
331
+ ->will ($ this ->returnValue ($ optionValue ));
332
+
333
+ $ this ->itemOptionFactoryMock ->expects ($ this ->any ())
334
+ ->method ('create ' )
335
+ ->will ($ this ->returnValue ($ itemMock ));
336
+
337
+ $ this ->assertEquals (null , $ fileObject ->parseOptionValue ($ userInput , []));
338
+ }
339
+
340
+ public function testParseOptionValueInvalid ()
341
+ {
342
+ $ optionValue = 'Invalid json serialized value... ' ;
343
+
344
+ $ userInput = 'Option [2] ' ;
345
+ $ fileObject = $ this ->getFileObject ();
346
+
347
+ $ itemMock = $ this ->getMockBuilder (\Magento \Quote \Model \Quote \Item \Option::class)
348
+ ->disableOriginalConstructor ()
349
+ ->setMethods (['load ' , 'getValue ' ])
350
+ ->getMock ();
351
+
352
+ $ itemMock ->expects ($ this ->any ())
353
+ ->method ('load ' )
354
+ ->will ($ this ->returnSelf ());
355
+
356
+ $ itemMock ->expects ($ this ->any ())
357
+ ->method ('getValue ' )
358
+ ->will ($ this ->returnValue ($ optionValue ));
359
+
360
+ $ this ->itemOptionFactoryMock ->expects ($ this ->any ())
361
+ ->method ('create ' )
362
+ ->will ($ this ->returnValue ($ itemMock ));
363
+
364
+ $ this ->serializer ->expects ($ this ->once ())
365
+ ->method ('unserialize ' )
366
+ ->with ($ optionValue )
367
+ ->willThrowException (new SerializationException (__ ('Invalid JSON value. ' )));
368
+
369
+ $ this ->assertEquals (null , $ fileObject ->parseOptionValue ($ userInput , []));
370
+ }
371
+
225
372
public function testPrepareOptionValueForRequest ()
226
373
{
227
374
$ optionValue = 'string ' ;
0 commit comments