Skip to content

Commit bd91958

Browse files
committed
Added tests for ToMany relations
1 parent a818966 commit bd91958

File tree

4 files changed

+163
-0
lines changed

4 files changed

+163
-0
lines changed

tests/Rules/Doctrine/ORM/EntityRelationRuleTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,44 @@ public function ruleProvider(): Iterator
6666
50,
6767
]
6868
]];
69+
70+
yield 'one to many' => [__DIR__ . '/data/EntityWithBrokenOneToManyRelations.php', [
71+
[
72+
'Property can contain iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity> but database expects Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
73+
24,
74+
],
75+
[
76+
'Property can contain Doctrine\Common\Collections\Collection but database expects Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
77+
30,
78+
],
79+
[
80+
'Database can contain Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity> but property expects array<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
81+
36,
82+
],
83+
[
84+
'Property can contain array<PHPStan\Rules\Doctrine\ORM\AnotherEntity> but database expects Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
85+
36,
86+
]
87+
]];
88+
89+
yield 'many to many' => [__DIR__ . '/data/EntityWithBrokenManyToManyRelations.php', [
90+
[
91+
'Property can contain iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity> but database expects Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
92+
24,
93+
],
94+
[
95+
'Property can contain Doctrine\Common\Collections\Collection but database expects Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
96+
30,
97+
],
98+
[
99+
'Database can contain Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity> but property expects array<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
100+
36,
101+
],
102+
[
103+
'Property can contain array<PHPStan\Rules\Doctrine\ORM\AnotherEntity> but database expects Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
104+
36,
105+
]
106+
]];
69107
}
70108

71109
}

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)