ldc-cocotte/tests/Integration/WithApp/App/Rules/AdultTest.php

81 lines
2.7 KiB
PHP
Raw Permalink Normal View History

<?php
declare(strict_types=1);
namespace Tests\Integration\WithApp\App\Rules;
use App\Rules\Adult;
use Carbon\CarbonImmutable;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\Validator;
afterEach(function (): void {
Date::setTestNow();
App::setLocale('fr');
});
describe('Adult validation rule', function (): void {
it('accepts adult birth dates in SQL and form formats', function (string $birthDate): void {
// GIVEN
Date::setTestNow(CarbonImmutable::parse('2026-11-16 12:00:00'));
// WHEN
$validator = Validator::make(
['birth_date' => $birthDate],
['birth_date' => ['required', new Adult()]],
);
// THEN
expect($validator->passes())->toBeTrue();
})->with([
'exact 18th birthday SQL' => ['2008-11-16'],
'older SQL' => ['1990-01-15'],
'exact 18th birthday form' => ['16/11/2008'],
'older form' => ['15/01/1990'],
]);
it('rejects minor birth dates with translated message in fr and en', function (string $locale, string $expectedMessage): void {
// GIVEN
Date::setTestNow(CarbonImmutable::parse('2026-11-16 12:00:00'));
App::setLocale($locale);
// WHEN
$validator = Validator::make(
['birth_date' => '2008-11-17'],
['birth_date' => ['required', new Adult()]],
);
// THEN
expect($validator->fails())->toBeTrue()
->and($validator->errors()->first('birth_date'))->toBe($expectedMessage);
})->with([
'french' => ['fr', 'Vous devez être majeur pour vous inscrire.'],
'english' => ['en', 'You must be of legal age to register.'],
]);
it('rejects non-string or malformed dates with translated message in fr and en', function (
string $locale,
mixed $value,
string $expectedMessage,
): void {
// GIVEN
App::setLocale($locale);
// WHEN
$validator = Validator::make(
['birth_date' => $value],
['birth_date' => [new Adult()]],
);
// THEN
expect($validator->fails())->toBeTrue()
->and($validator->errors()->first('birth_date'))->toBe($expectedMessage);
})->with([
'french invalid format' => ['fr', 'invalid-date', 'Veuillez entrer une date de naissance valide.'],
'english invalid format' => ['en', 'invalid-date', 'Please enter a valid birth date.'],
'french non-string' => ['fr', 12345, 'Veuillez entrer une date de naissance valide.'],
'english non-string' => ['en', 12345, 'Please enter a valid birth date.'],
]);
});