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\Mapping\Annotation\Obj; |
27
|
|
|
use Addiks\RDMBundle\Tests\ValueObjectExample; |
28
|
|
|
use Addiks\RDMBundle\Mapping\ObjectMapping; |
29
|
|
|
use Doctrine\ORM\Mapping\Column as ORMColumn; |
30
|
|
|
use Addiks\RDMBundle\Mapping\FieldMapping; |
31
|
|
|
use Addiks\RDMBundle\Mapping\Annotation\Arr; |
32
|
|
|
use Addiks\RDMBundle\Mapping\ArrayMapping; |
33
|
|
|
|
34
|
|
|
final class MappingAnnotationDriverTest extends TestCase |
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var MappingAnnotationDriver |
39
|
|
|
*/ |
40
|
|
|
private $mappingDriver; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var Reader |
44
|
|
|
*/ |
45
|
|
|
private $annotationReader; |
46
|
|
|
|
47
|
|
|
public function setUp() |
48
|
|
|
{ |
49
|
|
|
$this->annotationReader = $this->createMock(Reader::class); |
|
|
|
|
50
|
|
|
|
51
|
|
|
$this->mappingDriver = new MappingAnnotationDriver( |
52
|
|
|
$this->annotationReader |
|
|
|
|
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @test |
58
|
|
|
*/ |
59
|
|
|
public function shouldReadAnnotations() |
60
|
|
|
{ |
61
|
|
|
$someAnnotationA = new Service(); |
62
|
|
|
$someAnnotationA->id = "some_service"; |
63
|
|
|
$someAnnotationA->field = "foo"; |
|
|
|
|
64
|
|
|
|
65
|
|
|
$someAnnotationB = new Service(); |
66
|
|
|
$someAnnotationB->id = "other_service"; |
67
|
|
|
$someAnnotationB->field = "bar"; |
68
|
|
|
$someAnnotationB->lax = true; |
69
|
|
|
|
70
|
|
|
$someAnnotationC = new Choice(); |
71
|
|
|
$someAnnotationC->column = "baz_column"; |
72
|
|
|
$someAnnotationC->nullable = true; |
73
|
|
|
$someAnnotationC->choices = [ |
|
|
|
|
74
|
|
|
'foo' => $someAnnotationA, |
75
|
|
|
'bar' => $someAnnotationB, |
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
$someAnnotationD = new Choice(); |
79
|
|
|
$someAnnotationD->column = "faz_column"; |
80
|
|
|
$someAnnotationD->nullable = false; |
81
|
|
|
$someAnnotationD->choices = [ |
82
|
|
|
'foo' => $someAnnotationA, |
83
|
|
|
'bar' => $someAnnotationB, |
84
|
|
|
]; |
85
|
|
|
|
86
|
|
|
$someAnnotationF = new ORMColumn(); |
87
|
|
|
$someAnnotationF->name = "someField"; |
88
|
|
|
$someAnnotationF->length = 12; |
89
|
|
|
|
90
|
|
|
$someAnnotationE = new Obj(); |
91
|
|
|
$someAnnotationE->{"class"} = ValueObjectExample::class; |
92
|
|
|
$someAnnotationE->fields = [ |
|
|
|
|
93
|
|
|
'foo' => $someAnnotationA, |
94
|
|
|
'bar' => $someAnnotationF, |
95
|
|
|
]; |
96
|
|
|
|
97
|
|
|
$someAnnotationG = new Arr(); |
98
|
|
|
$someAnnotationG->entries = [ |
99
|
|
|
'foo' => $someAnnotationA, |
100
|
|
|
'bar' => $someAnnotationF, |
101
|
|
|
]; |
102
|
|
|
|
103
|
|
|
/** @var string $entityClass */ |
104
|
|
|
$entityClass = EntityExample::class; |
105
|
|
|
|
106
|
|
|
/** @var array<MappingInterface> $expectedFieldMappings */ |
107
|
|
|
$expectedFieldMappings = [ |
108
|
|
|
'foo' => new ServiceMapping("some_service", false, "in entity '{$entityClass}' on field 'foo'"), |
109
|
|
|
'bar' => new ServiceMapping("other_service", true, "in entity '{$entityClass}' on field 'bar'"), |
110
|
|
|
'baz' => new ChoiceMapping('baz_column', [ |
111
|
|
|
'foo' => new ServiceMapping("some_service", false, "in entity '{$entityClass}' on field 'baz'"), |
112
|
|
|
'bar' => new ServiceMapping("other_service", true, "in entity '{$entityClass}' on field 'baz'"), |
113
|
|
|
], "in entity 'Addiks\RDMBundle\Tests\Hydration\EntityExample' on field 'baz'"), |
114
|
|
|
'faz' => new ChoiceMapping(new DBALColumn('faz_column', Type::getType('string'), [ |
115
|
|
|
'notnull' => true, |
116
|
|
|
'length' => 255, |
117
|
|
|
]), [ |
118
|
|
|
'foo' => new ServiceMapping("some_service", false, "in entity '{$entityClass}' on field 'faz'"), |
119
|
|
|
'bar' => new ServiceMapping("other_service", true, "in entity '{$entityClass}' on field 'faz'"), |
120
|
|
|
], "in entity 'Addiks\RDMBundle\Tests\Hydration\EntityExample' on field 'faz'"), |
121
|
|
|
'boo' => new ObjectMapping(ValueObjectExample::class, [ |
122
|
|
|
'foo' => new ServiceMapping("some_service", false, "in entity '{$entityClass}' on field 'boo->foo'"), |
123
|
|
|
'bar' => new FieldMapping(new DBALColumn('someField', Type::getType('string'), [ |
124
|
|
|
'notnull' => true, |
125
|
|
|
'precision' => 0, |
126
|
|
|
'length' => 12, |
127
|
|
|
]), "in entity '{$entityClass}' on field 'boo->bar'"), |
128
|
|
|
], "in entity 'Addiks\RDMBundle\Tests\Hydration\EntityExample' on field 'boo'"), |
129
|
|
|
'arr' => new ArrayMapping([ |
130
|
|
|
'foo' => new ServiceMapping("some_service", false, "in entity '{$entityClass}' on field 'arr->foo'"), |
131
|
|
|
'bar' => new FieldMapping(new DBALColumn('someField', Type::getType('string'), [ |
132
|
|
|
'notnull' => true, |
133
|
|
|
'precision' => 0, |
134
|
|
|
'length' => 12, |
135
|
|
|
]), "in entity '{$entityClass}' on field 'arr->bar'"), |
136
|
|
|
], "in entity 'Addiks\RDMBundle\Tests\Hydration\EntityExample' on field 'arr'") |
137
|
|
|
]; |
138
|
|
|
|
139
|
|
|
/** @var array<array<Service>> $annotationMap */ |
140
|
|
|
$annotationMap = [ |
141
|
|
|
'foo' => [$someAnnotationA], |
142
|
|
|
'bar' => [$someAnnotationB], |
143
|
|
|
'baz' => [$someAnnotationC], |
144
|
|
|
'faz' => [$someAnnotationD], |
145
|
|
|
'boo' => [$someAnnotationE], |
146
|
|
|
'arr' => [$someAnnotationG], |
147
|
|
|
]; |
148
|
|
|
|
149
|
|
|
$this->annotationReader->method('getPropertyAnnotations')->will($this->returnCallback( |
|
|
|
|
150
|
|
|
function (ReflectionProperty $propertyReflection) use ($annotationMap) { |
151
|
|
|
if (isset($annotationMap[$propertyReflection->getName()])) { |
152
|
|
|
return $annotationMap[$propertyReflection->getName()]; |
153
|
|
|
} else { |
154
|
|
|
return []; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
)); |
158
|
|
|
|
159
|
|
|
/** @var EntityMapping $actualMapping */ |
160
|
|
|
$actualMapping = $this->mappingDriver->loadRDMMetadataForClass(EntityExample::class); |
161
|
|
|
|
162
|
|
|
$this->assertEquals($expectedFieldMappings, $actualMapping->getFieldMappings()); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
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..