Skip to content

Instantly share code, notes, and snippets.

@assertchris
Last active February 1, 2023 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save assertchris/7d31ceaf8060b9e0ef8da9151054ad13 to your computer and use it in GitHub Desktop.
Save assertchris/7d31ceaf8060b9e0ef8da9151054ad13 to your computer and use it in GitHub Desktop.
<?php
namespace Tests\Feature;
use App\Models\MyModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class AuthMiddlewareRoutesTest extends TestCase
{
use RefreshDatabase;
public function test_public_routes_allow_guests()
{
$this->seed();
foreach ($this->publicRoutes() as $route) {
$response = $this->get((string) $route());
$response->assertStatus(200);
}
}
public function publicRoutes(): array
{
return [
fn() => route('show-home'),
fn() => route('show-terms'),
fn() => route('my-model.show-results'),
fn() => route('my-model.show-details', MyModel::firstOrFail()),
];
}
public function test_protected_routes_require_auth()
{
$this->seed();
foreach ($this->protectedRoutes() as $route) {
$response = $this->get((string) $route());
$response->assertStatus(302);
$response->assertLocation((string) route('login'));
}
}
public function protectedRoutes(): array
{
return [
fn() => route('verification.notice'),
fn() => route('show-profile'),
fn() => route('my-model.show-create-form'),
fn() => route('my-model.show-edit-form', MyModel::firstOrFail()),
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment