From 34984344f1794075301aa57607de275f2413bfc2 Mon Sep 17 00:00:00 2001 From: Andrii Tarykin Date: Mon, 14 Sep 2020 23:45:29 +0300 Subject: [PATCH] Adds "successRequestWithNoData" method --- .gitignore | 3 ++- src/JsonResponseTrait.php | 18 +++++++++++++++++- tests/JsonResponseTraitTest.php | 32 +++++++++++++++++++------------- 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 331c58f..8c8076f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .idea -vendor \ No newline at end of file +vendor +.phpunit.result.cache \ No newline at end of file diff --git a/src/JsonResponseTrait.php b/src/JsonResponseTrait.php index 99cf8bc..d926929 100644 --- a/src/JsonResponseTrait.php +++ b/src/JsonResponseTrait.php @@ -1,7 +1,12 @@ $validation_errors, ]); } + + /** + * Returns the empty response with 204 status code + * + * @see https://httpstatuses.com/204 + * + * @return \Illuminate\Http\Response + */ + public function successRequestWithNoData() { + return new Response(null, 204); + } } diff --git a/tests/JsonResponseTraitTest.php b/tests/JsonResponseTraitTest.php index 6cc9211..a084233 100644 --- a/tests/JsonResponseTraitTest.php +++ b/tests/JsonResponseTraitTest.php @@ -1,6 +1,7 @@ trait = $this->getMockForTrait('SecurityRobot\JsonResponseTrait'); + $this->trait = $this->getMockForTrait(JsonResponseTrait::class); } /** @@ -82,10 +83,8 @@ public function it_verifies_that_method_respondWithError_works() $message = 'An error'; $data = [ - 'error' => [ - 'http_code' => 200, - 'message' => $message - ] + 'code' => 200, + 'message' => $message ]; $response = $this->trait->respondWithError($message); @@ -157,10 +156,8 @@ public function it_verifies_that_method_errorForbidden_works() public function assertErrors($call_method, $default_message, $http_code) { $data = [ - 'error' => [ - 'http_code' => $http_code, - 'message' => $default_message - ] + 'code' => $http_code, + 'message' => $default_message ]; $response = $this->trait->{$call_method}(); @@ -172,10 +169,8 @@ public function assertErrors($call_method, $default_message, $http_code) $message = 'Custom message'; $data = [ - 'error' => [ - 'http_code' => $http_code, - 'message' => $message - ] + 'code' => $http_code, + 'message' => $message ]; $response = $this->trait->{$call_method}($message); @@ -184,4 +179,15 @@ public function assertErrors($call_method, $default_message, $http_code) $this->assertEquals($http_code, $response->getStatusCode()); $this->assertEquals(json_encode($data), $response->content()); } + + /** + * @test + */ + public function it_tests_that_successRequestWithNoData_works() { + $response = $this->trait->successRequestWithNoData(); + + $this->assertInstanceOf(Response::class, $response); + $this->assertEquals(204, $response->getStatusCode()); + $this->assertEquals('', $response->content()); + } }