1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Tests\ORM\Mapping; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\EventManager; |
8
|
|
|
use Doctrine\DBAL\Connection; |
9
|
|
|
use Doctrine\DBAL\Types\Type; |
10
|
|
|
use Doctrine\ORM\Configuration; |
11
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
12
|
|
|
use Doctrine\ORM\Event\OnClassMetadataNotFoundEventArgs; |
13
|
|
|
use Doctrine\ORM\Events; |
14
|
|
|
use Doctrine\ORM\Exception\ORMException; |
15
|
|
|
use Doctrine\ORM\Mapping; |
16
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
17
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataFactory; |
18
|
|
|
use Doctrine\ORM\Mapping\Driver\MappingDriver; |
19
|
|
|
use Doctrine\ORM\Mapping\MappingException; |
20
|
|
|
use Doctrine\ORM\Reflection\RuntimeReflectionService; |
21
|
|
|
use Doctrine\ORM\Sequencing\Generator; |
22
|
|
|
use Doctrine\Tests\Mocks\ConnectionMock; |
23
|
|
|
use Doctrine\Tests\Mocks\DriverMock; |
24
|
|
|
use Doctrine\Tests\Mocks\EntityManagerMock; |
25
|
|
|
use Doctrine\Tests\Mocks\MetadataDriverMock; |
26
|
|
|
use Doctrine\Tests\Models\CMS\CmsArticle; |
27
|
|
|
use Doctrine\Tests\Models\CMS\CmsUser; |
28
|
|
|
use Doctrine\Tests\Models\DDC4006\DDC4006User; |
29
|
|
|
use Doctrine\Tests\Models\JoinedInheritanceType\AnotherChildClass; |
30
|
|
|
use Doctrine\Tests\Models\JoinedInheritanceType\ChildClass; |
31
|
|
|
use Doctrine\Tests\Models\JoinedInheritanceType\RootClass; |
32
|
|
|
use Doctrine\Tests\Models\Quote; |
33
|
|
|
use Doctrine\Tests\OrmTestCase; |
34
|
|
|
use DoctrineGlobalArticle; |
35
|
|
|
use function array_search; |
|
|
|
|
36
|
|
|
use function reset; |
37
|
|
|
use function sprintf; |
38
|
|
|
|
39
|
|
|
class ClassMetadataFactoryTest extends OrmTestCase |
40
|
|
|
{ |
41
|
|
|
public function testGetMetadataForSingleClass() : void |
42
|
|
|
{ |
43
|
|
|
$mockDriver = new MetadataDriverMock(); |
44
|
|
|
$entityManager = $this->createEntityManager($mockDriver); |
45
|
|
|
|
46
|
|
|
$conn = $entityManager->getConnection(); |
47
|
|
|
$mockPlatform = $conn->getDatabasePlatform(); |
48
|
|
|
$mockPlatform->setPrefersSequences(true); |
|
|
|
|
49
|
|
|
$mockPlatform->setPrefersIdentityColumns(false); |
|
|
|
|
50
|
|
|
|
51
|
|
|
$cm1 = $this->createValidClassMetadata(); |
52
|
|
|
|
53
|
|
|
// SUT |
54
|
|
|
$cmf = new ClassMetadataFactory(); |
55
|
|
|
$cmf->setEntityManager($entityManager); |
56
|
|
|
$cmf->setMetadataFor($cm1->getClassName(), $cm1); |
57
|
|
|
|
58
|
|
|
// Prechecks |
59
|
|
|
self::assertCount(0, $cm1->getAncestorsIterator()); |
60
|
|
|
self::assertEquals(Mapping\InheritanceType::NONE, $cm1->inheritanceType); |
61
|
|
|
self::assertEquals(Mapping\GeneratorType::AUTO, $cm1->getProperty('id')->getValueGenerator()->getType()); |
|
|
|
|
62
|
|
|
self::assertTrue($cm1->hasField('name')); |
63
|
|
|
self::assertCount(4, $cm1->getDeclaredPropertiesIterator()); // 2 fields + 2 associations |
64
|
|
|
self::assertEquals('group', $cm1->table->getName()); |
65
|
|
|
|
66
|
|
|
// Go |
67
|
|
|
$cmMap1 = $cmf->getMetadataFor($cm1->getClassName()); |
68
|
|
|
|
69
|
|
|
self::assertSame($cm1, $cmMap1); |
70
|
|
|
self::assertEquals('group', $cmMap1->table->getName()); |
71
|
|
|
self::assertCount(0, $cmMap1->getAncestorsIterator()); |
72
|
|
|
self::assertTrue($cmMap1->hasField('name')); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testGetMetadataForThrowsExceptionOnUnknownCustomGeneratorClass() : void |
76
|
|
|
{ |
77
|
|
|
$cm1 = $this->createValidClassMetadata(); |
78
|
|
|
|
79
|
|
|
$cm1->getProperty('id')->setValueGenerator( |
|
|
|
|
80
|
|
|
new Mapping\ValueGeneratorMetadata( |
81
|
|
|
Mapping\GeneratorType::CUSTOM, |
82
|
|
|
[ |
83
|
|
|
'class' => 'NotExistingGenerator', |
84
|
|
|
'arguments' => [], |
85
|
|
|
] |
86
|
|
|
) |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
$cmf = $this->createTestFactory(); |
90
|
|
|
|
91
|
|
|
$cmf->setMetadataForClass($cm1->getClassName(), $cm1); |
92
|
|
|
|
93
|
|
|
$this->expectException(ORMException::class); |
94
|
|
|
|
95
|
|
|
$actual = $cmf->getMetadataFor($cm1->getClassName()); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testGetMetadataForThrowsExceptionOnMissingCustomGeneratorDefinition() : void |
99
|
|
|
{ |
100
|
|
|
$cm1 = $this->createValidClassMetadata(); |
101
|
|
|
|
102
|
|
|
$cm1->getProperty('id')->setValueGenerator( |
103
|
|
|
new Mapping\ValueGeneratorMetadata(Mapping\GeneratorType::CUSTOM) |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
$cmf = $this->createTestFactory(); |
107
|
|
|
|
108
|
|
|
$cmf->setMetadataForClass($cm1->getClassName(), $cm1); |
109
|
|
|
|
110
|
|
|
$this->expectException(ORMException::class); |
111
|
|
|
|
112
|
|
|
$actual = $cmf->getMetadataFor($cm1->getClassName()); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function testHasGetMetadataNamespaceSeparatorIsNotNormalized() : void |
116
|
|
|
{ |
117
|
|
|
require_once __DIR__ . '/../../Models/Global/GlobalNamespaceModel.php'; |
118
|
|
|
|
119
|
|
|
$metadataDriver = $this->createAnnotationDriver([__DIR__ . '/../../Models/Global/']); |
120
|
|
|
|
121
|
|
|
$entityManager = $this->createEntityManager($metadataDriver); |
122
|
|
|
|
123
|
|
|
$mf = $entityManager->getMetadataFactory(); |
124
|
|
|
|
125
|
|
|
self::assertSame( |
126
|
|
|
$mf->getMetadataFor(DoctrineGlobalArticle::class), |
127
|
|
|
$mf->getMetadataFor('\\' . DoctrineGlobalArticle::class) |
128
|
|
|
); |
129
|
|
|
self::assertTrue($mf->hasMetadataFor(DoctrineGlobalArticle::class)); |
130
|
|
|
self::assertTrue($mf->hasMetadataFor('\\' . DoctrineGlobalArticle::class)); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @group DDC-1512 |
135
|
|
|
*/ |
136
|
|
|
public function testIsTransient() : void |
137
|
|
|
{ |
138
|
|
|
$cmf = new ClassMetadataFactory(); |
|
|
|
|
139
|
|
|
$driver = $this->createMock(MappingDriver::class); |
140
|
|
|
$driver->expects($this->at(0)) |
141
|
|
|
->method('isTransient') |
142
|
|
|
->with($this->equalTo(CmsUser::class)) |
143
|
|
|
->will($this->returnValue(true)); |
144
|
|
|
$driver->expects($this->at(1)) |
145
|
|
|
->method('isTransient') |
146
|
|
|
->with($this->equalTo(CmsArticle::class)) |
147
|
|
|
->will($this->returnValue(false)); |
148
|
|
|
|
149
|
|
|
$em = $this->createEntityManager($driver); |
150
|
|
|
|
151
|
|
|
self::assertTrue($em->getMetadataFactory()->isTransient(CmsUser::class)); |
152
|
|
|
self::assertFalse($em->getMetadataFactory()->isTransient(CmsArticle::class)); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @dataProvider classesInInheritanceWithNoMapProvider() |
157
|
|
|
*/ |
158
|
|
|
public function testNoDefaultDiscriminatorMapIsAssumed(string $rootClassName, string $targetClassName) : void |
159
|
|
|
{ |
160
|
|
|
$cmf = new ClassMetadataFactory(); |
161
|
|
|
$driver = $this->createAnnotationDriver([__DIR__ . '/../../Models/JoinedInheritanceType/']); |
162
|
|
|
$em = $this->createEntityManager($driver); |
163
|
|
|
$cmf->setEntityManager($em); |
164
|
|
|
|
165
|
|
|
$this->expectException(MappingException::class); |
166
|
|
|
$this->expectExceptionMessage( |
167
|
|
|
sprintf("Entity class '%s' is using inheritance but no discriminator map was defined.", $rootClassName) |
168
|
|
|
); |
169
|
|
|
|
170
|
|
|
$cmf->getMetadataFor($targetClassName); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @return string[] |
175
|
|
|
*/ |
176
|
|
|
public function classesInInheritanceWithNoMapProvider() : iterable |
177
|
|
|
{ |
178
|
|
|
yield 'root entity' => [RootClass::class, RootClass::class]; |
|
|
|
|
179
|
|
|
yield 'child entity' => [RootClass::class, ChildClass::class]; |
180
|
|
|
yield 'another child entity' => [RootClass::class, AnotherChildClass::class]; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function testGetAllMetadataWorksWithBadConnection() : void |
184
|
|
|
{ |
185
|
|
|
// DDC-3551 |
186
|
|
|
$conn = $this->createMock(Connection::class); |
187
|
|
|
$mockDriver = new MetadataDriverMock(); |
188
|
|
|
$conn->expects($this->any()) |
189
|
|
|
->method('getEventManager') |
190
|
|
|
->willReturn(new EventManager()); |
191
|
|
|
$em = $this->createEntityManager($mockDriver, $conn); |
192
|
|
|
|
193
|
|
|
$conn->expects($this->any()) |
194
|
|
|
->method('getDatabasePlatform') |
195
|
|
|
->will($this->throwException(new \Exception('Exception thrown in test when calling getDatabasePlatform'))); |
196
|
|
|
|
197
|
|
|
$cmf = new ClassMetadataFactory(); |
198
|
|
|
$cmf->setEntityManager($em); |
199
|
|
|
|
200
|
|
|
// getting all the metadata should work, even if get DatabasePlatform blows up |
201
|
|
|
$metadata = $cmf->getAllMetadata(); |
202
|
|
|
// this will just be an empty array - there was no error |
203
|
|
|
self::assertEquals([], $metadata); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
protected function createEntityManager($metadataDriver, $conn = null) |
207
|
|
|
{ |
208
|
|
|
$driverMock = new DriverMock(); |
209
|
|
|
$config = new Configuration(); |
210
|
|
|
|
211
|
|
|
$config->setProxyDir(__DIR__ . '/../../Proxies'); |
212
|
|
|
$config->setProxyNamespace('Doctrine\Tests\Proxies'); |
213
|
|
|
|
214
|
|
|
if (! $conn) { |
215
|
|
|
$conn = new ConnectionMock([], $driverMock, $config, new EventManager()); |
216
|
|
|
} |
217
|
|
|
$eventManager = $conn->getEventManager(); |
218
|
|
|
|
219
|
|
|
$config->setMetadataDriverImpl($metadataDriver); |
220
|
|
|
|
221
|
|
|
return EntityManagerMock::create($conn, $config, $eventManager); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @return ClassMetadataFactoryTestSubject |
226
|
|
|
*/ |
227
|
|
|
protected function createTestFactory() |
228
|
|
|
{ |
229
|
|
|
$mockDriver = new MetadataDriverMock(); |
230
|
|
|
$entityManager = $this->createEntityManager($mockDriver); |
231
|
|
|
$cmf = new ClassMetadataFactoryTestSubject(); |
232
|
|
|
$cmf->setEntityManager($entityManager); |
233
|
|
|
return $cmf; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @return ClassMetadata |
238
|
|
|
*/ |
239
|
|
|
protected function createValidClassMetadata() |
240
|
|
|
{ |
241
|
|
|
// Self-made metadata |
242
|
|
|
$metadataBuildingContext = new Mapping\ClassMetadataBuildingContext( |
243
|
|
|
$this->createMock(ClassMetadataFactory::class), |
244
|
|
|
new RuntimeReflectionService() |
245
|
|
|
); |
246
|
|
|
|
247
|
|
|
$cm1 = new ClassMetadata(TestEntity1::class, $metadataBuildingContext); |
248
|
|
|
|
249
|
|
|
$tableMetadata = new Mapping\TableMetadata(); |
250
|
|
|
$tableMetadata->setName('group'); |
251
|
|
|
|
252
|
|
|
$cm1->setTable($tableMetadata); |
253
|
|
|
|
254
|
|
|
// Add a mapped field |
255
|
|
|
$fieldMetadata = new Mapping\FieldMetadata('id'); |
256
|
|
|
|
257
|
|
|
$fieldMetadata->setType(Type::getType('integer')); |
258
|
|
|
$fieldMetadata->setPrimaryKey(true); |
259
|
|
|
$fieldMetadata->setValueGenerator(new Mapping\ValueGeneratorMetadata(Mapping\GeneratorType::AUTO)); |
260
|
|
|
|
261
|
|
|
$cm1->addProperty($fieldMetadata); |
262
|
|
|
|
263
|
|
|
// Add a mapped field |
264
|
|
|
$fieldMetadata = new Mapping\FieldMetadata('name'); |
265
|
|
|
|
266
|
|
|
$fieldMetadata->setType(Type::getType('string')); |
267
|
|
|
|
268
|
|
|
$cm1->addProperty($fieldMetadata); |
269
|
|
|
|
270
|
|
|
// and a mapped association |
271
|
|
|
$association = new Mapping\OneToOneAssociationMetadata('other'); |
272
|
|
|
|
273
|
|
|
$association->setTargetEntity(TestEntity1::class); |
274
|
|
|
$association->setMappedBy('this'); |
275
|
|
|
|
276
|
|
|
$cm1->addProperty($association); |
277
|
|
|
|
278
|
|
|
// and an association on the owning side |
279
|
|
|
$joinColumns = []; |
280
|
|
|
|
281
|
|
|
$joinColumn = new Mapping\JoinColumnMetadata(); |
282
|
|
|
|
283
|
|
|
$joinColumn->setColumnName('other_id'); |
284
|
|
|
$joinColumn->setReferencedColumnName('id'); |
285
|
|
|
|
286
|
|
|
$joinColumns[] = $joinColumn; |
287
|
|
|
|
288
|
|
|
$association = new Mapping\OneToOneAssociationMetadata('association'); |
289
|
|
|
|
290
|
|
|
$association->setJoinColumns($joinColumns); |
291
|
|
|
$association->setTargetEntity(TestEntity1::class); |
292
|
|
|
|
293
|
|
|
$cm1->addProperty($association); |
294
|
|
|
|
295
|
|
|
return $cm1; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @group DDC-1845 |
300
|
|
|
*/ |
301
|
|
|
public function testQuoteMetadata() : void |
302
|
|
|
{ |
303
|
|
|
$cmf = new ClassMetadataFactory(); |
304
|
|
|
$driver = $this->createAnnotationDriver([__DIR__ . '/../../Models/Quote/']); |
305
|
|
|
$em = $this->createEntityManager($driver); |
306
|
|
|
$cmf->setEntityManager($em); |
307
|
|
|
|
308
|
|
|
$userMetadata = $cmf->getMetadataFor(Quote\User::class); |
309
|
|
|
$phoneMetadata = $cmf->getMetadataFor(Quote\Phone::class); |
310
|
|
|
$groupMetadata = $cmf->getMetadataFor(Quote\Group::class); |
311
|
|
|
$addressMetadata = $cmf->getMetadataFor(Quote\Address::class); |
312
|
|
|
|
313
|
|
|
// Phone Class Metadata |
314
|
|
|
self::assertNotNull($phoneMetadata->getProperty('number')); |
315
|
|
|
self::assertEquals('phone-number', $phoneMetadata->getProperty('number')->getColumnName()); |
|
|
|
|
316
|
|
|
|
317
|
|
|
$user = $phoneMetadata->getProperty('user'); |
318
|
|
|
$userJoinColumns = $user->getJoinColumns(); |
|
|
|
|
319
|
|
|
$phoneUserJoinColumn = reset($userJoinColumns); |
320
|
|
|
|
321
|
|
|
self::assertEquals('user-id', $phoneUserJoinColumn->getColumnName()); |
322
|
|
|
self::assertEquals('user-id', $phoneUserJoinColumn->getReferencedColumnName()); |
323
|
|
|
|
324
|
|
|
// Address Class Metadata |
325
|
|
|
self::assertNotNull($addressMetadata->getProperty('id')); |
326
|
|
|
self::assertNotNull($addressMetadata->getProperty('zip')); |
327
|
|
|
self::assertEquals('address-id', $addressMetadata->getProperty('id')->getColumnName()); |
328
|
|
|
self::assertEquals('address-zip', $addressMetadata->getProperty('zip')->getColumnName()); |
329
|
|
|
|
330
|
|
|
// User Class Metadata |
331
|
|
|
self::assertNotNull($userMetadata->getProperty('id')); |
332
|
|
|
self::assertNotNull($userMetadata->getProperty('name')); |
333
|
|
|
self::assertEquals('user-id', $userMetadata->getProperty('id')->getColumnName()); |
334
|
|
|
self::assertEquals('user-name', $userMetadata->getProperty('name')->getColumnName()); |
335
|
|
|
|
336
|
|
|
$group = $groupMetadata->getProperty('parent'); |
337
|
|
|
$groupJoinColumns = $group->getJoinColumns(); |
338
|
|
|
$groupUserJoinColumn = reset($groupJoinColumns); |
339
|
|
|
|
340
|
|
|
self::assertEquals('parent-id', $groupUserJoinColumn->getColumnName()); |
341
|
|
|
self::assertEquals('group-id', $groupUserJoinColumn->getReferencedColumnName()); |
342
|
|
|
|
343
|
|
|
$user = $addressMetadata->getProperty('user'); |
344
|
|
|
$userJoinColumns = $user->getJoinColumns(); |
345
|
|
|
$addressUserJoinColumn = reset($userJoinColumns); |
346
|
|
|
|
347
|
|
|
self::assertEquals('user-id', $addressUserJoinColumn->getColumnName()); |
348
|
|
|
self::assertEquals('user-id', $addressUserJoinColumn->getReferencedColumnName()); |
349
|
|
|
|
350
|
|
|
$groups = $userMetadata->getProperty('groups'); |
351
|
|
|
$groupsJoinTable = $groups->getJoinTable(); |
|
|
|
|
352
|
|
|
$userGroupsJoinColumns = $groupsJoinTable->getJoinColumns(); |
353
|
|
|
$userGroupsJoinColumn = reset($userGroupsJoinColumns); |
354
|
|
|
$userGroupsInverseJoinColumns = $groupsJoinTable->getInverseJoinColumns(); |
355
|
|
|
$userGroupsInverseJoinColumn = reset($userGroupsInverseJoinColumns); |
356
|
|
|
|
357
|
|
|
self::assertEquals('quote-users-groups', $groupsJoinTable->getName()); |
358
|
|
|
self::assertEquals('user-id', $userGroupsJoinColumn->getColumnName()); |
359
|
|
|
self::assertEquals('user-id', $userGroupsJoinColumn->getReferencedColumnName()); |
360
|
|
|
self::assertEquals('group-id', $userGroupsInverseJoinColumn->getColumnName()); |
361
|
|
|
self::assertEquals('group-id', $userGroupsInverseJoinColumn->getReferencedColumnName()); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* @group DDC-3385 |
366
|
|
|
* @group 1181 |
367
|
|
|
* @group 385 |
368
|
|
|
*/ |
369
|
|
|
public function testFallbackLoadingCausesEventTriggeringThatCanModifyFetchedMetadata() : void |
370
|
|
|
{ |
371
|
|
|
$test = $this; |
372
|
|
|
|
373
|
|
|
/** @var ClassMetadata $metadata */ |
374
|
|
|
$metadata = $this->createMock(ClassMetadata::class); |
375
|
|
|
$cmf = new ClassMetadataFactory(); |
376
|
|
|
$mockDriver = new MetadataDriverMock(); |
377
|
|
|
$em = $this->createEntityManager($mockDriver); |
378
|
|
|
$listener = $this->getMockBuilder(\stdClass::class)->setMethods(['onClassMetadataNotFound'])->getMock(); |
379
|
|
|
$eventManager = $em->getEventManager(); |
380
|
|
|
|
381
|
|
|
$cmf->setEntityManager($em); |
382
|
|
|
|
383
|
|
|
$listener |
384
|
|
|
->expects($this->any()) |
385
|
|
|
->method('onClassMetadataNotFound') |
386
|
|
|
->will($this->returnCallback(function (OnClassMetadataNotFoundEventArgs $args) use ($metadata, $em, $test) { |
387
|
|
|
$test->assertNull($args->getFoundMetadata()); |
388
|
|
|
$test->assertSame('Foo', $args->getClassName()); |
389
|
|
|
$test->assertSame($em, $args->getObjectManager()); |
390
|
|
|
|
391
|
|
|
$args->setFoundMetadata($metadata); |
392
|
|
|
})); |
393
|
|
|
|
394
|
|
|
$eventManager->addEventListener([Events::onClassMetadataNotFound], $listener); |
395
|
|
|
|
396
|
|
|
self::assertSame($metadata, $cmf->getMetadataFor('Foo')); |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* @group DDC-3427 |
401
|
|
|
*/ |
402
|
|
|
public function testAcceptsEntityManagerInterfaceInstances() : void |
403
|
|
|
{ |
404
|
|
|
$classMetadataFactory = new ClassMetadataFactory(); |
405
|
|
|
|
406
|
|
|
/** @var EntityManagerInterface $entityManager */ |
407
|
|
|
$entityManager = $this->createMock(EntityManagerInterface::class); |
408
|
|
|
|
409
|
|
|
$classMetadataFactory->setEntityManager($entityManager); |
410
|
|
|
|
411
|
|
|
// not really the cleanest way to check it, but we won't add a getter to the CMF just for the sake of testing. |
412
|
|
|
self::assertAttributeSame($entityManager, 'em', $classMetadataFactory); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* @group embedded |
417
|
|
|
* @group DDC-3305 |
418
|
|
|
*/ |
419
|
|
|
public function testRejectsEmbeddableWithoutValidClassName() : void |
420
|
|
|
{ |
421
|
|
|
$metadata = $this->createValidClassMetadata(); |
422
|
|
|
|
423
|
|
|
$metadata->mapEmbedded( |
424
|
|
|
[ |
425
|
|
|
'fieldName' => 'embedded', |
426
|
|
|
'class' => '', |
427
|
|
|
'columnPrefix' => false, |
428
|
|
|
] |
429
|
|
|
); |
430
|
|
|
|
431
|
|
|
$cmf = $this->createTestFactory(); |
432
|
|
|
|
433
|
|
|
$cmf->setMetadataForClass($metadata->getClassName(), $metadata); |
434
|
|
|
|
435
|
|
|
$this->expectException(MappingException::class); |
436
|
|
|
$this->expectExceptionMessage('The embed mapping \'embedded\' misses the \'class\' attribute.'); |
437
|
|
|
|
438
|
|
|
$cmf->getMetadataFor($metadata->getClassName()); |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* @group embedded |
443
|
|
|
* @group DDC-4006 |
444
|
|
|
*/ |
445
|
|
|
public function testInheritsIdGeneratorMappingFromEmbeddable() : void |
446
|
|
|
{ |
447
|
|
|
$cmf = new ClassMetadataFactory(); |
448
|
|
|
$driver = $this->createAnnotationDriver([__DIR__ . '/../../Models/DDC4006/']); |
449
|
|
|
$em = $this->createEntityManager($driver); |
450
|
|
|
$cmf->setEntityManager($em); |
451
|
|
|
|
452
|
|
|
$userMetadata = $cmf->getMetadataFor(DDC4006User::class); |
453
|
|
|
|
454
|
|
|
self::assertTrue($userMetadata->isIdGeneratorIdentity()); |
|
|
|
|
455
|
|
|
} |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
/* Test subject class with overridden factory method for mocking purposes */ |
459
|
|
|
class ClassMetadataFactoryTestSubject extends ClassMetadataFactory |
460
|
|
|
{ |
461
|
|
|
private $mockMetadata = []; |
462
|
|
|
private $requestedClasses = []; |
463
|
|
|
|
464
|
|
|
protected function newClassMetadataInstance( |
465
|
|
|
string $className, |
466
|
|
|
?Mapping\ClassMetadata $parent, |
|
|
|
|
467
|
|
|
Mapping\ClassMetadataBuildingContext $metadataBuildingContext |
|
|
|
|
468
|
|
|
) : ClassMetadata { |
469
|
|
|
$this->requestedClasses[] = $className; |
470
|
|
|
|
471
|
|
|
if (! isset($this->mockMetadata[$className])) { |
472
|
|
|
throw new \InvalidArgumentException(sprintf('No mock metadata found for class %s.', $className)); |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
return $this->mockMetadata[$className]; |
476
|
|
|
} |
477
|
|
|
|
478
|
|
|
public function setMetadataForClass($className, $metadata) |
479
|
|
|
{ |
480
|
|
|
$this->mockMetadata[$className] = $metadata; |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
public function getRequestedClasses() |
484
|
|
|
{ |
485
|
|
|
return $this->requestedClasses; |
486
|
|
|
} |
487
|
|
|
} |
488
|
|
|
|
489
|
|
|
class TestEntity1 |
490
|
|
|
{ |
491
|
|
|
private $id; |
|
|
|
|
492
|
|
|
private $name; |
|
|
|
|
493
|
|
|
private $other; |
|
|
|
|
494
|
|
|
private $association; |
|
|
|
|
495
|
|
|
private $embedded; |
|
|
|
|
496
|
|
|
} |
497
|
|
|
|
498
|
|
|
class CustomIdGenerator implements Generator |
499
|
|
|
{ |
500
|
|
|
/** |
501
|
|
|
* {@inheritdoc} |
502
|
|
|
*/ |
503
|
|
|
public function generate(EntityManagerInterface $em, ?object $entity) |
504
|
|
|
{ |
505
|
|
|
} |
506
|
|
|
|
507
|
|
|
/** |
508
|
|
|
* {@inheritdoc} |
509
|
|
|
*/ |
510
|
|
|
public function isPostInsertGenerator() : bool |
511
|
|
|
{ |
512
|
|
|
return false; |
513
|
|
|
} |
514
|
|
|
} |
515
|
|
|
|