phpstan-phpunit icon indicating copy to clipboard operation
phpstan-phpunit copied to clipboard

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

Open Lreus opened this issue 1 year ago • 0 comments

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

Lreus avatar Dec 16 '22 09:12 Lreus