Skip to content

Commit

Permalink
add ApiResponse::invalid helper
Browse files Browse the repository at this point in the history
  • Loading branch information
lakuapik committed Dec 11, 2023
1 parent 6d11964 commit 171a986
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/ApiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ public function badRequest(mixed $errors = []): static
->message(__('api-response::trans.request_is_bad_request'));
}

public function invalid(string $key, string|array $messages): static
{
$messages = is_string($messages) ? [$messages] : $messages;

return static::error([$key => $messages], static::HTTP_UNPROCESSABLE_ENTITY)
->title(__('api-response::trans.validation_error'))
->message(__('api-response::trans.given_data_was_invalid'));
}

public function statusCode(int $code): static
{
return $this->setStatusCode($code);
Expand Down
1 change: 1 addition & 0 deletions src/Facades/ApiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* @method static \KodePandai\ApiResponse\ApiResponse unauthorized(mixed $errors = [])
* @method static \KodePandai\ApiResponse\ApiResponse forbidden(mixed $errors = [])
* @method static \KodePandai\ApiResponse\ApiResponse badRequest(mixed $errors = [])
* @method static \KodePandai\ApiResponse\ApiResponse invalid(string $key, string|array $messages)
* @method \KodePandai\ApiResponse\ApiResponse statusCode(int $code)
* @method \KodePandai\ApiResponse\ApiResponse successful()
* @method \KodePandai\ApiResponse\ApiResponse notSuccessful()
Expand Down
32 changes: 28 additions & 4 deletions tests/Features/ApiResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
Route::get('api-unauthorized', fn () => ApiResponse::unauthorized());
Route::get('api-forbidden', fn () => ApiResponse::forbidden());
Route::get('api-badRequest', fn () => ApiResponse::badRequest());
Route::get('api-invalid', fn () => ApiResponse::invalid('key', []));

getJson('api-create')->assertStatus(Response::HTTP_OK);
getJson('api-success')->assertStatus(Response::HTTP_OK);
Expand All @@ -32,6 +33,7 @@
getJson('api-unauthorized')->assertStatus(Response::HTTP_UNAUTHORIZED);
getJson('api-forbidden')->assertStatus(Response::HTTP_FORBIDDEN);
getJson('api-badRequest')->assertStatus(Response::HTTP_BAD_REQUEST);
getJson('api-invalid')->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
});

it('returns correct response header', function () {
Expand Down Expand Up @@ -88,18 +90,40 @@

Route::get('api-error', function () use ($errors) {
return ApiResponse::error($errors)
->statusCode(Response::HTTP_UNPROCESSABLE_ENTITY);
->statusCode(Response::HTTP_GATEWAY_TIMEOUT);
});

getJson('api-error')
->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY)
->assertStatus(Response::HTTP_GATEWAY_TIMEOUT)
->assertJsonPath('success', false)
->assertJsonPath('title', __('Error'))
->assertJsonPath('message', __('Oops! Something went wrong.'))
->assertJsonPath('title', __('api-response::trans.error'))
->assertJsonPath('message', __('api-response::trans.something_went_wrong'))
->assertJsonPath('data', [])
->assertJsonPath('errors', $errors);
});

it('returns correct json structure for invalid api response', function () {
//
$messages = [
'Age must be a number',
'Age must be between 10-20',
];

Route::get('api-invalid', function () use ($messages) {
return ApiResponse::invalid('age', $messages)
->statusCode(Response::HTTP_UNPROCESSABLE_ENTITY);
});

getJson('api-invalid')
->dump()
->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY)
->assertJsonPath('success', false)
->assertJsonPath('title', __('api-response::trans.validation_error'))
->assertJsonPath('message', __('api-response::trans.given_data_was_invalid'))
->assertJsonPath('data', [])
->assertJsonPath('errors', ['age' => $messages]);
});

it('can handle data of type ResourceCollection|JsonResource|Collection', function () {
//
Route::get('r1', function () {
Expand Down

0 comments on commit 171a986

Please sign in to comment.