Total Complexity | 44 |
Total Lines | 1051 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ClassMetadataTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ClassMetadataTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
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() |
||
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() |
||
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() |
||
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() |
||
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() |
||
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")); |
||
778 | } |
||
779 | |||
780 | /** |
||
781 | * @group DDC-117 |
||
782 | */ |
||
783 | public function testIdentifierAssociationManyToMany() |
||
797 | } |
||
798 | |||
799 | /** |
||
800 | * @group DDC-996 |
||
801 | */ |
||
802 | public function testEmptyFieldNameThrowsException() |
||
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() |
||
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() |
||
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() |
||
1086 | ); |
||
1087 | } |
||
1088 | } |
||
1089 | |||
1090 | /** |
||
1091 | * @ORM\MappedSuperclass |
||
1092 | */ |
||
1093 | class DDC2700MappedSuperClass |
||
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.