|
5 | 5 | namespace BlueBeetle\ApiToolkit\Tests\Acceptance; |
6 | 6 |
|
7 | 7 | use BlueBeetle\ApiToolkit\Exceptions\ConfigureExceptionHandler; |
| 8 | +use BlueBeetle\ApiToolkit\Tests\Fixtures\Exceptions\StubDomainException; |
| 9 | +use BlueBeetle\ApiToolkit\Tests\Fixtures\Models\Product; |
8 | 10 | use BlueBeetle\ApiToolkit\Tests\TestCase; |
| 11 | +use BlueBeetle\IdempotencyMiddleware\IdempotencyException; |
| 12 | +use Exception; |
9 | 13 | use Illuminate\Auth\AuthenticationException; |
| 14 | +use Illuminate\Database\Eloquent\ModelNotFoundException; |
| 15 | +use Illuminate\Database\LazyLoadingViolationException; |
| 16 | +use Illuminate\Database\QueryException; |
10 | 17 | use Illuminate\Support\Facades\Route; |
11 | 18 | use Illuminate\Validation\ValidationException; |
12 | 19 | use PHPUnit\Framework\Attributes\Test; |
13 | 20 | use PHPUnit\Framework\Attributes\TestDox; |
14 | 21 | use RuntimeException; |
15 | 22 | use Symfony\Component\HttpFoundation\Response; |
16 | 23 | use Symfony\Component\HttpKernel\Exception\HttpException; |
| 24 | +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
| 25 | +use Symfony\Component\Routing\Exception\RouteNotFoundException; |
17 | 26 |
|
18 | 27 | final class ExceptionHandlerTest extends TestCase |
19 | 28 | { |
@@ -145,4 +154,202 @@ public function it_excludes_debug_when_disabled(): void |
145 | 154 | $response->assertStatus(Response::HTTP_BAD_REQUEST); |
146 | 155 | $response->assertJsonMissingPath('errors.0.meta'); |
147 | 156 | } |
| 157 | + |
| 158 | + #[Test] |
| 159 | + #[TestDox('it renders query exception as 400')] |
| 160 | + public function it_renders_query_exception(): void |
| 161 | + { |
| 162 | + $this->app['config']->set('app.debug', false); |
| 163 | + |
| 164 | + Route::get('/test', fn () => throw new QueryException( |
| 165 | + connectionName: 'testing', |
| 166 | + sql: 'SELECT * FROM invalid_table', |
| 167 | + bindings: [], |
| 168 | + previous: new Exception('Table not found'), |
| 169 | + )); |
| 170 | + |
| 171 | + $response = $this->get('/test'); |
| 172 | + |
| 173 | + $response->assertStatus(Response::HTTP_BAD_REQUEST); |
| 174 | + $response->assertJsonPath('errors.0.code', 'invalid_request_error'); |
| 175 | + $response->assertJsonPath('errors.0.detail', 'There was a problem during a database query'); |
| 176 | + } |
| 177 | + |
| 178 | + #[Test] |
| 179 | + #[TestDox('it renders query exception with detail when debug is on')] |
| 180 | + public function it_renders_query_exception_with_debug(): void |
| 181 | + { |
| 182 | + $this->app['config']->set('app.debug', true); |
| 183 | + |
| 184 | + Route::get('/test', fn () => throw new QueryException( |
| 185 | + connectionName: 'testing', |
| 186 | + sql: 'SELECT * FROM invalid_table', |
| 187 | + bindings: [], |
| 188 | + previous: new Exception('Table not found'), |
| 189 | + )); |
| 190 | + |
| 191 | + $response = $this->get('/test'); |
| 192 | + |
| 193 | + $response->assertStatus(Response::HTTP_BAD_REQUEST); |
| 194 | + $response->assertJsonPath('errors.0.code', 'invalid_request_error'); |
| 195 | + // When debug is on, the actual exception message is shown |
| 196 | + $this->assertNotSame('There was a problem during a database query', $response->json('errors.0.detail')); |
| 197 | + } |
| 198 | + |
| 199 | + #[Test] |
| 200 | + #[TestDox('it renders lazy loading violation as 400')] |
| 201 | + public function it_renders_lazy_loading_violation(): void |
| 202 | + { |
| 203 | + $this->app['config']->set('app.debug', false); |
| 204 | + |
| 205 | + Route::get('/test', fn () => throw new LazyLoadingViolationException( |
| 206 | + model: new Product(), |
| 207 | + relation: 'category', |
| 208 | + )); |
| 209 | + |
| 210 | + $response = $this->get('/test'); |
| 211 | + |
| 212 | + $response->assertStatus(Response::HTTP_BAD_REQUEST); |
| 213 | + $response->assertJsonPath('errors.0.code', 'invalid_request_error'); |
| 214 | + $response->assertJsonPath('errors.0.detail', 'There was a problem during a database query'); |
| 215 | + } |
| 216 | + |
| 217 | + #[Test] |
| 218 | + #[TestDox('it renders model not found exception')] |
| 219 | + public function it_renders_model_not_found(): void |
| 220 | + { |
| 221 | + $modelException = (new ModelNotFoundException())->setModel(Product::class, ['abc-123']); |
| 222 | + |
| 223 | + Route::get('/test', fn () => throw new NotFoundHttpException( |
| 224 | + message: '', |
| 225 | + previous: $modelException, |
| 226 | + )); |
| 227 | + |
| 228 | + $response = $this->get('/test'); |
| 229 | + |
| 230 | + $response->assertStatus(Response::HTTP_NOT_FOUND); |
| 231 | + $response->assertJsonPath('errors.0.code', 'invalid_request_error'); |
| 232 | + $response->assertJsonPath('errors.0.title', 'Not Found'); |
| 233 | + $this->assertStringContainsString('product', $response->json('errors.0.detail')); |
| 234 | + $this->assertStringContainsString('abc-123', $response->json('errors.0.detail')); |
| 235 | + } |
| 236 | + |
| 237 | + #[Test] |
| 238 | + #[TestDox('it renders idempotency exception')] |
| 239 | + public function it_renders_idempotency_exception(): void |
| 240 | + { |
| 241 | + Route::post('/test', fn () => throw new IdempotencyException('Request already processed')); |
| 242 | + |
| 243 | + $response = $this->postJson('/test'); |
| 244 | + |
| 245 | + $response->assertStatus(Response::HTTP_BAD_REQUEST); |
| 246 | + $response->assertJsonPath('errors.0.code', 'idempotency_error'); |
| 247 | + $response->assertJsonPath('errors.0.title', 'Idempotency Error'); |
| 248 | + $response->assertJsonPath('errors.0.detail', 'Request already processed'); |
| 249 | + } |
| 250 | + |
| 251 | + #[Test] |
| 252 | + #[TestDox('it renders route not found exception')] |
| 253 | + public function it_renders_route_not_found(): void |
| 254 | + { |
| 255 | + Route::get('/test', fn () => throw new RouteNotFoundException( |
| 256 | + 'Route [api.products.index] not defined.', |
| 257 | + )); |
| 258 | + |
| 259 | + $response = $this->get('/test'); |
| 260 | + |
| 261 | + $response->assertStatus(Response::HTTP_NOT_FOUND); |
| 262 | + $response->assertJsonPath('errors.0.code', 'invalid_request_error'); |
| 263 | + $response->assertJsonPath('errors.0.title', 'Not Found'); |
| 264 | + $this->assertStringContainsString('api.products.index', $response->json('errors.0.detail')); |
| 265 | + } |
| 266 | + |
| 267 | + #[Test] |
| 268 | + #[TestDox('it renders domain exceptions as 400')] |
| 269 | + public function it_renders_domain_exceptions(): void |
| 270 | + { |
| 271 | + $this->app['config']->set('api-toolkit.exceptions.domain', [ |
| 272 | + StubDomainException::class, |
| 273 | + ]); |
| 274 | + |
| 275 | + Route::get('/test', fn () => throw new StubDomainException('Insufficient stock')); |
| 276 | + |
| 277 | + $response = $this->get('/test'); |
| 278 | + |
| 279 | + $response->assertStatus(Response::HTTP_BAD_REQUEST); |
| 280 | + $response->assertJsonPath('errors.0.code', 'invalid_request_error'); |
| 281 | + $response->assertJsonPath('errors.0.title', 'Bad Request'); |
| 282 | + $response->assertJsonPath('errors.0.detail', 'Insufficient stock'); |
| 283 | + } |
| 284 | + |
| 285 | + #[Test] |
| 286 | + #[TestDox('it respects dont_report config')] |
| 287 | + public function it_respects_dont_report_config(): void |
| 288 | + { |
| 289 | + $this->app['config']->set('api-toolkit.exceptions.dont_report', [ |
| 290 | + RuntimeException::class, |
| 291 | + ]); |
| 292 | + |
| 293 | + Route::get('/test', fn () => throw new RuntimeException('Should not be reported')); |
| 294 | + |
| 295 | + $response = $this->get('/test'); |
| 296 | + |
| 297 | + // Still renders the response, but the exception should not be reported |
| 298 | + $response->assertStatus(Response::HTTP_INTERNAL_SERVER_ERROR); |
| 299 | + } |
| 300 | + |
| 301 | + #[Test] |
| 302 | + #[TestDox('it renders not found for root path')] |
| 303 | + public function it_renders_not_found_for_root_path(): void |
| 304 | + { |
| 305 | + $response = $this->get('/'); |
| 306 | + |
| 307 | + $response->assertStatus(Response::HTTP_NOT_FOUND); |
| 308 | + $this->assertStringContainsString('GET: /', $response->json('errors.0.detail')); |
| 309 | + } |
| 310 | + |
| 311 | + #[Test] |
| 312 | + #[TestDox('it sets json api content type on error responses')] |
| 313 | + public function it_sets_content_type(): void |
| 314 | + { |
| 315 | + Route::get('/test', fn () => throw new RuntimeException('Error')); |
| 316 | + |
| 317 | + $response = $this->get('/test'); |
| 318 | + |
| 319 | + $response->assertHeader('Content-Type', 'application/vnd.api+json'); |
| 320 | + } |
| 321 | + |
| 322 | + #[Test] |
| 323 | + #[TestDox('it renders multiple validation errors with source pointers')] |
| 324 | + public function it_renders_multiple_validation_errors(): void |
| 325 | + { |
| 326 | + Route::post('/test', function () { |
| 327 | + $validator = \Illuminate\Support\Facades\Validator::make( |
| 328 | + ['email' => 'not-an-email'], |
| 329 | + [ |
| 330 | + 'name' => ['required'], |
| 331 | + 'email' => ['required', 'email'], |
| 332 | + 'age' => ['required', 'integer'], |
| 333 | + ], |
| 334 | + ); |
| 335 | + |
| 336 | + throw new ValidationException($validator); |
| 337 | + }); |
| 338 | + |
| 339 | + $response = $this->postJson('/test'); |
| 340 | + |
| 341 | + $response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY); |
| 342 | + |
| 343 | + $errors = $response->json('errors'); |
| 344 | + |
| 345 | + // name required + email invalid + age required = 3 errors |
| 346 | + $this->assertGreaterThanOrEqual(3, count($errors)); |
| 347 | + |
| 348 | + // All errors should have validation_error code |
| 349 | + foreach ($errors as $error) { |
| 350 | + $this->assertSame('validation_error', $error['code']); |
| 351 | + $this->assertSame('Validation Error', $error['title']); |
| 352 | + $this->assertArrayHasKey('pointer', $error['source']); |
| 353 | + } |
| 354 | + } |
148 | 355 | } |
0 commit comments