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

Call to EntityManager::clear in loop not detected #595

Open
Khartir opened this issue Jul 2, 2024 · 1 comment
Open

Call to EntityManager::clear in loop not detected #595

Khartir opened this issue Jul 2, 2024 · 1 comment

Comments

@Khartir
Copy link
Contributor

Khartir commented Jul 2, 2024

I'm currently in the process of upgrading phpstan an phpstan-doctrine to the current version (phpstan 1.11.6, phpstan-doctrine 1.4.4) and I'm encountering a new error.

We have a test like the following:

public function testCleanup(EntityManager $entityManager): void
{
    if ([] !== $entityManager->getRepository(Favourite::class)->findAll()) {
        self::markTestSkipped('Invalid setup');
    }

    foreach ([1,2,3] as $dummy) {
        // setup ...
        $entityManager->clear();
    }

    self::assertCount(4, $entityManager->getRepository(Favourite::class)->findAll());

    // ...
}

This test reports Call to static method PHPUnit\Framework\Assert::assertCount() with 4 and array{} will always evaluate to false..
When adding a $entityManager->clear(); outside the loop, no error is reported.

@ondrejmirtes
Copy link
Member

As a workaround do this:


public function testCleanup(EntityManager $entityManager): void
{
    $initial = $entityManager->getRepository(Favourite::class)->findAll();
    if ([] !== $initial) {
        self::markTestSkipped('Invalid setup');
    }

    foreach ([1,2,3] as $dummy) {
        // setup ...
        $entityManager->clear();
    }

    $favourites = $entityManager->getRepository(Favourite::class)->findAll();

    self::assertCount(4, $favourites);

    // ...
}

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