|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace TypePHP\Tests\TypeChecking\Boundaries; |
| 6 | + |
| 7 | +use TypePHP\Exception\TypeError; |
| 8 | + |
| 9 | +class PromotedVarUnnamedOrder |
| 10 | +{ |
| 11 | + public function __construct( |
| 12 | + /** |
| 13 | + * @var positive-int |
| 14 | + */ |
| 15 | + public int $orderId, |
| 16 | + ) { |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +class PromotedVarNamedSku |
| 21 | +{ |
| 22 | + public function __construct( |
| 23 | + /** |
| 24 | + * @var non-empty-string $sku |
| 25 | + */ |
| 26 | + public string $sku, |
| 27 | + ) { |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +class PromotedToolingVarOrder |
| 32 | +{ |
| 33 | + public function __construct( |
| 34 | + /** |
| 35 | + * @phpstan-var positive-int |
| 36 | + */ |
| 37 | + public int $id, |
| 38 | + /** |
| 39 | + * @psalm-var non-empty-string |
| 40 | + */ |
| 41 | + public string $code, |
| 42 | + ) { |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +class PromotedMixedMultipleOrder |
| 47 | +{ |
| 48 | + public function __construct( |
| 49 | + /** |
| 50 | + * @var positive-int |
| 51 | + */ |
| 52 | + public int $id, |
| 53 | + /** |
| 54 | + * @var non-empty-string |
| 55 | + */ |
| 56 | + public string $name, |
| 57 | + /** |
| 58 | + * @var int<1, 100> |
| 59 | + */ |
| 60 | + public int $quantity = 1, |
| 61 | + ) { |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +if (PHP_VERSION_ID >= 80400) { |
| 66 | + require_once __DIR__ . '/../../Fixtures/PropertyHooks/AsymmetricPromotedPropertyFixtures.php'; |
| 67 | +} |
| 68 | + |
| 69 | +describe('Constructor Property Promotion (CPP) with /** @var */ Annotations', function () { |
| 70 | + describe('Standard Promoted Properties (PHP 8.0+)', function () { |
| 71 | + test('accepts valid value on promoted parameter with unnamed /** @var */', function () { |
| 72 | + $order = new PromotedVarUnnamedOrder(100); |
| 73 | + |
| 74 | + expect($order->orderId)->toBe(100); |
| 75 | + }); |
| 76 | + |
| 77 | + test('rejects invalid value on promoted parameter with unnamed /** @var */', function () { |
| 78 | + expect(fn () => new PromotedVarUnnamedOrder(-50)) |
| 79 | + ->toThrow(TypeError::class, 'Argument $orderId must be of type positive-int, negative int (-50) given') |
| 80 | + ; |
| 81 | + }); |
| 82 | + |
| 83 | + test('accepts valid value on promoted parameter with named /** @var Type $name */', function () { |
| 84 | + $product = new PromotedVarNamedSku('SKU-100'); |
| 85 | + |
| 86 | + expect($product->sku)->toBe('SKU-100'); |
| 87 | + }); |
| 88 | + |
| 89 | + test('rejects invalid value on promoted parameter with named /** @var Type $name */', function () { |
| 90 | + expect(fn () => new PromotedVarNamedSku('')) |
| 91 | + ->toThrow(TypeError::class, 'Argument $sku must be of type non-empty-string') |
| 92 | + ; |
| 93 | + }); |
| 94 | + |
| 95 | + test('enforces @phpstan-var and @psalm-var on promoted parameters', function () { |
| 96 | + $order = new PromotedToolingVarOrder(42, 'ORDER-42'); |
| 97 | + expect($order->id)->toBe(42)->and($order->code)->toBe('ORDER-42'); |
| 98 | + |
| 99 | + expect(fn () => new PromotedToolingVarOrder(-1, 'ORDER-42')) |
| 100 | + ->toThrow(TypeError::class, 'Argument $id must be of type positive-int') |
| 101 | + ; |
| 102 | + |
| 103 | + expect(fn () => new PromotedToolingVarOrder(42, '')) |
| 104 | + ->toThrow(TypeError::class, 'Argument $code must be of type non-empty-string') |
| 105 | + ; |
| 106 | + }); |
| 107 | + |
| 108 | + test('validates multiple promoted parameters with varied types', function () { |
| 109 | + $order = new PromotedMixedMultipleOrder(10, 'Widgets', 5); |
| 110 | + expect($order->id)->toBe(10) |
| 111 | + ->and($order->name)->toBe('Widgets') |
| 112 | + ->and($order->quantity)->toBe(5) |
| 113 | + ; |
| 114 | + |
| 115 | + expect(fn () => new PromotedMixedMultipleOrder(-5, 'Widgets', 5)) |
| 116 | + ->toThrow(TypeError::class, 'Argument $id must be of type positive-int') |
| 117 | + ; |
| 118 | + |
| 119 | + expect(fn () => new PromotedMixedMultipleOrder(10, '', 5)) |
| 120 | + ->toThrow(TypeError::class, 'Argument $name must be of type non-empty-string') |
| 121 | + ; |
| 122 | + |
| 123 | + expect(fn () => new PromotedMixedMultipleOrder(10, 'Widgets', 150)) |
| 124 | + ->toThrow(TypeError::class, 'Argument $quantity') |
| 125 | + ; |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
| 129 | + describe('PHP 8.4 Asymmetric Visibility Promoted Properties', function () { |
| 130 | + test('accepts valid value on asymmetric visibility promoted parameter with /** @var */', function () { |
| 131 | + if (PHP_VERSION_ID < 80400) { |
| 132 | + expect(true)->toBeTrue(); |
| 133 | + |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + $order = new \TypePHP\Tests\Fixtures\PropertyHooks\AsymmetricPromotedVarOrder(100); |
| 138 | + |
| 139 | + expect($order->orderId)->toBe(100); |
| 140 | + }); |
| 141 | + |
| 142 | + test('rejects invalid value on asymmetric visibility promoted parameter with /** @var */', function () { |
| 143 | + if (PHP_VERSION_ID < 80400) { |
| 144 | + expect(true)->toBeTrue(); |
| 145 | + |
| 146 | + return; |
| 147 | + } |
| 148 | + |
| 149 | + expect(fn () => new \TypePHP\Tests\Fixtures\PropertyHooks\AsymmetricPromotedVarOrder(-50)) |
| 150 | + ->toThrow(TypeError::class, 'Argument $orderId must be of type positive-int, negative int (-50) given') |
| 151 | + ; |
| 152 | + }); |
| 153 | + }); |
| 154 | +}); |
0 commit comments