Skip to content

Commit f64badd

Browse files
committed
Add more tests for 404 and fallback behavior
1 parent 8667b83 commit f64badd

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{ App::getLocale() }}

tests/Unit/Macros/LocalizedUrlMacroTest.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
use CodeZero\LocalizedRoutes\Tests\Stubs\Model;
77
use CodeZero\LocalizedRoutes\Tests\Stubs\ModelWithCustomRouteParameters;
88
use CodeZero\LocalizedRoutes\Tests\TestCase;
9+
use Illuminate\Database\Eloquent\ModelNotFoundException;
910
use Illuminate\Support\Facades\App;
11+
use Illuminate\Support\Facades\Config;
1012
use Illuminate\Support\Facades\Route;
1113
use Illuminate\Support\Facades\View;
1214

@@ -296,6 +298,94 @@ public function the_macro_does_not_blow_up_on_a_default_404_error()
296298
$response->assertResponseHasNoView();
297299
}
298300

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+
299389
/** @test */
300390
public function it_returns_a_localized_url_for_a_localized_fallback_route()
301391
{
@@ -415,4 +505,14 @@ public function it_returns_a_localized_url_for_a_non_localized_fallback_route_wh
415505
'nl' => 'http://nl.domain.test/en/non/existing/route',
416506
], $response->original);
417507
}
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+
}
418518
}

0 commit comments

Comments
 (0)