55 lines
1.8 KiB
PHP
55 lines
1.8 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('it can be instantiated with RFC3966 tel: format', function () {
|
|
// GIVEN
|
|
$raw = 'tel:+33-1-02-03-04-05';
|
|
|
|
// WHEN
|
|
$phone = new PhoneNumber($raw);
|
|
|
|
// THEN
|
|
expect($phone->value)->toBe('tel:+33-1-02-03-04-05')
|
|
->and((string) $phone)->toBe('tel:+33-1-02-03-04-05');
|
|
});
|
|
});
|