Skip to content

Commit

Permalink
Adds "successRequestWithNoData" method
Browse files Browse the repository at this point in the history
  • Loading branch information
ksbomj committed Sep 14, 2020
1 parent 6759b4b commit 3498434
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea
vendor
vendor
.phpunit.result.cache
18 changes: 17 additions & 1 deletion src/JsonResponseTrait.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<?php

declare(strict_types=1);

namespace SecurityRobot;

trait JsonErrorResponse {
use Illuminate\Http\{Response, JsonResponse};

trait JsonResponseTrait {
/**
* @var int HTTP status code
*
Expand Down Expand Up @@ -143,4 +148,15 @@ public function respondValidationErrors(array $validation_errors, string $messag
'errors' => $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);
}
}
32 changes: 19 additions & 13 deletions tests/JsonResponseTraitTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Tests;

use Illuminate\Http\Response;
use PHPUnit\Framework\TestCase;
use \Illuminate\Http\JsonResponse;
use SecurityRobot\JsonResponseTrait;
Expand All @@ -16,7 +17,7 @@ public function setUp(): void
{
parent::setUp();

$this->trait = $this->getMockForTrait('SecurityRobot\JsonResponseTrait');
$this->trait = $this->getMockForTrait(JsonResponseTrait::class);
}

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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}();
Expand All @@ -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);
Expand All @@ -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());
}
}

0 comments on commit 3498434

Please sign in to comment.