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