From 1f2734b85c8c2e4aa21a78c62145a5f1a17bb12b Mon Sep 17 00:00:00 2001 From: Guilherme Branco Stracini Date: Thu, 16 Jan 2025 00:47:47 +0000 Subject: [PATCH] Add methods `getBodyAsJson` and `getBodyAsArray` to `Response` class (#307) * Update Response.php +semver: minor * style: format code with PHP CS Fixer This commit fixes the style issues introduced in 16302cb according to the output from PHP CS Fixer. Details: https://github.com/guibranco/pancake/pull/307 * Update Response.php * Update Response.php * #1 test - Add unit tests for Response class methods * #1 remove unused import from ResponseTest.php * #1 fix logical condition checks for JSON validation * #1 refactor - removed unnecessary validation for JSON body checks * #1 added unit tests for Response class functionality * #1 remove obsolete test method for Response class --------- Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com> --- src/Response.php | 41 +++++++++++++++++++++++++++++ tests/Unit/ResponseTest.php | 51 +++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) diff --git a/src/Response.php b/src/Response.php index 105c5987b..4e2e1c9f6 100644 --- a/src/Response.php +++ b/src/Response.php @@ -3,6 +3,7 @@ namespace GuiBranco\Pancake; use GuiBranco\Pancake\RequestException; +use JsonException; class Response { @@ -71,6 +72,46 @@ public function getBody(): ?string return $this->body; } + /** + * Get the HTTP request body as a decoded JSON object. + * + * @return object|null Returns an object if the body is valid JSON, or null otherwise. + * @throws JsonException If JSON decoding fails. + */ + public function getBodyAsJson(): ?object + { + if ($this->body === null) { + return null; + } + + try { + $decoded = json_decode($this->body, false, 512, JSON_THROW_ON_ERROR); + return $decoded; + } catch (JsonException $e) { + throw new JsonException('Invalid JSON string: ' . $e->getMessage(), 0, $e); + } + } + + /** + * Get the HTTP request body as a decoded JSON array. + * + * @return array|null Returns an array if the body is valid JSON, or null otherwise. + * @throws JsonException If JSON decoding fails. + */ + public function getBodyAsArray(): ?array + { + if ($this->body === null) { + return null; + } + + try { + $decoded = json_decode($this->body, true, 512, JSON_THROW_ON_ERROR); + return $decoded; + } catch (JsonException $e) { + throw new JsonException('Invalid JSON string: ' . $e->getMessage(), 0, $e); + } + } + /** * Retrieves the message. * diff --git a/tests/Unit/ResponseTest.php b/tests/Unit/ResponseTest.php index 418ffdd1d..b2ef2a3dc 100644 --- a/tests/Unit/ResponseTest.php +++ b/tests/Unit/ResponseTest.php @@ -93,4 +93,55 @@ public function testToJson() $this->assertJsonStringEqualsJsonString($expectedJson, $response->toJson()); } + + public function testGetBodyAsJson() + { + $response = Response::success(json_encode(['key' => 'value']), "http://example.com", ["Content-Type" => "application/json"], 200); + + $this->assertEquals((object) ['key' => 'value'], $response->getBodyAsJson()); + } + + public function testGetBodyAsJsonReturnsNull() + { + $response = Response::error("Error occurred", "http://example.com", 400); + + $this->assertEquals(null, $response->getBodyAsJson()); + } + + public function testGetBodyAsJsonThrowsException() + { + $response = Response::success("Invalid JSON", "http://example.com", ["Content-Type" => "application/json"], 200); + + $this->expectException(JsonException::class); + $response->getBodyAsJson(); + } + + public function testGetBodyAsJsonWithEmptyObject() + { + $response = Response::success('{}', "http://example.com", ["Content-Type" => "application/json"], 200); + + $this->assertEquals(new \stdClass(), $response->getBodyAsJson()); + } + + public function testGetBodyAsArray() + { + $response = Response::success(json_encode(['key' => 'value']), "http://example.com", ["Content-Type" => "application/json"], 200); + + $this->assertEquals(['key' => 'value'], $response->getBodyAsArray()); + } + + public function testGetBodyAsArrayReturnsNull() + { + $response = Response::error("Error occurred", "http://example.com", 400); + + $this->assertEquals(null, $response->getBodyAsArray()); + } + + public function testGetBodyAsArrayThrowsException() + { + $response = Response::success("Invalid JSON", "http://example.com", ["Content-Type" => "application/json"], 200); + + $this->expectException(JsonException::class); + $response->getBodyAsArray(); + } }