-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathEntityColumnRuleTest.php
181 lines (167 loc) · 5.72 KB
/
EntityColumnRuleTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Doctrine\ORM;
use Doctrine\DBAL\Types\Type;
use Iterator;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPStan\Type\Doctrine\DescriptorRegistry;
use PHPStan\Type\Doctrine\Descriptors\ArrayType;
use PHPStan\Type\Doctrine\Descriptors\BigIntType;
use PHPStan\Type\Doctrine\Descriptors\BinaryType;
use PHPStan\Type\Doctrine\Descriptors\DateTimeImmutableType;
use PHPStan\Type\Doctrine\Descriptors\DateTimeType;
use PHPStan\Type\Doctrine\Descriptors\DateType;
use PHPStan\Type\Doctrine\Descriptors\IntegerType;
use PHPStan\Type\Doctrine\Descriptors\Ramsey\UuidTypeDescriptor;
use PHPStan\Type\Doctrine\Descriptors\ReflectionDescriptor;
use PHPStan\Type\Doctrine\Descriptors\StringType;
use PHPStan\Type\Doctrine\ObjectMetadataResolver;
use Ramsey\Uuid\Doctrine\UuidType;
/**
* @extends RuleTestCase<EntityColumnRule>
*/
class EntityColumnRuleTest extends RuleTestCase
{
protected function getRule(): Rule
{
if (!Type::hasType(CustomType::NAME)) {
Type::addType(CustomType::NAME, CustomType::class);
}
if (!Type::hasType(UuidType::NAME)) {
Type::addType(UuidType::NAME, UuidType::class);
}
return new EntityColumnRule(
new ObjectMetadataResolver(__DIR__ . '/entity-manager.php', null),
new DescriptorRegistry([
new BigIntType(),
new StringType(),
new DateTimeType(),
new DateTimeImmutableType(),
new BinaryType(),
new IntegerType(),
new ReflectionDescriptor(CustomType::class, $this->createBroker()),
new DateType(),
new UuidTypeDescriptor(UuidType::class),
new ArrayType(),
]),
true
);
}
public function testRule(): void
{
$this->analyse([__DIR__ . '/data/MyBrokenEntity.php'], [
[
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$id type mapping mismatch: database can contain string but property expects int|null.',
19,
],
[
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$one type mapping mismatch: database can contain string|null but property expects string.',
25,
],
[
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$two type mapping mismatch: property can contain string|null but database expects string.',
31,
],
[
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$three type mapping mismatch: database can contain DateTime but property expects DateTimeImmutable.',
37,
],
[
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$four type mapping mismatch: database can contain DateTimeImmutable but property expects DateTime.',
43,
],
[
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$four type mapping mismatch: property can contain DateTime but database expects DateTimeImmutable.',
43,
],
[
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$uuidInvalidType type mapping mismatch: database can contain Ramsey\Uuid\UuidInterface but property expects int.',
72,
],
[
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$uuidInvalidType type mapping mismatch: property can contain int but database expects Ramsey\Uuid\UuidInterface|string.',
72,
],
[
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$arrayOrNull type mapping mismatch: property can contain array|null but database expects array.',
96,
],
[
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$arrayOfIntegersOrNull type mapping mismatch: property can contain array|null but database expects array.',
102,
],
]);
}
public function testSuperclass(): void
{
$this->analyse([__DIR__ . '/data/MyBrokenSuperclass.php'], [
[
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenSuperclass::$five type mapping mismatch: database can contain resource but property expects int.',
17,
],
]);
}
/**
* @dataProvider generatedIdsProvider
* @param string $file
* @param mixed[] $expectedErrors
*/
public function testGeneratedIds(string $file, array $expectedErrors): void
{
$this->analyse([$file], $expectedErrors);
}
/**
* @return \Iterator<string, mixed[]>
*/
public function generatedIdsProvider(): Iterator
{
yield 'not nullable' => [__DIR__ . '/data/GeneratedIdEntity1.php', []];
yield 'nullable column' => [
__DIR__ . '/data/GeneratedIdEntity2.php',
[
[
'Property PHPStan\Rules\Doctrine\ORM\GeneratedIdEntity2::$id type mapping mismatch: database can contain string|null but property expects string.',
19,
],
],
];
yield 'nullable property' => [__DIR__ . '/data/GeneratedIdEntity3.php', []];
yield 'nullable both' => [__DIR__ . '/data/GeneratedIdEntity4.php', []];
yield 'composite' => [__DIR__ . '/data/CompositePrimaryKeyEntity1.php', []];
}
public function testCustomType(): void
{
$this->analyse([__DIR__ . '/data/EntityWithCustomType.php'], [
[
'Property PHPStan\Rules\Doctrine\ORM\EntityWithCustomType::$foo type mapping mismatch: database can contain DateTimeInterface but property expects int.',
24,
],
[
'Property PHPStan\Rules\Doctrine\ORM\EntityWithCustomType::$foo type mapping mismatch: property can contain int but database expects array.',
24,
],
]);
}
public function testEmbeddable(): void
{
$this->analyse([__DIR__ . '/data/Embeddable.php'], []);
}
public function testBrokenEmbeddable(): void
{
$this->analyse([__DIR__ . '/data/BrokenEmbeddable.php'], [
[
'Property PHPStan\Rules\Doctrine\ORM\BrokenEmbeddable::$one type mapping mismatch: database can contain string|null but property expects string.',
16,
],
]);
}
public function testUnknownType(): void
{
$this->analyse([__DIR__ . '/data/EntityWithUnknownType.php'], [
[
'Property PHPStan\Rules\Doctrine\ORM\EntityWithUnknownType::$foo: Doctrine type "unknown" does not have any registered descriptor.',
24,
],
]);
}
}