From 2cab39aca93381c978b0d02dd88b63fb8f6c2ade Mon Sep 17 00:00:00 2001 From: Luke Kuzmish <42181698+cosmastech@users.noreply.github.com> Date: Sat, 19 Oct 2024 13:50:50 -0400 Subject: [PATCH 1/2] add responseStatusCode --- src/Concerns/ResponsableData.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Concerns/ResponsableData.php b/src/Concerns/ResponsableData.php index 33578544..33393143 100644 --- a/src/Concerns/ResponsableData.php +++ b/src/Concerns/ResponsableData.php @@ -12,6 +12,8 @@ trait ResponsableData { + protected int $responseStatusCode; + public function toResponse($request) { $contextFactory = TransformationContextFactory::create() @@ -63,8 +65,20 @@ public function toResponse($request) ); } + public function setResponseStatusCode(int $responseStatusCode): self + { + $this->responseStatusCode = $responseStatusCode; + + return $this; + } + protected function calculateResponseStatus(Request $request): int { + $responseStatusCode = $this->responseStatusCode ?? null; + if ($responseStatusCode !== null) { + return $responseStatusCode; + } + return $request->isMethod(Request::METHOD_POST) ? Response::HTTP_CREATED : Response::HTTP_OK; } From b98311f779c80f9c0e8848b0f56befe08971852e Mon Sep 17 00:00:00 2001 From: Luke Kuzmish Date: Sun, 20 Oct 2024 07:45:36 -0400 Subject: [PATCH 2/2] tests --- src/Concerns/ResponsableData.php | 6 +++--- tests/PartialsTest.php | 13 +++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Concerns/ResponsableData.php b/src/Concerns/ResponsableData.php index 33393143..56816baf 100644 --- a/src/Concerns/ResponsableData.php +++ b/src/Concerns/ResponsableData.php @@ -12,7 +12,7 @@ trait ResponsableData { - protected int $responseStatusCode; + protected int $_responseStatusCode; public function toResponse($request) { @@ -67,14 +67,14 @@ public function toResponse($request) public function setResponseStatusCode(int $responseStatusCode): self { - $this->responseStatusCode = $responseStatusCode; + $this->_responseStatusCode = $responseStatusCode; return $this; } protected function calculateResponseStatus(Request $request): int { - $responseStatusCode = $this->responseStatusCode ?? null; + $responseStatusCode = $this->_responseStatusCode ?? null; if ($responseStatusCode !== null) { return $responseStatusCode; } diff --git a/tests/PartialsTest.php b/tests/PartialsTest.php index 28027407..4e89b87b 100644 --- a/tests/PartialsTest.php +++ b/tests/PartialsTest.php @@ -1647,3 +1647,16 @@ public static function allowedRequestExcept(): ?array ]); // Not really a test with expectation, we just want to check we don't end up in an infinite loop }); + +it('sets response code', function () { + // Given + $data = new SimpleData("roxanne"); + // And + $data->setResponseStatusCode(404); + + // When + $jsonResponse = $data->toResponse(request()); + + // Then + expect($jsonResponse->status())->toBe(404); +});