|
3 | 3 | namespace PHPStan\Type; |
4 | 4 |
|
5 | 5 | use PHPStan\Type\Constant\ConstantStringType; |
| 6 | +use PHPStan\TrinaryLogic; |
6 | 7 | use PHPStan\Type\IntegerType; |
7 | 8 | use PHPStan\Type\NeverType; |
| 9 | +use PHPStan\Type\StringType; |
| 10 | +use PHPStan\Type\Type; |
| 11 | +use PHPStan\Type\ConditionalTypeForParameter; |
8 | 12 | use PHPStan\Type\UnionType; |
9 | 13 | use PHPUnit\Framework\TestCase; |
| 14 | +use PHPUnit\Framework\Attributes\DataProvider; |
10 | 15 |
|
11 | 16 | class LateResolvableTypeTraitTest extends TestCase |
12 | 17 | { |
| 18 | + public static function dataIsSuperTypeOf(): array |
| 19 | + { |
| 20 | + return self::provideCases(); |
| 21 | + } |
| 22 | + |
| 23 | + public static function dataIsSubTypeOf(): array |
| 24 | + { |
| 25 | + return self::provideCases(); |
| 26 | + } |
13 | 27 |
|
14 | | - public function testIsSuperTypeOfForConditional(): void |
| 28 | + private static function createConditional( |
| 29 | + string $parameterName = '$operator', |
| 30 | + string $targetLiteral = 'in', |
| 31 | + ?Type $ifType = null, |
| 32 | + ?Type $elseType = null, |
| 33 | + bool $negated = false, |
| 34 | + ): ConditionalTypeForParameter |
15 | 35 | { |
16 | | - $conditional = new ConditionalTypeForParameter( |
17 | | - '$operator', |
18 | | - new ConstantStringType('in'), |
19 | | - new IntegerType(), |
20 | | - new NeverType(), |
21 | | - false, |
| 36 | + return new ConditionalTypeForParameter( |
| 37 | + $parameterName, |
| 38 | + new ConstantStringType($targetLiteral), |
| 39 | + $ifType ?? new IntegerType(), |
| 40 | + $elseType ?? new NeverType(), |
| 41 | + $negated, |
22 | 42 | ); |
| 43 | + } |
23 | 44 |
|
24 | | - $this->assertSame('Yes', $conditional->isSuperTypeOf($conditional)->describe()); |
| 45 | + /** |
| 46 | + * @return list<array{Type, Type, TrinaryLogic}> |
| 47 | + */ |
| 48 | + private static function provideCases(): array |
| 49 | + { |
| 50 | + return [ |
| 51 | + 'conditional vs same conditional' => [ |
| 52 | + self::createConditional(), |
| 53 | + self::createConditional(), |
| 54 | + TrinaryLogic::createYes(), |
| 55 | + ], |
| 56 | + 'conditional vs union containing it' => [ |
| 57 | + self::createConditional(), |
| 58 | + new UnionType([new StringType(), self::createConditional()]), |
| 59 | + TrinaryLogic::createYes(), |
| 60 | + ], |
| 61 | + ]; |
| 62 | + } |
25 | 63 |
|
26 | | - $unionWithConditional = new UnionType([ |
27 | | - new StringType(), |
28 | | - $conditional, |
29 | | - ]); |
| 64 | + #[DataProvider('dataIsSuperTypeOf')] |
| 65 | + public function testIsSuperTypeOf(Type $left, Type $right, TrinaryLogic $expected): void |
| 66 | + { |
| 67 | + $actual = $left->isSuperTypeOf($right); |
| 68 | + $this->assertSame($expected->describe(), $actual->describe()); |
| 69 | + } |
30 | 70 |
|
31 | | - $this->assertSame('Yes', $conditional->isSuperTypeOf($unionWithConditional)->describe()); |
| 71 | + #[DataProvider('dataIsSubTypeOf')] |
| 72 | + public function testIsSubTypeOf(Type $left, Type $right, TrinaryLogic $expected): void |
| 73 | + { |
| 74 | + $actual = $left->isSubTypeOf($right); |
| 75 | + $this->assertSame($expected->describe(), $actual->describe()); |
32 | 76 | } |
33 | 77 |
|
34 | 78 | } |
0 commit comments