ldc-boutique-cnc-sable/tests/Unit/Domain/Models/ValueObjects/PhoneNumberTest.php

62 lines
2 KiB
PHP

<?php
declare(strict_types=1);
use Domain\Models\ValueObjects\PhoneNumber;
describe('PhoneNumber Value Object', function () {
test('it can be instantiated with a valid phone number', function () {
// GIVEN
$raw = '+33102030405';
// WHEN
$phone = new PhoneNumber($raw);
// THEN
expect($phone->value)->toBe('+33102030405')
->and((string) $phone)->toBe('+33102030405');
});
test('it throws when phone is empty', function () {
// GIVEN & WHEN & THEN
expect(fn () => new PhoneNumber(' '))
->toThrow(\InvalidArgumentException::class, 'Phone number cannot be empty.');
});
test('it throws when phone is shorter than 4 characters', function () {
// GIVEN & WHEN & THEN
expect(fn () => new PhoneNumber('123'))
->toThrow(\InvalidArgumentException::class, 'Phone number must have at least 4 characters.');
});
test('it throws when phone exceeds 30 characters', function () {
// GIVEN & WHEN & THEN
expect(fn () => new PhoneNumber(str_repeat('1', 31)))
->toThrow(\InvalidArgumentException::class, 'Phone number cannot exceed 30 characters.');
});
test('it throws when phone contains invalid characters', function () {
// GIVEN & WHEN & THEN
expect(fn () => new PhoneNumber('01 02 03 ab cd'))
->toThrow(\InvalidArgumentException::class, 'Phone number format is invalid.');
});
test('tryFrom returns null when value is null or empty', function () {
// GIVEN & WHEN & THEN
expect(PhoneNumber::tryFrom(null))->toBeNull()
->and(PhoneNumber::tryFrom(''))->toBeNull()
->and(PhoneNumber::tryFrom(' '))->toBeNull();
});
test('tryFrom returns PhoneNumber when value is valid', function () {
// GIVEN
$raw = '+33102030405';
// WHEN
$phone = PhoneNumber::tryFrom($raw);
// THEN
expect($phone)->not->toBeNull()
->and($phone?->value)->toBe('+33102030405');
});
});