FallbackRouteTest.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace Illuminate\Tests\Integration\Routing;
  3. use Illuminate\Support\Facades\Route;
  4. use Orchestra\Testbench\TestCase;
  5. class FallbackRouteTest extends TestCase
  6. {
  7. public function testBasicFallback()
  8. {
  9. Route::fallback(function () {
  10. return response('fallback', 404);
  11. });
  12. Route::get('one', function () {
  13. return 'one';
  14. });
  15. $this->assertStringContainsString('one', $this->get('/one')->getContent());
  16. $this->assertStringContainsString('fallback', $this->get('/non-existing')->getContent());
  17. $this->assertEquals(404, $this->get('/non-existing')->getStatusCode());
  18. }
  19. public function testFallbackWithPrefix()
  20. {
  21. Route::group(['prefix' => 'prefix'], function () {
  22. Route::fallback(function () {
  23. return response('fallback', 404);
  24. });
  25. Route::get('one', function () {
  26. return 'one';
  27. });
  28. });
  29. $this->assertStringContainsString('one', $this->get('/prefix/one')->getContent());
  30. $this->assertStringContainsString('fallback', $this->get('/prefix/non-existing')->getContent());
  31. $this->assertStringContainsString('fallback', $this->get('/prefix/non-existing/with/multiple/segments')->getContent());
  32. $this->assertStringContainsString('Not Found', $this->get('/non-existing')->getContent());
  33. }
  34. public function testFallbackWithWildcards()
  35. {
  36. Route::fallback(function () {
  37. return response('fallback', 404);
  38. });
  39. Route::get('one', function () {
  40. return 'one';
  41. });
  42. Route::get('{any}', function () {
  43. return 'wildcard';
  44. })->where('any', '.*');
  45. $this->assertStringContainsString('one', $this->get('/one')->getContent());
  46. $this->assertStringContainsString('wildcard', $this->get('/non-existing')->getContent());
  47. $this->assertEquals(200, $this->get('/non-existing')->getStatusCode());
  48. }
  49. public function testNoRoutes()
  50. {
  51. Route::fallback(function () {
  52. return response('fallback', 404);
  53. });
  54. $this->assertStringContainsString('fallback', $this->get('/non-existing')->getContent());
  55. $this->assertEquals(404, $this->get('/non-existing')->getStatusCode());
  56. }
  57. public function testRespondWithNamedFallbackRoute()
  58. {
  59. Route::fallback(function () {
  60. return response('fallback', 404);
  61. })->name('testFallbackRoute');
  62. Route::get('one', function () {
  63. return Route::respondWithRoute('testFallbackRoute');
  64. });
  65. $this->assertStringContainsString('fallback', $this->get('/non-existing')->getContent());
  66. $this->assertStringContainsString('fallback', $this->get('/one')->getContent());
  67. }
  68. public function testNoFallbacks()
  69. {
  70. Route::get('one', function () {
  71. return 'one';
  72. });
  73. $this->assertStringContainsString('one', $this->get('/one')->getContent());
  74. $this->assertEquals(200, $this->get('/one')->getStatusCode());
  75. }
  76. }