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'); }); });