ldc-mesbr-app/tests/Unit/Domain/Models/ValueObjects/CampaignRequest/ApplicantTest.php
2026-10-12 17:08:07 +02:00

121 lines
3.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit\Domain\Models\ValueObjects\CampaignRequest;
use Domain\Models\ValueObjects\CampaignRequest\Applicant;
use Shared\ValueObjects\Email;
covers(Applicant::class);
describe('applicant', function (): void {
$firstname = 'John';
$lastname = 'Doe';
$email = new Email('john.doe@example.com');
it('creates a valid applicant', function () use ($firstname, $lastname, $email): void {
// GIVEN / WHEN
$applicant = new Applicant(
$firstname,
$lastname,
$email,
);
// THEN
expect($applicant)->toBeInstanceOf(Applicant::class)
->and($applicant->firstname)->toBe($firstname)
->and($applicant->lastname)->toBe($lastname)
->and($applicant->email)->toBe($email);
});
it('rejects an empty or whitespace-only firstname', function (string $invalidFirstname) use ($lastname, $email): void {
// WHEN
$action = fn (): Applicant => new Applicant(
$invalidFirstname,
$lastname,
$email,
);
// THEN
expect($action)->toThrow(\DomainException::class, 'Applicant firstname cannot be empty.');
})->with([
'empty' => '',
'spaces' => ' ',
]);
it('rejects a firstname that exceeds the maximum allowed length', function () use ($lastname, $email): void {
// GIVEN
$tooLongFirstname = str_repeat('a', 201);
// WHEN
$action = fn (): Applicant => new Applicant(
$tooLongFirstname,
$lastname,
$email,
);
// THEN
expect($action)->toThrow(\DomainException::class, 'Applicant firstname exceeds the maximum allowed length.');
});
it('accepts a firstname of exactly the maximum length', function () use ($lastname, $email): void {
// GIVEN
$maxLengthFirstname = str_repeat('a', 200);
// WHEN
$applicant = new Applicant(
$maxLengthFirstname,
$lastname,
$email,
);
// THEN
expect(mb_strlen($applicant->firstname))->toBe(200);
});
it('rejects an empty or whitespace-only lastname', function (string $invalidLastname) use ($firstname, $email): void {
// WHEN
$action = fn (): Applicant => new Applicant(
$firstname,
$invalidLastname,
$email,
);
// THEN
expect($action)->toThrow(\DomainException::class, 'Applicant lastname cannot be empty.');
})->with([
'empty' => '',
'spaces' => ' ',
]);
it('rejects a lastname that exceeds the maximum allowed length', function () use ($firstname, $email): void {
// GIVEN
$tooLongLastname = str_repeat('a', 201);
// WHEN
$action = fn (): Applicant => new Applicant(
$firstname,
$tooLongLastname,
$email,
);
// THEN
expect($action)->toThrow(\DomainException::class, 'Applicant lastname exceeds the maximum allowed length.');
});
it('accepts a lastname of exactly the maximum length', function () use ($firstname, $email): void {
// GIVEN
$maxLengthLastname = str_repeat('a', 200);
// WHEN
$applicant = new Applicant(
$firstname,
$maxLengthLastname,
$email,
);
// THEN
expect(mb_strlen($applicant->lastname))->toBe(200);
});
});