60 lines
1.8 KiB
PHP
60 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace Tests;
|
|
|
|
use App\Http\Middleware\VerifyCsrfToken;
|
|
use Illuminate\Foundation\Http\Middleware\ValidateCsrfToken;
|
|
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
|
|
use Illuminate\Testing\TestResponse;
|
|
use Laravel\Fortify\Features;
|
|
|
|
abstract class TestCase extends BaseTestCase
|
|
{
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
config([
|
|
'app.debug' => false,
|
|
'session.driver' => 'array',
|
|
'cache.default' => 'array',
|
|
'mail.default' => 'array',
|
|
]);
|
|
|
|
/**
|
|
* Feature tests post directly to web routes. Keep CSRF enabled in the
|
|
* application, but exclude it in PHPUnit so tests exercise validation,
|
|
* authorization, tenancy and persistence instead of failing with 419.
|
|
*/
|
|
if (class_exists(ValidateCsrfToken::class)) {
|
|
ValidateCsrfToken::except(['*']);
|
|
$this->withoutMiddleware(ValidateCsrfToken::class);
|
|
}
|
|
|
|
if (class_exists(VerifyCsrfToken::class)) {
|
|
VerifyCsrfToken::except(['*']);
|
|
$this->withoutMiddleware(VerifyCsrfToken::class);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* POST with a real CSRF token for tests that intentionally verify web-form behaviour.
|
|
*
|
|
* @param array<string, mixed> $data
|
|
*/
|
|
protected function csrfPost(string $uri, array $data = []): TestResponse
|
|
{
|
|
$token = 'test-csrf-token';
|
|
|
|
return $this->withSession(['_token' => $token])
|
|
->post($uri, ['_token' => $token, ...$data]);
|
|
}
|
|
|
|
protected function skipUnlessFortifyHas(string $feature, ?string $message = null): void
|
|
{
|
|
if (! Features::enabled($feature)) {
|
|
$this->markTestSkipped($message ?? "Fortify feature [{$feature}] is not enabled.");
|
|
}
|
|
}
|
|
}
|