|
6 | 6 | use CodeZero\LocalizedRoutes\Tests\Stubs\Model;
|
7 | 7 | use CodeZero\LocalizedRoutes\Tests\Stubs\ModelWithCustomRouteParameters;
|
8 | 8 | use CodeZero\LocalizedRoutes\Tests\TestCase;
|
| 9 | +use Illuminate\Database\Eloquent\ModelNotFoundException; |
9 | 10 | use Illuminate\Support\Facades\App;
|
| 11 | +use Illuminate\Support\Facades\Config; |
10 | 12 | use Illuminate\Support\Facades\Route;
|
11 | 13 | use Illuminate\Support\Facades\View;
|
12 | 14 |
|
@@ -296,6 +298,94 @@ public function the_macro_does_not_blow_up_on_a_default_404_error()
|
296 | 298 | $response->assertResponseHasNoView();
|
297 | 299 | }
|
298 | 300 |
|
| 301 | + /** @test */ |
| 302 | + public function a_404_is_not_localized_when_triggered_by_a_non_existing_route() |
| 303 | + { |
| 304 | + $this->setSupportedLocales(['en', 'nl']); |
| 305 | + $this->setUseLocaleMiddleware(true); |
| 306 | + $this->setAppLocale('en'); |
| 307 | + $this->setCustomErrorViewPath(); |
| 308 | + |
| 309 | + $response = $this->get('/nl/abort'); |
| 310 | + $response->assertNotFound(); |
| 311 | + $response->assertResponseHasNoView(); |
| 312 | + $this->assertEquals('en', trim($response->original)); |
| 313 | + } |
| 314 | + |
| 315 | + /** @test */ |
| 316 | + public function a_404_is_localized_when_a_registered_route_throws_a_not_found_exception() |
| 317 | + { |
| 318 | + $this->setSupportedLocales(['en', 'nl']); |
| 319 | + $this->setUseLocaleMiddleware(true); |
| 320 | + $this->setAppLocale('en'); |
| 321 | + $this->setCustomErrorViewPath(); |
| 322 | + |
| 323 | + Route::localized(function () { |
| 324 | + Route::get('abort', function () { |
| 325 | + abort(404); |
| 326 | + }); |
| 327 | + }); |
| 328 | + |
| 329 | + $response = $this->get('/nl/abort'); |
| 330 | + $response->assertNotFound(); |
| 331 | + $response->assertResponseHasNoView(); |
| 332 | + $this->assertEquals('nl', trim($response->original)); |
| 333 | + } |
| 334 | + |
| 335 | + /** @test */ |
| 336 | + public function a_404_is_localized_when_a_registered_route_throws_a_model_not_found_exception() |
| 337 | + { |
| 338 | + $this->setSupportedLocales(['en', 'nl']); |
| 339 | + $this->setUseLocaleMiddleware(true); |
| 340 | + $this->setAppLocale('en'); |
| 341 | + $this->setCustomErrorViewPath(); |
| 342 | + |
| 343 | + Route::localized(function () { |
| 344 | + Route::get('route/{model}', function ($model) { |
| 345 | + throw new ModelNotFoundException(); |
| 346 | + }); |
| 347 | + }); |
| 348 | + |
| 349 | + $response = $this->get('/nl/route/mismatch'); |
| 350 | + $response->assertNotFound(); |
| 351 | + $response->assertResponseHasNoView(); |
| 352 | + $this->assertEquals('nl', trim($response->original)); |
| 353 | + } |
| 354 | + |
| 355 | + /** @test */ |
| 356 | + public function a_fallback_route_is_not_triggered_when_a_registered_route_throws_a_not_found_exception() |
| 357 | + { |
| 358 | + Route::get('abort', function () { |
| 359 | + return abort(404); |
| 360 | + }); |
| 361 | + |
| 362 | + Route::fallback(function () { |
| 363 | + return 'fallback'; |
| 364 | + }); |
| 365 | + |
| 366 | + $response = $this->get('/abort'); |
| 367 | + $response->assertNotFound(); |
| 368 | + $response->assertResponseHasNoView(); |
| 369 | + $this->assertNotEquals('fallback', $response->original); |
| 370 | + } |
| 371 | + |
| 372 | + /** @test */ |
| 373 | + public function a_fallback_route_is_not_triggered_when_a_registered_route_throws_a_model_not_found_exception() |
| 374 | + { |
| 375 | + Route::get('route/{model}', function ($model) { |
| 376 | + throw new ModelNotFoundException(); |
| 377 | + }); |
| 378 | + |
| 379 | + Route::fallback(function () { |
| 380 | + return 'fallback'; |
| 381 | + }); |
| 382 | + |
| 383 | + $response = $this->call('GET', '/route/mismatch'); |
| 384 | + $response->assertNotFound(); |
| 385 | + $response->assertResponseHasNoView(); |
| 386 | + $this->assertNotEquals('fallback', $response->original); |
| 387 | + } |
| 388 | + |
299 | 389 | /** @test */
|
300 | 390 | public function it_returns_a_localized_url_for_a_localized_fallback_route()
|
301 | 391 | {
|
@@ -415,4 +505,14 @@ public function it_returns_a_localized_url_for_a_non_localized_fallback_route_wh
|
415 | 505 | 'nl' => 'http://nl.domain.test/en/non/existing/route',
|
416 | 506 | ], $response->original);
|
417 | 507 | }
|
| 508 | + |
| 509 | + /** |
| 510 | + * Set a custom view path so Laravel will find our custom 440 error view. |
| 511 | + * |
| 512 | + * @return void |
| 513 | + */ |
| 514 | + protected function setCustomErrorViewPath() |
| 515 | + { |
| 516 | + Config::set('view.paths', __DIR__ . '/../../Stubs/views'); |
| 517 | + } |
418 | 518 | }
|
0 commit comments