|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use TypePHP\Tests\Fixtures\Domain\Cat; |
| 6 | +use TypePHP\Tests\Fixtures\Domain\Dog; |
| 7 | +use TypePHP\Tests\Fixtures\Generics\DogConditionalBox; |
| 8 | +use TypePHP\Tests\Fixtures\Generics\GenericConditionalFactory; |
| 9 | + |
| 10 | +/** |
| 11 | + * Standalone function with generic conditional return type |
| 12 | + * |
| 13 | + * @template T |
| 14 | + * |
| 15 | + * @param T $typeSample |
| 16 | + * @param mixed $value |
| 17 | + * |
| 18 | + * @return (T is Dog ? positive-int : non-empty-string) |
| 19 | + */ |
| 20 | +function testStandaloneGenericConditional(mixed $typeSample, mixed $value): mixed |
| 21 | +{ |
| 22 | + return $value; |
| 23 | +} |
| 24 | + |
| 25 | +describe('Conditional Types with Generics (T is Target ? If : Else)', function () { |
| 26 | + |
| 27 | + describe('Standalone Generic Functions', function () { |
| 28 | + |
| 29 | + test('evaluates positive-int branch when template T is inferred as Dog', function () { |
| 30 | + $dog = new Dog(); |
| 31 | + |
| 32 | + expect(testStandaloneGenericConditional($dog, 100))->toBe(100); |
| 33 | + |
| 34 | + expect(fn () => testStandaloneGenericConditional($dog, -50)) |
| 35 | + ->toThrow(TypeError::class, 'Return value must be of type positive-int') |
| 36 | + ; |
| 37 | + }); |
| 38 | + |
| 39 | + test('evaluates non-empty-string branch when template T is inferred as Cat', function () { |
| 40 | + $cat = new Cat(); |
| 41 | + |
| 42 | + expect(testStandaloneGenericConditional($cat, 'valid_string'))->toBe('valid_string'); |
| 43 | + |
| 44 | + expect(fn () => testStandaloneGenericConditional($cat, '')) |
| 45 | + ->toThrow(TypeError::class, 'Return value must be of type non-empty-string') |
| 46 | + ; |
| 47 | + }); |
| 48 | + |
| 49 | + }); |
| 50 | + |
| 51 | + describe('Negated Conditional Types (T is not Target ? If : Else)', function () { |
| 52 | + |
| 53 | + test('evaluates non-empty-string branch when T is not Dog', function () { |
| 54 | + $factory = new GenericConditionalFactory(); |
| 55 | + $cat = new Cat(); |
| 56 | + |
| 57 | + expect($factory->processNegated($cat, 'hello'))->toBe('hello'); |
| 58 | + |
| 59 | + expect(fn () => $factory->processNegated($cat, '')) |
| 60 | + ->toThrow(TypeError::class, 'Return value must be of type non-empty-string') |
| 61 | + ; |
| 62 | + }); |
| 63 | + |
| 64 | + test('evaluates positive-int branch when T is Dog in negated conditional', function () { |
| 65 | + $factory = new GenericConditionalFactory(); |
| 66 | + $dog = new Dog(); |
| 67 | + |
| 68 | + expect($factory->processNegated($dog, 42))->toBe(42); |
| 69 | + |
| 70 | + expect(fn () => $factory->processNegated($dog, -10)) |
| 71 | + ->toThrow(TypeError::class, 'Return value must be of type positive-int') |
| 72 | + ; |
| 73 | + }); |
| 74 | + |
| 75 | + }); |
| 76 | + |
| 77 | + describe('class-string<T> Factories with Conditional Return Types', function () { |
| 78 | + |
| 79 | + test('evaluates list<positive-int> when class-string is Dog::class', function () { |
| 80 | + $factory = new GenericConditionalFactory(); |
| 81 | + |
| 82 | + expect($factory->createPayload(Dog::class, [10, 20, 30]))->toBe([10, 20, 30]); |
| 83 | + |
| 84 | + expect(fn () => $factory->createPayload(Dog::class, [10, -5])) |
| 85 | + ->toThrow(TypeError::class, 'Return value') |
| 86 | + ; |
| 87 | + }); |
| 88 | + |
| 89 | + test('evaluates list<non-empty-string> when class-string is Cat::class', function () { |
| 90 | + $factory = new GenericConditionalFactory(); |
| 91 | + |
| 92 | + expect($factory->createPayload(Cat::class, ['a', 'b']))->toBe(['a', 'b']); |
| 93 | + |
| 94 | + expect(fn () => $factory->createPayload(Cat::class, ['a', ''])) |
| 95 | + ->toThrow(TypeError::class, 'Return value') |
| 96 | + ; |
| 97 | + }); |
| 98 | + |
| 99 | + }); |
| 100 | + |
| 101 | + describe('Inherited Generic Classes with Conditionals', function () { |
| 102 | + |
| 103 | + test('evaluates conditional return types inherited from abstract parent generic class', function () { |
| 104 | + $dogBox = new DogConditionalBox(); |
| 105 | + |
| 106 | + expect($dogBox->processInput(100))->toBe(100); |
| 107 | + |
| 108 | + expect(fn () => $dogBox->processInput(-99)) |
| 109 | + ->toThrow(TypeError::class, 'Return value must be of type positive-int') |
| 110 | + ; |
| 111 | + }); |
| 112 | + |
| 113 | + }); |
| 114 | + |
| 115 | +}); |
0 commit comments