-
Notifications
You must be signed in to change notification settings - Fork 478
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix generic variance with BenevolentUnionType
- Loading branch information
1 parent
9f51f8e
commit 49dcc50
Showing
2 changed files
with
110 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
tests/PHPStan/Type/Generic/TemplateTypeVarianceTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Generic; | ||
|
||
use PHPStan\TrinaryLogic; | ||
use PHPStan\Type\BenevolentUnionType; | ||
use PHPStan\Type\IntegerType; | ||
use PHPStan\Type\StringType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\UnionType; | ||
use PHPStan\Type\VerbosityLevel; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class TemplateTypeVarianceTest extends TestCase | ||
{ | ||
|
||
public function dataIsValidVariance(): iterable | ||
{ | ||
foreach ([TemplateTypeVariance::createInvariant(), TemplateTypeVariance::createCovariant()] as $variance) { | ||
yield [ | ||
$variance, | ||
new BenevolentUnionType([new IntegerType(), new StringType()]), | ||
new BenevolentUnionType([new IntegerType(), new StringType()]), | ||
TrinaryLogic::createYes(), | ||
TrinaryLogic::createYes(), | ||
]; | ||
|
||
yield [ | ||
$variance, | ||
new IntegerType(), | ||
new BenevolentUnionType([new IntegerType(), new StringType()]), | ||
TrinaryLogic::createYes(), | ||
TrinaryLogic::createYes(), | ||
]; | ||
|
||
yield [ | ||
$variance, | ||
new BenevolentUnionType([new IntegerType(), new StringType()]), | ||
new IntegerType(), | ||
TrinaryLogic::createYes(), | ||
TrinaryLogic::createYes(), | ||
]; | ||
|
||
yield [ | ||
$variance, | ||
new StringType(), | ||
new BenevolentUnionType([new IntegerType(), new StringType()]), | ||
TrinaryLogic::createYes(), | ||
TrinaryLogic::createYes(), | ||
]; | ||
|
||
yield [ | ||
$variance, | ||
new BenevolentUnionType([new IntegerType(), new StringType()]), | ||
new StringType(), | ||
TrinaryLogic::createYes(), | ||
TrinaryLogic::createYes(), | ||
]; | ||
|
||
yield [ | ||
$variance, | ||
new BenevolentUnionType([new IntegerType(), new StringType()]), | ||
new UnionType([new IntegerType(), new StringType()]), | ||
TrinaryLogic::createYes(), | ||
TrinaryLogic::createYes(), | ||
]; | ||
|
||
yield [ | ||
$variance, | ||
new UnionType([new IntegerType(), new StringType()]), | ||
new BenevolentUnionType([new IntegerType(), new StringType()]), | ||
TrinaryLogic::createYes(), | ||
TrinaryLogic::createYes(), | ||
]; | ||
} | ||
} | ||
|
||
/** | ||
* @dataProvider dataIsValidVariance | ||
*/ | ||
public function testIsValidVariance( | ||
TemplateTypeVariance $variance, | ||
Type $a, | ||
Type $b, | ||
TrinaryLogic $expected, | ||
TrinaryLogic $expectedInversed | ||
): void | ||
{ | ||
$this->assertSame( | ||
$expected->describe(), | ||
$variance->isValidVariance($a, $b)->describe(), | ||
sprintf('%s->isValidVariance(%s, %s)', $variance->describe(), $a->describe(VerbosityLevel::precise()), $b->describe(VerbosityLevel::precise())) | ||
); | ||
$this->assertSame( | ||
$expectedInversed->describe(), | ||
$variance->isValidVariance($b, $a)->describe(), | ||
sprintf('%s->isValidVariance(%s, %s)', $variance->describe(), $b->describe(VerbosityLevel::precise()), $a->describe(VerbosityLevel::precise())) | ||
); | ||
} | ||
|
||
} |