|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use function Pest\Laravel\{getJson, postJson}; |
| 6 | +use Illuminate\Auth\AuthenticationException; |
| 7 | +use Illuminate\Auth\Access\AuthorizationException; |
| 8 | +use Illuminate\Http\Response; |
| 9 | +use Illuminate\Support\Facades\Route; |
| 10 | +use Pedrosalpr\LaravelApiProblem\Tests\Handlers\TestExceptionHandler; |
| 11 | +use Symfony\Component\HttpKernel\Exception\HttpException; |
| 12 | + |
| 13 | +// Antes de cada teste, substitua o manipulador de exceções do Laravel |
| 14 | +// pelo seu manipulador de exceções de teste. |
| 15 | +beforeEach(function () { |
| 16 | + $this->app->singleton( |
| 17 | + \Illuminate\Contracts\Debug\ExceptionHandler::class, |
| 18 | + TestExceptionHandler::class // Use sua classe de handler de teste aqui |
| 19 | + ); |
| 20 | +}); |
| 21 | + |
| 22 | +test('authentication exception returns 401 problem json', function () { |
| 23 | + // Definir uma rota que lança a exceção diretamente. |
| 24 | + Route::get('/api/protected', fn () => throw new AuthenticationException('Unauthenticated.')); |
| 25 | + |
| 26 | + getJson('/api/protected') |
| 27 | + ->assertStatus(Response::HTTP_UNAUTHORIZED) |
| 28 | + ->assertHeader('Content-Type', 'application/problem+json') |
| 29 | + ->assertJson([ |
| 30 | + 'status' => Response::HTTP_UNAUTHORIZED, |
| 31 | + 'title' => 'Unauthorized', |
| 32 | + 'detail' => 'Unauthenticated.', |
| 33 | + ]); |
| 34 | +}); |
| 35 | + |
| 36 | +test('authorization exception returns 403 problem json', function () { |
| 37 | + // Definir uma rota que lança a exceção diretamente. |
| 38 | + Route::get('/api/forbidden', fn () => throw new AuthorizationException('This action is unauthorized.')); |
| 39 | + |
| 40 | + getJson('/api/forbidden') |
| 41 | + ->assertStatus(Response::HTTP_FORBIDDEN) |
| 42 | + ->assertHeader('Content-Type', 'application/problem+json') |
| 43 | + ->assertJson([ |
| 44 | + 'status' => Response::HTTP_FORBIDDEN, |
| 45 | + 'title' => 'Forbidden', |
| 46 | + 'detail' => 'This action is unauthorized.', |
| 47 | + ]); |
| 48 | +}); |
| 49 | + |
| 50 | +test('validation exception returns 422 problem json', function () { |
| 51 | + Route::post('/api/validate', function () { |
| 52 | + request()->validate(['email' => 'required|email']); |
| 53 | + }); |
| 54 | + |
| 55 | + postJson('/api/validate', ['email' => 'invalid-email']) |
| 56 | + ->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY) |
| 57 | + ->assertHeader('Content-Type', 'application/problem+json') |
| 58 | + ->assertJson([ |
| 59 | + 'status' => Response::HTTP_UNPROCESSABLE_ENTITY, |
| 60 | + 'title' => 'Unprocessable Entity', |
| 61 | + 'detail' => 'The given data was invalid.', |
| 62 | + ]) |
| 63 | + ->assertJsonStructure([ |
| 64 | + 'status', |
| 65 | + 'title', |
| 66 | + 'detail', |
| 67 | + 'errors' => [ |
| 68 | + 'email', |
| 69 | + ], |
| 70 | + ]); |
| 71 | +}); |
| 72 | + |
| 73 | +test('not found exception returns 404 problem json', function () { |
| 74 | + // A rota não é definida, o Laravel lança a exceção automaticamente |
| 75 | + getJson('/api/non-existent-route') |
| 76 | + ->assertStatus(404) |
| 77 | + ->assertHeader('Content-Type', 'application/problem+json') |
| 78 | + ->assertJson([ |
| 79 | + 'status' => 404, |
| 80 | + 'title' => 'Not Found', |
| 81 | + 'detail' => 'The route api/non-existent-route could not be found.', |
| 82 | + ]); |
| 83 | +}); |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | +test('method not allowed exception returns 405 problem json', function () { |
| 88 | + // Definir uma rota que só aceita POST |
| 89 | + Route::post('/api/only-post', fn () => ['message' => 'success']); |
| 90 | + |
| 91 | + // Tentar acessar com o método GET |
| 92 | + getJson('/api/only-post') |
| 93 | + ->assertStatus(Response::HTTP_METHOD_NOT_ALLOWED) |
| 94 | + ->assertHeader('Content-Type', 'application/problem+json') |
| 95 | + ->assertJson([ |
| 96 | + 'status' => Response::HTTP_METHOD_NOT_ALLOWED, |
| 97 | + 'title' => 'Method Not Allowed', |
| 98 | + 'detail' => 'The GET method is not supported for route api/only-post. Supported methods: POST.', |
| 99 | + ]); |
| 100 | +}); |
| 101 | + |
| 102 | +test('generic exception returns 500 problem json', function () { |
| 103 | + Route::get('/api/internal-error', function () { |
| 104 | + throw new Exception('An internal server error occurred.'); |
| 105 | + }); |
| 106 | + |
| 107 | + getJson('/api/internal-error') |
| 108 | + ->assertStatus(500) |
| 109 | + ->assertHeader('Content-Type', 'application/problem+json') |
| 110 | + ->assertJson([ |
| 111 | + 'status' => 500, |
| 112 | + 'title' => 'Internal Server Error', |
| 113 | + 'detail' => 'An internal server error occurred.', |
| 114 | + ]); |
| 115 | +}); |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | +test('http exception returns correct problem json', function () { |
| 120 | + Route::get('/api/custom-error', function () { |
| 121 | + throw new HttpException(Response::HTTP_UNAUTHORIZED, 'You do not have permission.'); |
| 122 | + }); |
| 123 | + |
| 124 | + getJson('/api/custom-error') |
| 125 | + ->assertStatus(Response::HTTP_UNAUTHORIZED) |
| 126 | + ->assertHeader('Content-Type', 'application/problem+json') |
| 127 | + ->assertJson([ |
| 128 | + 'status' => Response::HTTP_UNAUTHORIZED, |
| 129 | + 'title' => 'Unauthorized', |
| 130 | + 'detail' => 'You do not have permission.', |
| 131 | + ]); |
| 132 | +}); |
0 commit comments