Skip to content

Commit 28bd4ec

Browse files
committed
Add data-provider tests for late-resolvable isSuperTypeOf/isSubTypeOf
1 parent 0ba6805 commit 28bd4ec

File tree

1 file changed

+57
-13
lines changed

1 file changed

+57
-13
lines changed

tests/PHPStan/Type/LateResolvableTypeTraitTest.php

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,76 @@
33
namespace PHPStan\Type;
44

55
use PHPStan\Type\Constant\ConstantStringType;
6+
use PHPStan\TrinaryLogic;
67
use PHPStan\Type\IntegerType;
78
use PHPStan\Type\NeverType;
9+
use PHPStan\Type\StringType;
10+
use PHPStan\Type\Type;
11+
use PHPStan\Type\ConditionalTypeForParameter;
812
use PHPStan\Type\UnionType;
913
use PHPUnit\Framework\TestCase;
14+
use PHPUnit\Framework\Attributes\DataProvider;
1015

1116
class LateResolvableTypeTraitTest extends TestCase
1217
{
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+
}
1327

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
1535
{
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,
2242
);
43+
}
2344

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+
}
2563

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+
}
3070

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());
3276
}
3377

3478
}

0 commit comments

Comments
 (0)