Completed
Pull Request — master (#6151)
by Andy
09:42
created

ClassMetadataTest::testRemoveFieldMapping()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 20
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\ORM\Mapping;
4
5
use Doctrine\Common\Persistence\Mapping\RuntimeReflectionService;
6
use Doctrine\Common\Persistence\Mapping\StaticReflectionService;
7
use Doctrine\ORM\Mapping\ClassMetadata;
8
use Doctrine\ORM\Events;
9
use Doctrine\ORM\Mapping\DefaultNamingStrategy;
10
use Doctrine\ORM\Mapping\MappingException;
11
use Doctrine\ORM\Mapping\UnderscoreNamingStrategy;
12
use Doctrine\Tests\OrmTestCase;
13
use Doctrine\Tests\Proxies\__CG__\Doctrine\Tests\Models\CMS\CmsUser;
14
15
require_once __DIR__ . '/../../Models/Global/GlobalNamespaceModel.php';
16
17
class ClassMetadataTest extends OrmTestCase
18
{
19
    public function testClassMetadataInstanceSerialization()
20
    {
21
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
22
        $cm->initializeReflection(new RuntimeReflectionService());
23
24
        // Test initial state
25
        $this->assertTrue(count($cm->getReflectionProperties()) == 0);
26
        $this->assertInstanceOf('ReflectionClass', $cm->reflClass);
27
        $this->assertEquals('Doctrine\Tests\Models\CMS\CmsUser', $cm->name);
28
        $this->assertEquals('Doctrine\Tests\Models\CMS\CmsUser', $cm->rootEntityName);
29
        $this->assertEquals(array(), $cm->subClasses);
30
        $this->assertEquals(array(), $cm->parentClasses);
31
        $this->assertEquals(ClassMetadata::INHERITANCE_TYPE_NONE, $cm->inheritanceType);
32
33
        // Customize state
34
        $cm->setInheritanceType(ClassMetadata::INHERITANCE_TYPE_SINGLE_TABLE);
35
        $cm->setSubclasses(array("One", "Two", "Three"));
36
        $cm->setParentClasses(array("UserParent"));
37
        $cm->setCustomRepositoryClass("UserRepository");
38
        $cm->setDiscriminatorColumn(array('name' => 'disc', 'type' => 'integer'));
39
        $cm->mapOneToOne(array('fieldName' => 'phonenumbers', 'targetEntity' => 'CmsAddress', 'mappedBy' => 'foo'));
40
        $cm->markReadOnly();
41
        $cm->addNamedQuery(array('name' => 'dql', 'query' => 'foo'));
42
        $this->assertEquals(1, count($cm->associationMappings));
43
44
        $serialized = serialize($cm);
45
        $cm = unserialize($serialized);
46
        $cm->wakeupReflection(new RuntimeReflectionService());
47
48
        // Check state
49
        $this->assertTrue(count($cm->getReflectionProperties()) > 0);
50
        $this->assertEquals('Doctrine\Tests\Models\CMS', $cm->namespace);
51
        $this->assertInstanceOf('ReflectionClass', $cm->reflClass);
52
        $this->assertEquals('Doctrine\Tests\Models\CMS\CmsUser', $cm->name);
53
        $this->assertEquals('UserParent', $cm->rootEntityName);
54
        $this->assertEquals(array('Doctrine\Tests\Models\CMS\One', 'Doctrine\Tests\Models\CMS\Two', 'Doctrine\Tests\Models\CMS\Three'), $cm->subClasses);
55
        $this->assertEquals(array('UserParent'), $cm->parentClasses);
56
        $this->assertEquals('Doctrine\Tests\Models\CMS\UserRepository', $cm->customRepositoryClassName);
57
        $this->assertEquals(array('name' => 'disc', 'type' => 'integer', 'fieldName' => 'disc'), $cm->discriminatorColumn);
58
        $this->assertTrue($cm->associationMappings['phonenumbers']['type'] == ClassMetadata::ONE_TO_ONE);
59
        $this->assertEquals(1, count($cm->associationMappings));
60
        $oneOneMapping = $cm->getAssociationMapping('phonenumbers');
61
        $this->assertTrue($oneOneMapping['fetch'] == ClassMetadata::FETCH_LAZY);
62
        $this->assertEquals('phonenumbers', $oneOneMapping['fieldName']);
63
        $this->assertEquals('Doctrine\Tests\Models\CMS\CmsAddress', $oneOneMapping['targetEntity']);
64
        $this->assertTrue($cm->isReadOnly);
65
        $this->assertEquals(array('dql' => array('name'=>'dql','query'=>'foo','dql'=>'foo')), $cm->namedQueries);
66
    }
67
68
    public function testFieldIsNullable()
69
    {
70
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
71
        $cm->initializeReflection(new RuntimeReflectionService());
72
73
        // Explicit Nullable
74
        $cm->mapField(array('fieldName' => 'status', 'nullable' => true, 'type' => 'string', 'length' => 50));
75
        $this->assertTrue($cm->isNullable('status'));
76
77
        // Explicit Not Nullable
78
        $cm->mapField(array('fieldName' => 'username', 'nullable' => false, 'type' => 'string', 'length' => 50));
79
        $this->assertFalse($cm->isNullable('username'));
80
81
        // Implicit Not Nullable
82
        $cm->mapField(array('fieldName' => 'name', 'type' => 'string', 'length' => 50));
83
        $this->assertFalse($cm->isNullable('name'), "By default a field should not be nullable.");
84
    }
85
86
    /**
87
     * @group DDC-115
88
     */
89
    public function testMapAssociationInGlobalNamespace()
90
    {
91
        require_once __DIR__."/../../Models/Global/GlobalNamespaceModel.php";
92
93
        $cm = new ClassMetadata('DoctrineGlobal_Article');
94
        $cm->initializeReflection(new RuntimeReflectionService());
95
        $cm->mapManyToMany(array(
96
            'fieldName' => 'author',
97
            'targetEntity' => 'DoctrineGlobal_User',
98
            'joinTable' => array(
99
                'name' => 'bar',
100
                'joinColumns' => array(array('name' => 'bar_id', 'referencedColumnName' => 'id')),
101
                'inverseJoinColumns' => array(array('name' => 'baz_id', 'referencedColumnName' => 'id')),
102
            ),
103
        ));
104
105
        $this->assertEquals("DoctrineGlobal_User", $cm->associationMappings['author']['targetEntity']);
106
    }
107
108
    public function testMapManyToManyJoinTableDefaults()
109
    {
110
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
111
        $cm->initializeReflection(new RuntimeReflectionService());
112
        $cm->mapManyToMany(
113
            array(
114
            'fieldName' => 'groups',
115
            'targetEntity' => 'CmsGroup'
116
        ));
117
118
        $assoc = $cm->associationMappings['groups'];
119
        //$this->assertInstanceOf('Doctrine\ORM\Mapping\ManyToManyMapping', $assoc);
120
        $this->assertEquals(array(
121
            'name' => 'cmsuser_cmsgroup',
122
            'joinColumns' => array(array('name' => 'cmsuser_id', 'referencedColumnName' => 'id', 'onDelete' => 'CASCADE')),
123
            'inverseJoinColumns' => array(array('name' => 'cmsgroup_id', 'referencedColumnName' => 'id', 'onDelete' => 'CASCADE'))
124
        ), $assoc['joinTable']);
125
        $this->assertTrue($assoc['isOnDeleteCascade']);
126
    }
127
128
    public function testSerializeManyToManyJoinTableCascade()
129
    {
130
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
131
        $cm->initializeReflection(new RuntimeReflectionService());
132
        $cm->mapManyToMany(
133
            array(
134
            'fieldName' => 'groups',
135
            'targetEntity' => 'CmsGroup'
136
        ));
137
138
        /* @var $assoc \Doctrine\ORM\Mapping\ManyToManyMapping */
139
        $assoc = $cm->associationMappings['groups'];
140
        $assoc = unserialize(serialize($assoc));
141
142
        $this->assertTrue($assoc['isOnDeleteCascade']);
143
    }
144
145
    /**
146
     * @group DDC-115
147
     */
148
    public function testSetDiscriminatorMapInGlobalNamespace()
149
    {
150
        require_once __DIR__."/../../Models/Global/GlobalNamespaceModel.php";
151
152
        $cm = new ClassMetadata('DoctrineGlobal_User');
153
        $cm->initializeReflection(new RuntimeReflectionService());
154
        $cm->setDiscriminatorMap(array('descr' => 'DoctrineGlobal_Article', 'foo' => 'DoctrineGlobal_User'));
155
156
        $this->assertEquals("DoctrineGlobal_Article", $cm->discriminatorMap['descr']);
157
        $this->assertEquals("DoctrineGlobal_User", $cm->discriminatorMap['foo']);
158
    }
159
160
    /**
161
     * @group DDC-115
162
     */
163
    public function testSetSubClassesInGlobalNamespace()
164
    {
165
        require_once __DIR__."/../../Models/Global/GlobalNamespaceModel.php";
166
167
        $cm = new ClassMetadata('DoctrineGlobal_User');
168
        $cm->initializeReflection(new RuntimeReflectionService());
169
        $cm->setSubclasses(array('DoctrineGlobal_Article'));
170
171
        $this->assertEquals("DoctrineGlobal_Article", $cm->subClasses[0]);
172
    }
173
174
    /**
175
     * @group DDC-268
176
     */
177
    public function testSetInvalidVersionMapping_ThrowsException()
178
    {
179
        $field = array();
180
        $field['fieldName'] = 'foo';
181
        $field['type'] = 'string';
182
183
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
184
        $cm->initializeReflection(new RuntimeReflectionService());
185
186
        $this->expectException(\Doctrine\ORM\Mapping\MappingException::class);
187
        $cm->setVersionMapping($field);
188
    }
189
190
    public function testGetSingleIdentifierFieldName_MultipleIdentifierEntity_ThrowsException()
191
    {
192
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
193
        $cm->initializeReflection(new RuntimeReflectionService());
194
        $cm->isIdentifierComposite  = true;
195
196
        $this->expectException(\Doctrine\ORM\Mapping\MappingException::class);
197
        $cm->getSingleIdentifierFieldName();
198
    }
199
200
    public function testDuplicateAssociationMappingException()
201
    {
202
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
203
        $cm->initializeReflection(new RuntimeReflectionService());
204
205
        $a1 = array('fieldName' => 'foo', 'sourceEntity' => 'stdClass', 'targetEntity' => 'stdClass', 'mappedBy' => 'foo');
206
        $a2 = array('fieldName' => 'foo', 'sourceEntity' => 'stdClass', 'targetEntity' => 'stdClass', 'mappedBy' => 'foo');
207
208
        $cm->addInheritedAssociationMapping($a1);
209
        $this->expectException(\Doctrine\ORM\Mapping\MappingException::class);
210
        $cm->addInheritedAssociationMapping($a2);
211
    }
212
213
    public function testDuplicateColumnName_ThrowsMappingException()
214
    {
215
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
216
        $cm->initializeReflection(new RuntimeReflectionService());
217
218
        $cm->mapField(array('fieldName' => 'name', 'columnName' => 'name'));
219
220
        $this->expectException(\Doctrine\ORM\Mapping\MappingException::class);
221
        $cm->mapField(array('fieldName' => 'username', 'columnName' => 'name'));
222
    }
223
224
    public function testDuplicateColumnName_DiscriminatorColumn_ThrowsMappingException()
225
    {
226
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
227
        $cm->initializeReflection(new RuntimeReflectionService());
228
229
        $cm->mapField(array('fieldName' => 'name', 'columnName' => 'name'));
230
231
        $this->expectException(\Doctrine\ORM\Mapping\MappingException::class);
232
        $cm->setDiscriminatorColumn(array('name' => 'name'));
233
    }
234
235
    public function testDuplicateColumnName_DiscriminatorColumn2_ThrowsMappingException()
236
    {
237
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
238
        $cm->initializeReflection(new RuntimeReflectionService());
239
240
        $cm->setDiscriminatorColumn(array('name' => 'name'));
241
242
        $this->expectException(\Doctrine\ORM\Mapping\MappingException::class);
243
        $cm->mapField(array('fieldName' => 'name', 'columnName' => 'name'));
244
    }
245
246
    public function testDuplicateFieldAndAssociationMapping1_ThrowsException()
247
    {
248
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
249
        $cm->initializeReflection(new RuntimeReflectionService());
250
251
        $cm->mapField(array('fieldName' => 'name', 'columnName' => 'name'));
252
253
        $this->expectException(\Doctrine\ORM\Mapping\MappingException::class);
254
        $cm->mapOneToOne(array('fieldName' => 'name', 'targetEntity' => 'CmsUser'));
255
    }
256
257
    public function testDuplicateFieldAndAssociationMapping2_ThrowsException()
258
    {
259
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
260
        $cm->initializeReflection(new RuntimeReflectionService());
261
262
        $cm->mapOneToOne(array('fieldName' => 'name', 'targetEntity' => 'CmsUser'));
263
264
        $this->expectException(\Doctrine\ORM\Mapping\MappingException::class);
265
        $cm->mapField(array('fieldName' => 'name', 'columnName' => 'name'));
266
    }
267
268
    /**
269
     * @group DDC-1224
270
     */
271
    public function testGetTemporaryTableNameSchema()
272
    {
273
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
274
        $cm->initializeReflection(new RuntimeReflectionService());
275
276
        $cm->setTableName('foo.bar');
277
278
        $this->assertEquals('foo_bar_id_tmp', $cm->getTemporaryIdTableName());
279
    }
280
281
    public function testDefaultTableName()
282
    {
283
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
284
        $cm->initializeReflection(new RuntimeReflectionService());
285
286
        // When table's name is not given
287
        $primaryTable = array();
288
        $cm->setPrimaryTable($primaryTable);
289
290
        $this->assertEquals('CmsUser', $cm->getTableName());
291
        $this->assertEquals('CmsUser', $cm->table['name']);
292
293
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress');
294
        $cm->initializeReflection(new RuntimeReflectionService());
295
        // When joinTable's name is not given
296
        $cm->mapManyToMany(array(
297
            'fieldName' => 'user',
298
            'targetEntity' => 'CmsUser',
299
            'inversedBy' => 'users',
300
            'joinTable' => array('joinColumns' => array(array('referencedColumnName' => 'id')),
301
                                 'inverseJoinColumns' => array(array('referencedColumnName' => 'id')))));
302
        $this->assertEquals('cmsaddress_cmsuser', $cm->associationMappings['user']['joinTable']['name']);
303
    }
304
305
    public function testDefaultJoinColumnName()
306
    {
307
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress');
308
        $cm->initializeReflection(new RuntimeReflectionService());
309
310
        // this is really dirty, but it's the simplest way to test whether
311
        // joinColumn's name will be automatically set to user_id
312
        $cm->mapOneToOne(array(
313
            'fieldName' => 'user',
314
            'targetEntity' => 'CmsUser',
315
            'joinColumns' => array(array('referencedColumnName' => 'id'))));
316
        $this->assertEquals('user_id', $cm->associationMappings['user']['joinColumns'][0]['name']);
317
318
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress');
319
        $cm->initializeReflection(new RuntimeReflectionService());
320
        $cm->mapManyToMany(array(
321
            'fieldName' => 'user',
322
            'targetEntity' => 'CmsUser',
323
            'inversedBy' => 'users',
324
            'joinTable' => array('name' => 'user_CmsUser',
325
                                'joinColumns' => array(array('referencedColumnName' => 'id')),
326
                                'inverseJoinColumns' => array(array('referencedColumnName' => 'id')))));
327
        $this->assertEquals('cmsaddress_id', $cm->associationMappings['user']['joinTable']['joinColumns'][0]['name']);
328
        $this->assertEquals('cmsuser_id', $cm->associationMappings['user']['joinTable']['inverseJoinColumns'][0]['name']);
329
    }
330
331
    /**
332
     * @group DDC-559
333
     */
334
    public function testUnderscoreNamingStrategyDefaults()
335
    {
336
        $namingStrategy     = new UnderscoreNamingStrategy(CASE_UPPER);
337
        $oneToOneMetadata   = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress', $namingStrategy);
338
        $manyToManyMetadata = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress', $namingStrategy);
339
340
        $oneToOneMetadata->mapOneToOne(array(
341
            'fieldName'     => 'user',
342
            'targetEntity'  => 'CmsUser'
343
        ));
344
345
        $manyToManyMetadata->mapManyToMany(array(
346
            'fieldName'     => 'user',
347
            'targetEntity'  => 'CmsUser'
348
        ));
349
350
        $this->assertEquals(array('USER_ID'=>'ID'), $oneToOneMetadata->associationMappings['user']['sourceToTargetKeyColumns']);
351
        $this->assertEquals(array('USER_ID'=>'USER_ID'), $oneToOneMetadata->associationMappings['user']['joinColumnFieldNames']);
352
        $this->assertEquals(array('ID'=>'USER_ID'), $oneToOneMetadata->associationMappings['user']['targetToSourceKeyColumns']);
353
354
        $this->assertEquals('USER_ID', $oneToOneMetadata->associationMappings['user']['joinColumns'][0]['name']);
355
        $this->assertEquals('ID', $oneToOneMetadata->associationMappings['user']['joinColumns'][0]['referencedColumnName']);
356
357
358
        $this->assertEquals('CMS_ADDRESS_CMS_USER', $manyToManyMetadata->associationMappings['user']['joinTable']['name']);
359
360
        $this->assertEquals(array('CMS_ADDRESS_ID','CMS_USER_ID'), $manyToManyMetadata->associationMappings['user']['joinTableColumns']);
361
        $this->assertEquals(array('CMS_ADDRESS_ID'=>'ID'), $manyToManyMetadata->associationMappings['user']['relationToSourceKeyColumns']);
362
        $this->assertEquals(array('CMS_USER_ID'=>'ID'), $manyToManyMetadata->associationMappings['user']['relationToTargetKeyColumns']);
363
364
        $this->assertEquals('CMS_ADDRESS_ID', $manyToManyMetadata->associationMappings['user']['joinTable']['joinColumns'][0]['name']);
365
        $this->assertEquals('CMS_USER_ID', $manyToManyMetadata->associationMappings['user']['joinTable']['inverseJoinColumns'][0]['name']);
366
367
        $this->assertEquals('ID', $manyToManyMetadata->associationMappings['user']['joinTable']['joinColumns'][0]['referencedColumnName']);
368
        $this->assertEquals('ID', $manyToManyMetadata->associationMappings['user']['joinTable']['inverseJoinColumns'][0]['referencedColumnName']);
369
370
371
        $cm = new ClassMetadata('DoctrineGlobal_Article', $namingStrategy);
372
        $cm->mapManyToMany(array('fieldName' => 'author', 'targetEntity' => 'Doctrine\Tests\Models\CMS\CmsUser'));
373
        $this->assertEquals('DOCTRINE_GLOBAL_ARTICLE_CMS_USER', $cm->associationMappings['author']['joinTable']['name']);
374
    }
375
376
    /**
377
     * @group DDC-886
378
     */
379
    public function testSetMultipleIdentifierSetsComposite()
380
    {
381
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
382
        $cm->initializeReflection(new RuntimeReflectionService());
383
384
        $cm->mapField(array('fieldName' => 'name'));
385
        $cm->mapField(array('fieldName' => 'username'));
386
387
        $cm->setIdentifier(array('name', 'username'));
388
        $this->assertTrue($cm->isIdentifierComposite);
389
    }
390
391
    /**
392
     * @group DDC-944
393
     */
394
    public function testMappingNotFound()
395
    {
396
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
397
        $cm->initializeReflection(new RuntimeReflectionService());
398
399
        $this->expectException(MappingException::class);
400
        $this->expectExceptionMessage("No mapping found for field 'foo' on class 'Doctrine\Tests\Models\CMS\CmsUser'.");
401
402
        $cm->getFieldMapping('foo');
403
    }
404
405
    /**
406
     * @group DDC-961
407
     */
408
    public function testJoinTableMappingDefaults()
409
    {
410
        $cm = new ClassMetadata('DoctrineGlobal_Article');
411
        $cm->initializeReflection(new RuntimeReflectionService());
412
413
        $cm->mapManyToMany(array('fieldName' => 'author', 'targetEntity' => 'Doctrine\Tests\Models\CMS\CmsUser'));
414
415
        $this->assertEquals('doctrineglobal_article_cmsuser', $cm->associationMappings['author']['joinTable']['name']);
416
    }
417
418
    /**
419
     * @group DDC-117
420
     */
421
    public function testMapIdentifierAssociation()
422
    {
423
        $cm = new ClassMetadata('Doctrine\Tests\Models\DDC117\DDC117ArticleDetails');
424
        $cm->initializeReflection(new RuntimeReflectionService());
425
426
        $cm->mapOneToOne(array(
427
            'fieldName' => 'article',
428
            'id' => true,
429
            'targetEntity' => 'Doctrine\Tests\Models\DDC117\DDC117Article',
430
            'joinColumns' => array(),
431
        ));
432
433
        $this->assertTrue($cm->containsForeignIdentifier, "Identifier Association should set 'containsForeignIdentifier' boolean flag.");
434
        $this->assertEquals(array("article"), $cm->identifier);
435
    }
436
437
    /**
438
     * @group DDC-117
439
     */
440
    public function testOrphanRemovalIdentifierAssociation()
441
    {
442
        $cm = new ClassMetadata('Doctrine\Tests\Models\DDC117\DDC117ArticleDetails');
443
        $cm->initializeReflection(new RuntimeReflectionService());
444
445
        $this->expectException(MappingException::class);
446
        $this->expectExceptionMessage('The orphan removal option is not allowed on an association that');
447
448
        $cm->mapOneToOne(array(
449
            'fieldName' => 'article',
450
            'id' => true,
451
            'targetEntity' => 'Doctrine\Tests\Models\DDC117\DDC117Article',
452
            'orphanRemoval' => true,
453
            'joinColumns' => array(),
454
        ));
455
    }
456
457
    /**
458
     * @group DDC-117
459
     */
460
    public function testInverseIdentifierAssociation()
461
    {
462
        $cm = new ClassMetadata('Doctrine\Tests\Models\DDC117\DDC117ArticleDetails');
463
        $cm->initializeReflection(new RuntimeReflectionService());
464
465
        $this->expectException(MappingException::class);
466
        $this->expectExceptionMessage('An inverse association is not allowed to be identifier in');
467
468
        $cm->mapOneToOne(array(
469
            'fieldName' => 'article',
470
            'id' => true,
471
            'mappedBy' => 'details', // INVERSE!
472
            'targetEntity' => 'Doctrine\Tests\Models\DDC117\DDC117Article',
473
            'joinColumns' => array(),
474
        ));
475
    }
476
477
    /**
478
     * @group DDC-117
479
     */
480
    public function testIdentifierAssociationManyToMany()
481
    {
482
        $cm = new ClassMetadata('Doctrine\Tests\Models\DDC117\DDC117ArticleDetails');
483
        $cm->initializeReflection(new RuntimeReflectionService());
484
485
        $this->expectException(MappingException::class);
486
        $this->expectExceptionMessage('Many-to-many or one-to-many associations are not allowed to be identifier in');
487
488
        $cm->mapManyToMany(array(
489
            'fieldName' => 'article',
490
            'id' => true,
491
            'targetEntity' => 'Doctrine\Tests\Models\DDC117\DDC117Article',
492
            'joinColumns' => array(),
493
        ));
494
    }
495
496
    /**
497
     * @group DDC-996
498
     */
499
    public function testEmptyFieldNameThrowsException()
500
    {
501
        $this->expectException(MappingException::class);
502
        $this->expectExceptionMessage("The field or association mapping misses the 'fieldName' attribute in entity 'Doctrine\Tests\Models\CMS\CmsUser'.");
503
504
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
505
        $cm->initializeReflection(new RuntimeReflectionService());
506
507
        $cm->mapField(array('fieldName' => ''));
508
    }
509
510
    public function testRetrievalOfNamedQueries()
511
    {
512
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
513
        $cm->initializeReflection(new RuntimeReflectionService());
514
515
516
        $this->assertEquals(0, count($cm->getNamedQueries()));
517
518
        $cm->addNamedQuery(array(
519
            'name'  => 'userById',
520
            'query' => 'SELECT u FROM __CLASS__ u WHERE u.id = ?1'
521
        ));
522
523
        $this->assertEquals(1, count($cm->getNamedQueries()));
524
    }
525
526
    /**
527
     * @group DDC-1663
528
     */
529
    public function testRetrievalOfResultSetMappings()
530
    {
531
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
532
        $cm->initializeReflection(new RuntimeReflectionService());
533
534
535
        $this->assertEquals(0, count($cm->getSqlResultSetMappings()));
536
537
        $cm->addSqlResultSetMapping(array(
538
            'name'      => 'find-all',
539
            'entities'  => array(
540
                array(
541
                    'entityClass'   => 'Doctrine\Tests\Models\CMS\CmsUser',
542
                ),
543
            ),
544
        ));
545
546
        $this->assertEquals(1, count($cm->getSqlResultSetMappings()));
547
    }
548
549
    public function testExistanceOfNamedQuery()
550
    {
551
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
552
        $cm->initializeReflection(new RuntimeReflectionService());
553
554
555
        $cm->addNamedQuery(array(
556
            'name'  => 'all',
557
            'query' => 'SELECT u FROM __CLASS__ u'
558
        ));
559
560
        $this->assertTrue($cm->hasNamedQuery('all'));
561
        $this->assertFalse($cm->hasNamedQuery('userById'));
562
    }
563
564
    /**
565
     * @group DDC-1663
566
     */
567
    public function testRetrieveOfNamedNativeQuery()
568
    {
569
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
570
        $cm->initializeReflection(new RuntimeReflectionService());
571
572
        $cm->addNamedNativeQuery(array(
573
            'name'              => 'find-all',
574
            'query'             => 'SELECT * FROM cms_users',
575
            'resultSetMapping'  => 'result-mapping-name',
576
            'resultClass'       => 'Doctrine\Tests\Models\CMS\CmsUser',
577
        ));
578
579
        $cm->addNamedNativeQuery(array(
580
            'name'              => 'find-by-id',
581
            'query'             => 'SELECT * FROM cms_users WHERE id = ?',
582
            'resultClass'       => '__CLASS__',
583
            'resultSetMapping'  => 'result-mapping-name',
584
        ));
585
586
        $mapping = $cm->getNamedNativeQuery('find-all');
587
        $this->assertEquals('SELECT * FROM cms_users', $mapping['query']);
588
        $this->assertEquals('result-mapping-name', $mapping['resultSetMapping']);
589
        $this->assertEquals('Doctrine\Tests\Models\CMS\CmsUser', $mapping['resultClass']);
590
591
        $mapping = $cm->getNamedNativeQuery('find-by-id');
592
        $this->assertEquals('SELECT * FROM cms_users WHERE id = ?', $mapping['query']);
593
        $this->assertEquals('result-mapping-name', $mapping['resultSetMapping']);
594
        $this->assertEquals('Doctrine\Tests\Models\CMS\CmsUser', $mapping['resultClass']);
595
    }
596
597
    /**
598
     * @group DDC-1663
599
     */
600
    public function testRetrieveOfSqlResultSetMapping()
601
    {
602
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
603
        $cm->initializeReflection(new RuntimeReflectionService());
604
605
        $cm->addSqlResultSetMapping(array(
606
            'name'      => 'find-all',
607
            'entities'  => array(
608
                array(
609
                    'entityClass'   => '__CLASS__',
610
                    'fields'        => array(
611
                        array(
612
                            'name'  => 'id',
613
                            'column'=> 'id'
614
                        ),
615
                        array(
616
                            'name'  => 'name',
617
                            'column'=> 'name'
618
                        )
619
                    )
620
                ),
621
                array(
622
                    'entityClass'   => 'Doctrine\Tests\Models\CMS\CmsEmail',
623
                    'fields'        => array(
624
                        array(
625
                            'name'  => 'id',
626
                            'column'=> 'id'
627
                        ),
628
                        array(
629
                            'name'  => 'email',
630
                            'column'=> 'email'
631
                        )
632
                    )
633
                )
634
            ),
635
            'columns'   => array(
636
                array(
637
                    'name' => 'scalarColumn'
638
                )
639
            )
640
        ));
641
642
        $mapping = $cm->getSqlResultSetMapping('find-all');
643
644
        $this->assertEquals('Doctrine\Tests\Models\CMS\CmsUser', $mapping['entities'][0]['entityClass']);
645
        $this->assertEquals(array('name'=>'id','column'=>'id'), $mapping['entities'][0]['fields'][0]);
646
        $this->assertEquals(array('name'=>'name','column'=>'name'), $mapping['entities'][0]['fields'][1]);
647
648
        $this->assertEquals('Doctrine\Tests\Models\CMS\CmsEmail', $mapping['entities'][1]['entityClass']);
649
        $this->assertEquals(array('name'=>'id','column'=>'id'), $mapping['entities'][1]['fields'][0]);
650
        $this->assertEquals(array('name'=>'email','column'=>'email'), $mapping['entities'][1]['fields'][1]);
651
652
        $this->assertEquals('scalarColumn', $mapping['columns'][0]['name']);
653
    }
654
655
    /**
656
     * @group DDC-1663
657
     */
658
    public function testExistanceOfSqlResultSetMapping()
659
    {
660
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
661
        $cm->initializeReflection(new RuntimeReflectionService());
662
663
        $cm->addSqlResultSetMapping(array(
664
            'name'      => 'find-all',
665
            'entities'  => array(
666
                array(
667
                    'entityClass'   => 'Doctrine\Tests\Models\CMS\CmsUser',
668
                ),
669
            ),
670
        ));
671
672
        $this->assertTrue($cm->hasSqlResultSetMapping('find-all'));
673
        $this->assertFalse($cm->hasSqlResultSetMapping('find-by-id'));
674
    }
675
676
    /**
677
     * @group DDC-1663
678
     */
679
    public function testExistanceOfNamedNativeQuery()
680
    {
681
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
682
        $cm->initializeReflection(new RuntimeReflectionService());
683
684
685
        $cm->addNamedNativeQuery(array(
686
            'name'              => 'find-all',
687
            'query'             => 'SELECT * FROM cms_users',
688
            'resultClass'       => 'Doctrine\Tests\Models\CMS\CmsUser',
689
            'resultSetMapping'  => 'result-mapping-name'
690
        ));
691
692
        $this->assertTrue($cm->hasNamedNativeQuery('find-all'));
693
        $this->assertFalse($cm->hasNamedNativeQuery('find-by-id'));
694
    }
695
696
    public function testRetrieveOfNamedQuery()
697
    {
698
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
699
        $cm->initializeReflection(new RuntimeReflectionService());
700
701
702
        $cm->addNamedQuery(array(
703
            'name'  => 'userById',
704
            'query' => 'SELECT u FROM __CLASS__ u WHERE u.id = ?1'
705
        ));
706
707
        $this->assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1', $cm->getNamedQuery('userById'));
708
    }
709
710
    /**
711
     * @group DDC-1663
712
     */
713
    public function testRetrievalOfNamedNativeQueries()
714
    {
715
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
716
        $cm->initializeReflection(new RuntimeReflectionService());
717
718
        $this->assertEquals(0, count($cm->getNamedNativeQueries()));
719
720
        $cm->addNamedNativeQuery(array(
721
            'name'              => 'find-all',
722
            'query'             => 'SELECT * FROM cms_users',
723
            'resultClass'       => 'Doctrine\Tests\Models\CMS\CmsUser',
724
            'resultSetMapping'  => 'result-mapping-name'
725
        ));
726
727
        $this->assertEquals(1, count($cm->getNamedNativeQueries()));
728
    }
729
730
    /**
731
     * @group DDC-2451
732
     */
733
    public function testSerializeEntityListeners()
734
    {
735
        $metadata = new ClassMetadata('Doctrine\Tests\Models\Company\CompanyContract');
736
737
        $metadata->initializeReflection(new RuntimeReflectionService());
738
        $metadata->addEntityListener(Events::prePersist, 'CompanyContractListener', 'prePersistHandler');
739
        $metadata->addEntityListener(Events::postPersist, 'CompanyContractListener', 'postPersistHandler');
740
741
        $serialize   = serialize($metadata);
742
        $unserialize = unserialize($serialize);
743
744
        $this->assertEquals($metadata->entityListeners, $unserialize->entityListeners);
745
    }
746
747
    /**
748
     * @expectedException \Doctrine\ORM\Mapping\MappingException
749
     * @expectedExceptionMessage Query named "userById" in "Doctrine\Tests\Models\CMS\CmsUser" was already declared, but it must be declared only once
750
     */
751
    public function testNamingCollisionNamedQueryShouldThrowException()
752
    {
753
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
754
        $cm->initializeReflection(new RuntimeReflectionService());
755
756
        $cm->addNamedQuery(array(
757
            'name'  => 'userById',
758
            'query' => 'SELECT u FROM __CLASS__ u WHERE u.id = ?1'
759
        ));
760
761
        $cm->addNamedQuery(array(
762
            'name'  => 'userById',
763
            'query' => 'SELECT u FROM __CLASS__ u WHERE u.id = ?1'
764
        ));
765
    }
766
767
    /**
768
     * @group DDC-1663
769
     *
770
     * @expectedException \Doctrine\ORM\Mapping\MappingException
771
     * @expectedExceptionMessage Query named "find-all" in "Doctrine\Tests\Models\CMS\CmsUser" was already declared, but it must be declared only once
772
     */
773
    public function testNamingCollisionNamedNativeQueryShouldThrowException()
774
    {
775
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
776
        $cm->initializeReflection(new RuntimeReflectionService());
777
778
        $cm->addNamedNativeQuery(array(
779
            'name'              => 'find-all',
780
            'query'             => 'SELECT * FROM cms_users',
781
            'resultClass'       => 'Doctrine\Tests\Models\CMS\CmsUser',
782
            'resultSetMapping'  => 'result-mapping-name'
783
        ));
784
785
        $cm->addNamedNativeQuery(array(
786
            'name'              => 'find-all',
787
            'query'             => 'SELECT * FROM cms_users',
788
            'resultClass'       => 'Doctrine\Tests\Models\CMS\CmsUser',
789
            'resultSetMapping'  => 'result-mapping-name'
790
        ));
791
    }
792
793
    /**
794
     * @group DDC-1663
795
     *
796
     * @expectedException \Doctrine\ORM\Mapping\MappingException
797
     * @expectedExceptionMessage Result set mapping named "find-all" in "Doctrine\Tests\Models\CMS\CmsUser" was already declared, but it must be declared only once
798
     */
799
    public function testNamingCollisionSqlResultSetMappingShouldThrowException()
800
    {
801
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
802
        $cm->initializeReflection(new RuntimeReflectionService());
803
804
        $cm->addSqlResultSetMapping(array(
805
            'name'      => 'find-all',
806
            'entities'  => array(
807
                array(
808
                    'entityClass'   => 'Doctrine\Tests\Models\CMS\CmsUser',
809
                ),
810
            ),
811
        ));
812
813
        $cm->addSqlResultSetMapping(array(
814
            'name'      => 'find-all',
815
            'entities'  => array(
816
                array(
817
                    'entityClass'   => 'Doctrine\Tests\Models\CMS\CmsUser',
818
                ),
819
            ),
820
        ));
821
    }
822
823
    /**
824
     * @group DDC-1068
825
     */
826
    public function testClassCaseSensitivity()
827
    {
828
        $user = new CmsUser();
829
        $cm = new ClassMetadata('DOCTRINE\TESTS\MODELS\CMS\CMSUSER');
830
        $cm->initializeReflection(new RuntimeReflectionService());
831
832
        $this->assertEquals('Doctrine\Tests\Models\CMS\CmsUser', $cm->name);
833
    }
834
835
    /**
836
     * @group DDC-659
837
     */
838
    public function testLifecycleCallbackNotFound()
839
    {
840
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
841
        $cm->initializeReflection(new RuntimeReflectionService());
842
        $cm->addLifecycleCallback('notfound', 'postLoad');
843
844
        $this->expectException(MappingException::class);
845
        $this->expectExceptionMessage("Entity 'Doctrine\Tests\Models\CMS\CmsUser' has no method 'notfound' to be registered as lifecycle callback.");
846
847
        $cm->validateLifecycleCallbacks(new RuntimeReflectionService());
848
    }
849
850
    /**
851
     * @group ImproveErrorMessages
852
     */
853
    public function testTargetEntityNotFound()
854
    {
855
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
856
        $cm->initializeReflection(new RuntimeReflectionService());
857
        $cm->mapManyToOne(array('fieldName' => 'address', 'targetEntity' => 'UnknownClass'));
858
859
        $this->expectException(MappingException::class);
860
        $this->expectExceptionMessage("The target-entity Doctrine\Tests\Models\CMS\UnknownClass cannot be found in 'Doctrine\Tests\Models\CMS\CmsUser#address'.");
861
862
        $cm->validateAssociations();
863
    }
864
865
    /**
866
     * @group DDC-1663
867
     *
868
     * @expectedException \Doctrine\ORM\Mapping\MappingException
869
     * @expectedExceptionMessage Query name on entity class 'Doctrine\Tests\Models\CMS\CmsUser' is not defined.
870
     */
871
    public function testNameIsMandatoryForNamedQueryMappingException()
872
    {
873
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
874
        $cm->initializeReflection(new RuntimeReflectionService());
875
        $cm->addNamedQuery(array(
876
            'query' => 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u',
877
        ));
878
    }
879
880
    /**
881
     * @group DDC-1663
882
     *
883
     * @expectedException \Doctrine\ORM\Mapping\MappingException
884
     * @expectedExceptionMessage Query name on entity class 'Doctrine\Tests\Models\CMS\CmsUser' is not defined.
885
     */
886
    public function testNameIsMandatoryForNameNativeQueryMappingException()
887
    {
888
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
889
        $cm->initializeReflection(new RuntimeReflectionService());
890
        $cm->addNamedQuery(array(
891
            'query'             => 'SELECT * FROM cms_users',
892
            'resultClass'       => 'Doctrine\Tests\Models\CMS\CmsUser',
893
            'resultSetMapping'  => 'result-mapping-name'
894
        ));
895
    }
896
897
    /**
898
     * @group DDC-1663
899
     *
900
     * @expectedException \Doctrine\ORM\Mapping\MappingException
901
     * @expectedExceptionMessage Result set mapping named "find-all" in "Doctrine\Tests\Models\CMS\CmsUser requires a entity class name.
902
     */
903
    public function testNameIsMandatoryForEntityNameSqlResultSetMappingException()
904
    {
905
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
906
        $cm->initializeReflection(new RuntimeReflectionService());
907
        $cm->addSqlResultSetMapping(array(
908
            'name'      => 'find-all',
909
            'entities'  => array(
910
                array(
911
                    'fields' => array()
912
                )
913
            ),
914
        ));
915
    }
916
917
    /**
918
     * @expectedException \Doctrine\ORM\Mapping\MappingException
919
     * @expectedExceptionMessage Discriminator column name on entity class 'Doctrine\Tests\Models\CMS\CmsUser' is not defined.
920
     */
921
    public function testNameIsMandatoryForDiscriminatorColumnsMappingException()
922
    {
923
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
924
        $cm->initializeReflection(new RuntimeReflectionService());
925
        $cm->setDiscriminatorColumn(array());
926
    }
927
928
    /**
929
     * @group DDC-984
930
     * @group DDC-559
931
     * @group DDC-1575
932
     */
933
    public function testFullyQualifiedClassNameShouldBeGivenToNamingStrategy()
934
    {
935
        $namingStrategy     = new MyNamespacedNamingStrategy();
936
        $addressMetadata    = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress', $namingStrategy);
937
        $articleMetadata    = new ClassMetadata('DoctrineGlobal_Article', $namingStrategy);
938
        $routingMetadata    = new ClassMetadata('Doctrine\Tests\Models\Routing\RoutingLeg',$namingStrategy);
939
940
        $addressMetadata->initializeReflection(new RuntimeReflectionService());
941
        $articleMetadata->initializeReflection(new RuntimeReflectionService());
942
        $routingMetadata->initializeReflection(new RuntimeReflectionService());
943
944
        $addressMetadata->mapManyToMany(array(
945
            'fieldName'     => 'user',
946
            'targetEntity'  => 'CmsUser'
947
        ));
948
949
        $articleMetadata->mapManyToMany(array(
950
            'fieldName'     => 'author',
951
            'targetEntity'  => 'Doctrine\Tests\Models\CMS\CmsUser'
952
        ));
953
954
        $this->assertEquals('routing_routingleg', $routingMetadata->table['name']);
955
        $this->assertEquals('cms_cmsaddress_cms_cmsuser', $addressMetadata->associationMappings['user']['joinTable']['name']);
956
        $this->assertEquals('doctrineglobal_article_cms_cmsuser', $articleMetadata->associationMappings['author']['joinTable']['name']);
957
    }
958
959
    /**
960
     * @group DDC-984
961
     * @group DDC-559
962
     */
963
    public function testFullyQualifiedClassNameShouldBeGivenToNamingStrategyPropertyToColumnName()
964
    {
965
        $namingStrategy = new MyPrefixNamingStrategy();
966
        $metadata       = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress', $namingStrategy);
967
968
        $metadata->initializeReflection(new RuntimeReflectionService());
969
970
        $metadata->mapField(array('fieldName'=>'country'));
971
        $metadata->mapField(array('fieldName'=>'city'));
972
973
        $this->assertEquals($metadata->fieldNames, array(
974
            'cmsaddress_country'   => 'country',
975
            'cmsaddress_city'      => 'city'
976
        ));
977
    }
978
979
    /**
980
     * @group DDC-1746
981
     */
982
    public function testInvalidCascade()
983
    {
984
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
985
        $cm->initializeReflection(new RuntimeReflectionService());
986
987
        $this->expectException(MappingException::class);
988
        $this->expectExceptionMessage("You have specified invalid cascade options for Doctrine\Tests\Models\CMS\CmsUser::\$address: 'invalid'; available options: 'remove', 'persist', 'refresh', 'merge', and 'detach'");
989
990
        $cm->mapManyToOne(array('fieldName' => 'address', 'targetEntity' => 'UnknownClass', 'cascade' => array('invalid')));
991
     }
992
993
    /**
994
     * @group DDC-964
995
     * @expectedException        Doctrine\ORM\Mapping\MappingException
996
     * @expectedExceptionMessage Invalid field override named 'invalidPropertyName' for class 'Doctrine\Tests\Models\DDC964\DDC964Admin
997
     */
998
    public function testInvalidPropertyAssociationOverrideNameException()
999
    {
1000
        $cm = new ClassMetadata('Doctrine\Tests\Models\DDC964\DDC964Admin');
1001
        $cm->initializeReflection(new RuntimeReflectionService());
1002
        $cm->mapManyToOne(array('fieldName' => 'address', 'targetEntity' => 'DDC964Address'));
1003
1004
        $cm->setAssociationOverride('invalidPropertyName', array());
1005
    }
1006
1007
    /**
1008
     * @group DDC-964
1009
     * @expectedException        Doctrine\ORM\Mapping\MappingException
1010
     * @expectedExceptionMessage Invalid field override named 'invalidPropertyName' for class 'Doctrine\Tests\Models\DDC964\DDC964Guest'.
1011
     */
1012
    public function testInvalidPropertyAttributeOverrideNameException()
1013
    {
1014
        $cm = new ClassMetadata('Doctrine\Tests\Models\DDC964\DDC964Guest');
1015
        $cm->initializeReflection(new RuntimeReflectionService());
1016
        $cm->mapField(array('fieldName' => 'name'));
1017
1018
        $cm->setAttributeOverride('invalidPropertyName', array());
1019
    }
1020
1021
    /**
1022
     * @group DDC-964
1023
     * @expectedException        Doctrine\ORM\Mapping\MappingException
1024
     * @expectedExceptionMessage The column type of attribute 'name' on class 'Doctrine\Tests\Models\DDC964\DDC964Guest' could not be changed.
1025
     */
1026
    public function testInvalidOverrideAttributeFieldTypeException()
1027
    {
1028
        $cm = new ClassMetadata('Doctrine\Tests\Models\DDC964\DDC964Guest');
1029
        $cm->initializeReflection(new RuntimeReflectionService());
1030
        $cm->mapField(array('fieldName' => 'name', 'type'=>'string'));
1031
1032
        $cm->setAttributeOverride('name', array('type'=>'date'));
1033
    }
1034
1035
    /**
1036
     * @group DDC-1955
1037
     *
1038
     * @expectedException        Doctrine\ORM\Mapping\MappingException
1039
     * @expectedExceptionMessage Entity Listener "\InvalidClassName" declared on "Doctrine\Tests\Models\CMS\CmsUser" not found.
1040
     */
1041
    public function testInvalidEntityListenerClassException()
1042
    {
1043
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
1044
        $cm->initializeReflection(new RuntimeReflectionService());
1045
1046
        $cm->addEntityListener(Events::postLoad, '\InvalidClassName', 'postLoadHandler');
1047
    }
1048
1049
    /**
1050
     * @group DDC-1955
1051
     *
1052
     * @expectedException        Doctrine\ORM\Mapping\MappingException
1053
     * @expectedExceptionMessage Entity Listener "\Doctrine\Tests\Models\Company\CompanyContractListener" declared on "Doctrine\Tests\Models\CMS\CmsUser" has no method "invalidMethod".
1054
     */
1055
    public function testInvalidEntityListenerMethodException()
1056
    {
1057
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
1058
        $cm->initializeReflection(new RuntimeReflectionService());
1059
1060
        $cm->addEntityListener(Events::postLoad, '\Doctrine\Tests\Models\Company\CompanyContractListener', 'invalidMethod');
1061
    }
1062
1063
    public function testManyToManySelfReferencingNamingStrategyDefaults()
1064
    {
1065
        $cm = new ClassMetadata('Doctrine\Tests\Models\CustomType\CustomTypeParent');
1066
        $cm->initializeReflection(new RuntimeReflectionService());
1067
        $cm->mapManyToMany(
1068
            array(
1069
                'fieldName' => 'friendsWithMe',
1070
                'targetEntity' => 'CustomTypeParent'
1071
            )
1072
        );
1073
1074
        $this->assertEquals(
1075
            array(
1076
                'name' => 'customtypeparent_customtypeparent',
1077
                'joinColumns' => array(array('name' => 'customtypeparent_source', 'referencedColumnName' => 'id', 'onDelete' => 'CASCADE')),
1078
                'inverseJoinColumns' => array(array('name' => 'customtypeparent_target', 'referencedColumnName' => 'id', 'onDelete' => 'CASCADE')),
1079
            ),
1080
            $cm->associationMappings['friendsWithMe']['joinTable']
1081
        );
1082
        $this->assertEquals(array('customtypeparent_source', 'customtypeparent_target'), $cm->associationMappings['friendsWithMe']['joinTableColumns']);
1083
        $this->assertEquals(array('customtypeparent_source' => 'id'), $cm->associationMappings['friendsWithMe']['relationToSourceKeyColumns']);
1084
        $this->assertEquals(array('customtypeparent_target' => 'id'), $cm->associationMappings['friendsWithMe']['relationToTargetKeyColumns']);
1085
    }
1086
1087
    /**
1088
     * @group DDC-2608
1089
     */
1090
    public function testSetSequenceGeneratorThrowsExceptionWhenSequenceNameIsMissing()
1091
    {
1092
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
1093
        $cm->initializeReflection(new RuntimeReflectionService());
1094
1095
        $this->expectException(\Doctrine\ORM\Mapping\MappingException::class);
1096
        $cm->setSequenceGeneratorDefinition(array());
1097
    }
1098
1099
    /**
1100
     * @group DDC-2662
1101
     */
1102
    public function testQuotedSequenceName()
1103
    {
1104
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
1105
        $cm->initializeReflection(new RuntimeReflectionService());
1106
1107
        $cm->setSequenceGeneratorDefinition(array('sequenceName' => '`foo`'));
1108
1109
        $this->assertEquals(array('sequenceName' => 'foo', 'quoted' => true), $cm->sequenceGeneratorDefinition);
1110
    }
1111
1112
    /**
1113
     * @group DDC-2700
1114
     */
1115
    public function testIsIdentifierMappedSuperClass()
1116
    {
1117
        $class = new ClassMetadata(__NAMESPACE__ . '\\DDC2700MappedSuperClass');
1118
1119
        $this->assertFalse($class->isIdentifier('foo'));
1120
    }
1121
1122
    /**
1123
     * @group DDC-3120
1124
     */
1125
    public function testCanInstantiateInternalPhpClassSubclass()
1126
    {
1127
        $classMetadata = new ClassMetadata(__NAMESPACE__ . '\\MyArrayObjectEntity');
1128
1129
        $this->assertInstanceOf(__NAMESPACE__ . '\\MyArrayObjectEntity', $classMetadata->newInstance());
1130
    }
1131
1132
    /**
1133
     * @group DDC-3120
1134
     */
1135
    public function testCanInstantiateInternalPhpClassSubclassFromUnserializedMetadata()
1136
    {
1137
        /* @var $classMetadata ClassMetadata */
1138
        $classMetadata = unserialize(serialize(new ClassMetadata(__NAMESPACE__ . '\\MyArrayObjectEntity')));
1139
1140
        $classMetadata->wakeupReflection(new RuntimeReflectionService());
1141
1142
        $this->assertInstanceOf(__NAMESPACE__ . '\\MyArrayObjectEntity', $classMetadata->newInstance());
1143
    }
1144
1145
    public function testWakeupReflectionWithEmbeddableAndStaticReflectionService()
1146
    {
1147
        $classMetadata = new ClassMetadata('Doctrine\Tests\ORM\Mapping\TestEntity1');
1148
1149
        $classMetadata->mapEmbedded(array(
1150
            'fieldName'    => 'test',
1151
            'class'        => 'Doctrine\Tests\ORM\Mapping\TestEntity1',
1152
            'columnPrefix' => false,
1153
        ));
1154
1155
        $field = array(
1156
            'fieldName' => 'test.embeddedProperty',
1157
            'type' => 'string',
1158
            'originalClass' => 'Doctrine\Tests\ORM\Mapping\TestEntity1',
1159
            'declaredField' => 'test',
1160
            'originalField' => 'embeddedProperty'
1161
        );
1162
1163
        $classMetadata->mapField($field);
1164
        $classMetadata->wakeupReflection(new StaticReflectionService());
1165
1166
        $this->assertEquals(array('test' => null, 'test.embeddedProperty' => null), $classMetadata->getReflectionProperties());
1167
    }
1168
1169
    /**
1170
     * @expectedException        Doctrine\ORM\Mapping\MappingException
1171
     * @expectedExceptionMessage No mapping found for field 'foo' on class 'Doctrine\Tests\ORM\Mapping\TestEntity1'.
1172
     */
1173
    public function testRemoveFieldMappingInvalidFieldException()
1174
    {
1175
        $classMetadata = new ClassMetadata('Doctrine\Tests\ORM\Mapping\TestEntity1');
1176
        $classMetadata->removeFieldMapping('foo');
1177
    }
1178
1179
    public function testRemoveFieldMapping()
1180
    {
1181
        $classMetadata = new ClassMetadata('Doctrine\Tests\ORM\Mapping\TestEntity1');
1182
        $classMetadata->mapEmbedded(array(
1183
            'fieldName'    => 'test',
1184
            'class'        => 'Doctrine\Tests\ORM\Mapping\TestEntity1',
1185
            'columnPrefix' => false,
1186
        ));
1187
1188
        $field = array(
1189
            'fieldName' => 'test.embeddedProperty',
1190
            'type' => 'string',
1191
            'originalClass' => 'Doctrine\Tests\ORM\Mapping\TestEntity1',
1192
            'declaredField' => 'test',
1193
            'originalField' => 'embeddedProperty',
1194
        );
1195
        $classMetadata->mapField($field);
1196
1197
        $this->assertArrayHasKey('test.embeddedProperty', $classMetadata->fieldNames);
1198
        $this->assertArrayHasKey('test.embeddedProperty', $classMetadata->fieldMappings);
1199
        $this->assertArrayHasKey('test.embeddedProperty', $classMetadata->columnNames);
1200
1201
        $classMetadata->removeFieldMapping('test.embeddedProperty');
1202
1203
        $this->assertArrayNotHasKey('test.embeddedProperty', $classMetadata->fieldNames);
1204
        $this->assertArrayNotHasKey('test.embeddedProperty', $classMetadata->fieldMappings);
1205
        $this->assertArrayNotHasKey('test.embeddedProperty', $classMetadata->columnNames);
1206
    }
1207
1208
    public function testEmbeddableAttributeOverride()
1209
    {
1210
        $classMetadata = new ClassMetadata('Doctrine\Tests\ORM\Mapping\TestEntity1');
1211
        $classMetadata->mapEmbedded(array(
1212
            'fieldName'    => 'test',
1213
            'class'        => 'Doctrine\Tests\ORM\Mapping\TestEntity1',
1214
            'columnPrefix' => false,
1215
        ));
1216
1217
        $field = array(
1218
            'fieldName' => 'test.embeddedProperty',
1219
            'type' => 'string',
1220
            'originalClass' => 'Doctrine\Tests\ORM\Mapping\TestEntity1',
1221
            'declaredField' => 'test',
1222
            'originalField' => 'embeddedProperty',
1223
        );
1224
        $classMetadata->mapField($field);
1225
1226
        $overridefield = array(
1227
            'fieldName' => 'test.embeddedProperty',
1228
            'type' => 'string',
1229
            'columnName' => 'test.overridden',
1230
        );
1231
        $classMetadata->setAttributeOverride('test.embeddedProperty', $overridefield);
1232
1233
        $this->assertEquals($classMetadata->fieldMappings['test.embeddedProperty'], array(
1234
            'fieldName' => 'test.embeddedProperty',
1235
            'type' => 'string',
1236
            'columnName' => 'test.overridden',
1237
            'originalClass' => 'Doctrine\Tests\ORM\Mapping\TestEntity1',
1238
            'declaredField' => 'test',
1239
            'originalField' => 'embeddedProperty',
1240
        ));
1241
    }
1242
1243
    public function testGetColumnNamesWithGivenFieldNames()
1244
    {
1245
        $metadata = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
1246
        $metadata->initializeReflection(new RuntimeReflectionService());
1247
1248
        $metadata->mapField(array('fieldName' => 'status', 'type' => 'string', 'columnName' => 'foo'));
1249
        $metadata->mapField(array('fieldName' => 'username', 'type' => 'string', 'columnName' => 'bar'));
1250
        $metadata->mapField(array('fieldName' => 'name', 'type' => 'string', 'columnName' => 'baz'));
1251
1252
        self::assertSame(['foo', 'baz'], $metadata->getColumnNames(['status', 'name']));
1253
    }
1254
}
1255
1256
/**
1257
 * @MappedSuperclass
1258
 */
1259
class DDC2700MappedSuperClass
1260
{
1261
    /** @Column */
1262
    private $foo;
1263
}
1264
1265
class MyNamespacedNamingStrategy extends DefaultNamingStrategy
1266
{
1267
    /**
1268
     * {@inheritdoc}
1269
     */
1270
    public function classToTableName($className)
1271
    {
1272
        if (strpos($className, '\\') !== false) {
1273
            $className = str_replace('\\', '_', str_replace('Doctrine\Tests\Models\\', '', $className));
1274
        }
1275
1276
        return strtolower($className);
1277
    }
1278
}
1279
1280
class MyPrefixNamingStrategy extends DefaultNamingStrategy
1281
{
1282
    /**
1283
     * {@inheritdoc}
1284
     */
1285
    public function propertyToColumnName($propertyName, $className = null)
1286
    {
1287
        return strtolower($this->classToTableName($className)) . '_' . $propertyName;
1288
    }
1289
}
1290
1291
class MyArrayObjectEntity extends \ArrayObject
1292
{
1293
}
1294