|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Tests\Integration\Auth\Middleware; |
| 4 | + |
| 5 | +use Illuminate\Auth\Middleware\RedirectIfAuthenticated; |
| 6 | +use Illuminate\Contracts\Routing\Registrar; |
| 7 | +use Illuminate\Support\Str; |
| 8 | +use Illuminate\Tests\Integration\Auth\Fixtures\AuthenticationTestUser; |
| 9 | +use Orchestra\Testbench\Factories\UserFactory; |
| 10 | +use Orchestra\Testbench\TestCase; |
| 11 | + |
| 12 | +class RedirectIfAuthenticatedTest extends TestCase |
| 13 | +{ |
| 14 | + protected function setUp(): void |
| 15 | + { |
| 16 | + parent::setUp(); |
| 17 | + |
| 18 | + $this->withoutExceptionHandling(); |
| 19 | + |
| 20 | + /** @var \Illuminate\Contracts\Routing\Registrar $router */ |
| 21 | + $this->router = $this->app->make(Registrar::class); |
| 22 | + |
| 23 | + $this->router->get('/login', function () { |
| 24 | + return response('Login Form'); |
| 25 | + })->middleware(RedirectIfAuthenticated::class); |
| 26 | + |
| 27 | + UserFactory::new()->create(); |
| 28 | + |
| 29 | + $user = AuthenticationTestUser::first(); |
| 30 | + $this->router->get('/login', function () { |
| 31 | + return response('Login Form'); |
| 32 | + })->middleware(RedirectIfAuthenticated::class); |
| 33 | + |
| 34 | + UserFactory::new()->create(); |
| 35 | + |
| 36 | + $this->user = AuthenticationTestUser::first(); |
| 37 | + } |
| 38 | + |
| 39 | + protected function defineEnvironment($app) |
| 40 | + { |
| 41 | + $app['config']->set('app.key', Str::random(32)); |
| 42 | + $app['config']->set('auth.providers.users.model', AuthenticationTestUser::class); |
| 43 | + } |
| 44 | + |
| 45 | + protected function defineDatabaseMigrations() |
| 46 | + { |
| 47 | + $this->loadLaravelMigrations(); |
| 48 | + } |
| 49 | + |
| 50 | + public function testWhenDashboardNamedRouteIsAvailable() |
| 51 | + { |
| 52 | + $this->router->get('/named-dashboard', function () { |
| 53 | + return response('Named Dashboard'); |
| 54 | + })->name('dashboard'); |
| 55 | + |
| 56 | + $response = $this->actingAs($this->user)->get('/login'); |
| 57 | + |
| 58 | + $response->assertRedirect('/named-dashboard'); |
| 59 | + } |
| 60 | + |
| 61 | + public function testWhenHomeNamedRouteIsAvailable() |
| 62 | + { |
| 63 | + $this->router->get('/named-home', function () { |
| 64 | + return response('Named Home'); |
| 65 | + })->name('home'); |
| 66 | + |
| 67 | + $response = $this->actingAs($this->user)->get('/login'); |
| 68 | + |
| 69 | + $response->assertRedirect('/named-home'); |
| 70 | + } |
| 71 | + |
| 72 | + public function testWhenDashboardSlugIsAvailable() |
| 73 | + { |
| 74 | + $this->router->get('/dashboard', function () { |
| 75 | + return response('My Dashboard'); |
| 76 | + }); |
| 77 | + |
| 78 | + $response = $this->actingAs($this->user)->get('/login'); |
| 79 | + |
| 80 | + $response->assertRedirect('/dashboard'); |
| 81 | + } |
| 82 | + |
| 83 | + public function testWhenHomeSlugIsAvailable() |
| 84 | + { |
| 85 | + $this->router->get('/home', function () { |
| 86 | + return response('My Home'); |
| 87 | + })->name('home'); |
| 88 | + |
| 89 | + $response = $this->actingAs($this->user)->get('/login'); |
| 90 | + |
| 91 | + $response->assertRedirect('/home'); |
| 92 | + } |
| 93 | + |
| 94 | + public function testWhenHomeOrDashboardAreNotAvailable() |
| 95 | + { |
| 96 | + $response = $this->actingAs($this->user)->get('/login'); |
| 97 | + |
| 98 | + $response->assertRedirect('/'); |
| 99 | + } |
| 100 | + |
| 101 | + public function testWhenGuest() |
| 102 | + { |
| 103 | + $response = $this->get('/login'); |
| 104 | + |
| 105 | + $response->assertOk(); |
| 106 | + $response->assertSee('Login Form'); |
| 107 | + } |
| 108 | +} |
0 commit comments