phpstan / phpstan-phpunit

PHPUnit extensions and rules for PHPStan
MIT License
468 stars 47 forks source link

assertCount with remembered values causing false positives #208

Open janedbal opened 6 months ago

janedbal commented 6 months ago

Simplified reproducible example:

class AssertCountProblemTest extends TestCase
{

    /**
     * @var list<string>
     */
    private static array $database = [];

    public function testFoo(): void
    {
        self::assertCount(0, $this->getDataFromDatabase());

        self::editDataInDatabase();

        $newData = $this->getDataFromDatabase();
        self::assertCount(1, $newData); // error: Call to static method PHPUnit\Framework\Assert::assertCount() with 1 and array{} will always evaluate to false.
    }

    /**
     * @return list<string>
     */
    private function getDataFromDatabase(): array
    {
        return self::$database;
    }

    private static function editDataInDatabase(): void
    {
        self::$database[] = 'new data';
    }

}

This poped up while upgrading phpstan-phpunit from 1.3.15 to 1.4.0

janedbal commented 6 months ago

1.3.16 is the version where this breaks.

Ciloe commented 4 days ago

Same here at version 1.4.1.

I solved temporarly by creating a variable, and using it in the Count function :

// Before :
self::assertCount(1, $this->getDataFromDatabase());

// After :
$newData = $this->getDataFromDatabase();
self::assertCount(1, $newData);