ldc-boutique-cnc-sable/tests/Unit/Domain/Models/ValueObjects/CodeTest.php
Jérémy Munsch e0c4b34ed8
feat(employees): implement deterministic 6-character code generation using SerialCaster
- Add kwaadpepper/serial-int-caster and SerialCasterServiceProvider
- Update CodeGenerationService to encode Matricule deterministically with length 6
- Update Code value object to support lengths between 6 and 10 with 53 non-ambiguous characters
- Add database migration to sanitize legacy ambiguous codes
- Recreate and update unit and integration tests for CodeGenerationService and Code value object
2026-10-27 09:58:46 +01:00

89 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit\Domain\Models\ValueObjects;
use Domain\Models\ValueObjects\Code;
describe('Code Value Object', function () {
it('can instantiate with valid lengths', function (string $value) {
// GIVEN
// WHEN
$vo = new Code($value);
// THEN
expect($vo)->toBeInstanceOf(Code::class)
->and($vo->value)->toBe($value);
})->with([
'standard length 6' => 'ABCDEF',
'legacy code length 8' => 'ABCDEFGH',
'length 9' => '123456789',
'length 10' => 'ABCDEFGH12',
]);
it('throws exception if code is empty', function () {
// GIVEN
$value = ' ';
// WHEN
$action = fn () => new Code($value);
// THEN
expect($action)->toThrow(\DomainException::class);
});
it('throws exception if code length is invalid', function (string $value) {
// GIVEN
// WHEN
$action = fn () => new Code($value);
// THEN
expect($action)->toThrow(\DomainException::class);
})->with([
'too short (5)' => '12345',
'too long (11)' => '12345678911',
]);
it('throws exception if code contains invalid chars', function (string $value) {
// GIVEN
// WHEN
$action = fn () => new Code($value);
// THEN
expect($action)->toThrow(\DomainException::class, 'Code contains invalid characters');
})->with([
'special char' => 'ABCDEF#123',
'contains uppercase O' => 'ABCDEFGHO1',
'contains lowercase o' => 'abcdefgho1',
'contains zero' => 'ABCDEFGH01',
'contains uppercase I' => 'ABCDEFGH1I',
'contains lowercase l' => 'abcdefgh1l',
]);
});
describe('Code methods', function (): void {
it('can be cast to string', function (): void {
// GIVEN
$vo = new Code('ABCDEFGH12');
// WHEN
$str = (string) $vo;
// THEN
expect($str)->toBe('ABCDEFGH12');
});
it('can equate', function (): void {
// GIVEN
$vo1 = new Code('ABCDEFGH12');
$vo2 = new Code('ABCDEFGH12');
$vo3 = new Code('1234567891');
// WHEN
$eq1 = $vo1->equals($vo2);
$eq2 = $vo1->equals($vo3);
$eq3 = $vo1->equals('ABCDEFGH12');
// THEN
expect($eq1)->toBeTrue()
->and($eq2)->toBeFalse()
->and($eq3)->toBeFalse();
});
});