Skip to content

Commit 673b115

Browse files
committed
Rewrite more tests to use TypeInferenceTestCase instead of LevelsTestCase
1 parent 1fcd8ca commit 673b115

16 files changed

+68
-172
lines changed

tests/DoctrineIntegration/ODM/DocumentManagerIntegrationTest.php

-43
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\DoctrineIntegration\ODM;
4+
5+
use PHPStan\Testing\TypeInferenceTestCase;
6+
use const PHP_VERSION_ID;
7+
8+
class DocumentManagerTypeInferenceTest extends TypeInferenceTestCase
9+
{
10+
11+
/**
12+
* @return iterable<mixed>
13+
*/
14+
public function dataFileAsserts(): iterable
15+
{
16+
if (PHP_VERSION_ID >= 80000) {
17+
return [];
18+
}
19+
20+
yield from $this->gatherAssertTypes(__DIR__ . '/data/documentManagerDynamicReturn.php');
21+
yield from $this->gatherAssertTypes(__DIR__ . '/data/documentRepositoryDynamicReturn.php');
22+
yield from $this->gatherAssertTypes(__DIR__ . '/data/documentManagerMergeReturn.php');
23+
yield from $this->gatherAssertTypes(__DIR__ . '/data/customRepositoryUsage.php');
24+
}
25+
26+
/**
27+
* @dataProvider dataFileAsserts
28+
* @param mixed ...$args
29+
*/
30+
public function testFileAsserts(
31+
string $assertType,
32+
string $file,
33+
...$args
34+
): void
35+
{
36+
$this->assertFileAsserts($assertType, $file, ...$args);
37+
}
38+
39+
public static function getAdditionalConfigFiles(): array
40+
{
41+
return [__DIR__ . '/phpstan.neon'];
42+
}
43+
44+
}

tests/DoctrineIntegration/ODM/data/customRepositoryUsage-2.json

-7
This file was deleted.

tests/DoctrineIntegration/ODM/data/customRepositoryUsage-4.json

-7
This file was deleted.

tests/DoctrineIntegration/ODM/data/customRepositoryUsage.php

+4-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Doctrine\ODM\MongoDB\Mapping\Annotations\Document;
88
use Doctrine\ODM\MongoDB\Mapping\Annotations\Id;
99
use RuntimeException;
10+
use function PHPStan\Testing\assertType;
1011

1112
class Example
1213
{
@@ -23,13 +24,9 @@ public function __construct(DocumentManager $documentManager)
2324
public function get(): void
2425
{
2526
$test = $this->repository->get('testing');
27+
assertType(MyDocument::class, $test);
2628
$test->doSomethingElse();
2729
}
28-
29-
public function nonexistant(): void
30-
{
31-
$this->repository->nonexistant();
32-
}
3330
}
3431

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

62+
assertType(MyDocument::class, $document);
63+
6564
return $document;
6665
}
6766
}

tests/DoctrineIntegration/ODM/data/documentManagerDynamicReturn-2.json

-17
This file was deleted.

tests/DoctrineIntegration/ODM/data/documentManagerDynamicReturn-4.json

-7
This file was deleted.

tests/DoctrineIntegration/ODM/data/documentManagerDynamicReturn.php

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Doctrine\ODM\MongoDB\Mapping\Annotations\Document;
77
use Doctrine\ODM\MongoDB\Mapping\Annotations\Id;
88
use RuntimeException;
9+
use function PHPStan\Testing\assertType;
910

1011
class Example
1112
{
@@ -27,20 +28,24 @@ public function findDynamicType(): void
2728
throw new RuntimeException('Sorry, but no...');
2829
}
2930

31+
assertType(MyDocument::class, $test);
32+
3033
$test->doSomething();
3134
$test->doSomethingElse();
3235
}
3336

3437
public function getReferenceDynamicType(): void
3538
{
3639
$test = $this->documentManager->getReference(MyDocument::class, 'blah-123');
40+
assertType(MyDocument::class, $test);
3741
$test->doSomething();
3842
$test->doSomethingElse();
3943
}
4044

4145
public function getPartialReferenceDynamicType(): void
4246
{
4347
$test = $this->documentManager->getPartialReference(MyDocument::class, 'blah-123');
48+
assertType(MyDocument::class, $test);
4449
$test->doSomething();
4550
$test->doSomethingElse();
4651
}

tests/DoctrineIntegration/ODM/data/documentManagerMergeReturn-4.json

-7
This file was deleted.

tests/DoctrineIntegration/ODM/data/documentManagerMergeReturn.php

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Doctrine\ODM\MongoDB\DocumentManager;
66
use Doctrine\ODM\MongoDB\Mapping\Annotations\Document;
77
use Doctrine\ODM\MongoDB\Mapping\Annotations\Id;
8+
use function PHPStan\Testing\assertType;
89

910
class Example
1011
{
@@ -21,6 +22,7 @@ public function __construct(DocumentManager $documentManager)
2122
public function merge(): void
2223
{
2324
$test = $this->documentManager->merge(new MyDocument());
25+
assertType(MyDocument::class, $test);
2426
$test->doSomething();
2527
}
2628
}

tests/DoctrineIntegration/ODM/data/documentRepositoryDynamicReturn-2.json

-22
This file was deleted.

tests/DoctrineIntegration/ODM/data/documentRepositoryDynamicReturn-4.json

-7
This file was deleted.

tests/DoctrineIntegration/ODM/data/documentRepositoryDynamicReturn-6.json

-7
This file was deleted.

tests/DoctrineIntegration/ODM/data/documentRepositoryDynamicReturn-7.json

-42
This file was deleted.

tests/DoctrineIntegration/ODM/data/documentRepositoryDynamicReturn.php

+13
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Doctrine\ODM\MongoDB\Mapping\Annotations\Document;
88
use Doctrine\ODM\MongoDB\Mapping\Annotations\Id;
99
use RuntimeException;
10+
use function PHPStan\Testing\assertType;
1011

1112
class Example
1213
{
@@ -28,6 +29,8 @@ public function findDynamicType(): void
2829
throw new RuntimeException('Sorry, but no...');
2930
}
3031

32+
assertType('object', $test);
33+
3134
$test->doSomething();
3235
$test->doSomethingElse();
3336
}
@@ -40,13 +43,16 @@ public function findOneByDynamicType(): void
4043
throw new RuntimeException('Sorry, but no...');
4144
}
4245

46+
assertType('object', $test);
47+
4348
$test->doSomething();
4449
$test->doSomethingElse();
4550
}
4651

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

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

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

95+
assertType(MyDocument::class, $test);
96+
8897
$test->doSomething();
8998
$test->doSomethingElse();
9099
}
@@ -97,13 +106,16 @@ public function findOneByDynamicType(): void
97106
throw new RuntimeException('Sorry, but no...');
98107
}
99108

109+
assertType(MyDocument::class, $test);
110+
100111
$test->doSomething();
101112
$test->doSomethingElse();
102113
}
103114

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

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

118131
foreach ($items as $test) {
119132
$test->doSomething();

tests/DoctrineIntegration/ODM/phpstan.neon

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
includes:
22
- ../../../extension.neon
3-
- phar://phpstan.phar/conf/bleedingEdge.neon
43

54
parameters:
65
doctrine:

0 commit comments

Comments
 (0)