Skip to content

Commit 15bf6ca

Browse files
lookymanondrejmirtes
authored andcommitted
Added tests for ToMany relations
1 parent 94a40d8 commit 15bf6ca

File tree

4 files changed

+199
-32
lines changed

4 files changed

+199
-32
lines changed

tests/Rules/Doctrine/ORM/EntityRelationRuleTest.php

Lines changed: 74 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,43 +29,85 @@ public function ruleProvider(): Iterator
2929
{
3030
yield 'nice entity' => [__DIR__ . '/data/EntityWithRelations.php', []];
3131

32-
yield 'one to one' => [__DIR__ . '/data/EntityWithBrokenOneToOneRelations.php', [
32+
yield 'one to one' => [__DIR__ . '/data/EntityWithBrokenOneToOneRelations.php',
3333
[
34-
'Property can contain PHPStan\Rules\Doctrine\ORM\AnotherEntity|null but database expects PHPStan\Rules\Doctrine\ORM\AnotherEntity.',
35-
31,
36-
],
37-
[
38-
'Database can contain PHPStan\Rules\Doctrine\ORM\AnotherEntity|null but property expects PHPStan\Rules\Doctrine\ORM\AnotherEntity.',
39-
37,
40-
],
41-
[
42-
'Database can contain PHPStan\Rules\Doctrine\ORM\AnotherEntity|null but property expects PHPStan\Rules\Doctrine\ORM\MyEntity|null.',
43-
50,
44-
],
45-
[
46-
'Property can contain PHPStan\Rules\Doctrine\ORM\MyEntity|null but database expects PHPStan\Rules\Doctrine\ORM\AnotherEntity|null.',
47-
50,
48-
]
49-
]];
34+
[
35+
'Property can contain PHPStan\Rules\Doctrine\ORM\AnotherEntity|null but database expects PHPStan\Rules\Doctrine\ORM\AnotherEntity.',
36+
31,
37+
],
38+
[
39+
'Database can contain PHPStan\Rules\Doctrine\ORM\AnotherEntity|null but property expects PHPStan\Rules\Doctrine\ORM\AnotherEntity.',
40+
37,
41+
],
42+
[
43+
'Database can contain PHPStan\Rules\Doctrine\ORM\AnotherEntity|null but property expects PHPStan\Rules\Doctrine\ORM\MyEntity|null.',
44+
50,
45+
],
46+
[
47+
'Property can contain PHPStan\Rules\Doctrine\ORM\MyEntity|null but database expects PHPStan\Rules\Doctrine\ORM\AnotherEntity|null.',
48+
50,
49+
],
50+
]];
5051

51-
yield 'many to one' => [__DIR__ . '/data/EntityWithBrokenManyToOneRelations.php', [
52+
yield 'many to one' => [__DIR__ . '/data/EntityWithBrokenManyToOneRelations.php',
5253
[
53-
'Property can contain PHPStan\Rules\Doctrine\ORM\AnotherEntity|null but database expects PHPStan\Rules\Doctrine\ORM\AnotherEntity.',
54-
31,
55-
],
56-
[
57-
'Database can contain PHPStan\Rules\Doctrine\ORM\AnotherEntity|null but property expects PHPStan\Rules\Doctrine\ORM\AnotherEntity.',
58-
37,
59-
],
54+
[
55+
'Property can contain PHPStan\Rules\Doctrine\ORM\AnotherEntity|null but database expects PHPStan\Rules\Doctrine\ORM\AnotherEntity.',
56+
31,
57+
],
58+
[
59+
'Database can contain PHPStan\Rules\Doctrine\ORM\AnotherEntity|null but property expects PHPStan\Rules\Doctrine\ORM\AnotherEntity.',
60+
37,
61+
],
62+
[
63+
'Database can contain PHPStan\Rules\Doctrine\ORM\AnotherEntity|null but property expects PHPStan\Rules\Doctrine\ORM\MyEntity|null.',
64+
50,
65+
],
66+
[
67+
'Property can contain PHPStan\Rules\Doctrine\ORM\MyEntity|null but database expects PHPStan\Rules\Doctrine\ORM\AnotherEntity|null.',
68+
50,
69+
],
70+
]];
71+
72+
yield 'one to many' => [__DIR__ . '/data/EntityWithBrokenOneToManyRelations.php',
6073
[
61-
'Database can contain PHPStan\Rules\Doctrine\ORM\AnotherEntity|null but property expects PHPStan\Rules\Doctrine\ORM\MyEntity|null.',
62-
50,
63-
],
74+
[
75+
'Property can contain iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity> but database expects Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
76+
24,
77+
],
78+
[
79+
'Property can contain Doctrine\Common\Collections\Collection but database expects Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
80+
30,
81+
],
82+
[
83+
'Database can contain Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity> but property expects array<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
84+
36,
85+
],
86+
[
87+
'Property can contain array<PHPStan\Rules\Doctrine\ORM\AnotherEntity> but database expects Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
88+
36,
89+
],
90+
]];
91+
92+
yield 'many to many' => [__DIR__ . '/data/EntityWithBrokenManyToManyRelations.php',
6493
[
65-
'Property can contain PHPStan\Rules\Doctrine\ORM\MyEntity|null but database expects PHPStan\Rules\Doctrine\ORM\AnotherEntity|null.',
66-
50,
67-
]
68-
]];
94+
[
95+
'Property can contain iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity> but database expects Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
96+
24,
97+
],
98+
[
99+
'Property can contain Doctrine\Common\Collections\Collection but database expects Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
100+
30,
101+
],
102+
[
103+
'Database can contain Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity> but property expects array<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
104+
36,
105+
],
106+
[
107+
'Property can contain array<PHPStan\Rules\Doctrine\ORM\AnotherEntity> but database expects Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
108+
36,
109+
],
110+
]];
69111
}
70112

71113
}

tests/Rules/Doctrine/ORM/data/AnotherEntity.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,29 @@ class AnotherEntity
2222
*/
2323
private $manyToOne;
2424

25+
/**
26+
* @ORM\ManyToOne(targetEntity="PHPStan\Rules\Doctrine\ORM\EntityWithBrokenOneToManyRelations")
27+
*/
28+
private $one;
29+
30+
/**
31+
* @ORM\ManyToOne(targetEntity="PHPStan\Rules\Doctrine\ORM\EntityWithBrokenOneToManyRelations")
32+
*/
33+
private $two;
34+
35+
/**
36+
* @ORM\ManyToOne(targetEntity="PHPStan\Rules\Doctrine\ORM\EntityWithBrokenOneToManyRelations")
37+
*/
38+
private $three;
39+
40+
/**
41+
* @ORM\ManyToOne(targetEntity="PHPStan\Rules\Doctrine\ORM\EntityWithBrokenOneToManyRelations")
42+
*/
43+
private $four;
44+
45+
/**
46+
* @ORM\ManyToOne(targetEntity="PHPStan\Rules\Doctrine\ORM\EntityWithBrokenOneToManyRelations")
47+
*/
48+
private $five;
49+
2550
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Doctrine\ORM;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
7+
/**
8+
* @ORM\Entity()
9+
*/
10+
class EntityWithBrokenManyToManyRelations
11+
{
12+
13+
/**
14+
* @ORM\Id()
15+
* @ORM\Column(type="int")
16+
* @var int
17+
*/
18+
private $id;
19+
20+
/**
21+
* @ORM\ManyToMany(targetEntity="PHPStan\Rules\Doctrine\ORM\AnotherEntity")
22+
* @var iterable<\PHPStan\Rules\Doctrine\ORM\AnotherEntity>
23+
*/
24+
private $manyToManyWithIterableAnnotation;
25+
26+
/**
27+
* @ORM\ManyToMany(targetEntity="PHPStan\Rules\Doctrine\ORM\AnotherEntity")
28+
* @var \Doctrine\Common\Collections\Collection
29+
*/
30+
private $manyToManyWithCollectionAnnotation;
31+
32+
/**
33+
* @ORM\ManyToMany(targetEntity="PHPStan\Rules\Doctrine\ORM\AnotherEntity")
34+
* @var \PHPStan\Rules\Doctrine\ORM\AnotherEntity[]
35+
*/
36+
private $manyToManyWithArrayAnnotation;
37+
38+
/**
39+
* @ORM\ManyToMany(targetEntity="PHPStan\Rules\Doctrine\ORM\AnotherEntity")
40+
* @var \Doctrine\Common\Collections\Collection&iterable<\PHPStan\Rules\Doctrine\ORM\AnotherEntity>
41+
*/
42+
private $manyToManyWithCorrectAnnotation;
43+
44+
/**
45+
* @ORM\ManyToMany(targetEntity="PHPStan\Rules\Doctrine\ORM\AnotherEntity")
46+
* @var \Doctrine\Common\Collections\Collection|\PHPStan\Rules\Doctrine\ORM\AnotherEntity[]
47+
*/
48+
private $manyToManyWithCorrectOldStyleAnnotation;
49+
50+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Doctrine\ORM;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
7+
/**
8+
* @ORM\Entity()
9+
*/
10+
class EntityWithBrokenOneToManyRelations
11+
{
12+
13+
/**
14+
* @ORM\Id()
15+
* @ORM\Column(type="int")
16+
* @var int
17+
*/
18+
private $id;
19+
20+
/**
21+
* @ORM\OneToMany(targetEntity="PHPStan\Rules\Doctrine\ORM\AnotherEntity", mappedBy="one")
22+
* @var iterable<\PHPStan\Rules\Doctrine\ORM\AnotherEntity>
23+
*/
24+
private $oneToManyWithIterableAnnotation;
25+
26+
/**
27+
* @ORM\OneToMany(targetEntity="PHPStan\Rules\Doctrine\ORM\AnotherEntity", mappedBy="two")
28+
* @var \Doctrine\Common\Collections\Collection
29+
*/
30+
private $oneToManyWithCollectionAnnotation;
31+
32+
/**
33+
* @ORM\OneToMany(targetEntity="PHPStan\Rules\Doctrine\ORM\AnotherEntity", mappedBy="three")
34+
* @var \PHPStan\Rules\Doctrine\ORM\AnotherEntity[]
35+
*/
36+
private $oneToManyWithArrayAnnotation;
37+
38+
/**
39+
* @ORM\OneToMany(targetEntity="PHPStan\Rules\Doctrine\ORM\AnotherEntity", mappedBy="four")
40+
* @var \Doctrine\Common\Collections\Collection&iterable<\PHPStan\Rules\Doctrine\ORM\AnotherEntity>
41+
*/
42+
private $oneToManyWithCorrectAnnotation;
43+
44+
/**
45+
* @ORM\OneToMany(targetEntity="PHPStan\Rules\Doctrine\ORM\AnotherEntity", mappedBy="five")
46+
* @var \Doctrine\Common\Collections\Collection|\PHPStan\Rules\Doctrine\ORM\AnotherEntity[]
47+
*/
48+
private $oneToManyWithCorrectOldStyleAnnotation;
49+
50+
}

0 commit comments

Comments
 (0)