Skip to content

Commit

Permalink
Merge commit 'f92aab7' into 1.1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Oct 26, 2022
2 parents ade3496 + f92aab7 commit fdda536
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
55 changes: 55 additions & 0 deletions tests/Rules/PHPUnit/ImpossibleCheckTypeMethodCallRuleTest.php
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',
];
}

}
53 changes: 53 additions & 0 deletions tests/Rules/PHPUnit/data/bug-141.php
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 tests/Rules/PHPUnit/data/impossible-assert-method-call.php
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);
}

}

0 comments on commit fdda536

Please sign in to comment.