Total Complexity | 53 |
Total Lines | 1317 |
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() |
||
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 | |||
776 | $cm->addProperty($association); |
||
777 | } |
||
778 | |||
779 | /** |
||
780 | * @group DDC-117 |
||
781 | */ |
||
782 | public function testIdentifierAssociationManyToMany() |
||
783 | { |
||
784 | $cm = new ClassMetadata(DDC117ArticleDetails::class, $this->metadataBuildingContext); |
||
785 | $cm->setTable(new Mapping\TableMetadata("ddc117_article_details")); |
||
786 | |||
787 | $this->expectException(MappingException::class); |
||
788 | $this->expectExceptionMessage('Many-to-many or one-to-many associations are not allowed to be identifier in'); |
||
789 | |||
790 | $association = new Mapping\ManyToManyAssociationMetadata('article'); |
||
791 | |||
792 | $association->setTargetEntity(DDC117Article::class); |
||
793 | $association->setPrimaryKey(true); |
||
794 | |||
795 | $cm->addProperty($association); |
||
796 | } |
||
797 | |||
798 | /** |
||
799 | * @group DDC-996 |
||
800 | */ |
||
801 | public function testEmptyFieldNameThrowsException() |
||
802 | { |
||
803 | $this->expectException(MappingException::class); |
||
804 | $this->expectExceptionMessage("The field or association mapping misses the 'fieldName' attribute in entity '" . CMS\CmsUser::class . "'."); |
||
805 | |||
806 | $cm = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
||
807 | $cm->setTable(new Mapping\TableMetadata('cms_users')); |
||
808 | |||
809 | $fieldMetadata = new Mapping\FieldMetadata(''); |
||
810 | |||
811 | $fieldMetadata->setType(Type::getType('string')); |
||
812 | |||
813 | $cm->addProperty($fieldMetadata); |
||
814 | } |
||
815 | |||
816 | /** |
||
817 | * @group DDC-1663 |
||
818 | */ |
||
819 | public function testRetrievalOfResultSetMappings() |
||
820 | { |
||
821 | $cm = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
||
822 | |||
823 | self::assertCount(0, $cm->getSqlResultSetMappings()); |
||
824 | |||
825 | $cm->addSqlResultSetMapping( |
||
826 | [ |
||
827 | 'name' => 'find-all', |
||
828 | 'entities' => [ |
||
829 | [ |
||
830 | 'entityClass' => CMS\CmsUser::class, |
||
831 | ], |
||
832 | ], |
||
833 | ] |
||
834 | ); |
||
835 | |||
836 | self::assertCount(1, $cm->getSqlResultSetMappings()); |
||
837 | } |
||
838 | |||
839 | /** |
||
840 | * @group DDC-1663 |
||
841 | */ |
||
842 | public function testRetrieveOfNamedNativeQuery() |
||
876 | } |
||
877 | |||
878 | /** |
||
879 | * @group DDC-1663 |
||
880 | */ |
||
881 | public function testRetrieveOfSqlResultSetMapping() |
||
882 | { |
||
883 | $cm = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
||
884 | $cm->setTable(new Mapping\TableMetadata('cms_users')); |
||
885 | |||
886 | $cm->addSqlResultSetMapping( |
||
887 | [ |
||
888 | 'name' => 'find-all', |
||
889 | 'entities' => [ |
||
890 | [ |
||
891 | 'entityClass' => '__CLASS__', |
||
892 | 'fields' => [ |
||
893 | [ |
||
894 | 'name' => 'id', |
||
895 | 'column'=> 'id' |
||
896 | ], |
||
897 | [ |
||
898 | 'name' => 'name', |
||
899 | 'column'=> 'name' |
||
900 | ] |
||
901 | ] |
||
902 | ], |
||
903 | [ |
||
904 | 'entityClass' => CMS\CmsEmail::class, |
||
905 | 'fields' => [ |
||
906 | [ |
||
907 | 'name' => 'id', |
||
908 | 'column'=> 'id' |
||
909 | ], |
||
910 | [ |
||
911 | 'name' => 'email', |
||
912 | 'column'=> 'email' |
||
913 | ] |
||
914 | ] |
||
915 | ] |
||
916 | ], |
||
917 | 'columns' => [['name' => 'scalarColumn']] |
||
918 | ] |
||
919 | ); |
||
920 | |||
921 | $mapping = $cm->getSqlResultSetMapping('find-all'); |
||
922 | |||
923 | self::assertEquals('__CLASS__', $mapping['entities'][0]['entityClass']); |
||
924 | self::assertEquals(['name'=>'id','column'=>'id'], $mapping['entities'][0]['fields'][0]); |
||
925 | self::assertEquals(['name'=>'name','column'=>'name'], $mapping['entities'][0]['fields'][1]); |
||
926 | |||
927 | self::assertEquals(CMS\CmsEmail::class, $mapping['entities'][1]['entityClass']); |
||
928 | self::assertEquals(['name'=>'id','column'=>'id'], $mapping['entities'][1]['fields'][0]); |
||
929 | self::assertEquals(['name'=>'email','column'=>'email'], $mapping['entities'][1]['fields'][1]); |
||
930 | |||
931 | self::assertEquals('scalarColumn', $mapping['columns'][0]['name']); |
||
932 | } |
||
933 | |||
934 | /** |
||
935 | * @group DDC-1663 |
||
936 | */ |
||
937 | public function testExistanceOfSqlResultSetMapping() |
||
938 | { |
||
939 | $cm = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
||
940 | $cm->setTable(new Mapping\TableMetadata('cms_users')); |
||
941 | |||
942 | $cm->addSqlResultSetMapping( |
||
943 | [ |
||
944 | 'name' => 'find-all', |
||
945 | 'entities' => [ |
||
946 | [ |
||
947 | 'entityClass' => CMS\CmsUser::class, |
||
948 | ], |
||
949 | ], |
||
950 | ] |
||
951 | ); |
||
952 | |||
953 | self::assertTrue($cm->hasSqlResultSetMapping('find-all')); |
||
954 | self::assertFalse($cm->hasSqlResultSetMapping('find-by-id')); |
||
955 | } |
||
956 | |||
957 | /** |
||
958 | * @group DDC-1663 |
||
959 | */ |
||
960 | public function testExistanceOfNamedNativeQuery() |
||
961 | { |
||
962 | $cm = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
||
963 | $cm->setTable(new Mapping\TableMetadata('cms_users')); |
||
964 | |||
965 | $cm->addNamedNativeQuery( |
||
966 | 'find-all', |
||
967 | 'SELECT * FROM cms_users', |
||
968 | [ |
||
969 | 'resultClass' => CMS\CmsUser::class, |
||
970 | ] |
||
971 | ); |
||
972 | |||
973 | self::assertTrue($cm->hasNamedNativeQuery('find-all')); |
||
974 | self::assertFalse($cm->hasNamedNativeQuery('find-by-id')); |
||
975 | } |
||
976 | |||
977 | /** |
||
978 | * @group DDC-1663 |
||
979 | */ |
||
980 | public function testRetrievalOfNamedNativeQueries() |
||
981 | { |
||
982 | $cm = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
||
983 | $cm->setTable(new Mapping\TableMetadata('cms_users')); |
||
984 | |||
985 | self::assertCount(0, $cm->getNamedNativeQueries()); |
||
986 | |||
987 | $cm->addNamedNativeQuery( |
||
988 | 'find-all', |
||
989 | 'SELECT * FROM cms_users', |
||
990 | [ |
||
991 | 'resultClass' => CMS\CmsUser::class, |
||
992 | ] |
||
993 | ); |
||
994 | |||
995 | self::assertCount(1, $cm->getNamedNativeQueries()); |
||
996 | } |
||
997 | |||
998 | /** |
||
999 | * @group DDC-2451 |
||
1000 | */ |
||
1001 | public function testSerializeEntityListeners() |
||
1002 | { |
||
1003 | $metadata = new ClassMetadata(CompanyContract::class, $this->metadataBuildingContext); |
||
1004 | |||
1005 | $metadata->addEntityListener(Events::prePersist, CompanyContractListener::class, 'prePersistHandler'); |
||
1006 | $metadata->addEntityListener(Events::postPersist, CompanyContractListener::class, 'postPersistHandler'); |
||
1007 | |||
1008 | $serialize = serialize($metadata); |
||
1009 | $unserialize = unserialize($serialize); |
||
1010 | |||
1011 | self::assertEquals($metadata->entityListeners, $unserialize->entityListeners); |
||
1012 | } |
||
1013 | |||
1014 | /** |
||
1015 | * @group DDC-1663 |
||
1016 | * |
||
1017 | * @expectedException \Doctrine\ORM\Mapping\MappingException |
||
1018 | * @expectedExceptionMessage Query named "find-all" in "Doctrine\Tests\Models\CMS\CmsUser" was already declared, but it must be declared only once |
||
1019 | */ |
||
1020 | public function testNamingCollisionNamedNativeQueryShouldThrowException() |
||
1021 | { |
||
1022 | $cm = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
||
1023 | $cm->setTable(new Mapping\TableMetadata('cms_users')); |
||
1024 | |||
1025 | $cm->addNamedNativeQuery( |
||
1026 | 'find-all', |
||
1027 | 'SELECT * FROM cms_users', |
||
1028 | [ |
||
1029 | 'resultClass' => CMS\CmsUser::class, |
||
1030 | ] |
||
1031 | ); |
||
1032 | |||
1033 | $cm->addNamedNativeQuery( |
||
1034 | 'find-all', |
||
1035 | 'SELECT * FROM cms_users', |
||
1036 | [ |
||
1037 | 'resultClass' => CMS\CmsUser::class, |
||
1038 | ] |
||
1039 | ); |
||
1040 | } |
||
1041 | |||
1042 | /** |
||
1043 | * @group DDC-1663 |
||
1044 | * |
||
1045 | * @expectedException \Doctrine\ORM\Mapping\MappingException |
||
1046 | * @expectedExceptionMessage Result set mapping named "find-all" in "Doctrine\Tests\Models\CMS\CmsUser" was already declared, but it must be declared only once |
||
1047 | */ |
||
1048 | public function testNamingCollisionSqlResultSetMappingShouldThrowException() |
||
1049 | { |
||
1050 | $cm = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
||
1051 | $cm->setTable(new Mapping\TableMetadata('cms_users')); |
||
1052 | |||
1053 | $cm->addSqlResultSetMapping( |
||
1054 | [ |
||
1055 | 'name' => 'find-all', |
||
1056 | 'entities' => [ |
||
1057 | [ |
||
1058 | 'entityClass' => CMS\CmsUser::class, |
||
1059 | ], |
||
1060 | ], |
||
1061 | ] |
||
1062 | ); |
||
1063 | |||
1064 | $cm->addSqlResultSetMapping( |
||
1065 | [ |
||
1066 | 'name' => 'find-all', |
||
1067 | 'entities' => [ |
||
1068 | [ |
||
1069 | 'entityClass' => CMS\CmsUser::class, |
||
1070 | ], |
||
1071 | ], |
||
1072 | ] |
||
1073 | ); |
||
1074 | } |
||
1075 | |||
1076 | /** |
||
1077 | * @group DDC-1068 |
||
1078 | */ |
||
1079 | public function testClassCaseSensitivity() |
||
1080 | { |
||
1081 | $cm = new ClassMetadata(strtoupper(CMS\CmsUser::class), $this->metadataBuildingContext); |
||
1082 | $cm->setTable(new Mapping\TableMetadata('cms_users')); |
||
1083 | |||
1084 | self::assertEquals(CMS\CmsUser::class, $cm->getClassName()); |
||
1085 | } |
||
1086 | |||
1087 | /** |
||
1088 | * @group DDC-659 |
||
1089 | */ |
||
1090 | public function testLifecycleCallbackNotFound() |
||
1091 | { |
||
1092 | $cm = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
||
1093 | $cm->setTable(new Mapping\TableMetadata('cms_users')); |
||
1094 | |||
1095 | $cm->addLifecycleCallback('notfound', 'postLoad'); |
||
1096 | |||
1097 | $this->expectException(MappingException::class); |
||
1098 | $this->expectExceptionMessage("Entity '" . CMS\CmsUser::class . "' has no method 'notfound' to be registered as lifecycle callback."); |
||
1099 | |||
1100 | $cm->validateLifecycleCallbacks(new RuntimeReflectionService()); |
||
1101 | } |
||
1102 | |||
1103 | /** |
||
1104 | * @group ImproveErrorMessages |
||
1105 | */ |
||
1106 | public function testTargetEntityNotFound() |
||
1107 | { |
||
1108 | $cm = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
||
1109 | $cm->setTable(new Mapping\TableMetadata('cms_users')); |
||
1110 | |||
1111 | $association = new Mapping\ManyToOneAssociationMetadata('address'); |
||
1112 | |||
1113 | $association->setTargetEntity('UnknownClass'); |
||
1114 | |||
1115 | $cm->addProperty($association); |
||
1116 | |||
1117 | $this->expectException(MappingException::class); |
||
1118 | $this->expectExceptionMessage("The target-entity 'UnknownClass' cannot be found in '" . CMS\CmsUser::class . "#address'."); |
||
1119 | |||
1120 | $cm->validateAssociations(); |
||
1121 | } |
||
1122 | |||
1123 | /** |
||
1124 | * @group DDC-1663 |
||
1125 | * |
||
1126 | * @expectedException \Doctrine\ORM\Mapping\MappingException |
||
1127 | * @expectedExceptionMessage Result set mapping named "find-all" in "Doctrine\Tests\Models\CMS\CmsUser requires a entity class name. |
||
1128 | */ |
||
1129 | public function testNameIsMandatoryForEntityNameSqlResultSetMappingException() |
||
1130 | { |
||
1131 | $cm = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
||
1132 | $cm->setTable(new Mapping\TableMetadata('cms_users')); |
||
1133 | |||
1134 | $cm->addSqlResultSetMapping( |
||
1135 | [ |
||
1136 | 'name' => 'find-all', |
||
1137 | 'entities' => [ |
||
1138 | [ |
||
1139 | 'fields' => [] |
||
1140 | ] |
||
1141 | ], |
||
1142 | ] |
||
1143 | ); |
||
1144 | } |
||
1145 | |||
1146 | /** |
||
1147 | * @group DDC-1746 |
||
1148 | * @expectedException \Doctrine\ORM\Mapping\MappingException |
||
1149 | * @expectedExceptionMessage You have specified invalid cascade options for Doctrine\Tests\Models\CMS\CmsUser::$address: 'invalid'; available options: 'remove', 'persist', and 'refresh' |
||
1150 | */ |
||
1151 | public function testInvalidCascade() |
||
1152 | { |
||
1153 | $cm = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
||
1154 | $cm->setTable(new Mapping\TableMetadata('cms_users')); |
||
1155 | |||
1156 | $association = new Mapping\ManyToOneAssociationMetadata('address'); |
||
1157 | |||
1158 | $association->setTargetEntity('UnknownClass'); |
||
1159 | $association->setCascade(['invalid']); |
||
1160 | |||
1161 | $cm->addProperty($association); |
||
1162 | } |
||
1163 | |||
1164 | /** |
||
1165 | * @group DDC-964 |
||
1166 | * @expectedException \Doctrine\ORM\Mapping\MappingException |
||
1167 | * @expectedExceptionMessage Invalid field override named 'invalidPropertyName' for class 'Doctrine\Tests\Models\DDC964\DDC964Admin' |
||
1168 | */ |
||
1169 | public function testInvalidPropertyAssociationOverrideNameException() |
||
1170 | { |
||
1171 | $cm = new ClassMetadata(DDC964Admin::class, $this->metadataBuildingContext); |
||
1172 | $cm->setTable(new Mapping\TableMetadata("ddc964_admin")); |
||
1173 | |||
1174 | $association = new Mapping\ManyToOneAssociationMetadata('address'); |
||
1175 | |||
1176 | $association->setTargetEntity(DDC964Address::class); |
||
1177 | |||
1178 | $cm->addProperty($association); |
||
1179 | |||
1180 | $cm->setPropertyOverride(new Mapping\ManyToOneAssociationMetadata('invalidPropertyName')); |
||
1181 | } |
||
1182 | |||
1183 | /** |
||
1184 | * @group DDC-964 |
||
1185 | * @expectedException \Doctrine\ORM\Mapping\MappingException |
||
1186 | * @expectedExceptionMessage Invalid field override named 'invalidPropertyName' for class 'Doctrine\Tests\Models\DDC964\DDC964Guest'. |
||
1187 | */ |
||
1188 | public function testInvalidPropertyAttributeOverrideNameException() |
||
1189 | { |
||
1190 | $cm = new ClassMetadata(DDC964Guest::class, $this->metadataBuildingContext); |
||
1191 | $cm->setTable(new Mapping\TableMetadata("ddc964_guest")); |
||
1192 | |||
1193 | $fieldMetadata = new Mapping\FieldMetadata('name'); |
||
1194 | $fieldMetadata->setType(Type::getType('string')); |
||
1195 | |||
1196 | $cm->addProperty($fieldMetadata); |
||
1197 | |||
1198 | $fieldMetadata = new Mapping\FieldMetadata('invalidPropertyName'); |
||
1199 | $fieldMetadata->setType(Type::getType('string')); |
||
1200 | |||
1201 | $cm->setPropertyOverride($fieldMetadata); |
||
1202 | } |
||
1203 | |||
1204 | /** |
||
1205 | * @group DDC-1955 |
||
1206 | * |
||
1207 | * @expectedException \Doctrine\ORM\Mapping\MappingException |
||
1208 | * @expectedExceptionMessage Entity Listener "\InvalidClassName" declared on "Doctrine\Tests\Models\CMS\CmsUser" not found. |
||
1209 | */ |
||
1210 | public function testInvalidEntityListenerClassException() |
||
1216 | } |
||
1217 | |||
1218 | /** |
||
1219 | * @group DDC-1955 |
||
1220 | * |
||
1221 | * @expectedException \Doctrine\ORM\Mapping\MappingException |
||
1222 | * @expectedExceptionMessage Entity Listener "Doctrine\Tests\Models\Company\CompanyContractListener" declared on "Doctrine\Tests\Models\CMS\CmsUser" has no method "invalidMethod". |
||
1223 | */ |
||
1224 | public function testInvalidEntityListenerMethodException() |
||
1225 | { |
||
1226 | $cm = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
||
1227 | $cm->setTable(new Mapping\TableMetadata('cms_users')); |
||
1228 | |||
1229 | $cm->addEntityListener(Events::postLoad, 'Doctrine\Tests\Models\Company\CompanyContractListener', 'invalidMethod'); |
||
1230 | } |
||
1231 | |||
1232 | public function testManyToManySelfReferencingNamingStrategyDefaults() |
||
1270 | } |
||
1271 | |||
1272 | /** |
||
1273 | * @group DDC-2662 |
||
1274 | * @group 6682 |
||
1275 | */ |
||
1276 | public function testQuotedSequenceName() : void |
||
1277 | { |
||
1278 | self::markTestIncomplete( |
||
1279 | '@guilhermeblanco, in #6683 we added allocationSize/initialValue as to the sequence definition but with the' |
||
1280 | . ' changes you have made I am not sure if the "initialValue" should still be verified here or if it should' |
||
1281 | . ' part of the metadata drivers' |
||
1282 | ); |
||
1283 | |||
1284 | $cm = new ClassMetadata(CMS\CmsUser::class, $this->metadataBuildingContext); |
||
1285 | $cm->setTable(new Mapping\TableMetadata('cms_users')); |
||
1286 | |||
1287 | $id = new Mapping\FieldMetadata('id'); |
||
1288 | $id->setValueGenerator(new Mapping\ValueGeneratorMetadata( |
||
1289 | Mapping\GeneratorType::SEQUENCE, |
||
1290 | [ |
||
1291 | 'sequenceName' => 'foo', |
||
1292 | 'allocationSize' => 1, |
||
1293 | ] |
||
1294 | )); |
||
1295 | $cm->addProperty($id); |
||
1296 | |||
1297 | self::assertEquals( |
||
1298 | ['sequenceName' => 'foo', 'allocationSize' => 1, 'initialValue' => '1'], |
||
1299 | $cm->getProperty('id')->getValueGenerator()->getDefinition() |
||
1300 | ); |
||
1301 | } |
||
1302 | |||
1303 | /** |
||
1304 | * @group DDC-2700 |
||
1305 | */ |
||
1306 | public function testIsIdentifierMappedSuperClass() |
||
1307 | { |
||
1308 | $class = new ClassMetadata(DDC2700MappedSuperClass::class, $this->metadataBuildingContext); |
||
1309 | |||
1310 | self::assertFalse($class->isIdentifier('foo')); |
||
1311 | } |
||
1312 | |||
1313 | /** |
||
1314 | * @group embedded |
||
1315 | */ |
||
1316 | public function testWakeupReflectionWithEmbeddableAndStaticReflectionService() |
||
1352 | ); |
||
1353 | } |
||
1354 | } |
||
1355 | |||
1356 | /** |
||
1357 | * @ORM\MappedSuperclass |
||
1358 | */ |
||
1359 | class DDC2700MappedSuperClass |
||
1360 | { |
||
1361 | /** @ORM\Column */ |
||
1362 | private $foo; |
||
1363 | } |
||
1364 | |||
1365 | class MyNamespacedNamingStrategy extends DefaultNamingStrategy |
||
1366 | { |
||
1367 | /** |
||
1368 | * {@inheritdoc} |
||
1369 | */ |
||
1370 | public function classToTableName($className) |
||
1371 | { |
||
1372 | if (strpos($className, '\\') !== false) { |
||
1373 | $className = str_replace('\\', '_', str_replace('Doctrine\Tests\Models\\', '', $className)); |
||
1374 | } |
||
1375 | |||
1376 | return strtolower($className); |
||
1377 | } |
||
1378 | } |
||
1379 | |||
1380 | class MyPrefixNamingStrategy extends DefaultNamingStrategy |
||
1381 | { |
||
1382 | /** |
||
1383 | * {@inheritdoc} |
||
1384 | */ |
||
1385 | public function propertyToColumnName($propertyName, $className = null) |
||
1386 | { |
||
1387 | return strtolower($this->classToTableName($className)) . '_' . $propertyName; |
||
1388 | } |
||
1389 | } |
||
1390 |
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.