@@ -21,18 +21,30 @@ class JsonTest extends \PHPUnit_Framework_TestCase
2121 /** @var \PHPUnit_Framework_MockObject_MockObject */
2222 protected $ _appStateMock ;
2323
24+ /** @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject */
25+ private $ serializerMock ;
26+
2427 protected function setUp ()
2528 {
2629 /** Prepare mocks for SUT constructor. */
2730 $ this ->decoderMock = $ this ->getMockBuilder (\Magento \Framework \Json \Decoder::class)
2831 ->disableOriginalConstructor ()
2932 ->setMethods (['decode ' ])
3033 ->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 ();
3243 /** Initialize SUT. */
3344 $ this ->_jsonDeserializer = new \Magento \Framework \Webapi \Rest \Request \Deserializer \Json (
3445 $ this ->decoderMock ,
35- $ this ->_appStateMock
46+ $ this ->_appStateMock ,
47+ $ this ->serializerMock
3648 );
3749 parent ::setUp ();
3850 }
@@ -60,13 +72,13 @@ public function testDeserialize()
6072 'key2 ' => 'test2 ' ,
6173 'array ' => ['test01 ' => 'some1 ' , 'test02 ' => 'some2 ' ],
6274 ];
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+ );
7082 /** Initialize SUT. */
7183 $ this ->assertEquals (
7284 $ expectedDecodedJson ,
@@ -78,9 +90,10 @@ public function testDeserialize()
7890 public function testDeserializeInvalidEncodedBodyExceptionDeveloperModeOff ()
7991 {
8092 /** 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 ));
8497 $ this ->_appStateMock ->expects ($ this ->once ())
8598 ->method ('getMode ' )
8699 ->will ($ this ->returnValue ('production ' ));
@@ -103,15 +116,14 @@ public function testDeserializeInvalidEncodedBodyExceptionDeveloperModeOff()
103116 public function testDeserializeInvalidEncodedBodyExceptionDeveloperModeOn ()
104117 {
105118 /** 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+ );
115127 $ this ->_appStateMock ->expects ($ this ->once ())
116128 ->method ('getMode ' )
117129 ->will ($ this ->returnValue ('developer ' ));
0 commit comments