Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assertCount with remembered values causing false positives #208

Open
janedbal opened this issue May 21, 2024 · 2 comments
Open

assertCount with remembered values causing false positives #208

janedbal opened this issue May 21, 2024 · 2 comments

Comments

@janedbal
Copy link
Contributor

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
Copy link
Contributor Author

1.3.16 is the version where this breaks.

@Ciloe
Copy link

Ciloe commented Nov 22, 2024

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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants