Skip to content

Commit

Permalink
Rewrite more tests to use TypeInferenceTestCase instead of LevelsTest…
Browse files Browse the repository at this point in the history
…Case
  • Loading branch information
ondrejmirtes committed Jul 17, 2023
1 parent 1fcd8ca commit 673b115
Show file tree
Hide file tree
Showing 16 changed files with 68 additions and 172 deletions.
43 changes: 0 additions & 43 deletions tests/DoctrineIntegration/ODM/DocumentManagerIntegrationTest.php

This file was deleted.

44 changes: 44 additions & 0 deletions tests/DoctrineIntegration/ODM/DocumentManagerTypeInferenceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php declare(strict_types = 1);

namespace PHPStan\DoctrineIntegration\ODM;

use PHPStan\Testing\TypeInferenceTestCase;
use const PHP_VERSION_ID;

class DocumentManagerTypeInferenceTest extends TypeInferenceTestCase
{

/**
* @return iterable<mixed>
*/
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'];
}

}

This file was deleted.

This file was deleted.

9 changes: 4 additions & 5 deletions tests/DoctrineIntegration/ODM/data/customRepositoryUsage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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();
}
}

/**
Expand Down Expand Up @@ -62,6 +59,8 @@ public function get(string $id): MyDocument
throw new RuntimeException('Not found...');
}

assertType(MyDocument::class, $document);

return $document;
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -27,20 +28,24 @@ public function findDynamicType(): void
throw new RuntimeException('Sorry, but no...');
}

assertType(MyDocument::class, $test);

$test->doSomething();
$test->doSomethingElse();
}

public function getReferenceDynamicType(): void
{
$test = $this->documentManager->getReference(MyDocument::class, 'blah-123');
assertType(MyDocument::class, $test);
$test->doSomething();
$test->doSomethingElse();
}

public function getPartialReferenceDynamicType(): void
{
$test = $this->documentManager->getPartialReference(MyDocument::class, 'blah-123');
assertType(MyDocument::class, $test);
$test->doSomething();
$test->doSomethingElse();
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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();
}
}
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -28,6 +29,8 @@ public function findDynamicType(): void
throw new RuntimeException('Sorry, but no...');
}

assertType('object', $test);

$test->doSomething();
$test->doSomethingElse();
}
Expand All @@ -40,13 +43,16 @@ public function findOneByDynamicType(): void
throw new RuntimeException('Sorry, but no...');
}

assertType('object', $test);

$test->doSomething();
$test->doSomethingElse();
}

public function findAllDynamicType(): void
{
$items = $this->repository->findAll();
assertType('array<int, object>', $items);

foreach ($items as $test) {
$test->doSomething();
Expand All @@ -57,6 +63,7 @@ public function findAllDynamicType(): void
public function findByDynamicType(): void
{
$items = $this->repository->findBy(['blah' => 'testing']);
assertType('array<int, object>', $items);

foreach ($items as $test) {
$test->doSomething();
Expand Down Expand Up @@ -85,6 +92,8 @@ public function findDynamicType(): void
throw new RuntimeException('Sorry, but no...');
}

assertType(MyDocument::class, $test);

$test->doSomething();
$test->doSomethingElse();
}
Expand All @@ -97,13 +106,16 @@ public function findOneByDynamicType(): void
throw new RuntimeException('Sorry, but no...');
}

assertType(MyDocument::class, $test);

$test->doSomething();
$test->doSomethingElse();
}

public function findAllDynamicType(): void
{
$items = $this->repository->findAll();
assertType('array<int, ' . MyDocument::class . '>', $items);

foreach ($items as $test) {
$test->doSomething();
Expand All @@ -114,6 +126,7 @@ public function findAllDynamicType(): void
public function findByDynamicType(): void
{
$items = $this->repository->findBy(['blah' => 'testing']);
assertType('array<int, ' . MyDocument::class . '>', $items);

foreach ($items as $test) {
$test->doSomething();
Expand Down
1 change: 0 additions & 1 deletion tests/DoctrineIntegration/ODM/phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
includes:
- ../../../extension.neon
- phar://phpstan.phar/conf/bleedingEdge.neon

parameters:
doctrine:
Expand Down

0 comments on commit 673b115

Please sign in to comment.