66use Http \Client \Exception \NetworkException ;
77use Http \Client \HttpClient ;
88use Nyholm \Psr7 \Request ;
9+ use Nyholm \Psr7 \Stream ;
910use PHPUnit \Framework \TestCase ;
1011use Psr \Http \Message \ResponseInterface ;
12+ use Psr \Http \Message \StreamInterface ;
1113use Symfony \Component \Cache \Simple \FilesystemCache ;
1214
1315class JsonResponseDecoderTest extends TestCase
@@ -42,7 +44,7 @@ public function testGetDecodedResponse()
4244 $ request = new Request ('GET ' , 'endpoint/data.json ' );
4345 $ response = $ this ->prophesize (ResponseInterface::class);
4446 $ response ->getStatusCode ()->willReturn (200 );
45- $ response ->getBody ()->willReturn (json_encode (['json ' => 'data ' ]));
47+ $ response ->getBody ()->willReturn ($ this -> getResponseBodyStub ( json_encode (['json ' => 'data ' ]) ));
4648
4749 $ this ->client ->sendRequest ($ request )->willReturn ($ response ->reveal ());
4850
@@ -59,7 +61,7 @@ public function testGetDecodedResponseEmptyOnRequestError()
5961 $ request = new Request ('GET ' , 'endpoint/data.json ' );
6062 $ response = $ this ->prophesize (ResponseInterface::class);
6163 $ response ->getStatusCode ()->willReturn (500 );
62- $ response ->getBody ()->willReturn ('' );
64+ $ response ->getBody ()->willReturn ($ this -> getResponseBodyStub ( '' ) );
6365
6466 $ this ->client ->sendRequest ($ request )->willReturn ($ response ->reveal ());
6567
@@ -76,7 +78,7 @@ public function testGetDecodedResponseFromCacheOnRequestError()
7678 $ request = new Request ('GET ' , 'endpoint/data.json ' );
7779 $ response = $ this ->prophesize (ResponseInterface::class);
7880 $ response ->getStatusCode ()->willReturn (500 );
79- $ response ->getBody ()->willReturn ('' );
81+ $ response ->getBody ()->willReturn ($ this -> getResponseBodyStub ( '' ) );
8082
8183 $ this ->simpleCache ->has ('4429b090fd82239e188859ae626162e5e790b4db ' )->willReturn (true );
8284 $ this ->simpleCache ->get ('4429b090fd82239e188859ae626162e5e790b4db ' )->willReturn (['json ' => 'cache ' ]);
@@ -96,7 +98,7 @@ public function testGetDecodedResponseCachesDataIfEnabled()
9698 $ request = new Request ('GET ' , 'endpoint/data.json ' );
9799 $ response = $ this ->prophesize (ResponseInterface::class);
98100 $ response ->getStatusCode ()->willReturn (200 );
99- $ response ->getBody ()->willReturn (json_encode (['json ' => 'cache ' ]));
101+ $ response ->getBody ()->willReturn ($ this -> getResponseBodyStub ( json_encode (['json ' => 'cache ' ]) ));
100102
101103 $ this ->simpleCache ->has ('4429b090fd82239e188859ae626162e5e790b4db ' )->willReturn (false );
102104 $ this ->simpleCache ->set ('4429b090fd82239e188859ae626162e5e790b4db ' , ['json ' => 'cache ' ])->shouldBeCalledOnce ();
@@ -154,7 +156,7 @@ public function testGetDecodedResponseReturnsBodyWhenJsonDecodingFails()
154156 $ request = new Request ('GET ' , 'endpoint/data.json ' );
155157 $ response = $ this ->prophesize (ResponseInterface::class);
156158 $ response ->getStatusCode ()->willReturn (200 );
157- $ response ->getBody ()->willReturn ('{invalid_json ' );
159+ $ response ->getBody ()->willReturn ($ this -> getResponseBodyStub ( '{invalid_json ' ) );
158160
159161 $ this ->client ->sendRequest ($ request )->willReturn ($ response ->reveal ());
160162 $ this ->simpleCache ->set ('4429b090fd82239e188859ae626162e5e790b4db ' , '{invalid_json ' )->shouldBeCalledOnce ();
@@ -166,4 +168,16 @@ public function testGetDecodedResponseReturnsBodyWhenJsonDecodingFails()
166168
167169 $ this ->markAsRisky ();
168170 }
171+
172+ /**
173+ * @param $responseString
174+ * @return Stream|object
175+ */
176+ private function getResponseBodyStub ($ responseString )
177+ {
178+ $ responseBody = $ this ->prophesize (StreamInterface::class);
179+ $ responseBody ->getContents ()->willReturn ($ responseString );
180+
181+ return $ responseBody ->reveal ();
182+ }
169183}
0 commit comments