From 5ec21de1e4900f0d1378d27d818cb627d1d367f6 Mon Sep 17 00:00:00 2001 From: gam6itko Date: Fri, 15 Sep 2023 18:57:31 +0300 Subject: [PATCH] FakeHttp add method `getJsonParsedBody` --- src/Http/TestResponse.php | 8 ++++++++ tests/src/Http/FakeHttpTest.php | 14 ++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/Http/TestResponse.php b/src/Http/TestResponse.php index 216c5bc..e6d3d04 100644 --- a/src/Http/TestResponse.php +++ b/src/Http/TestResponse.php @@ -219,4 +219,12 @@ public function getCookies(): array { return $this->cookies; } + + public function getJsonParsedBody(): array + { + return \json_decode( + (string)$this->response->getBody(), + true + ); + } } diff --git a/tests/src/Http/FakeHttpTest.php b/tests/src/Http/FakeHttpTest.php index 2c54083..31032d4 100644 --- a/tests/src/Http/FakeHttpTest.php +++ b/tests/src/Http/FakeHttpTest.php @@ -45,4 +45,18 @@ public function testGetWithDefaultHeaders(): void $response = $http->get('/get/headers'); $response->assertBodySame('{"baz":["bar"],"foo":["bar"]}'); } + + public function testGetJsonParsedBody(): void + { + $http = $this->fakeHttp(); + $arr = [ + 'foo' => 'bar', + 'list' => [1, 2, 3, 4] + ]; + $response = $http->get('/get/query-params', $arr); + self::assertSame( + $arr, + $response->getJsonParsedBody() + ); + } }