Skip to content

assertCount with remembered values causing false positives #208

Closed
@janedbal

Description

@janedbal

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions