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

PhpStan does not consider usage of Phpunit Assert::assertNotEmpty and Assert::assertContainsOnlyInstancesOf when returning an Array #158

Open
Lreus opened this issue Dec 16, 2022 · 0 comments

Comments

@Lreus
Copy link

Lreus commented Dec 16, 2022

Hello,

I am building a test returning an array of entities to be used in some others tests marked with @depends.
Despite I check that my returned array is not empty an contains only instances of a specific class, phpstan keeps alerting
that my returned value does not match. Here is my code:

     /**
     * @return Post[]
     *
     * @throws \Exception
     */
    public function testFindForHomePageReturnsCollectionOfPosts(): array
    {
        $testSubject = self::getContainer()->get(PostRepository::class);
        if (!$testSubject instanceof PostRepository) {
            throw new ServiceNotFoundException(PostRepository::class);
        }

        $result = $testSubject->findForHomePage()->toArray();
        self::assertNotEmpty($result);
        self::assertContainsOnlyInstancesOf(Post::class, $result);

        return $result;
    }

And the alert thrown by phpstan (line 34 is the return $result statement):
image

Context:

Symfony 6.2 KernelTestCase
phpunit/phpunit 9.5.25
symfony/phpunit-bridge 6.2.0

PhpStan 1.9.3
extensions:

  • phpstan-phpunit 1.3.2
  • phpstan-doctrine 1.3.18
  • phpstan-symfony 1.2.15
  • phpstan/phpstan-webmozart-assert 1.2.1

Thanks a lot for your help :)

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

1 participant