|
6 | 6 | use PhpTypedValues\Exception\IntegerTypeException; |
7 | 7 | use PhpTypedValues\Float\FloatPositive; |
8 | 8 | use PhpTypedValues\Undefined\Alias\Undefined; |
9 | | -use PhpTypedValues\Usage\Example\NullableType; |
| 9 | +use PhpTypedValues\Usage\Example\OptionalType; |
10 | 10 |
|
11 | | -it('constructs NullableType from scalars and exposes typed values', function (): void { |
12 | | - $vo = NullableType::fromScalars(id: 1, firstName: 'Foobar', height: 170); |
| 11 | +it('constructs OptionalType from scalars and exposes typed values', function (): void { |
| 12 | + $vo = OptionalType::fromScalars(id: 1, firstName: 'Foobar', height: 170); |
13 | 13 |
|
14 | 14 | expect($vo->getId()->toString())->toBe('1'); |
15 | 15 | expect($vo->getFirstName()->toString())->toBe('Foobar'); |
16 | 16 | expect($vo->getHeight()->toString())->toBe('170'); |
17 | 17 | }); |
18 | 18 |
|
19 | 19 | it('checks string cast', function (): void { |
20 | | - $voInt = NullableType::fromScalars(id: 1, firstName: 'Test', height: 99); |
| 20 | + $voInt = OptionalType::fromScalars(id: 1, firstName: 'Test', height: 99); |
21 | 21 | expect($voInt->getHeight()->value())->toBe(99.0); |
22 | 22 | }); |
23 | 23 |
|
24 | 24 | it('treats empty firstName as Undefined (late-fail semantics)', function (): void { |
25 | | - $vo = NullableType::fromScalars(id: 1, firstName: '', height: 10.0); |
| 25 | + $vo = OptionalType::fromScalars(id: 1, firstName: '', height: 10.0); |
26 | 26 | expect($vo->getFirstName())->toBeInstanceOf(Undefined::class); |
27 | 27 | }); |
28 | 28 |
|
29 | 29 | it('fails early when height is negative', function (): void { |
30 | | - expect(fn() => NullableType::fromScalars(id: 1, firstName: 'Foobar', height: -10.0)) |
| 30 | + expect(fn() => OptionalType::fromScalars(id: 1, firstName: 'Foobar', height: -10.0)) |
31 | 31 | ->toThrow(FloatTypeException::class, 'Expected positive float, got "-10"'); |
32 | 32 | }); |
33 | 33 |
|
34 | 34 | it('accepts int/float/numeric-string heights and preserves string formatting via fromString casting', function (): void { |
35 | | - $asInt = NullableType::fromScalars(id: 1, firstName: 'Foobar', height: 170); |
36 | | - $asFloat = NullableType::fromScalars(id: 1, firstName: 'Foobar', height: 170.5); |
37 | | - $asString = NullableType::fromScalars(id: 1, firstName: 'Foobar', height: '42.25'); |
| 35 | + $asInt = OptionalType::fromScalars(id: 1, firstName: 'Foobar', height: 170); |
| 36 | + $asFloat = OptionalType::fromScalars(id: 1, firstName: 'Foobar', height: 170.5); |
| 37 | + $asString = OptionalType::fromScalars(id: 1, firstName: 'Foobar', height: '42.25'); |
38 | 38 |
|
39 | 39 | expect($asInt->getHeight())->toBeInstanceOf(FloatPositive::class) |
40 | 40 | ->and($asInt->getHeight()->toString())->toBe('170') |
|
45 | 45 | }); |
46 | 46 |
|
47 | 47 | it('treats null height as Undefined (late-fail semantics)', function (): void { |
48 | | - $obj = NullableType::fromScalars(id: 1, firstName: 'Foobar', height: null); |
| 48 | + $obj = OptionalType::fromScalars(id: 1, firstName: 'Foobar', height: null); |
49 | 49 | expect($obj->getHeight())->toBeInstanceOf(Undefined::class); |
50 | 50 | }); |
51 | 51 |
|
52 | 52 | it('null firstName produces Undefined via tryFromMixed while height succeeds', function (): void { |
53 | | - $obj = NullableType::fromScalars(id: 1, firstName: null, height: 180); |
| 53 | + $obj = OptionalType::fromScalars(id: 1, firstName: null, height: 180); |
54 | 54 | expect($obj->getFirstName())->toBeInstanceOf(Undefined::class) |
55 | 55 | ->and($obj->getHeight())->toBeInstanceOf(FloatPositive::class); |
56 | 56 | }); |
57 | 57 |
|
58 | 58 | it('invalid id throws IntegerTypeException with exact message', function (): void { |
59 | | - expect(fn() => NullableType::fromScalars(id: 0, firstName: 'Name', height: 100)) |
| 59 | + expect(fn() => OptionalType::fromScalars(id: 0, firstName: 'Name', height: 100)) |
60 | 60 | ->toThrow(IntegerTypeException::class, 'Expected positive integer, got "0"'); |
61 | 61 | }); |
62 | 62 |
|
63 | 63 | it('non-numeric height string throws FloatTypeException from assertFloatString', function (): void { |
64 | | - expect(fn() => NullableType::fromScalars(id: 1, firstName: 'Name', height: 'abc')) |
| 64 | + expect(fn() => OptionalType::fromScalars(id: 1, firstName: 'Name', height: 'abc')) |
65 | 65 | ->toThrow(FloatTypeException::class, 'String "abc" has no valid float value'); |
66 | 66 | }); |
0 commit comments