Skip to content

Commit d1808fd

Browse files
lookymanondrejmirtes
authored andcommitted
Added tests for OneToOne relations
1 parent 3999dd2 commit d1808fd

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

tests/Rules/Doctrine/ORM/EntityRelationRuleTest.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\Rules\Doctrine\ORM;
44

5+
use Iterator;
56
use PHPStan\Rules\Rule;
67
use PHPStan\Testing\RuleTestCase;
78
use PHPStan\Type\Doctrine\ObjectMetadataResolver;
@@ -16,9 +17,35 @@ protected function getRule(): Rule
1617
);
1718
}
1819

19-
public function testRule(): void
20+
/**
21+
* @dataProvider ruleProvider
22+
*/
23+
public function testRule(string $file, array $expectedErrors): void
2024
{
21-
$this->analyse([__DIR__ . '/data/EntityWithRelations.php'], []);
25+
$this->analyse([$file], $expectedErrors);
26+
}
27+
28+
public function ruleProvider(): Iterator
29+
{
30+
yield [__DIR__ . '/data/EntityWithRelations.php', []];
31+
yield [__DIR__ . '/data/EntityWithBrokenOneToOneRelations.php', [
32+
[
33+
'Property can contain PHPStan\Rules\Doctrine\ORM\AnotherEntity|null but database expects PHPStan\Rules\Doctrine\ORM\AnotherEntity.',
34+
31,
35+
],
36+
[
37+
'Database can contain PHPStan\Rules\Doctrine\ORM\AnotherEntity|null but property expects PHPStan\Rules\Doctrine\ORM\AnotherEntity.',
38+
37,
39+
],
40+
[
41+
'Database can contain PHPStan\Rules\Doctrine\ORM\AnotherEntity|null but property expects PHPStan\Rules\Doctrine\ORM\MyEntity|null.',
42+
50,
43+
],
44+
[
45+
'Property can contain PHPStan\Rules\Doctrine\ORM\MyEntity|null but database expects PHPStan\Rules\Doctrine\ORM\AnotherEntity|null.',
46+
50,
47+
]
48+
]];
2249
}
2350

2451
}
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 EntityWithBrokenOneToOneRelations
11+
{
12+
13+
/**
14+
* @ORM\Id()
15+
* @ORM\Column(type="int")
16+
* @var int
17+
*/
18+
private $id;
19+
20+
/**
21+
* @ORM\OneToOne(targetEntity="PHPStan\Rules\Doctrine\ORM\AnotherEntity")
22+
* @var \PHPStan\Rules\Doctrine\ORM\AnotherEntity|null
23+
*/
24+
private $oneToOneNullableBoth;
25+
26+
/**
27+
* @ORM\OneToOne(targetEntity="PHPStan\Rules\Doctrine\ORM\AnotherEntity")
28+
* @ORM\JoinColumn(nullable=false)
29+
* @var \PHPStan\Rules\Doctrine\ORM\AnotherEntity|null
30+
*/
31+
private $oneToOneNullableProperty;
32+
33+
/**
34+
* @ORM\OneToOne(targetEntity="PHPStan\Rules\Doctrine\ORM\AnotherEntity")
35+
* @var \PHPStan\Rules\Doctrine\ORM\AnotherEntity
36+
*/
37+
private $oneToOneNullableColumn;
38+
39+
/**
40+
* @ORM\OneToOne(targetEntity="PHPStan\Rules\Doctrine\ORM\AnotherEntity")
41+
* @ORM\JoinColumn(nullable=false)
42+
* @var \PHPStan\Rules\Doctrine\ORM\AnotherEntity
43+
*/
44+
private $oneToOneNonNullable;
45+
46+
/**
47+
* @ORM\OneToOne(targetEntity="PHPStan\Rules\Doctrine\ORM\AnotherEntity")
48+
* @var \PHPStan\Rules\Doctrine\ORM\MyEntity|null
49+
*/
50+
private $oneToOneWrongClass;
51+
52+
}

0 commit comments

Comments
 (0)