Skip to content

Commit 16c38f2

Browse files
committed
add initial testing
1 parent 98059b7 commit 16c38f2

File tree

5 files changed

+176
-25
lines changed

5 files changed

+176
-25
lines changed

Diff for: .idea/php-test-framework.xml

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: .phpunit.result.cache

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"version":1,"defects":{"Devgoto\\LaravelApiResponse\\Tests\\Feature\\ResponseTest::it_returns_200_on_success_response":5},"times":{"Devgoto\\LaravelApiResponse\\Tests\\Feature\\ResponseTest::test_success_response_default with data set \"respondWithSuccess(), default empty array\"":0.239,"Devgoto\\LaravelApiResponse\\Tests\\Feature\\ResponseTest::it_returns_200_on_success_response":0.114,"Devgoto\\LaravelApiResponse\\Tests\\Feature\\ResponseTest::it_returns_201_on_created_response":0.008,"Devgoto\\LaravelApiResponse\\Tests\\Feature\\ResponseTest::it_returns_202_on_accepted_response":0.009,"Devgoto\\LaravelApiResponse\\Tests\\Feature\\ResponseTest::it_returns_204_on_no_content_response":0.008,"Devgoto\\LaravelApiResponse\\Tests\\Feature\\ResponseTest::it_returns_400_on_bad_request_response":0.007,"Devgoto\\LaravelApiResponse\\Tests\\Feature\\ResponseTest::it_returns_401_on_unauthorized_response":0.01,"Devgoto\\LaravelApiResponse\\Tests\\Feature\\ResponseTest::it_returns_403_on_not_found_response":0.007,"Devgoto\\LaravelApiResponse\\Tests\\Feature\\ResponseTest::it_returns_404_on_forbidden_response":0.14,"Devgoto\\LaravelApiResponse\\Tests\\Feature\\ResponseTest::it_returns_405_on_method_not_allowed_response":0.008,"Devgoto\\LaravelApiResponse\\Tests\\Feature\\ResponseTest::it_returns_406_on_not_acceptable_response":0.007,"Devgoto\\LaravelApiResponse\\Tests\\Feature\\ResponseTest::it_returns_418_on_teapot_response":0.008,"Devgoto\\LaravelApiResponse\\Tests\\Feature\\ResponseTest::it_returns_422_on_unprocessable_entity_response":0.009}}

Diff for: composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
},
2323
"autoload-dev": {
2424
"psr-4": {
25-
"Devgoto\\ApiResponse\\Tests\\": "tests"
25+
"Devgoto\\LaravelApiResponse\\Tests\\": "tests"
2626
}
2727
},
2828
"scripts": {

Diff for: src/Traits/BuildsResponse.php

+70-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,79 @@
22

33
namespace Devgoto\Traits;
44

5+
use Illuminate\Http\Response;
6+
57
trait BuildsResponse
68
{
7-
private function respond(array $data, int $statusCode = 200)
9+
private function respond($data, int $statusCode = 200, bool $success = true)
10+
{
11+
return $this->jsonResponse(['data' => $data, 'success' => $success, 'code' => 0], $statusCode);
12+
}
13+
14+
private function respondWithError($error, int $statusCode = 400, bool $success = false)
15+
{
16+
return $this->jsonResponse(['error' => $error, 'success' => $success, 'code' => 0], $statusCode);
17+
}
18+
19+
public function respondWithSuccess($data = null)
20+
{
21+
return $this->respond($data, Response::HTTP_OK, true);
22+
}
23+
24+
public function respondWithCreated($data = null)
25+
{
26+
return $this->respond($data, Response::HTTP_CREATED, true);
27+
}
28+
29+
30+
public function respondWithAccepted($data = null)
31+
{
32+
return $this->respond($data, Response::HTTP_ACCEPTED, true);
33+
}
34+
35+
public function respondWithNoContent()
36+
{
37+
return response(null, Response::HTTP_NO_CONTENT);
38+
}
39+
40+
public function respondWithBadRequest($message = 'Bad request')
41+
{
42+
return $this->respondWithError($message, Response::HTTP_BAD_REQUEST);
43+
}
44+
45+
public function respondWithUnathorized($message = 'Unauthorized')
46+
{
47+
return $this->respondWithError($message, Response::HTTP_UNAUTHORIZED);
48+
}
49+
50+
public function respondWithForbidden($message = 'Forbidden')
51+
{
52+
return $this->respondWithError($message, Response::HTTP_FORBIDDEN);
53+
}
54+
55+
public function respondWithNotFound($message = 'Not found')
56+
{
57+
return $this->respondWithError($message, Response::HTTP_NOT_FOUND);
58+
}
59+
60+
public function respondWithMethodNotAllowed($message = 'Method not allowed')
61+
{
62+
return $this->respondWithError($message, Response::HTTP_METHOD_NOT_ALLOWED);
63+
}
64+
65+
public function respondWithNotAcceptable($message = 'Not acceptable')
66+
{
67+
return $this->respondWithError($message, Response::HTTP_NOT_ACCEPTABLE);
68+
}
69+
70+
public function respondWithTeapot($message = 'I am a teapot')
71+
{
72+
return $this->respondWithError($message, Response::HTTP_I_AM_A_TEAPOT);
73+
}
74+
75+
public function respondWithUnprocessableEntity($message = 'Unprocessable entity')
876
{
9-
return $this->jsonResponse($data, $statusCode);
77+
return $this->respondWithError($message, Response::HTTP_UNPROCESSABLE_ENTITY);
1078
}
1179

1280
private function jsonResponse(array $data, int $statusCode = 200)

Diff for: tests/Feature/ResponseTest.php

+90-22
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,108 @@
11
<?php
22

3-
namespace Devgoto\ApiResponse\Tests\Feature;
3+
namespace Devgoto\LaravelApiResponse\Tests\Feature;
44

55
use Devgoto\Traits\BuildsResponse;
6-
use Illuminate\Http\JsonResponse;
6+
use Illuminate\Http\Response;
77
use Orchestra\Testbench\TestCase;
8-
use Symfony\Component\HttpFoundation\Response;
98

109
class ResponseTest extends TestCase
1110
{
12-
protected object $service;
11+
use BuildsResponse;
1312

14-
public function setUp(): void
13+
/** @test */
14+
public function it_returns_200_on_success_response()
1515
{
16-
$this->service = $this->getObjectForTrait(BuildsResponse::class);
17-
parent::setUp();
16+
$response = $this->respondWithSuccess();
17+
18+
$this->assertEquals(Response::HTTP_OK, $response->status());
19+
}
20+
21+
/** @test */
22+
public function it_returns_201_on_created_response()
23+
{
24+
$response = $this->respondWithCreated();
25+
26+
$this->assertEquals(Response::HTTP_CREATED, $response->status());
27+
}
28+
29+
/** @test */
30+
public function it_returns_202_on_accepted_response()
31+
{
32+
$response = $this->respondWithAccepted();
33+
34+
$this->assertEquals(Response::HTTP_ACCEPTED, $response->status());
35+
}
36+
37+
/** @test */
38+
public function it_returns_204_on_no_content_response()
39+
{
40+
$response = $this->respondWithNoContent();
41+
42+
$this->assertEquals(Response::HTTP_NO_CONTENT, $response->status());
43+
}
44+
45+
/** @test */
46+
public function it_returns_400_on_bad_request_response()
47+
{
48+
$response = $this->respondWithBadRequest();
49+
50+
$this->assertEquals(Response::HTTP_BAD_REQUEST, $response->status());
1851
}
1952

20-
/**
21-
* @dataProvider successDefaultDataProvider
22-
*/
23-
public function test_success_response_default(array $args)
53+
/** @test */
54+
public function it_returns_401_on_unauthorized_response()
2455
{
25-
$response = $this->service->respondWithSuccess($args);
26-
self::assertInstanceOf(JsonResponse::class, $response);
56+
$response = $this->respondWithUnathorized();
57+
58+
$this->assertEquals(Response::HTTP_UNAUTHORIZED, $response->status());
2759
}
2860

29-
public function successDefaultDataProvider(): array
61+
/** @test */
62+
public function it_returns_403_on_not_found_response()
3063
{
31-
return [
32-
'respondWithSuccess(), default empty array' => [
33-
'default' => [],
34-
'args' => [],
35-
'code' => Response::HTTP_OK,
36-
'data' => []
37-
]
38-
];
64+
$response = $this->respondWithNotFound();
65+
66+
$this->assertEquals(Response::HTTP_NOT_FOUND, $response->status());
67+
}
68+
69+
/** @test */
70+
public function it_returns_404_on_forbidden_response()
71+
{
72+
$response = $this->respondWithForbidden();
73+
74+
$this->assertEquals(Response::HTTP_FORBIDDEN, $response->status());
75+
}
76+
77+
/** @test */
78+
public function it_returns_405_on_method_not_allowed_response()
79+
{
80+
$response = $this->respondWithMethodNotAllowed();
81+
82+
$this->assertEquals(Response::HTTP_METHOD_NOT_ALLOWED, $response->status());
83+
}
84+
85+
/** @test */
86+
public function it_returns_406_on_not_acceptable_response()
87+
{
88+
$response = $this->respondWithNotAcceptable();
89+
90+
$this->assertEquals(Response::HTTP_NOT_ACCEPTABLE, $response->status());
91+
}
92+
93+
/** @test */
94+
public function it_returns_418_on_teapot_response()
95+
{
96+
$response = $this->respondWithTeapot();
97+
98+
$this->assertEquals(Response::HTTP_I_AM_A_TEAPOT, $response->status());
99+
}
100+
101+
/** @test */
102+
public function it_returns_422_on_unprocessable_entity_response()
103+
{
104+
$response = $this->respondWithUnprocessableEntity();
105+
106+
$this->assertEquals(Response::HTTP_UNPROCESSABLE_ENTITY, $response->status());
39107
}
40108
}

0 commit comments

Comments
 (0)