From 673b115e60c983a8f1c464a79f4c2229e14e1957 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Mon, 17 Jul 2023 11:38:24 +0200 Subject: [PATCH] Rewrite more tests to use TypeInferenceTestCase instead of LevelsTestCase --- .../ODM/DocumentManagerIntegrationTest.php | 43 ------------------ .../ODM/DocumentManagerTypeInferenceTest.php | 44 +++++++++++++++++++ .../ODM/data/customRepositoryUsage-2.json | 7 --- .../ODM/data/customRepositoryUsage-4.json | 7 --- .../ODM/data/customRepositoryUsage.php | 9 ++-- .../data/documentManagerDynamicReturn-2.json | 17 ------- .../data/documentManagerDynamicReturn-4.json | 7 --- .../ODM/data/documentManagerDynamicReturn.php | 5 +++ .../data/documentManagerMergeReturn-4.json | 7 --- .../ODM/data/documentManagerMergeReturn.php | 2 + .../documentRepositoryDynamicReturn-2.json | 22 ---------- .../documentRepositoryDynamicReturn-4.json | 7 --- .../documentRepositoryDynamicReturn-6.json | 7 --- .../documentRepositoryDynamicReturn-7.json | 42 ------------------ .../data/documentRepositoryDynamicReturn.php | 13 ++++++ tests/DoctrineIntegration/ODM/phpstan.neon | 1 - 16 files changed, 68 insertions(+), 172 deletions(-) delete mode 100644 tests/DoctrineIntegration/ODM/DocumentManagerIntegrationTest.php create mode 100644 tests/DoctrineIntegration/ODM/DocumentManagerTypeInferenceTest.php delete mode 100644 tests/DoctrineIntegration/ODM/data/customRepositoryUsage-2.json delete mode 100644 tests/DoctrineIntegration/ODM/data/customRepositoryUsage-4.json delete mode 100644 tests/DoctrineIntegration/ODM/data/documentManagerDynamicReturn-2.json delete mode 100644 tests/DoctrineIntegration/ODM/data/documentManagerDynamicReturn-4.json delete mode 100644 tests/DoctrineIntegration/ODM/data/documentManagerMergeReturn-4.json delete mode 100644 tests/DoctrineIntegration/ODM/data/documentRepositoryDynamicReturn-2.json delete mode 100644 tests/DoctrineIntegration/ODM/data/documentRepositoryDynamicReturn-4.json delete mode 100644 tests/DoctrineIntegration/ODM/data/documentRepositoryDynamicReturn-6.json delete mode 100644 tests/DoctrineIntegration/ODM/data/documentRepositoryDynamicReturn-7.json diff --git a/tests/DoctrineIntegration/ODM/DocumentManagerIntegrationTest.php b/tests/DoctrineIntegration/ODM/DocumentManagerIntegrationTest.php deleted file mode 100644 index bd436981..00000000 --- a/tests/DoctrineIntegration/ODM/DocumentManagerIntegrationTest.php +++ /dev/null @@ -1,43 +0,0 @@ -= 80000) { - return []; - } - - return [ - ['documentManagerDynamicReturn'], - ['documentRepositoryDynamicReturn'], - ['documentManagerMergeReturn'], - ['customRepositoryUsage'], - ]; - } - - public function getDataPath(): string - { - return __DIR__ . '/data'; - } - - public function getPhpStanExecutablePath(): string - { - return __DIR__ . '/../../../vendor/phpstan/phpstan/phpstan'; - } - - public function getPhpStanConfigPath(): string - { - return __DIR__ . '/phpstan.neon'; - } - -} diff --git a/tests/DoctrineIntegration/ODM/DocumentManagerTypeInferenceTest.php b/tests/DoctrineIntegration/ODM/DocumentManagerTypeInferenceTest.php new file mode 100644 index 00000000..8fe0c036 --- /dev/null +++ b/tests/DoctrineIntegration/ODM/DocumentManagerTypeInferenceTest.php @@ -0,0 +1,44 @@ + + */ + public function dataFileAsserts(): iterable + { + if (PHP_VERSION_ID >= 80000) { + return []; + } + + yield from $this->gatherAssertTypes(__DIR__ . '/data/documentManagerDynamicReturn.php'); + yield from $this->gatherAssertTypes(__DIR__ . '/data/documentRepositoryDynamicReturn.php'); + yield from $this->gatherAssertTypes(__DIR__ . '/data/documentManagerMergeReturn.php'); + yield from $this->gatherAssertTypes(__DIR__ . '/data/customRepositoryUsage.php'); + } + + /** + * @dataProvider dataFileAsserts + * @param mixed ...$args + */ + public function testFileAsserts( + string $assertType, + string $file, + ...$args + ): void + { + $this->assertFileAsserts($assertType, $file, ...$args); + } + + public static function getAdditionalConfigFiles(): array + { + return [__DIR__ . '/phpstan.neon']; + } + +} diff --git a/tests/DoctrineIntegration/ODM/data/customRepositoryUsage-2.json b/tests/DoctrineIntegration/ODM/data/customRepositoryUsage-2.json deleted file mode 100644 index 47d27e81..00000000 --- a/tests/DoctrineIntegration/ODM/data/customRepositoryUsage-2.json +++ /dev/null @@ -1,7 +0,0 @@ -[ - { - "message": "Call to an undefined method PHPStan\\DoctrineIntegration\\ODM\\CustomRepositoryUsage\\MyRepository::nonexistant().", - "line": 31, - "ignorable": true - } -] \ No newline at end of file diff --git a/tests/DoctrineIntegration/ODM/data/customRepositoryUsage-4.json b/tests/DoctrineIntegration/ODM/data/customRepositoryUsage-4.json deleted file mode 100644 index 0e637224..00000000 --- a/tests/DoctrineIntegration/ODM/data/customRepositoryUsage-4.json +++ /dev/null @@ -1,7 +0,0 @@ -[ - { - "message": "Property PHPStan\\DoctrineIntegration\\ODM\\CustomRepositoryUsage\\MyDocument::$id is unused.", - "line": 45, - "ignorable": true - } -] \ No newline at end of file diff --git a/tests/DoctrineIntegration/ODM/data/customRepositoryUsage.php b/tests/DoctrineIntegration/ODM/data/customRepositoryUsage.php index 646bd3a9..1699d7f0 100644 --- a/tests/DoctrineIntegration/ODM/data/customRepositoryUsage.php +++ b/tests/DoctrineIntegration/ODM/data/customRepositoryUsage.php @@ -7,6 +7,7 @@ use Doctrine\ODM\MongoDB\Mapping\Annotations\Document; use Doctrine\ODM\MongoDB\Mapping\Annotations\Id; use RuntimeException; +use function PHPStan\Testing\assertType; class Example { @@ -23,13 +24,9 @@ public function __construct(DocumentManager $documentManager) public function get(): void { $test = $this->repository->get('testing'); + assertType(MyDocument::class, $test); $test->doSomethingElse(); } - - public function nonexistant(): void - { - $this->repository->nonexistant(); - } } /** @@ -62,6 +59,8 @@ public function get(string $id): MyDocument throw new RuntimeException('Not found...'); } + assertType(MyDocument::class, $document); + return $document; } } diff --git a/tests/DoctrineIntegration/ODM/data/documentManagerDynamicReturn-2.json b/tests/DoctrineIntegration/ODM/data/documentManagerDynamicReturn-2.json deleted file mode 100644 index af15d7c5..00000000 --- a/tests/DoctrineIntegration/ODM/data/documentManagerDynamicReturn-2.json +++ /dev/null @@ -1,17 +0,0 @@ -[ - { - "message": "Call to an undefined method PHPStan\\DoctrineIntegration\\ODM\\DocumentManagerDynamicReturn\\MyDocument::doSomethingElse().", - "line": 31, - "ignorable": true - }, - { - "message": "Call to an undefined method PHPStan\\DoctrineIntegration\\ODM\\DocumentManagerDynamicReturn\\MyDocument::doSomethingElse().", - "line": 38, - "ignorable": true - }, - { - "message": "Call to an undefined method PHPStan\\DoctrineIntegration\\ODM\\DocumentManagerDynamicReturn\\MyDocument::doSomethingElse().", - "line": 45, - "ignorable": true - } -] \ No newline at end of file diff --git a/tests/DoctrineIntegration/ODM/data/documentManagerDynamicReturn-4.json b/tests/DoctrineIntegration/ODM/data/documentManagerDynamicReturn-4.json deleted file mode 100644 index 55be23eb..00000000 --- a/tests/DoctrineIntegration/ODM/data/documentManagerDynamicReturn-4.json +++ /dev/null @@ -1,7 +0,0 @@ -[ - { - "message": "Property PHPStan\\DoctrineIntegration\\ODM\\DocumentManagerDynamicReturn\\MyDocument::$id is unused.", - "line": 59, - "ignorable": true - } -] \ No newline at end of file diff --git a/tests/DoctrineIntegration/ODM/data/documentManagerDynamicReturn.php b/tests/DoctrineIntegration/ODM/data/documentManagerDynamicReturn.php index a3778f09..b25bd1e3 100644 --- a/tests/DoctrineIntegration/ODM/data/documentManagerDynamicReturn.php +++ b/tests/DoctrineIntegration/ODM/data/documentManagerDynamicReturn.php @@ -6,6 +6,7 @@ use Doctrine\ODM\MongoDB\Mapping\Annotations\Document; use Doctrine\ODM\MongoDB\Mapping\Annotations\Id; use RuntimeException; +use function PHPStan\Testing\assertType; class Example { @@ -27,6 +28,8 @@ public function findDynamicType(): void throw new RuntimeException('Sorry, but no...'); } + assertType(MyDocument::class, $test); + $test->doSomething(); $test->doSomethingElse(); } @@ -34,6 +37,7 @@ public function findDynamicType(): void public function getReferenceDynamicType(): void { $test = $this->documentManager->getReference(MyDocument::class, 'blah-123'); + assertType(MyDocument::class, $test); $test->doSomething(); $test->doSomethingElse(); } @@ -41,6 +45,7 @@ public function getReferenceDynamicType(): void public function getPartialReferenceDynamicType(): void { $test = $this->documentManager->getPartialReference(MyDocument::class, 'blah-123'); + assertType(MyDocument::class, $test); $test->doSomething(); $test->doSomethingElse(); } diff --git a/tests/DoctrineIntegration/ODM/data/documentManagerMergeReturn-4.json b/tests/DoctrineIntegration/ODM/data/documentManagerMergeReturn-4.json deleted file mode 100644 index f8487924..00000000 --- a/tests/DoctrineIntegration/ODM/data/documentManagerMergeReturn-4.json +++ /dev/null @@ -1,7 +0,0 @@ -[ - { - "message": "Property PHPStan\\DoctrineIntegration\\ODM\\DocumentManagerMergeReturn\\MyDocument::$id is unused.", - "line": 38, - "ignorable": true - } -] \ No newline at end of file diff --git a/tests/DoctrineIntegration/ODM/data/documentManagerMergeReturn.php b/tests/DoctrineIntegration/ODM/data/documentManagerMergeReturn.php index 4a190fe1..54b196b0 100644 --- a/tests/DoctrineIntegration/ODM/data/documentManagerMergeReturn.php +++ b/tests/DoctrineIntegration/ODM/data/documentManagerMergeReturn.php @@ -5,6 +5,7 @@ use Doctrine\ODM\MongoDB\DocumentManager; use Doctrine\ODM\MongoDB\Mapping\Annotations\Document; use Doctrine\ODM\MongoDB\Mapping\Annotations\Id; +use function PHPStan\Testing\assertType; class Example { @@ -21,6 +22,7 @@ public function __construct(DocumentManager $documentManager) public function merge(): void { $test = $this->documentManager->merge(new MyDocument()); + assertType(MyDocument::class, $test); $test->doSomething(); } } diff --git a/tests/DoctrineIntegration/ODM/data/documentRepositoryDynamicReturn-2.json b/tests/DoctrineIntegration/ODM/data/documentRepositoryDynamicReturn-2.json deleted file mode 100644 index 1ccb9004..00000000 --- a/tests/DoctrineIntegration/ODM/data/documentRepositoryDynamicReturn-2.json +++ /dev/null @@ -1,22 +0,0 @@ -[ - { - "message": "Call to an undefined method PHPStan\\DoctrineIntegration\\ODM\\DocumentRepositoryDynamicReturn\\MyDocument::doSomethingElse().", - "line": 89, - "ignorable": true - }, - { - "message": "Call to an undefined method PHPStan\\DoctrineIntegration\\ODM\\DocumentRepositoryDynamicReturn\\MyDocument::doSomethingElse().", - "line": 101, - "ignorable": true - }, - { - "message": "Call to an undefined method PHPStan\\DoctrineIntegration\\ODM\\DocumentRepositoryDynamicReturn\\MyDocument::doSomethingElse().", - "line": 110, - "ignorable": true - }, - { - "message": "Call to an undefined method PHPStan\\DoctrineIntegration\\ODM\\DocumentRepositoryDynamicReturn\\MyDocument::doSomethingElse().", - "line": 120, - "ignorable": true - } -] \ No newline at end of file diff --git a/tests/DoctrineIntegration/ODM/data/documentRepositoryDynamicReturn-4.json b/tests/DoctrineIntegration/ODM/data/documentRepositoryDynamicReturn-4.json deleted file mode 100644 index 8dd666cd..00000000 --- a/tests/DoctrineIntegration/ODM/data/documentRepositoryDynamicReturn-4.json +++ /dev/null @@ -1,7 +0,0 @@ -[ - { - "message": "Property PHPStan\\DoctrineIntegration\\ODM\\DocumentRepositoryDynamicReturn\\MyDocument::$id is unused.", - "line": 135, - "ignorable": true - } -] \ No newline at end of file diff --git a/tests/DoctrineIntegration/ODM/data/documentRepositoryDynamicReturn-6.json b/tests/DoctrineIntegration/ODM/data/documentRepositoryDynamicReturn-6.json deleted file mode 100644 index 89cd810f..00000000 --- a/tests/DoctrineIntegration/ODM/data/documentRepositoryDynamicReturn-6.json +++ /dev/null @@ -1,7 +0,0 @@ -[ - { - "message": "Property PHPStan\\DoctrineIntegration\\ODM\\DocumentRepositoryDynamicReturn\\Example::$repository with generic class Doctrine\\ODM\\MongoDB\\Repository\\DocumentRepository does not specify its types: TDocumentClass", - "line": 16, - "ignorable": true - } -] \ No newline at end of file diff --git a/tests/DoctrineIntegration/ODM/data/documentRepositoryDynamicReturn-7.json b/tests/DoctrineIntegration/ODM/data/documentRepositoryDynamicReturn-7.json deleted file mode 100644 index 1ff56542..00000000 --- a/tests/DoctrineIntegration/ODM/data/documentRepositoryDynamicReturn-7.json +++ /dev/null @@ -1,42 +0,0 @@ -[ - { - "message": "Call to an undefined method object::doSomething().", - "line": 31, - "ignorable": true - }, - { - "message": "Call to an undefined method object::doSomethingElse().", - "line": 32, - "ignorable": true - }, - { - "message": "Call to an undefined method object::doSomething().", - "line": 43, - "ignorable": true - }, - { - "message": "Call to an undefined method object::doSomethingElse().", - "line": 44, - "ignorable": true - }, - { - "message": "Call to an undefined method object::doSomething().", - "line": 52, - "ignorable": true - }, - { - "message": "Call to an undefined method object::doSomethingElse().", - "line": 53, - "ignorable": true - }, - { - "message": "Call to an undefined method object::doSomething().", - "line": 62, - "ignorable": true - }, - { - "message": "Call to an undefined method object::doSomethingElse().", - "line": 63, - "ignorable": true - } -] \ No newline at end of file diff --git a/tests/DoctrineIntegration/ODM/data/documentRepositoryDynamicReturn.php b/tests/DoctrineIntegration/ODM/data/documentRepositoryDynamicReturn.php index 63db505e..0b85b18f 100644 --- a/tests/DoctrineIntegration/ODM/data/documentRepositoryDynamicReturn.php +++ b/tests/DoctrineIntegration/ODM/data/documentRepositoryDynamicReturn.php @@ -7,6 +7,7 @@ use Doctrine\ODM\MongoDB\Mapping\Annotations\Document; use Doctrine\ODM\MongoDB\Mapping\Annotations\Id; use RuntimeException; +use function PHPStan\Testing\assertType; class Example { @@ -28,6 +29,8 @@ public function findDynamicType(): void throw new RuntimeException('Sorry, but no...'); } + assertType('object', $test); + $test->doSomething(); $test->doSomethingElse(); } @@ -40,6 +43,8 @@ public function findOneByDynamicType(): void throw new RuntimeException('Sorry, but no...'); } + assertType('object', $test); + $test->doSomething(); $test->doSomethingElse(); } @@ -47,6 +52,7 @@ public function findOneByDynamicType(): void public function findAllDynamicType(): void { $items = $this->repository->findAll(); + assertType('array', $items); foreach ($items as $test) { $test->doSomething(); @@ -57,6 +63,7 @@ public function findAllDynamicType(): void public function findByDynamicType(): void { $items = $this->repository->findBy(['blah' => 'testing']); + assertType('array', $items); foreach ($items as $test) { $test->doSomething(); @@ -85,6 +92,8 @@ public function findDynamicType(): void throw new RuntimeException('Sorry, but no...'); } + assertType(MyDocument::class, $test); + $test->doSomething(); $test->doSomethingElse(); } @@ -97,6 +106,8 @@ public function findOneByDynamicType(): void throw new RuntimeException('Sorry, but no...'); } + assertType(MyDocument::class, $test); + $test->doSomething(); $test->doSomethingElse(); } @@ -104,6 +115,7 @@ public function findOneByDynamicType(): void public function findAllDynamicType(): void { $items = $this->repository->findAll(); + assertType('array', $items); foreach ($items as $test) { $test->doSomething(); @@ -114,6 +126,7 @@ public function findAllDynamicType(): void public function findByDynamicType(): void { $items = $this->repository->findBy(['blah' => 'testing']); + assertType('array', $items); foreach ($items as $test) { $test->doSomething(); diff --git a/tests/DoctrineIntegration/ODM/phpstan.neon b/tests/DoctrineIntegration/ODM/phpstan.neon index 446d7256..a797d338 100644 --- a/tests/DoctrineIntegration/ODM/phpstan.neon +++ b/tests/DoctrineIntegration/ODM/phpstan.neon @@ -1,6 +1,5 @@ includes: - ../../../extension.neon - - phar://phpstan.phar/conf/bleedingEdge.neon parameters: doctrine: