1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Tests\ORM\Mapping; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Types\Type; |
8
|
|
|
use Doctrine\ORM\EntityRepository; |
9
|
|
|
use Doctrine\ORM\Events; |
10
|
|
|
use Doctrine\ORM\Mapping; |
11
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
12
|
|
|
use Doctrine\ORM\Mapping\DiscriminatorColumnMetadata; |
13
|
|
|
use Doctrine\ORM\Mapping\Factory\DefaultNamingStrategy; |
14
|
|
|
use Doctrine\ORM\Mapping\Factory\UnderscoreNamingStrategy; |
15
|
|
|
use Doctrine\ORM\Mapping\JoinColumnMetadata; |
16
|
|
|
use Doctrine\ORM\Mapping\MappingException; |
17
|
|
|
use Doctrine\ORM\Reflection\RuntimeReflectionService; |
18
|
|
|
use Doctrine\ORM\Reflection\StaticReflectionService; |
19
|
|
|
use Doctrine\Tests\Models\CMS; |
20
|
|
|
use Doctrine\Tests\Models\Company\CompanyContract; |
21
|
|
|
use Doctrine\Tests\Models\Company\CompanyContractListener; |
22
|
|
|
use Doctrine\Tests\Models\CustomType\CustomTypeParent; |
23
|
|
|
use Doctrine\Tests\Models\DDC117\DDC117Article; |
24
|
|
|
use Doctrine\Tests\Models\DDC117\DDC117ArticleDetails; |
25
|
|
|
use Doctrine\Tests\Models\DDC6412\DDC6412File; |
26
|
|
|
use Doctrine\Tests\Models\DDC964\DDC964Address; |
27
|
|
|
use Doctrine\Tests\Models\DDC964\DDC964Admin; |
28
|
|
|
use Doctrine\Tests\Models\DDC964\DDC964Guest; |
29
|
|
|
use Doctrine\Tests\Models\Routing\RoutingLeg; |
30
|
|
|
use Doctrine\Tests\OrmTestCase; |
31
|
|
|
use DoctrineGlobal_Article; |
32
|
|
|
|
33
|
|
|
require_once __DIR__ . '/../../Models/Global/GlobalNamespaceModel.php'; |
34
|
|
|
|
35
|
|
|
class ClassMetadataTest extends OrmTestCase |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @var Mapping\ClassMetadataBuildingContext|\PHPUnit_Framework_MockObject_MockObject |
39
|
|
|
*/ |
40
|
|
|
private $metadataBuildingContext; |
41
|
|
|
|
42
|
|
|
public function setUp() |
43
|
|
|
{ |
44
|
|
|
parent::setUp(); |
45
|
|
|
|
46
|
|
|
$this->metadataBuildingContext = new Mapping\ClassMetadataBuildingContext( |
47
|
|
|
$this->createMock(Mapping\ClassMetadataFactory::class), |
48
|
|
|
new RuntimeReflectionService() |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testClassMetadataInstanceSimpleState() |
53
|
|
|
{ |
54
|
|
|
$cm = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
55
|
|
|
$cm->setTable(new Mapping\TableMetadata('cms_users')); |
56
|
|
|
|
57
|
|
|
self::assertInstanceOf(\ReflectionClass::class, $cm->getReflectionClass()); |
58
|
|
|
self::assertEquals(CMS\CmsUser::class, $cm->getClassName()); |
59
|
|
|
self::assertEquals(CMS\CmsUser::class, $cm->getRootClassName()); |
60
|
|
|
self::assertEquals([], $cm->getSubClasses()); |
61
|
|
|
self::assertCount(0, $cm->getAncestorsIterator()); |
62
|
|
|
self::assertEquals(Mapping\InheritanceType::NONE, $cm->inheritanceType); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testClassMetadataInstanceSerialization() |
66
|
|
|
{ |
67
|
|
|
$parent = new ClassMetadata(CMS\CmsEmployee::class, $this->metadataBuildingContext); |
68
|
|
|
$parent->setTable(new Mapping\TableMetadata('cms_employee')); |
69
|
|
|
|
70
|
|
|
$cm = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
71
|
|
|
$cm->setTable($parent->table); |
72
|
|
|
$cm->setParent($parent); |
73
|
|
|
|
74
|
|
|
$discrColumn = new DiscriminatorColumnMetadata(); |
75
|
|
|
|
76
|
|
|
$discrColumn->setColumnName('disc'); |
77
|
|
|
$discrColumn->setType(Type::getType('integer')); |
78
|
|
|
|
79
|
|
|
$cm->setInheritanceType(Mapping\InheritanceType::SINGLE_TABLE); |
|
|
|
|
80
|
|
|
$cm->setSubclasses([ |
81
|
|
|
'Doctrine\Tests\Models\CMS\One', |
82
|
|
|
'Doctrine\Tests\Models\CMS\Two', |
83
|
|
|
'Doctrine\Tests\Models\CMS\Three' |
84
|
|
|
]); |
85
|
|
|
$cm->setCustomRepositoryClassName('Doctrine\Tests\Models\CMS\UserRepository'); |
86
|
|
|
$cm->setDiscriminatorColumn($discrColumn); |
87
|
|
|
$cm->asReadOnly(); |
88
|
|
|
|
89
|
|
|
$association = new Mapping\OneToOneAssociationMetadata('phonenumbers'); |
90
|
|
|
|
91
|
|
|
$association->setTargetEntity(CMS\CmsAddress::class); |
92
|
|
|
$association->setMappedBy('foo'); |
93
|
|
|
|
94
|
|
|
$cm->addProperty($association); |
95
|
|
|
|
96
|
|
|
self::assertCount(1, $cm->getDeclaredPropertiesIterator()); |
|
|
|
|
97
|
|
|
|
98
|
|
|
$serialized = serialize($cm); |
99
|
|
|
$cm = unserialize($serialized); |
100
|
|
|
|
101
|
|
|
$cm->wakeupReflection(new RuntimeReflectionService()); |
102
|
|
|
|
103
|
|
|
// Check state |
104
|
|
|
self::assertInstanceOf(\ReflectionClass::class, $cm->getReflectionClass()); |
105
|
|
|
self::assertEquals(CMS\CmsUser::class, $cm->getClassName()); |
106
|
|
|
self::assertEquals(CMS\CmsEmployee::class, $cm->getRootClassName()); |
107
|
|
|
self::assertEquals('Doctrine\Tests\Models\CMS\UserRepository', $cm->getCustomRepositoryClassName()); |
108
|
|
|
self::assertEquals( |
109
|
|
|
[ |
110
|
|
|
'Doctrine\Tests\Models\CMS\One', |
111
|
|
|
'Doctrine\Tests\Models\CMS\Two', |
112
|
|
|
'Doctrine\Tests\Models\CMS\Three' |
113
|
|
|
], |
114
|
|
|
$cm->getSubClasses() |
115
|
|
|
); |
116
|
|
|
self::assertCount(1, $cm->getAncestorsIterator()); |
117
|
|
|
self::assertEquals(CMS\CmsEmployee::class, $cm->getAncestorsIterator()->current()->getClassName()); |
118
|
|
|
self::assertEquals($discrColumn, $cm->discriminatorColumn); |
119
|
|
|
self::assertTrue($cm->isReadOnly()); |
120
|
|
|
self::assertCount(1, $cm->getDeclaredPropertiesIterator()); |
121
|
|
|
self::assertInstanceOf(Mapping\OneToOneAssociationMetadata::class, $cm->getProperty('phonenumbers')); |
122
|
|
|
|
123
|
|
|
$oneOneMapping = $cm->getProperty('phonenumbers'); |
124
|
|
|
|
125
|
|
|
self::assertEquals(Mapping\FetchMode::LAZY, $oneOneMapping->getFetchMode()); |
126
|
|
|
self::assertEquals(CMS\CmsAddress::class, $oneOneMapping->getTargetEntity()); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function testFieldIsNullable() |
130
|
|
|
{ |
131
|
|
|
$metadata = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
132
|
|
|
$metadata->setTable(new Mapping\TableMetadata('cms_users')); |
133
|
|
|
|
134
|
|
|
// Explicit Nullable |
135
|
|
|
$fieldMetadata = new Mapping\FieldMetadata('status'); |
136
|
|
|
|
137
|
|
|
$fieldMetadata->setType(Type::getType('string')); |
138
|
|
|
$fieldMetadata->setLength(50); |
139
|
|
|
$fieldMetadata->setNullable(true); |
140
|
|
|
|
141
|
|
|
$metadata->addProperty($fieldMetadata); |
142
|
|
|
|
143
|
|
|
$property = $metadata->getProperty('status'); |
144
|
|
|
|
145
|
|
|
self::assertTrue($property->isNullable()); |
|
|
|
|
146
|
|
|
|
147
|
|
|
// Explicit Not Nullable |
148
|
|
|
$fieldMetadata = new Mapping\FieldMetadata('username'); |
149
|
|
|
|
150
|
|
|
$fieldMetadata->setType(Type::getType('string')); |
151
|
|
|
$fieldMetadata->setLength(50); |
152
|
|
|
$fieldMetadata->setNullable(false); |
153
|
|
|
|
154
|
|
|
$metadata->addProperty($fieldMetadata); |
155
|
|
|
|
156
|
|
|
$property = $metadata->getProperty('username'); |
157
|
|
|
|
158
|
|
|
self::assertFalse($property->isNullable()); |
159
|
|
|
|
160
|
|
|
// Implicit Not Nullable |
161
|
|
|
$fieldMetadata = new Mapping\FieldMetadata('name'); |
162
|
|
|
|
163
|
|
|
$fieldMetadata->setType(Type::getType('string')); |
164
|
|
|
$fieldMetadata->setLength(50); |
165
|
|
|
|
166
|
|
|
$metadata->addProperty($fieldMetadata); |
167
|
|
|
|
168
|
|
|
$property = $metadata->getProperty('name'); |
169
|
|
|
|
170
|
|
|
self::assertFalse($property->isNullable(), "By default a field should not be nullable."); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @group DDC-115 |
175
|
|
|
*/ |
176
|
|
|
public function testMapAssociationInGlobalNamespace() |
177
|
|
|
{ |
178
|
|
|
require_once __DIR__."/../../Models/Global/GlobalNamespaceModel.php"; |
179
|
|
|
|
180
|
|
|
$cm = new ClassMetadata(DoctrineGlobal_Article::class, $this->metadataBuildingContext); |
181
|
|
|
$cm->setTable(new Mapping\TableMetadata('doctrine_global_article')); |
182
|
|
|
|
183
|
|
|
$joinTable = new Mapping\JoinTableMetadata(); |
184
|
|
|
$joinTable->setName('bar'); |
185
|
|
|
|
186
|
|
|
$joinColumn = new Mapping\JoinColumnMetadata(); |
187
|
|
|
$joinColumn->setColumnName("bar_id"); |
188
|
|
|
$joinColumn->setReferencedColumnName("id"); |
189
|
|
|
|
190
|
|
|
$joinTable->addJoinColumn($joinColumn); |
191
|
|
|
|
192
|
|
|
$joinColumn = new Mapping\JoinColumnMetadata(); |
193
|
|
|
$joinColumn->setColumnName("baz_id"); |
194
|
|
|
$joinColumn->setReferencedColumnName("id"); |
195
|
|
|
|
196
|
|
|
$joinTable->addInverseJoinColumn($joinColumn); |
197
|
|
|
|
198
|
|
|
$association = new Mapping\ManyToManyAssociationMetadata('author'); |
199
|
|
|
|
200
|
|
|
$association->setJoinTable($joinTable); |
201
|
|
|
$association->setTargetEntity('DoctrineGlobal_User'); |
202
|
|
|
|
203
|
|
|
$cm->addProperty($association); |
204
|
|
|
|
205
|
|
|
self::assertEquals("DoctrineGlobal_User", $cm->getProperty('author')->getTargetEntity()); |
|
|
|
|
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function testMapManyToManyJoinTableDefaults() |
209
|
|
|
{ |
210
|
|
|
$cm = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
211
|
|
|
$cm->setTable(new Mapping\TableMetadata('cms_users')); |
212
|
|
|
|
213
|
|
|
$association = new Mapping\ManyToManyAssociationMetadata('groups'); |
214
|
|
|
|
215
|
|
|
$association->setTargetEntity(CMS\CmsGroup::class); |
216
|
|
|
|
217
|
|
|
$cm->addProperty($association); |
218
|
|
|
|
219
|
|
|
$association = $cm->getProperty('groups'); |
220
|
|
|
|
221
|
|
|
$joinColumns = []; |
222
|
|
|
|
223
|
|
|
$joinColumn = new Mapping\JoinColumnMetadata(); |
224
|
|
|
|
225
|
|
|
$joinColumn->setColumnName("cmsuser_id"); |
226
|
|
|
$joinColumn->setReferencedColumnName("id"); |
227
|
|
|
$joinColumn->setOnDelete("CASCADE"); |
228
|
|
|
|
229
|
|
|
$joinColumns[] = $joinColumn; |
230
|
|
|
|
231
|
|
|
$inverseJoinColumns = []; |
232
|
|
|
|
233
|
|
|
$joinColumn = new Mapping\JoinColumnMetadata(); |
234
|
|
|
|
235
|
|
|
$joinColumn->setColumnName("cmsgroup_id"); |
236
|
|
|
$joinColumn->setReferencedColumnName("id"); |
237
|
|
|
$joinColumn->setOnDelete("CASCADE"); |
238
|
|
|
|
239
|
|
|
$inverseJoinColumns[] = $joinColumn; |
240
|
|
|
|
241
|
|
|
$joinTable = $association->getJoinTable(); |
|
|
|
|
242
|
|
|
|
243
|
|
|
self::assertEquals('cmsuser_cmsgroup', $joinTable->getName()); |
244
|
|
|
self::assertEquals($joinColumns, $joinTable->getJoinColumns()); |
245
|
|
|
self::assertEquals($inverseJoinColumns, $joinTable->getInverseJoinColumns()); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
public function testSerializeManyToManyJoinTableCascade() |
249
|
|
|
{ |
250
|
|
|
$cm = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
251
|
|
|
$cm->setTable(new Mapping\TableMetadata('cms_users')); |
252
|
|
|
|
253
|
|
|
$association = new Mapping\ManyToManyAssociationMetadata('groups'); |
254
|
|
|
|
255
|
|
|
$association->setTargetEntity(CMS\CmsGroup::class); |
256
|
|
|
|
257
|
|
|
$cm->addProperty($association); |
258
|
|
|
|
259
|
|
|
$association = $cm->getProperty('groups'); |
260
|
|
|
$association = unserialize(serialize($association)); |
261
|
|
|
|
262
|
|
|
$joinTable = $association->getJoinTable(); |
263
|
|
|
|
264
|
|
|
foreach ($joinTable->getJoinColumns() as $joinColumn) { |
265
|
|
|
self::assertEquals('CASCADE', $joinColumn->getOnDelete()); |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @group DDC-115 |
271
|
|
|
*/ |
272
|
|
|
public function testSetDiscriminatorMapInGlobalNamespace() |
273
|
|
|
{ |
274
|
|
|
require_once __DIR__."/../../Models/Global/GlobalNamespaceModel.php"; |
275
|
|
|
|
276
|
|
|
$cm = new ClassMetadata('DoctrineGlobal_User', $this->metadataBuildingContext); |
277
|
|
|
$cm->setTable(new Mapping\TableMetadata('doctrine_global_user')); |
278
|
|
|
|
279
|
|
|
$cm->setDiscriminatorMap(['descr' => 'DoctrineGlobal_Article', 'foo' => 'DoctrineGlobal_User']); |
280
|
|
|
|
281
|
|
|
self::assertEquals("DoctrineGlobal_Article", $cm->discriminatorMap['descr']); |
282
|
|
|
self::assertEquals("DoctrineGlobal_User", $cm->discriminatorMap['foo']); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @group DDC-115 |
287
|
|
|
*/ |
288
|
|
|
public function testSetSubClassesInGlobalNamespace() |
289
|
|
|
{ |
290
|
|
|
require_once __DIR__."/../../Models/Global/GlobalNamespaceModel.php"; |
291
|
|
|
|
292
|
|
|
$cm = new ClassMetadata('DoctrineGlobal_User', $this->metadataBuildingContext); |
293
|
|
|
$cm->setTable(new Mapping\TableMetadata('doctrine_global_user')); |
294
|
|
|
|
295
|
|
|
$cm->setSubclasses(['DoctrineGlobal_Article']); |
296
|
|
|
|
297
|
|
|
self::assertEquals("DoctrineGlobal_Article", $cm->getSubClasses()[0]); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* @group DDC-268 |
302
|
|
|
*/ |
303
|
|
|
public function testSetInvalidVersionMapping_ThrowsException() |
304
|
|
|
{ |
305
|
|
|
$cm = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
306
|
|
|
$cm->setTable(new Mapping\TableMetadata('cms_users')); |
307
|
|
|
|
308
|
|
|
$property = new Mapping\VersionFieldMetadata('foo'); |
309
|
|
|
|
310
|
|
|
$property->setDeclaringClass($cm); |
311
|
|
|
$property->setColumnName('foo'); |
312
|
|
|
$property->setType(Type::getType('string')); |
313
|
|
|
|
314
|
|
|
$this->expectException(MappingException::class); |
315
|
|
|
|
316
|
|
|
$cm->addProperty($property); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
public function testGetSingleIdentifierFieldName_MultipleIdentifierEntity_ThrowsException() |
320
|
|
|
{ |
321
|
|
|
$cm = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
322
|
|
|
$cm->setTable(new Mapping\TableMetadata('cms_users')); |
323
|
|
|
|
324
|
|
|
$fieldMetadata = new Mapping\FieldMetadata('name'); |
325
|
|
|
$fieldMetadata->setType(Type::getType('string')); |
326
|
|
|
|
327
|
|
|
$cm->addProperty($fieldMetadata); |
328
|
|
|
|
329
|
|
|
$fieldMetadata = new Mapping\FieldMetadata('username'); |
330
|
|
|
$fieldMetadata->setType(Type::getType('string')); |
331
|
|
|
|
332
|
|
|
$cm->addProperty($fieldMetadata); |
333
|
|
|
|
334
|
|
|
$cm->setIdentifier(['name', 'username']); |
335
|
|
|
|
336
|
|
|
$this->expectException(MappingException::class); |
337
|
|
|
|
338
|
|
|
$cm->getSingleIdentifierFieldName(); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
public function testGetSingleIdentifierFieldName_NoIdEntity_ThrowsException() |
342
|
|
|
{ |
343
|
|
|
$cm = new ClassMetadata(DDC6412File::class, $this->metadataBuildingContext); |
344
|
|
|
$cm->setTable(new Mapping\TableMetadata('ddc6412_file')); |
345
|
|
|
|
346
|
|
|
$this->expectException(\Doctrine\ORM\Mapping\MappingException::class); |
347
|
|
|
|
348
|
|
|
$cm->getSingleIdentifierFieldName(); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
public function testDuplicateAssociationMappingException() |
352
|
|
|
{ |
353
|
|
|
$cm = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
354
|
|
|
$cm->setTable(new Mapping\TableMetadata('cms_users')); |
355
|
|
|
|
356
|
|
|
$association = new Mapping\OneToOneAssociationMetadata('foo'); |
357
|
|
|
|
358
|
|
|
$association->setDeclaringClass($cm); |
359
|
|
|
$association->setSourceEntity(\stdClass::class); |
360
|
|
|
$association->setTargetEntity(\stdClass::class); |
361
|
|
|
$association->setMappedBy('foo'); |
362
|
|
|
|
363
|
|
|
$cm->addInheritedProperty($association); |
364
|
|
|
|
365
|
|
|
$this->expectException(MappingException::class); |
366
|
|
|
|
367
|
|
|
$association = new Mapping\OneToOneAssociationMetadata('foo'); |
368
|
|
|
|
369
|
|
|
$association->setDeclaringClass($cm); |
370
|
|
|
$association->setSourceEntity(\stdClass::class); |
371
|
|
|
$association->setTargetEntity(\stdClass::class); |
372
|
|
|
$association->setMappedBy('foo'); |
373
|
|
|
|
374
|
|
|
$cm->addInheritedProperty($association); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
public function testDuplicateColumnName_ThrowsMappingException() |
378
|
|
|
{ |
379
|
|
|
$cm = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
380
|
|
|
$cm->setTable(new Mapping\TableMetadata('cms_users')); |
381
|
|
|
|
382
|
|
|
$fieldMetadata = new Mapping\FieldMetadata('name'); |
383
|
|
|
|
384
|
|
|
$fieldMetadata->setType(Type::getType('string')); |
385
|
|
|
|
386
|
|
|
$cm->addProperty($fieldMetadata); |
387
|
|
|
|
388
|
|
|
$this->expectException(MappingException::class); |
389
|
|
|
|
390
|
|
|
$fieldMetadata = new Mapping\FieldMetadata('username'); |
391
|
|
|
|
392
|
|
|
$fieldMetadata->setType(Type::getType('string')); |
393
|
|
|
$fieldMetadata->setColumnName('name'); |
394
|
|
|
|
395
|
|
|
$cm->addProperty($fieldMetadata); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
public function testDuplicateColumnName_DiscriminatorColumn_ThrowsMappingException() |
399
|
|
|
{ |
400
|
|
|
$cm = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
401
|
|
|
$cm->setTable(new Mapping\TableMetadata('cms_users')); |
402
|
|
|
|
403
|
|
|
$fieldMetadata = new Mapping\FieldMetadata('name'); |
404
|
|
|
|
405
|
|
|
$fieldMetadata->setType(Type::getType('string')); |
406
|
|
|
|
407
|
|
|
$cm->addProperty($fieldMetadata); |
408
|
|
|
|
409
|
|
|
$discrColumn = new DiscriminatorColumnMetadata(); |
410
|
|
|
|
411
|
|
|
$discrColumn->setColumnName('name'); |
412
|
|
|
$discrColumn->setType(Type::getType('string')); |
413
|
|
|
$discrColumn->setLength(255); |
414
|
|
|
|
415
|
|
|
$this->expectException(\Doctrine\ORM\Mapping\MappingException::class); |
416
|
|
|
|
417
|
|
|
$cm->setDiscriminatorColumn($discrColumn); |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
public function testDuplicateColumnName_DiscriminatorColumn2_ThrowsMappingException() |
421
|
|
|
{ |
422
|
|
|
$cm = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
423
|
|
|
$cm->setTable(new Mapping\TableMetadata('cms_users')); |
424
|
|
|
|
425
|
|
|
$discrColumn = new DiscriminatorColumnMetadata(); |
426
|
|
|
|
427
|
|
|
$discrColumn->setColumnName('name'); |
428
|
|
|
$discrColumn->setType(Type::getType('string')); |
429
|
|
|
$discrColumn->setLength(255); |
430
|
|
|
|
431
|
|
|
$cm->setDiscriminatorColumn($discrColumn); |
432
|
|
|
|
433
|
|
|
$this->expectException(\Doctrine\ORM\Mapping\MappingException::class); |
434
|
|
|
|
435
|
|
|
$fieldMetadata = new Mapping\FieldMetadata('name'); |
436
|
|
|
|
437
|
|
|
$fieldMetadata->setType(Type::getType('string')); |
438
|
|
|
|
439
|
|
|
$cm->addProperty($fieldMetadata); |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
public function testDuplicateFieldAndAssociationMapping1_ThrowsException() |
443
|
|
|
{ |
444
|
|
|
$cm = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
445
|
|
|
$cm->setTable(new Mapping\TableMetadata('cms_users')); |
446
|
|
|
|
447
|
|
|
$fieldMetadata = new Mapping\FieldMetadata('name'); |
448
|
|
|
|
449
|
|
|
$fieldMetadata->setType(Type::getType('string')); |
450
|
|
|
|
451
|
|
|
$cm->addProperty($fieldMetadata); |
452
|
|
|
|
453
|
|
|
$this->expectException(\Doctrine\ORM\Mapping\MappingException::class); |
454
|
|
|
|
455
|
|
|
$association = new Mapping\OneToOneAssociationMetadata('name'); |
456
|
|
|
|
457
|
|
|
$association->setTargetEntity(CMS\CmsUser::class); |
458
|
|
|
|
459
|
|
|
$cm->addProperty($association); |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
public function testDuplicateFieldAndAssociationMapping2_ThrowsException() |
463
|
|
|
{ |
464
|
|
|
$cm = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
465
|
|
|
$cm->setTable(new Mapping\TableMetadata('cms_users')); |
466
|
|
|
|
467
|
|
|
$association = new Mapping\OneToOneAssociationMetadata('name'); |
468
|
|
|
|
469
|
|
|
$association->setTargetEntity(CMS\CmsUser::class); |
470
|
|
|
|
471
|
|
|
$cm->addProperty($association); |
472
|
|
|
|
473
|
|
|
$this->expectException(\Doctrine\ORM\Mapping\MappingException::class); |
474
|
|
|
|
475
|
|
|
$fieldMetadata = new Mapping\FieldMetadata('name'); |
476
|
|
|
|
477
|
|
|
$fieldMetadata->setType(Type::getType('string')); |
478
|
|
|
|
479
|
|
|
$cm->addProperty($fieldMetadata); |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
/** |
483
|
|
|
* @group DDC-1224 |
484
|
|
|
*/ |
485
|
|
|
public function testGetTemporaryTableNameSchema() |
486
|
|
|
{ |
487
|
|
|
$cm = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
488
|
|
|
$cm->setTable(new Mapping\TableMetadata('cms_users')); |
489
|
|
|
|
490
|
|
|
$tableMetadata = new Mapping\TableMetadata(); |
491
|
|
|
|
492
|
|
|
$tableMetadata->setSchema('foo'); |
493
|
|
|
$tableMetadata->setName('bar'); |
494
|
|
|
|
495
|
|
|
$cm->setTable($tableMetadata); |
496
|
|
|
|
497
|
|
|
self::assertEquals('foo_bar_id_tmp', $cm->getTemporaryIdTableName()); |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
public function testDefaultTableName() |
501
|
|
|
{ |
502
|
|
|
$cm = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
503
|
|
|
$cm->setTable(new Mapping\TableMetadata('CmsUser')); |
504
|
|
|
|
505
|
|
|
// When table's name is not given |
506
|
|
|
self::assertEquals('CmsUser', $cm->getTableName()); |
507
|
|
|
self::assertEquals('CmsUser', $cm->table->getName()); |
508
|
|
|
|
509
|
|
|
$cm = new ClassMetadata(CMS\CmsAddress::class, $this->metadataBuildingContext); |
510
|
|
|
|
511
|
|
|
// When joinTable's name is not given |
512
|
|
|
$joinTable = new Mapping\JoinTableMetadata(); |
513
|
|
|
|
514
|
|
|
$joinColumn = new Mapping\JoinColumnMetadata(); |
515
|
|
|
$joinColumn->setReferencedColumnName("id"); |
516
|
|
|
|
517
|
|
|
$joinTable->addJoinColumn($joinColumn); |
518
|
|
|
|
519
|
|
|
$joinColumn = new Mapping\JoinColumnMetadata(); |
520
|
|
|
$joinColumn->setReferencedColumnName("id"); |
521
|
|
|
|
522
|
|
|
$joinTable->addInverseJoinColumn($joinColumn); |
523
|
|
|
|
524
|
|
|
$association = new Mapping\ManyToManyAssociationMetadata('user'); |
525
|
|
|
|
526
|
|
|
$association->setJoinTable($joinTable); |
527
|
|
|
$association->setTargetEntity(CMS\CmsUser::class); |
528
|
|
|
$association->setInversedBy('users'); |
529
|
|
|
|
530
|
|
|
$cm->addProperty($association); |
531
|
|
|
|
532
|
|
|
$association = $cm->getProperty('user'); |
533
|
|
|
|
534
|
|
|
self::assertEquals('cmsaddress_cmsuser', $association->getJoinTable()->getName()); |
535
|
|
|
} |
536
|
|
|
|
537
|
|
|
public function testDefaultJoinColumnName() |
538
|
|
|
{ |
539
|
|
|
$cm = new ClassMetadata(CMS\CmsAddress::class, $this->metadataBuildingContext); |
540
|
|
|
$cm->setTable(new Mapping\TableMetadata('cms_address')); |
541
|
|
|
|
542
|
|
|
// this is really dirty, but it's the simplest way to test whether |
543
|
|
|
// joinColumn's name will be automatically set to user_id |
544
|
|
|
$joinColumns = []; |
545
|
|
|
|
546
|
|
|
$joinColumn = new JoinColumnMetadata(); |
547
|
|
|
|
548
|
|
|
$joinColumn->setReferencedColumnName('id'); |
549
|
|
|
|
550
|
|
|
$joinColumns[] = $joinColumn; |
551
|
|
|
|
552
|
|
|
$association = new Mapping\OneToOneAssociationMetadata('user'); |
553
|
|
|
|
554
|
|
|
$association->setJoinColumns($joinColumns); |
555
|
|
|
$association->setTargetEntity(CMS\CmsUser::class); |
556
|
|
|
|
557
|
|
|
$cm->addProperty($association); |
558
|
|
|
|
559
|
|
|
$association = $cm->getProperty('user'); |
560
|
|
|
$joinColumns = $association->getJoinColumns(); |
|
|
|
|
561
|
|
|
$joinColumn = reset($joinColumns); |
562
|
|
|
|
563
|
|
|
self::assertEquals('user_id', $joinColumn->getColumnName()); |
564
|
|
|
|
565
|
|
|
$cm = new ClassMetadata(CMS\CmsAddress::class, $this->metadataBuildingContext); |
566
|
|
|
$cm->setTable(new Mapping\TableMetadata('cms_address')); |
567
|
|
|
|
568
|
|
|
$joinTable = new Mapping\JoinTableMetadata(); |
569
|
|
|
$joinTable->setName('user_CmsUser'); |
570
|
|
|
|
571
|
|
|
$joinColumn = new JoinColumnMetadata(); |
572
|
|
|
$joinColumn->setReferencedColumnName('id'); |
573
|
|
|
|
574
|
|
|
$joinTable->addJoinColumn($joinColumn); |
575
|
|
|
|
576
|
|
|
$joinColumn = new JoinColumnMetadata(); |
577
|
|
|
$joinColumn->setReferencedColumnName('id'); |
578
|
|
|
|
579
|
|
|
$joinTable->addInverseJoinColumn($joinColumn); |
580
|
|
|
|
581
|
|
|
$association = new Mapping\ManyToManyAssociationMetadata('user'); |
582
|
|
|
|
583
|
|
|
$association->setJoinTable($joinTable); |
584
|
|
|
$association->setTargetEntity(CMS\CmsUser::class); |
585
|
|
|
$association->setInversedBy('users'); |
586
|
|
|
|
587
|
|
|
$cm->addProperty($association); |
588
|
|
|
|
589
|
|
|
$association = $cm->getProperty('user'); |
590
|
|
|
$joinTable = $association->getJoinTable(); |
591
|
|
|
$joinColumns = $joinTable->getJoinColumns(); |
592
|
|
|
$joinColumn = reset($joinColumns); |
593
|
|
|
$inverseJoinColumns = $joinTable->getInverseJoinColumns(); |
594
|
|
|
$inverseJoinColumn = reset($inverseJoinColumns); |
595
|
|
|
|
596
|
|
|
self::assertEquals('cmsaddress_id', $joinColumn->getColumnName()); |
597
|
|
|
self::assertEquals('cmsuser_id', $inverseJoinColumn->getColumnName()); |
598
|
|
|
} |
599
|
|
|
|
600
|
|
|
/** |
601
|
|
|
* @group DDC-559 |
602
|
|
|
*/ |
603
|
|
|
public function testOneToOneUnderscoreNamingStrategyDefaults() |
604
|
|
|
{ |
605
|
|
|
$namingStrategy = new UnderscoreNamingStrategy(CASE_UPPER); |
606
|
|
|
|
607
|
|
|
$this->metadataBuildingContext = new Mapping\ClassMetadataBuildingContext( |
608
|
|
|
$this->createMock(Mapping\ClassMetadataFactory::class), |
609
|
|
|
new RuntimeReflectionService(), |
610
|
|
|
$namingStrategy |
611
|
|
|
); |
612
|
|
|
|
613
|
|
|
$metadata = new ClassMetadata(CMS\CmsAddress::class, $this->metadataBuildingContext); |
614
|
|
|
$metadata->setTable(new Mapping\TableMetadata('cms_address')); |
615
|
|
|
|
616
|
|
|
$association = new Mapping\OneToOneAssociationMetadata('user'); |
617
|
|
|
|
618
|
|
|
$association->setTargetEntity(CMS\CmsUser::class); |
619
|
|
|
|
620
|
|
|
$metadata->addProperty($association); |
621
|
|
|
|
622
|
|
|
$association = $metadata->getProperty('user'); |
623
|
|
|
$joinColumns = $association->getJoinColumns(); |
624
|
|
|
$joinColumn = reset($joinColumns); |
625
|
|
|
|
626
|
|
|
self::assertEquals('USER_ID', $joinColumn->getColumnName()); |
627
|
|
|
self::assertEquals('ID', $joinColumn->getReferencedColumnName()); |
628
|
|
|
} |
629
|
|
|
|
630
|
|
|
/** |
631
|
|
|
* @group DDC-559 |
632
|
|
|
*/ |
633
|
|
|
public function testManyToManyUnderscoreNamingStrategyDefaults() |
634
|
|
|
{ |
635
|
|
|
$namingStrategy = new UnderscoreNamingStrategy(CASE_UPPER); |
636
|
|
|
|
637
|
|
|
$this->metadataBuildingContext = new Mapping\ClassMetadataBuildingContext( |
638
|
|
|
$this->createMock(Mapping\ClassMetadataFactory::class), |
639
|
|
|
new RuntimeReflectionService(), |
640
|
|
|
$namingStrategy |
641
|
|
|
); |
642
|
|
|
|
643
|
|
|
$metadata = new ClassMetadata(CMS\CmsAddress::class, $this->metadataBuildingContext); |
644
|
|
|
$metadata->setTable(new Mapping\TableMetadata('cms_address')); |
645
|
|
|
|
646
|
|
|
$association = new Mapping\ManyToManyAssociationMetadata('user'); |
647
|
|
|
|
648
|
|
|
$association->setTargetEntity(CMS\CmsUser::class); |
649
|
|
|
|
650
|
|
|
$metadata->addProperty($association); |
651
|
|
|
|
652
|
|
|
$association = $metadata->getProperty('user'); |
653
|
|
|
$joinTable = $association->getJoinTable(); |
654
|
|
|
$joinColumns = $joinTable->getJoinColumns(); |
655
|
|
|
$joinColumn = reset($joinColumns); |
656
|
|
|
$inverseJoinColumns = $joinTable->getInverseJoinColumns(); |
657
|
|
|
$inverseJoinColumn = reset($inverseJoinColumns); |
658
|
|
|
|
659
|
|
|
self::assertEquals('CMS_ADDRESS_CMS_USER', $joinTable->getName()); |
660
|
|
|
|
661
|
|
|
self::assertEquals('CMS_ADDRESS_ID', $joinColumn->getColumnName()); |
662
|
|
|
self::assertEquals('ID', $joinColumn->getReferencedColumnName()); |
663
|
|
|
|
664
|
|
|
self::assertEquals('CMS_USER_ID', $inverseJoinColumn->getColumnName()); |
665
|
|
|
self::assertEquals('ID', $inverseJoinColumn->getReferencedColumnName()); |
666
|
|
|
|
667
|
|
|
$cm = new ClassMetadata('DoctrineGlobal_Article', $this->metadataBuildingContext); |
668
|
|
|
|
669
|
|
|
$association = new Mapping\ManyToManyAssociationMetadata('author'); |
670
|
|
|
|
671
|
|
|
$association->setTargetEntity(CMS\CmsUser::class); |
672
|
|
|
|
673
|
|
|
$cm->addProperty($association); |
674
|
|
|
|
675
|
|
|
$association = $cm->getProperty('author'); |
676
|
|
|
|
677
|
|
|
self::assertEquals('DOCTRINE_GLOBAL_ARTICLE_CMS_USER', $association->getJoinTable()->getName()); |
678
|
|
|
} |
679
|
|
|
|
680
|
|
|
/** |
681
|
|
|
* @group DDC-886 |
682
|
|
|
*/ |
683
|
|
|
public function testSetMultipleIdentifierSetsComposite() |
684
|
|
|
{ |
685
|
|
|
$cm = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
686
|
|
|
$cm->setTable(new Mapping\TableMetadata('cms_users')); |
687
|
|
|
|
688
|
|
|
$fieldMetadata = new Mapping\FieldMetadata('name'); |
689
|
|
|
$fieldMetadata->setType(Type::getType('string')); |
690
|
|
|
|
691
|
|
|
$cm->addProperty($fieldMetadata); |
692
|
|
|
|
693
|
|
|
$fieldMetadata = new Mapping\FieldMetadata('username'); |
694
|
|
|
$fieldMetadata->setType(Type::getType('string')); |
695
|
|
|
|
696
|
|
|
$cm->addProperty($fieldMetadata); |
697
|
|
|
|
698
|
|
|
$cm->setIdentifier(['name', 'username']); |
699
|
|
|
|
700
|
|
|
self::assertTrue($cm->isIdentifierComposite()); |
701
|
|
|
} |
702
|
|
|
|
703
|
|
|
/** |
704
|
|
|
* @group DDC-961 |
705
|
|
|
*/ |
706
|
|
|
public function testJoinTableMappingDefaults() |
707
|
|
|
{ |
708
|
|
|
$cm = new ClassMetadata('DoctrineGlobal_Article', $this->metadataBuildingContext); |
709
|
|
|
|
710
|
|
|
$association = new Mapping\ManyToManyAssociationMetadata('author'); |
711
|
|
|
|
712
|
|
|
$association->setTargetEntity(CMS\CmsUser::class); |
713
|
|
|
|
714
|
|
|
$cm->addProperty($association); |
715
|
|
|
|
716
|
|
|
$association = $cm->getProperty('author'); |
717
|
|
|
|
718
|
|
|
self::assertEquals('doctrineglobal_article_cmsuser', $association->getJoinTable()->getName()); |
719
|
|
|
} |
720
|
|
|
|
721
|
|
|
/** |
722
|
|
|
* @group DDC-117 |
723
|
|
|
*/ |
724
|
|
|
public function testMapIdentifierAssociation() |
725
|
|
|
{ |
726
|
|
|
$cm = new ClassMetadata(DDC117ArticleDetails::class, $this->metadataBuildingContext); |
727
|
|
|
$cm->setTable(new Mapping\TableMetadata("ddc117_article_details")); |
728
|
|
|
|
729
|
|
|
$association = new Mapping\OneToOneAssociationMetadata('article'); |
730
|
|
|
|
731
|
|
|
$association->setTargetEntity(DDC117Article::class); |
732
|
|
|
$association->setPrimaryKey(true); |
733
|
|
|
|
734
|
|
|
$cm->addProperty($association); |
735
|
|
|
|
736
|
|
|
self::assertEquals(["article"], $cm->identifier); |
737
|
|
|
} |
738
|
|
|
|
739
|
|
|
/** |
740
|
|
|
* @group DDC-117 |
741
|
|
|
*/ |
742
|
|
|
public function testOrphanRemovalIdentifierAssociation() |
743
|
|
|
{ |
744
|
|
|
$cm = new ClassMetadata(DDC117ArticleDetails::class, $this->metadataBuildingContext); |
745
|
|
|
$cm->setTable(new Mapping\TableMetadata("ddc117_article_details")); |
746
|
|
|
|
747
|
|
|
$this->expectException(MappingException::class); |
748
|
|
|
$this->expectExceptionMessage('The orphan removal option is not allowed on an association that'); |
749
|
|
|
|
750
|
|
|
$association = new Mapping\OneToOneAssociationMetadata('article'); |
751
|
|
|
|
752
|
|
|
$association->setTargetEntity(DDC117Article::class); |
753
|
|
|
$association->setPrimaryKey(true); |
754
|
|
|
$association->setOrphanRemoval(true); |
755
|
|
|
|
756
|
|
|
$cm->addProperty($association); |
757
|
|
|
} |
758
|
|
|
|
759
|
|
|
/** |
760
|
|
|
* @group DDC-117 |
761
|
|
|
*/ |
762
|
|
|
public function testInverseIdentifierAssociation() |
763
|
|
|
{ |
764
|
|
|
$cm = new ClassMetadata(DDC117ArticleDetails::class, $this->metadataBuildingContext); |
765
|
|
|
$cm->setTable(new Mapping\TableMetadata("ddc117_article_details")); |
766
|
|
|
|
767
|
|
|
$this->expectException(MappingException::class); |
768
|
|
|
$this->expectExceptionMessage('An inverse association is not allowed to be identifier in'); |
769
|
|
|
|
770
|
|
|
$association = new Mapping\OneToOneAssociationMetadata('article'); |
771
|
|
|
|
772
|
|
|
$association->setTargetEntity(DDC117Article::class); |
773
|
|
|
$association->setPrimaryKey(true); |
774
|
|
|
$association->setMappedBy('details'); |
775
|
|
|
$association->setOwningSide(false); |
776
|
|
|
|
777
|
|
|
$cm->addProperty($association); |
778
|
|
|
} |
779
|
|
|
|
780
|
|
|
/** |
781
|
|
|
* @group DDC-117 |
782
|
|
|
*/ |
783
|
|
|
public function testIdentifierAssociationManyToMany() |
784
|
|
|
{ |
785
|
|
|
$cm = new ClassMetadata(DDC117ArticleDetails::class, $this->metadataBuildingContext); |
786
|
|
|
$cm->setTable(new Mapping\TableMetadata("ddc117_article_details")); |
787
|
|
|
|
788
|
|
|
$this->expectException(MappingException::class); |
789
|
|
|
$this->expectExceptionMessage('Many-to-many or one-to-many associations are not allowed to be identifier in'); |
790
|
|
|
|
791
|
|
|
$association = new Mapping\ManyToManyAssociationMetadata('article'); |
792
|
|
|
|
793
|
|
|
$association->setTargetEntity(DDC117Article::class); |
794
|
|
|
$association->setPrimaryKey(true); |
795
|
|
|
|
796
|
|
|
$cm->addProperty($association); |
797
|
|
|
} |
798
|
|
|
|
799
|
|
|
/** |
800
|
|
|
* @group DDC-996 |
801
|
|
|
*/ |
802
|
|
|
public function testEmptyFieldNameThrowsException() |
803
|
|
|
{ |
804
|
|
|
$this->expectException(MappingException::class); |
805
|
|
|
$this->expectExceptionMessage("The field or association mapping misses the 'fieldName' attribute in entity '" . CMS\CmsUser::class . "'."); |
806
|
|
|
|
807
|
|
|
$cm = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
808
|
|
|
$cm->setTable(new Mapping\TableMetadata('cms_users')); |
809
|
|
|
|
810
|
|
|
$fieldMetadata = new Mapping\FieldMetadata(''); |
811
|
|
|
|
812
|
|
|
$fieldMetadata->setType(Type::getType('string')); |
813
|
|
|
|
814
|
|
|
$cm->addProperty($fieldMetadata); |
815
|
|
|
} |
816
|
|
|
|
817
|
|
|
/** |
818
|
|
|
* @group DDC-2451 |
819
|
|
|
*/ |
820
|
|
|
public function testSerializeEntityListeners() |
821
|
|
|
{ |
822
|
|
|
$metadata = new ClassMetadata(CompanyContract::class, $this->metadataBuildingContext); |
823
|
|
|
|
824
|
|
|
$metadata->addEntityListener(Events::prePersist, CompanyContractListener::class, 'prePersistHandler'); |
825
|
|
|
$metadata->addEntityListener(Events::postPersist, CompanyContractListener::class, 'postPersistHandler'); |
826
|
|
|
|
827
|
|
|
$serialize = serialize($metadata); |
828
|
|
|
$unserialize = unserialize($serialize); |
829
|
|
|
|
830
|
|
|
self::assertEquals($metadata->entityListeners, $unserialize->entityListeners); |
831
|
|
|
} |
832
|
|
|
|
833
|
|
|
/** |
834
|
|
|
* @group DDC-1068 |
835
|
|
|
*/ |
836
|
|
|
public function testClassCaseSensitivity() |
837
|
|
|
{ |
838
|
|
|
$cm = new ClassMetadata(strtoupper(CMS\CmsUser::class), $this->metadataBuildingContext); |
839
|
|
|
$cm->setTable(new Mapping\TableMetadata('cms_users')); |
840
|
|
|
|
841
|
|
|
self::assertEquals(CMS\CmsUser::class, $cm->getClassName()); |
842
|
|
|
} |
843
|
|
|
|
844
|
|
|
/** |
845
|
|
|
* @group DDC-659 |
846
|
|
|
*/ |
847
|
|
|
public function testLifecycleCallbackNotFound() |
848
|
|
|
{ |
849
|
|
|
$cm = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
850
|
|
|
$cm->setTable(new Mapping\TableMetadata('cms_users')); |
851
|
|
|
|
852
|
|
|
$cm->addLifecycleCallback('notfound', 'postLoad'); |
853
|
|
|
|
854
|
|
|
$this->expectException(MappingException::class); |
855
|
|
|
$this->expectExceptionMessage("Entity '" . CMS\CmsUser::class . "' has no method 'notfound' to be registered as lifecycle callback."); |
856
|
|
|
|
857
|
|
|
$cm->validateLifecycleCallbacks(new RuntimeReflectionService()); |
858
|
|
|
} |
859
|
|
|
|
860
|
|
|
/** |
861
|
|
|
* @group ImproveErrorMessages |
862
|
|
|
*/ |
863
|
|
|
public function testTargetEntityNotFound() |
864
|
|
|
{ |
865
|
|
|
$cm = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
866
|
|
|
$cm->setTable(new Mapping\TableMetadata('cms_users')); |
867
|
|
|
|
868
|
|
|
$association = new Mapping\ManyToOneAssociationMetadata('address'); |
869
|
|
|
|
870
|
|
|
$association->setTargetEntity('UnknownClass'); |
871
|
|
|
|
872
|
|
|
$cm->addProperty($association); |
873
|
|
|
|
874
|
|
|
$this->expectException(MappingException::class); |
875
|
|
|
$this->expectExceptionMessage("The target-entity 'UnknownClass' cannot be found in '" . CMS\CmsUser::class . "#address'."); |
876
|
|
|
|
877
|
|
|
$cm->validateAssociations(); |
878
|
|
|
} |
879
|
|
|
|
880
|
|
|
/** |
881
|
|
|
* @group DDC-1746 |
882
|
|
|
* @expectedException \Doctrine\ORM\Mapping\MappingException |
883
|
|
|
* @expectedExceptionMessage You have specified invalid cascade options for Doctrine\Tests\Models\CMS\CmsUser::$address: 'invalid'; available options: 'remove', 'persist', and 'refresh' |
884
|
|
|
*/ |
885
|
|
|
public function testInvalidCascade() |
886
|
|
|
{ |
887
|
|
|
$cm = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
888
|
|
|
$cm->setTable(new Mapping\TableMetadata('cms_users')); |
889
|
|
|
|
890
|
|
|
$association = new Mapping\ManyToOneAssociationMetadata('address'); |
891
|
|
|
|
892
|
|
|
$association->setTargetEntity('UnknownClass'); |
893
|
|
|
$association->setCascade(['invalid']); |
894
|
|
|
|
895
|
|
|
$cm->addProperty($association); |
896
|
|
|
} |
897
|
|
|
|
898
|
|
|
/** |
899
|
|
|
* @group DDC-964 |
900
|
|
|
* @expectedException \Doctrine\ORM\Mapping\MappingException |
901
|
|
|
* @expectedExceptionMessage Invalid field override named 'invalidPropertyName' for class 'Doctrine\Tests\Models\DDC964\DDC964Admin' |
902
|
|
|
*/ |
903
|
|
|
public function testInvalidPropertyAssociationOverrideNameException() |
904
|
|
|
{ |
905
|
|
|
$cm = new ClassMetadata(DDC964Admin::class, $this->metadataBuildingContext); |
906
|
|
|
$cm->setTable(new Mapping\TableMetadata("ddc964_admin")); |
907
|
|
|
|
908
|
|
|
$association = new Mapping\ManyToOneAssociationMetadata('address'); |
909
|
|
|
|
910
|
|
|
$association->setTargetEntity(DDC964Address::class); |
911
|
|
|
|
912
|
|
|
$cm->addProperty($association); |
913
|
|
|
|
914
|
|
|
$cm->setPropertyOverride(new Mapping\ManyToOneAssociationMetadata('invalidPropertyName')); |
915
|
|
|
} |
916
|
|
|
|
917
|
|
|
/** |
918
|
|
|
* @group DDC-964 |
919
|
|
|
* @expectedException \Doctrine\ORM\Mapping\MappingException |
920
|
|
|
* @expectedExceptionMessage Invalid field override named 'invalidPropertyName' for class 'Doctrine\Tests\Models\DDC964\DDC964Guest'. |
921
|
|
|
*/ |
922
|
|
|
public function testInvalidPropertyAttributeOverrideNameException() |
923
|
|
|
{ |
924
|
|
|
$cm = new ClassMetadata(DDC964Guest::class, $this->metadataBuildingContext); |
925
|
|
|
$cm->setTable(new Mapping\TableMetadata("ddc964_guest")); |
926
|
|
|
|
927
|
|
|
$fieldMetadata = new Mapping\FieldMetadata('name'); |
928
|
|
|
$fieldMetadata->setType(Type::getType('string')); |
929
|
|
|
|
930
|
|
|
$cm->addProperty($fieldMetadata); |
931
|
|
|
|
932
|
|
|
$fieldMetadata = new Mapping\FieldMetadata('invalidPropertyName'); |
933
|
|
|
$fieldMetadata->setType(Type::getType('string')); |
934
|
|
|
|
935
|
|
|
$cm->setPropertyOverride($fieldMetadata); |
936
|
|
|
} |
937
|
|
|
|
938
|
|
|
/** |
939
|
|
|
* @group DDC-1955 |
940
|
|
|
* |
941
|
|
|
* @expectedException \Doctrine\ORM\Mapping\MappingException |
942
|
|
|
* @expectedExceptionMessage Entity Listener "\InvalidClassName" declared on "Doctrine\Tests\Models\CMS\CmsUser" not found. |
943
|
|
|
*/ |
944
|
|
|
public function testInvalidEntityListenerClassException() |
945
|
|
|
{ |
946
|
|
|
$cm = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
947
|
|
|
$cm->setTable(new Mapping\TableMetadata('cms_users')); |
948
|
|
|
|
949
|
|
|
$cm->addEntityListener(Events::postLoad, '\InvalidClassName', 'postLoadHandler'); |
950
|
|
|
} |
951
|
|
|
|
952
|
|
|
/** |
953
|
|
|
* @group DDC-1955 |
954
|
|
|
* |
955
|
|
|
* @expectedException \Doctrine\ORM\Mapping\MappingException |
956
|
|
|
* @expectedExceptionMessage Entity Listener "Doctrine\Tests\Models\Company\CompanyContractListener" declared on "Doctrine\Tests\Models\CMS\CmsUser" has no method "invalidMethod". |
957
|
|
|
*/ |
958
|
|
|
public function testInvalidEntityListenerMethodException() |
959
|
|
|
{ |
960
|
|
|
$cm = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
961
|
|
|
$cm->setTable(new Mapping\TableMetadata('cms_users')); |
962
|
|
|
|
963
|
|
|
$cm->addEntityListener(Events::postLoad, 'Doctrine\Tests\Models\Company\CompanyContractListener', 'invalidMethod'); |
964
|
|
|
} |
965
|
|
|
|
966
|
|
|
public function testManyToManySelfReferencingNamingStrategyDefaults() |
967
|
|
|
{ |
968
|
|
|
$cm = new ClassMetadata(CustomTypeParent::class, $this->metadataBuildingContext); |
969
|
|
|
$cm->setTable(new Mapping\TableMetadata("custom_type_parent")); |
970
|
|
|
|
971
|
|
|
$association = new Mapping\ManyToManyAssociationMetadata('friendsWithMe'); |
972
|
|
|
|
973
|
|
|
$association->setTargetEntity(CustomTypeParent::class); |
974
|
|
|
|
975
|
|
|
$cm->addProperty($association); |
976
|
|
|
|
977
|
|
|
$association = $cm->getProperty('friendsWithMe'); |
978
|
|
|
|
979
|
|
|
$joinColumns = []; |
980
|
|
|
|
981
|
|
|
$joinColumn = new Mapping\JoinColumnMetadata(); |
982
|
|
|
|
983
|
|
|
$joinColumn->setColumnName("customtypeparent_source"); |
984
|
|
|
$joinColumn->setReferencedColumnName("id"); |
985
|
|
|
$joinColumn->setOnDelete("CASCADE"); |
986
|
|
|
|
987
|
|
|
$joinColumns[] = $joinColumn; |
988
|
|
|
|
989
|
|
|
$inverseJoinColumns = []; |
990
|
|
|
|
991
|
|
|
$joinColumn = new Mapping\JoinColumnMetadata(); |
992
|
|
|
|
993
|
|
|
$joinColumn->setColumnName("customtypeparent_target"); |
994
|
|
|
$joinColumn->setReferencedColumnName("id"); |
995
|
|
|
$joinColumn->setOnDelete("CASCADE"); |
996
|
|
|
|
997
|
|
|
$inverseJoinColumns[] = $joinColumn; |
998
|
|
|
|
999
|
|
|
$joinTable = $association->getJoinTable(); |
1000
|
|
|
|
1001
|
|
|
self::assertEquals('customtypeparent_customtypeparent', $joinTable->getName()); |
1002
|
|
|
self::assertEquals($joinColumns, $joinTable->getJoinColumns()); |
1003
|
|
|
self::assertEquals($inverseJoinColumns, $joinTable->getInverseJoinColumns()); |
1004
|
|
|
} |
1005
|
|
|
|
1006
|
|
|
/** |
1007
|
|
|
* @group DDC-2662 |
1008
|
|
|
* @group 6682 |
1009
|
|
|
*/ |
1010
|
|
|
public function testQuotedSequenceName() : void |
1011
|
|
|
{ |
1012
|
|
|
self::markTestIncomplete( |
1013
|
|
|
'@guilhermeblanco, in #6683 we added allocationSize/initialValue as to the sequence definition but with the' |
1014
|
|
|
. ' changes you have made I am not sure if the "initialValue" should still be verified here or if it should' |
1015
|
|
|
. ' part of the metadata drivers' |
1016
|
|
|
); |
1017
|
|
|
|
1018
|
|
|
$cm = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
1019
|
|
|
$cm->setTable(new Mapping\TableMetadata('cms_users')); |
1020
|
|
|
|
1021
|
|
|
$id = new Mapping\FieldMetadata('id'); |
1022
|
|
|
$id->setValueGenerator(new Mapping\ValueGeneratorMetadata( |
1023
|
|
|
Mapping\GeneratorType::SEQUENCE, |
1024
|
|
|
[ |
1025
|
|
|
'sequenceName' => 'foo', |
1026
|
|
|
'allocationSize' => 1, |
1027
|
|
|
] |
1028
|
|
|
)); |
1029
|
|
|
$cm->addProperty($id); |
1030
|
|
|
|
1031
|
|
|
self::assertEquals( |
1032
|
|
|
['sequenceName' => 'foo', 'allocationSize' => 1, 'initialValue' => '1'], |
1033
|
|
|
$cm->getProperty('id')->getValueGenerator()->getDefinition() |
1034
|
|
|
); |
1035
|
|
|
} |
1036
|
|
|
|
1037
|
|
|
/** |
1038
|
|
|
* @group DDC-2700 |
1039
|
|
|
*/ |
1040
|
|
|
public function testIsIdentifierMappedSuperClass() |
1041
|
|
|
{ |
1042
|
|
|
$class = new ClassMetadata(DDC2700MappedSuperClass::class, $this->metadataBuildingContext); |
1043
|
|
|
|
1044
|
|
|
self::assertFalse($class->isIdentifier('foo')); |
1045
|
|
|
} |
1046
|
|
|
|
1047
|
|
|
/** |
1048
|
|
|
* @group embedded |
1049
|
|
|
*/ |
1050
|
|
|
public function testWakeupReflectionWithEmbeddableAndStaticReflectionService() |
1051
|
|
|
{ |
1052
|
|
|
$metadata = new ClassMetadata(TestEntity1::class, $this->metadataBuildingContext); |
1053
|
|
|
$cm->setTable(new Mapping\TableMetadata("test_entity1")); |
|
|
|
|
1054
|
|
|
|
1055
|
|
|
$metadata->mapEmbedded( |
1056
|
|
|
[ |
1057
|
|
|
'fieldName' => 'test', |
1058
|
|
|
'class' => TestEntity1::class, |
1059
|
|
|
'columnPrefix' => false, |
1060
|
|
|
] |
1061
|
|
|
); |
1062
|
|
|
|
1063
|
|
|
$fieldMetadata = new Mapping\FieldMetadata('test.embeddedProperty'); |
1064
|
|
|
$fieldMetadata->setType(Type::getType('string')); |
1065
|
|
|
|
1066
|
|
|
$metadata->addProperty($fieldMetadata); |
1067
|
|
|
|
1068
|
|
|
/* |
|
|
|
|
1069
|
|
|
$mapping = [ |
1070
|
|
|
'originalClass' => TestEntity1::class, |
1071
|
|
|
'declaredField' => 'test', |
1072
|
|
|
'originalField' => 'embeddedProperty' |
1073
|
|
|
]; |
1074
|
|
|
|
1075
|
|
|
$metadata->addProperty('test.embeddedProperty', Type::getType('string'), $mapping); |
1076
|
|
|
*/ |
1077
|
|
|
|
1078
|
|
|
$metadata->wakeupReflection(new StaticReflectionService()); |
1079
|
|
|
|
1080
|
|
|
self::assertEquals( |
1081
|
|
|
[ |
1082
|
|
|
'test' => null, |
1083
|
|
|
'test.embeddedProperty' => null |
1084
|
|
|
], |
1085
|
|
|
$metadata->getReflectionProperties() |
|
|
|
|
1086
|
|
|
); |
1087
|
|
|
} |
1088
|
|
|
} |
1089
|
|
|
|
1090
|
|
|
/** |
1091
|
|
|
* @ORM\MappedSuperclass |
1092
|
|
|
*/ |
1093
|
|
|
class DDC2700MappedSuperClass |
1094
|
|
|
{ |
1095
|
|
|
/** @ORM\Column */ |
1096
|
|
|
private $foo; |
|
|
|
|
1097
|
|
|
} |
1098
|
|
|
|
1099
|
|
|
class MyNamespacedNamingStrategy extends DefaultNamingStrategy |
1100
|
|
|
{ |
1101
|
|
|
/** |
1102
|
|
|
* {@inheritdoc} |
1103
|
|
|
*/ |
1104
|
|
|
public function classToTableName($className) |
1105
|
|
|
{ |
1106
|
|
|
if (strpos($className, '\\') !== false) { |
1107
|
|
|
$className = str_replace('\\', '_', str_replace('Doctrine\Tests\Models\\', '', $className)); |
1108
|
|
|
} |
1109
|
|
|
|
1110
|
|
|
return strtolower($className); |
1111
|
|
|
} |
1112
|
|
|
} |
1113
|
|
|
|
1114
|
|
|
class MyPrefixNamingStrategy extends DefaultNamingStrategy |
1115
|
|
|
{ |
1116
|
|
|
/** |
1117
|
|
|
* {@inheritdoc} |
1118
|
|
|
*/ |
1119
|
|
|
public function propertyToColumnName($propertyName, $className = null) |
1120
|
|
|
{ |
1121
|
|
|
return strtolower($this->classToTableName($className)) . '_' . $propertyName; |
1122
|
|
|
} |
1123
|
|
|
} |
1124
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.