-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
tests/Rules/PHPUnit/ImpossibleCheckTypeMethodCallRuleTest.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,55 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\PHPUnit; | ||
|
||
use PHPStan\Rules\Comparison\ImpossibleCheckTypeMethodCallRule; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<ImpossibleCheckTypeMethodCallRule> | ||
*/ | ||
class ImpossibleCheckTypeMethodCallRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return self::getContainer()->getByType(ImpossibleCheckTypeMethodCallRule::class); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/impossible-assert-method-call.php'], [ | ||
[ | ||
'Call to method PHPUnit\Framework\Assert::assertEmpty() with array{} will always evaluate to true.', | ||
14, | ||
], | ||
[ | ||
'Call to method PHPUnit\Framework\Assert::assertEmpty() with array{1, 2, 3} will always evaluate to false.', | ||
15, | ||
], | ||
]); | ||
} | ||
|
||
public function testBug141(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/bug-141.php'], [ | ||
[ | ||
"Call to method PHPUnit\Framework\Assert::assertEmpty() with non-empty-array<'0.6.0'|'1.0.0'|'1.0.x-dev'|'1.1.x-dev'|'9999999-dev'|'dev-feature-b', true> will always evaluate to false.", | ||
23, | ||
], | ||
]); | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public static function getAdditionalConfigFiles(): array | ||
{ | ||
return [ | ||
__DIR__ . '/../../../extension.neon', | ||
__DIR__ . '/../../../vendor/phpstan/phpstan-strict-rules/rules.neon', | ||
]; | ||
} | ||
|
||
} |
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,53 @@ | ||
<?php | ||
|
||
namespace Bug141; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class Foo extends TestCase | ||
{ | ||
|
||
/** | ||
* @param array<'0.6.0'|'1.0.0'|'1.0.x-dev'|'1.1.x-dev'|'9999999-dev'|'dev-feature-b', true> $a | ||
*/ | ||
public function doFoo(array $a): void | ||
{ | ||
$this->assertEmpty($a); | ||
} | ||
|
||
/** | ||
* @param non-empty-array<'0.6.0'|'1.0.0'|'1.0.x-dev'|'1.1.x-dev'|'9999999-dev'|'dev-feature-b', true> $a | ||
*/ | ||
public function doBar(array $a): void | ||
{ | ||
$this->assertEmpty($a); | ||
} | ||
|
||
public function doBaz(): void | ||
{ | ||
$expected = [ | ||
'0.6.0' => true, | ||
'1.0.0' => true, | ||
'1.0.x-dev' => true, | ||
'1.1.x-dev' => true, | ||
'dev-feature-b' => true, | ||
'dev-feature/a-1.0-B' => true, | ||
'dev-master' => true, | ||
'9999999-dev' => true, // alias of dev-master | ||
]; | ||
|
||
/** @var array<string> */ | ||
$packages = ['0.6.0', '1.0.0', '1']; | ||
|
||
foreach ($packages as $version) { | ||
if (isset($expected[$version])) { | ||
unset($expected[$version]); | ||
} else { | ||
throw new \Exception('Unexpected version '.$version); | ||
} | ||
} | ||
|
||
$this->assertEmpty($expected); | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
tests/Rules/PHPUnit/data/impossible-assert-method-call.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,23 @@ | ||
<?php | ||
|
||
namespace ImpossibleAssertMethodCall; | ||
|
||
use Countable; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class Foo extends TestCase | ||
{ | ||
|
||
public function doFoo(Countable $c): void | ||
{ | ||
$this->assertEmpty($c); | ||
$this->assertEmpty([]); | ||
$this->assertEmpty([1, 2, 3]); | ||
} | ||
|
||
public function doBar(object $o): void | ||
{ | ||
$this->assertEmpty($o); | ||
} | ||
|
||
} |