Skip to content

Commit 94a40d8

Browse files
lookymanondrejmirtes
authored andcommitted
Added tests for ManyToOne relations
1 parent d1808fd commit 94a40d8

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

tests/Rules/Doctrine/ORM/EntityRelationRuleTest.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,28 @@ public function testRule(string $file, array $expectedErrors): void
2727

2828
public function ruleProvider(): Iterator
2929
{
30-
yield [__DIR__ . '/data/EntityWithRelations.php', []];
31-
yield [__DIR__ . '/data/EntityWithBrokenOneToOneRelations.php', [
30+
yield 'nice entity' => [__DIR__ . '/data/EntityWithRelations.php', []];
31+
32+
yield 'one to one' => [__DIR__ . '/data/EntityWithBrokenOneToOneRelations.php', [
33+
[
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+
]];
50+
51+
yield 'many to one' => [__DIR__ . '/data/EntityWithBrokenManyToOneRelations.php', [
3252
[
3353
'Property can contain PHPStan\Rules\Doctrine\ORM\AnotherEntity|null but database expects PHPStan\Rules\Doctrine\ORM\AnotherEntity.',
3454
31,
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 EntityWithBrokenManyToOneRelations
11+
{
12+
13+
/**
14+
* @ORM\Id()
15+
* @ORM\Column(type="int")
16+
* @var int
17+
*/
18+
private $id;
19+
20+
/**
21+
* @ORM\ManyToOne(targetEntity="PHPStan\Rules\Doctrine\ORM\AnotherEntity")
22+
* @var \PHPStan\Rules\Doctrine\ORM\AnotherEntity|null
23+
*/
24+
private $manyToOneNullableBoth;
25+
26+
/**
27+
* @ORM\ManyToOne(targetEntity="PHPStan\Rules\Doctrine\ORM\AnotherEntity")
28+
* @ORM\JoinColumn(nullable=false)
29+
* @var \PHPStan\Rules\Doctrine\ORM\AnotherEntity|null
30+
*/
31+
private $manyToOneNullableProperty;
32+
33+
/**
34+
* @ORM\ManyToOne(targetEntity="PHPStan\Rules\Doctrine\ORM\AnotherEntity")
35+
* @var \PHPStan\Rules\Doctrine\ORM\AnotherEntity
36+
*/
37+
private $manyToOneNullableColumn;
38+
39+
/**
40+
* @ORM\ManyToOne(targetEntity="PHPStan\Rules\Doctrine\ORM\AnotherEntity")
41+
* @ORM\JoinColumn(nullable=false)
42+
* @var \PHPStan\Rules\Doctrine\ORM\AnotherEntity
43+
*/
44+
private $manyToOneNonNullable;
45+
46+
/**
47+
* @ORM\ManyToOne(targetEntity="PHPStan\Rules\Doctrine\ORM\AnotherEntity")
48+
* @var \PHPStan\Rules\Doctrine\ORM\MyEntity|null
49+
*/
50+
private $manyToOneWrongClass;
51+
52+
}

0 commit comments

Comments
 (0)