1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (C) 2018 Gerrit Addiks. |
4
|
|
|
* This package (including this file) was released under the terms of the GPL-3.0. |
5
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
6
|
|
|
* If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy. |
7
|
|
|
* @license GPL-3.0 |
8
|
|
|
* @author Gerrit Addiks <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Addiks\RDMBundle\Tests\Mapping\Drivers; |
12
|
|
|
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
use Addiks\RDMBundle\Mapping\Drivers\MappingAnnotationDriver; |
15
|
|
|
use Addiks\RDMBundle\Tests\Hydration\EntityExample; |
16
|
|
|
use Addiks\RDMBundle\Mapping\Annotation\Service; |
17
|
|
|
use ReflectionProperty; |
18
|
|
|
use Doctrine\Common\Annotations\Reader; |
19
|
|
|
use Addiks\RDMBundle\Mapping\ServiceMapping; |
20
|
|
|
use Addiks\RDMBundle\Mapping\EntityMapping; |
21
|
|
|
use Addiks\RDMBundle\Mapping\Annotation\Choice; |
22
|
|
|
use Addiks\RDMBundle\Mapping\ChoiceMapping; |
23
|
|
|
use Doctrine\DBAL\Schema\Column as DBALColumn; |
24
|
|
|
use Doctrine\DBAL\Types\Type; |
25
|
|
|
use Addiks\RDMBundle\Mapping\MappingInterface; |
26
|
|
|
use Addiks\RDMBundle\Tests\ValueObjectExample; |
27
|
|
|
use Addiks\RDMBundle\Mapping\ObjectMapping; |
28
|
|
|
use Doctrine\ORM\Mapping\Column as ORMColumn; |
29
|
|
|
use Addiks\RDMBundle\Mapping\FieldMapping; |
30
|
|
|
use Addiks\RDMBundle\Mapping\ArrayMapping; |
31
|
|
|
use Addiks\RDMBundle\Mapping\Annotation\RDMObject; |
32
|
|
|
use Addiks\RDMBundle\Mapping\Annotation\RDMArray; |
33
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
34
|
|
|
|
35
|
|
|
final class MappingAnnotationDriverTest extends TestCase |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var MappingAnnotationDriver |
40
|
|
|
*/ |
41
|
|
|
private $mappingDriver; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var ContainerInterface |
45
|
|
|
*/ |
46
|
|
|
private $container; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var Reader |
50
|
|
|
*/ |
51
|
|
|
private $annotationReader; |
52
|
|
|
|
53
|
|
|
public function setUp(): void |
54
|
|
|
{ |
55
|
|
|
$this->container = $this->createMock(ContainerInterface::class); |
|
|
|
|
56
|
|
|
$this->annotationReader = $this->createMock(Reader::class); |
|
|
|
|
57
|
|
|
|
58
|
|
|
$this->mappingDriver = new MappingAnnotationDriver( |
59
|
|
|
$this->container, |
60
|
|
|
$this->annotationReader |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @test |
66
|
|
|
*/ |
67
|
|
|
public function shouldReadAnnotations() |
68
|
|
|
{ |
69
|
|
|
$someAnnotationA = new Service(); |
70
|
|
|
$someAnnotationA->id = "some_service"; |
71
|
|
|
$someAnnotationA->field = "foo"; |
|
|
|
|
72
|
|
|
|
73
|
|
|
$someAnnotationB = new Service(); |
74
|
|
|
$someAnnotationB->id = "other_service"; |
75
|
|
|
$someAnnotationB->field = "bar"; |
76
|
|
|
$someAnnotationB->lax = true; |
77
|
|
|
|
78
|
|
|
$someAnnotationC = new Choice(); |
79
|
|
|
$someAnnotationC->column = "baz_column"; |
80
|
|
|
$someAnnotationC->nullable = true; |
81
|
|
|
$someAnnotationC->choices = [ |
82
|
|
|
'foo' => $someAnnotationA, |
83
|
|
|
'bar' => $someAnnotationB, |
84
|
|
|
]; |
85
|
|
|
|
86
|
|
|
$someAnnotationD = new Choice(); |
87
|
|
|
$someAnnotationD->column = "faz_column"; |
88
|
|
|
$someAnnotationD->nullable = false; |
89
|
|
|
$someAnnotationD->choices = [ |
90
|
|
|
'foo' => $someAnnotationA, |
91
|
|
|
'bar' => $someAnnotationB, |
92
|
|
|
]; |
93
|
|
|
|
94
|
|
|
$someAnnotationF = new ORMColumn(); |
95
|
|
|
$someAnnotationF->name = "someField"; |
96
|
|
|
$someAnnotationF->length = 12; |
97
|
|
|
|
98
|
|
|
$someAnnotationE = new RDMObject(); |
99
|
|
|
$someAnnotationE->{"class"} = ValueObjectExample::class; |
100
|
|
|
$someAnnotationE->fields = [ |
101
|
|
|
'foo' => $someAnnotationA, |
102
|
|
|
'bar' => $someAnnotationF, |
103
|
|
|
]; |
104
|
|
|
|
105
|
|
|
$someAnnotationG = new RDMArray(); |
106
|
|
|
$someAnnotationG->entries = [ |
107
|
|
|
'foo' => $someAnnotationA, |
108
|
|
|
'bar' => $someAnnotationF, |
109
|
|
|
]; |
110
|
|
|
|
111
|
|
|
/** @var string $entityClass */ |
112
|
|
|
$entityClass = EntityExample::class; |
113
|
|
|
|
114
|
|
|
/** @var array<MappingInterface> $expectedFieldMappings */ |
115
|
|
|
$expectedFieldMappings = [ |
116
|
|
|
'foo' => new ServiceMapping( |
117
|
|
|
$this->container, |
118
|
|
|
"some_service", |
119
|
|
|
false, |
120
|
|
|
"in entity '{$entityClass}' on field 'foo'" |
121
|
|
|
), |
122
|
|
|
'bar' => new ServiceMapping( |
123
|
|
|
$this->container, |
124
|
|
|
"other_service", |
125
|
|
|
true, |
126
|
|
|
"in entity '{$entityClass}' on field 'bar'" |
127
|
|
|
), |
128
|
|
|
'baz' => new ChoiceMapping('baz_column', [ |
129
|
|
|
'foo' => new ServiceMapping( |
130
|
|
|
$this->container, |
131
|
|
|
"some_service", |
132
|
|
|
false, |
133
|
|
|
"in entity '{$entityClass}' on field 'baz'" |
134
|
|
|
), |
135
|
|
|
'bar' => new ServiceMapping( |
136
|
|
|
$this->container, |
137
|
|
|
"other_service", |
138
|
|
|
true, |
139
|
|
|
"in entity '{$entityClass}' on field 'baz'" |
140
|
|
|
), |
141
|
|
|
], "in entity 'Addiks\RDMBundle\Tests\Hydration\EntityExample' on field 'baz'"), |
142
|
|
|
'faz' => new ChoiceMapping(new DBALColumn('faz_column', Type::getType('string'), [ |
143
|
|
|
'notnull' => true, |
144
|
|
|
'length' => 255, |
145
|
|
|
]), [ |
146
|
|
|
'foo' => new ServiceMapping( |
147
|
|
|
$this->container, |
148
|
|
|
"some_service", |
149
|
|
|
false, |
150
|
|
|
"in entity '{$entityClass}' on field 'faz'" |
151
|
|
|
), |
152
|
|
|
'bar' => new ServiceMapping( |
153
|
|
|
$this->container, |
154
|
|
|
"other_service", |
155
|
|
|
true, |
156
|
|
|
"in entity '{$entityClass}' on field 'faz'" |
157
|
|
|
), |
158
|
|
|
], "in entity 'Addiks\RDMBundle\Tests\Hydration\EntityExample' on field 'faz'"), |
159
|
|
|
'boo' => new ObjectMapping(ValueObjectExample::class, [ |
160
|
|
|
'foo' => new ServiceMapping( |
161
|
|
|
$this->container, |
162
|
|
|
"some_service", |
163
|
|
|
false, |
164
|
|
|
"in entity '{$entityClass}' on field 'boo->foo'" |
165
|
|
|
), |
166
|
|
|
'bar' => new FieldMapping(new DBALColumn('someField', Type::getType('string'), [ |
167
|
|
|
'notnull' => true, |
168
|
|
|
'precision' => 0, |
169
|
|
|
'length' => 12, |
170
|
|
|
]), "in entity '{$entityClass}' on field 'boo->bar'"), |
171
|
|
|
], null, "in entity 'Addiks\RDMBundle\Tests\Hydration\EntityExample' on field 'boo'"), |
172
|
|
|
'arr' => new ArrayMapping([ |
173
|
|
|
'foo' => new ServiceMapping( |
174
|
|
|
$this->container, |
175
|
|
|
"some_service", |
176
|
|
|
false, |
177
|
|
|
"in entity '{$entityClass}' on field 'arr->foo'" |
178
|
|
|
), |
179
|
|
|
'bar' => new FieldMapping(new DBALColumn('someField', Type::getType('string'), [ |
180
|
|
|
'notnull' => true, |
181
|
|
|
'precision' => 0, |
182
|
|
|
'length' => 12, |
183
|
|
|
]), "in entity '{$entityClass}' on field 'arr->bar'"), |
184
|
|
|
], "in entity 'Addiks\RDMBundle\Tests\Hydration\EntityExample' on field 'arr'") |
185
|
|
|
]; |
186
|
|
|
|
187
|
|
|
/** @var array<array<Service>> $annotationMap */ |
188
|
|
|
$annotationMap = [ |
189
|
|
|
'foo' => [$someAnnotationA], |
190
|
|
|
'bar' => [$someAnnotationB], |
191
|
|
|
'baz' => [$someAnnotationC], |
192
|
|
|
'faz' => [$someAnnotationD], |
193
|
|
|
'boo' => [$someAnnotationE], |
194
|
|
|
'arr' => [$someAnnotationG], |
195
|
|
|
]; |
196
|
|
|
|
197
|
|
|
$this->annotationReader->method('getPropertyAnnotations')->will($this->returnCallback( |
|
|
|
|
198
|
|
|
function (ReflectionProperty $propertyReflection) use ($annotationMap) { |
199
|
|
|
if (isset($annotationMap[$propertyReflection->getName()])) { |
200
|
|
|
return $annotationMap[$propertyReflection->getName()]; |
201
|
|
|
} else { |
202
|
|
|
return []; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
)); |
206
|
|
|
|
207
|
|
|
/** @var EntityMapping $actualMapping */ |
208
|
|
|
$actualMapping = $this->mappingDriver->loadRDMMetadataForClass(EntityExample::class); |
209
|
|
|
|
210
|
|
|
$this->assertEquals($expectedFieldMappings, $actualMapping->getFieldMappings()); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
} |
214
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..