@@ -21,18 +21,26 @@ class JsonTest extends \PHPUnit\Framework\TestCase
21
21
/** @var \PHPUnit_Framework_MockObject_MockObject */
22
22
protected $ _appStateMock ;
23
23
24
+ /** @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject */
25
+ private $ serializerMock ;
26
+
24
27
protected function setUp ()
25
28
{
26
29
/** Prepare mocks for SUT constructor. */
27
30
$ this ->decoderMock = $ this ->getMockBuilder (\Magento \Framework \Json \Decoder::class)
28
31
->disableOriginalConstructor ()
29
32
->setMethods (['decode ' ])
30
33
->getMock ();
31
- $ this ->_appStateMock = $ this ->createMock (\Magento \Framework \App \State::class);
34
+ $ this ->_appStateMock = $ this ->createMock (
35
+ \Magento \Framework \App \State::class
36
+ );
37
+ $ this ->serializerMock = $ this ->getMockBuilder (\Magento \Framework \Serialize \Serializer \Json::class)
38
+ ->getMock ();
32
39
/** Initialize SUT. */
33
40
$ this ->_jsonDeserializer = new \Magento \Framework \Webapi \Rest \Request \Deserializer \Json (
34
41
$ this ->decoderMock ,
35
- $ this ->_appStateMock
42
+ $ this ->_appStateMock ,
43
+ $ this ->serializerMock
36
44
);
37
45
parent ::setUp ();
38
46
}
@@ -60,13 +68,13 @@ public function testDeserialize()
60
68
'key2 ' => 'test2 ' ,
61
69
'array ' => ['test01 ' => 'some1 ' , 'test02 ' => 'some2 ' ],
62
70
];
63
- $ this ->decoderMock ->expects (
64
- $ this -> once ( )
65
- )-> method (
66
- ' decode '
67
- )-> will (
68
- $ this -> returnValue ( $ expectedDecodedJson )
69
- );
71
+ $ this ->serializerMock ->expects ($ this -> any ())
72
+ -> method ( ' unserialize ' )
73
+ -> willReturnCallback (
74
+ function ( $ serializedData ) {
75
+ return json_decode ( $ serializedData , true );
76
+ }
77
+ );
70
78
/** Initialize SUT. */
71
79
$ this ->assertEquals (
72
80
$ expectedDecodedJson ,
@@ -78,9 +86,10 @@ public function testDeserialize()
78
86
public function testDeserializeInvalidEncodedBodyExceptionDeveloperModeOff ()
79
87
{
80
88
/** Prepare mocks for SUT constructor. */
81
- $ this ->decoderMock ->expects ($ this ->once ())
82
- ->method ('decode ' )
83
- ->will ($ this ->throwException (new \Zend_Json_Exception ));
89
+ $ this ->serializerMock
90
+ ->expects ($ this ->once ())
91
+ ->method ('unserialize ' )
92
+ ->will ($ this ->throwException (new \InvalidArgumentException ));
84
93
$ this ->_appStateMock ->expects ($ this ->once ())
85
94
->method ('getMode ' )
86
95
->will ($ this ->returnValue ('production ' ));
@@ -103,15 +112,14 @@ public function testDeserializeInvalidEncodedBodyExceptionDeveloperModeOff()
103
112
public function testDeserializeInvalidEncodedBodyExceptionDeveloperModeOn ()
104
113
{
105
114
/** Prepare mocks for SUT constructor. */
106
- $ this ->decoderMock ->expects (
107
- $ this ->once ()
108
- )->method (
109
- 'decode '
110
- )->will (
111
- $ this ->throwException (
112
- new \Zend_Json_Exception ('Decoding error: ' . PHP_EOL . 'Decoding failed: Syntax error ' )
113
- )
114
- );
115
+ $ this ->serializerMock
116
+ ->expects ($ this ->once ())
117
+ ->method ('unserialize ' )
118
+ ->will (
119
+ $ this ->throwException (
120
+ new \InvalidArgumentException ('Unable to unserialize value. ' )
121
+ )
122
+ );
115
123
$ this ->_appStateMock ->expects ($ this ->once ())
116
124
->method ('getMode ' )
117
125
->will ($ this ->returnValue ('developer ' ));
0 commit comments