@@ -21,18 +21,30 @@ 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 ->getMock (\Magento \Framework \App \State::class, [], [], '' , false );
34
+ $ this ->_appStateMock = $ this ->getMock (
35
+ \Magento \Framework \App \State::class,
36
+ [],
37
+ [],
38
+ '' ,
39
+ false
40
+ );
41
+ $ this ->serializerMock = $ this ->getMockBuilder (\Magento \Framework \Serialize \Serializer \Json::class)
42
+ ->getMock ();
32
43
/** Initialize SUT. */
33
44
$ this ->_jsonDeserializer = new \Magento \Framework \Webapi \Rest \Request \Deserializer \Json (
34
45
$ this ->decoderMock ,
35
- $ this ->_appStateMock
46
+ $ this ->_appStateMock ,
47
+ $ this ->serializerMock
36
48
);
37
49
parent ::setUp ();
38
50
}
@@ -60,13 +72,13 @@ public function testDeserialize()
60
72
'key2 ' => 'test2 ' ,
61
73
'array ' => ['test01 ' => 'some1 ' , 'test02 ' => 'some2 ' ],
62
74
];
63
- $ this ->decoderMock ->expects (
64
- $ this -> once ( )
65
- )-> method (
66
- ' decode '
67
- )-> will (
68
- $ this -> returnValue ( $ expectedDecodedJson )
69
- );
75
+ $ this ->serializerMock ->expects ($ this -> any ())
76
+ -> method ( ' unserialize ' )
77
+ -> willReturnCallback (
78
+ function ( $ serializedData ) {
79
+ return json_decode ( $ serializedData , true );
80
+ }
81
+ );
70
82
/** Initialize SUT. */
71
83
$ this ->assertEquals (
72
84
$ expectedDecodedJson ,
@@ -78,9 +90,10 @@ public function testDeserialize()
78
90
public function testDeserializeInvalidEncodedBodyExceptionDeveloperModeOff ()
79
91
{
80
92
/** Prepare mocks for SUT constructor. */
81
- $ this ->decoderMock ->expects ($ this ->once ())
82
- ->method ('decode ' )
83
- ->will ($ this ->throwException (new \Zend_Json_Exception ));
93
+ $ this ->serializerMock
94
+ ->expects ($ this ->once ())
95
+ ->method ('unserialize ' )
96
+ ->will ($ this ->throwException (new \InvalidArgumentException ));
84
97
$ this ->_appStateMock ->expects ($ this ->once ())
85
98
->method ('getMode ' )
86
99
->will ($ this ->returnValue ('production ' ));
@@ -103,15 +116,14 @@ public function testDeserializeInvalidEncodedBodyExceptionDeveloperModeOff()
103
116
public function testDeserializeInvalidEncodedBodyExceptionDeveloperModeOn ()
104
117
{
105
118
/** 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
- );
119
+ $ this ->serializerMock
120
+ ->expects ($ this ->once ())
121
+ ->method ('unserialize ' )
122
+ ->will (
123
+ $ this ->throwException (
124
+ new \InvalidArgumentException ('Unable to unserialize value. ' )
125
+ )
126
+ );
115
127
$ this ->_appStateMock ->expects ($ this ->once ())
116
128
->method ('getMode ' )
117
129
->will ($ this ->returnValue ('developer ' ));
0 commit comments