|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace TypePHP\Tests\TypeChecking\CallablesAndIterators; |
| 6 | + |
| 7 | +use Closure; |
| 8 | +use TypePHP\Exception\TypeError; |
| 9 | + |
| 10 | +interface PromotedBase |
| 11 | +{ |
| 12 | +} |
| 13 | + |
| 14 | +final class PromotedA implements PromotedBase |
| 15 | +{ |
| 16 | + public function __construct(public string $name = 'A') |
| 17 | + { |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +final class PromotedUnrelated |
| 22 | +{ |
| 23 | +} |
| 24 | + |
| 25 | +final class PromotedHelper |
| 26 | +{ |
| 27 | + public function format(?PromotedA $a = null): PromotedBase |
| 28 | + { |
| 29 | + return new PromotedA(); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +final class StoredClosureClass |
| 34 | +{ |
| 35 | + public Closure $factory; |
| 36 | + |
| 37 | + /** |
| 38 | + * @param Closure(?PromotedA=): PromotedBase $factory |
| 39 | + */ |
| 40 | + public function __construct(Closure $factory) |
| 41 | + { |
| 42 | + $this->factory = $factory; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +final class PromotedClosureClass |
| 47 | +{ |
| 48 | + /** |
| 49 | + * @param Closure(?PromotedA=): PromotedBase $factory |
| 50 | + */ |
| 51 | + public function __construct(public Closure $factory) |
| 52 | + { |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +final class PromotedCallableClass |
| 57 | +{ |
| 58 | + /** |
| 59 | + * @param callable(?PromotedA=): PromotedBase $formatter |
| 60 | + */ |
| 61 | + public function __construct(public mixed $formatter) |
| 62 | + { |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +describe('Promoted Property Callable Contracts & Consistency', function () { |
| 67 | + test('enforces parameter type validation when invoking closure on promoted constructor property', function () { |
| 68 | + $closure = static fn (?PromotedA $a = null): PromotedBase => new PromotedA(); |
| 69 | + |
| 70 | + $promoted = new PromotedClosureClass($closure); |
| 71 | + |
| 72 | + expect(($promoted->factory)(new PromotedA()))->toBeInstanceOf(PromotedBase::class); |
| 73 | + |
| 74 | + expect(($promoted->factory)())->toBeInstanceOf(PromotedBase::class); |
| 75 | + |
| 76 | + expect(fn () => ($promoted->factory)(new PromotedUnrelated())) |
| 77 | + ->toThrow(TypeError::class) |
| 78 | + ; |
| 79 | + }); |
| 80 | + |
| 81 | + test('enforces return type validation when invoking closure on promoted constructor property', function () { |
| 82 | + $badClosure = static fn (?PromotedA $a = null): mixed => new PromotedUnrelated(); |
| 83 | + |
| 84 | + $promoted = new PromotedClosureClass($badClosure); |
| 85 | + |
| 86 | + expect(fn () => ($promoted->factory)(new PromotedA())) |
| 87 | + ->toThrow(TypeError::class) |
| 88 | + ; |
| 89 | + }); |
| 90 | + |
| 91 | + test('behaves consistently between standard parameter assignment and promoted property', function () { |
| 92 | + $closure = static fn (?PromotedA $a = null): PromotedBase => new PromotedA(); |
| 93 | + |
| 94 | + $stored = new StoredClosureClass($closure); |
| 95 | + $promoted = new PromotedClosureClass($closure); |
| 96 | + |
| 97 | + expect(fn () => ($stored->factory)(new PromotedUnrelated())) |
| 98 | + ->toThrow(TypeError::class) |
| 99 | + ; |
| 100 | + |
| 101 | + expect(fn () => ($promoted->factory)(new PromotedUnrelated())) |
| 102 | + ->toThrow(TypeError::class) |
| 103 | + ; |
| 104 | + }); |
| 105 | + |
| 106 | + test('enforces contracts when callable is promoted in constructor (Closure, Array Callable, Invokable)', function () { |
| 107 | + $helper = new PromotedHelper(); |
| 108 | + |
| 109 | + $promotedArrayCallable = new PromotedCallableClass([$helper, 'format']); |
| 110 | + |
| 111 | + expect(($promotedArrayCallable->formatter)(new PromotedA()))->toBeInstanceOf(PromotedBase::class); |
| 112 | + |
| 113 | + expect(fn () => ($promotedArrayCallable->formatter)(new PromotedUnrelated())) |
| 114 | + ->toThrow(TypeError::class) |
| 115 | + ; |
| 116 | + |
| 117 | + $closure = static fn (?PromotedA $a = null): PromotedBase => new PromotedA(); |
| 118 | + $promotedClosure = new PromotedCallableClass($closure); |
| 119 | + |
| 120 | + expect(($promotedClosure->formatter)(new PromotedA()))->toBeInstanceOf(PromotedBase::class); |
| 121 | + |
| 122 | + expect(fn () => ($promotedClosure->formatter)(new PromotedUnrelated())) |
| 123 | + ->toThrow(TypeError::class) |
| 124 | + ; |
| 125 | + }); |
| 126 | +}); |
0 commit comments