-
-
Notifications
You must be signed in to change notification settings - Fork 509
/
Copy pathClassMetadataTest.php
822 lines (669 loc) · 27.9 KB
/
ClassMetadataTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
<?php
declare(strict_types=1);
namespace Doctrine\ODM\MongoDB\Tests\Mapping;
use Doctrine\ODM\MongoDB\Events;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Doctrine\ODM\MongoDB\Mapping\MappingException;
use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
use Doctrine\ODM\MongoDB\Tests\BaseTest;
use Doctrine\ODM\MongoDB\Types\Type;
use Doctrine\ODM\MongoDB\Utility\CollectionHelper;
use Documents\Account;
use Documents\Address;
use Documents\Album;
use Documents\Bars\Bar;
use Documents\CmsUser;
use Documents\SpecialUser;
use Documents\User;
use Documents\UserRepository;
use InvalidArgumentException;
use ProxyManager\Proxy\GhostObjectInterface;
use ReflectionClass;
use ReflectionException;
use stdClass;
use function array_merge;
use function get_class;
use function serialize;
use function unserialize;
class ClassMetadataTest extends BaseTest
{
public function testClassMetadataInstanceSerialization()
{
$cm = new ClassMetadata(CmsUser::class);
// Test initial state
$this->assertCount(0, $cm->getReflectionProperties());
$this->assertInstanceOf(ReflectionClass::class, $cm->reflClass);
$this->assertEquals(CmsUser::class, $cm->name);
$this->assertEquals(CmsUser::class, $cm->rootDocumentName);
$this->assertEquals([], $cm->subClasses);
$this->assertEquals([], $cm->parentClasses);
$this->assertEquals(ClassMetadata::INHERITANCE_TYPE_NONE, $cm->inheritanceType);
// Customize state
$cm->setInheritanceType(ClassMetadata::INHERITANCE_TYPE_SINGLE_COLLECTION);
$cm->setSubclasses(['One', 'Two', 'Three']);
$cm->setParentClasses(['UserParent']);
$cm->setCustomRepositoryClass(UserRepository::class);
$cm->setDiscriminatorField('disc');
$cm->mapOneEmbedded(['fieldName' => 'phonenumbers', 'targetDocument' => Bar::class]);
$cm->setShardKey(['_id' => '1']);
$cm->setCollectionCapped(true);
$cm->setCollectionMax(1000);
$cm->setCollectionSize(500);
$this->assertIsArray($cm->getFieldMapping('phonenumbers'));
$this->assertCount(1, $cm->fieldMappings);
$this->assertCount(1, $cm->associationMappings);
$serialized = serialize($cm);
$cm = unserialize($serialized);
// Check state
$this->assertGreaterThan(0, $cm->getReflectionProperties());
$this->assertInstanceOf(ReflectionClass::class, $cm->reflClass);
$this->assertEquals(CmsUser::class, $cm->name);
$this->assertEquals('UserParent', $cm->rootDocumentName);
$this->assertEquals(['One', 'Two', 'Three'], $cm->subClasses);
$this->assertEquals(['UserParent'], $cm->parentClasses);
$this->assertEquals(UserRepository::class, $cm->customRepositoryClassName);
$this->assertEquals('disc', $cm->discriminatorField);
$this->assertIsArray($cm->getFieldMapping('phonenumbers'));
$this->assertCount(1, $cm->fieldMappings);
$this->assertCount(1, $cm->associationMappings);
$this->assertEquals(['keys' => ['_id' => 1], 'options' => []], $cm->getShardKey());
$mapping = $cm->getFieldMapping('phonenumbers');
$this->assertEquals(Bar::class, $mapping['targetDocument']);
$this->assertTrue($cm->getCollectionCapped());
$this->assertEquals(1000, $cm->getCollectionMax());
$this->assertEquals(500, $cm->getCollectionSize());
}
public function testOwningSideAndInverseSide()
{
$cm = new ClassMetadata(User::class);
$cm->mapOneReference(['fieldName' => 'account', 'targetDocument' => Account::class, 'inversedBy' => 'user']);
$this->assertTrue($cm->fieldMappings['account']['isOwningSide']);
$cm = new ClassMetadata(Account::class);
$cm->mapOneReference(['fieldName' => 'user', 'targetDocument' => Account::class, 'mappedBy' => 'account']);
$this->assertTrue($cm->fieldMappings['user']['isInverseSide']);
}
public function testFieldIsNullable()
{
$cm = new ClassMetadata(CmsUser::class);
// Explicit Nullable
$cm->mapField(['fieldName' => 'status', 'nullable' => true, 'type' => 'string', 'length' => 50]);
$this->assertTrue($cm->isNullable('status'));
// Explicit Not Nullable
$cm->mapField(['fieldName' => 'username', 'nullable' => false, 'type' => 'string', 'length' => 50]);
$this->assertFalse($cm->isNullable('username'));
// Implicit Not Nullable
$cm->mapField(['fieldName' => 'name', 'type' => 'string', 'length' => 50]);
$this->assertFalse($cm->isNullable('name'), 'By default a field should not be nullable.');
}
/**
* @group DDC-115
*/
public function testMapAssocationInGlobalNamespace()
{
require_once __DIR__ . '/Documents/GlobalNamespaceDocument.php';
$cm = new ClassMetadata('DoctrineGlobal_Article');
$cm->mapManyEmbedded([
'fieldName' => 'author',
'targetDocument' => 'DoctrineGlobal_User',
]);
$this->assertEquals('DoctrineGlobal_User', $cm->fieldMappings['author']['targetDocument']);
}
public function testMapManyToManyJoinTableDefaults()
{
$cm = new ClassMetadata(CmsUser::class);
$cm->mapManyEmbedded(
[
'fieldName' => 'groups',
'targetDocument' => 'CmsGroup',
]
);
$assoc = $cm->fieldMappings['groups'];
$this->assertIsArray($assoc);
}
public function testGetAssociationTargetClassWithoutTargetDocument()
{
$cm = new ClassMetadata(CmsUser::class);
$cm->mapManyEmbedded(
[
'fieldName' => 'groups',
'targetDocument' => null,
]
);
$this->assertNull($cm->getAssociationTargetClass('groups'));
}
/**
* @group DDC-115
*/
public function testSetDiscriminatorMapInGlobalNamespace()
{
require_once __DIR__ . '/Documents/GlobalNamespaceDocument.php';
$cm = new ClassMetadata('DoctrineGlobal_User');
$cm->setDiscriminatorMap(['descr' => 'DoctrineGlobal_Article', 'foo' => 'DoctrineGlobal_User']);
$this->assertEquals('DoctrineGlobal_Article', $cm->discriminatorMap['descr']);
$this->assertEquals('DoctrineGlobal_User', $cm->discriminatorMap['foo']);
}
/**
* @group DDC-115
*/
public function testSetSubClassesInGlobalNamespace()
{
require_once __DIR__ . '/Documents/GlobalNamespaceDocument.php';
$cm = new ClassMetadata('DoctrineGlobal_User');
$cm->setSubclasses(['DoctrineGlobal_Article']);
$this->assertEquals('DoctrineGlobal_Article', $cm->subClasses[0]);
}
public function testDuplicateFieldMapping()
{
$cm = new ClassMetadata(CmsUser::class);
$a1 = ['reference' => true, 'type' => 'many', 'fieldName' => 'name', 'targetDocument' => 'stdClass'];
$a2 = ['type' => 'string', 'fieldName' => 'name'];
$cm->mapField($a1);
$cm->mapField($a2);
$this->assertEquals('string', $cm->fieldMappings['name']['type']);
}
public function testDuplicateColumnNameDiscriminatorColumnThrowsMappingException()
{
$cm = new ClassMetadata(CmsUser::class);
$cm->mapField(['fieldName' => 'name', 'type' => Type::STRING]);
$this->expectException(MappingException::class);
$cm->setDiscriminatorField('name');
}
public function testDuplicateFieldNameDiscriminatorColumn2ThrowsMappingException()
{
$cm = new ClassMetadata(CmsUser::class);
$cm->setDiscriminatorField('name');
$this->expectException(MappingException::class);
$cm->mapField(['fieldName' => 'name', 'type' => Type::STRING]);
}
public function testDuplicateFieldAndAssocationMapping1()
{
$cm = new ClassMetadata(CmsUser::class);
$cm->mapField(['fieldName' => 'name', 'type' => Type::STRING]);
$cm->mapOneEmbedded(['fieldName' => 'name', 'targetDocument' => 'CmsUser']);
$this->assertEquals('one', $cm->fieldMappings['name']['type']);
}
public function testDuplicateFieldAndAssocationMapping2()
{
$cm = new ClassMetadata(CmsUser::class);
$cm->mapOneEmbedded(['fieldName' => 'name', 'targetDocument' => 'CmsUser']);
$cm->mapField(['fieldName' => 'name', 'columnName' => 'name', 'type' => 'string']);
$this->assertEquals('string', $cm->fieldMappings['name']['type']);
}
public function testMapNotExistingFieldThrowsException()
{
$cm = new ClassMetadata(CmsUser::class);
$this->expectException(ReflectionException::class);
$cm->mapField(['fieldName' => 'namee', 'columnName' => 'name', 'type' => 'string']);
}
/**
* @dataProvider dataProviderMetadataClasses
*/
public function testEmbeddedDocumentWithDiscriminator(ClassMetadata $cm)
{
$cm->setDiscriminatorField('discriminator');
$cm->setDiscriminatorValue('discriminatorValue');
$serialized = serialize($cm);
$cm = unserialize($serialized);
$this->assertSame('discriminator', $cm->discriminatorField);
$this->assertSame('discriminatorValue', $cm->discriminatorValue);
}
public static function dataProviderMetadataClasses()
{
$document = new ClassMetadata(CmsUser::class);
$embeddedDocument = new ClassMetadata(CmsUser::class);
$embeddedDocument->isEmbeddedDocument = true;
$mappedSuperclass = new ClassMetadata(CmsUser::class);
$mappedSuperclass->isMappedSuperclass = true;
return [
'document' => [$document],
'mappedSuperclass' => [$mappedSuperclass],
'embeddedDocument' => [$embeddedDocument],
];
}
public function testDefaultDiscriminatorField()
{
$object = new class {
public $assoc;
public $assocWithTargetDocument;
public $assocWithDiscriminatorField;
};
$cm = new ClassMetadata(get_class($object));
$cm->mapField([
'fieldName' => 'assoc',
'reference' => true,
'type' => 'one',
]);
$cm->mapField([
'fieldName' => 'assocWithTargetDocument',
'reference' => true,
'type' => 'one',
'targetDocument' => 'stdClass',
]);
$cm->mapField([
'fieldName' => 'assocWithDiscriminatorField',
'reference' => true,
'type' => 'one',
'discriminatorField' => 'type',
]);
$mapping = $cm->getFieldMapping('assoc');
$this->assertEquals(
ClassMetadata::DEFAULT_DISCRIMINATOR_FIELD,
$mapping['discriminatorField'],
'Default discriminator field is set for associations without targetDocument and discriminatorField options'
);
$mapping = $cm->getFieldMapping('assocWithTargetDocument');
$this->assertArrayNotHasKey(
'discriminatorField',
$mapping,
'Default discriminator field is not set for associations with targetDocument option'
);
$mapping = $cm->getFieldMapping('assocWithDiscriminatorField');
$this->assertEquals(
'type',
$mapping['discriminatorField'],
'Default discriminator field is not set for associations with discriminatorField option'
);
}
public function testGetFieldValue()
{
$document = new Album('ten');
$metadata = $this->dm->getClassMetadata(Album::class);
$this->assertEquals($document->getName(), $metadata->getFieldValue($document, 'name'));
}
public function testGetFieldValueInitializesProxy()
{
$document = new Album('ten');
$this->dm->persist($document);
$this->dm->flush();
$this->dm->clear();
$proxy = $this->dm->getReference(Album::class, $document->getId());
$metadata = $this->dm->getClassMetadata(Album::class);
$this->assertEquals($document->getName(), $metadata->getFieldValue($proxy, 'name'));
$this->assertInstanceOf(GhostObjectInterface::class, $proxy);
$this->assertTrue($proxy->isProxyInitialized());
}
public function testGetFieldValueOfIdentifierDoesNotInitializeProxy()
{
$document = new Album('ten');
$this->dm->persist($document);
$this->dm->flush();
$this->dm->clear();
$proxy = $this->dm->getReference(Album::class, $document->getId());
$metadata = $this->dm->getClassMetadata(Album::class);
$this->assertEquals($document->getId(), $metadata->getFieldValue($proxy, 'id'));
$this->assertInstanceOf(GhostObjectInterface::class, $proxy);
$this->assertFalse($proxy->isProxyInitialized());
}
public function testSetFieldValue()
{
$document = new Album('ten');
$metadata = $this->dm->getClassMetadata(Album::class);
$metadata->setFieldValue($document, 'name', 'nevermind');
$this->assertEquals('nevermind', $document->getName());
}
public function testSetFieldValueWithProxy()
{
$document = new Album('ten');
$this->dm->persist($document);
$this->dm->flush();
$this->dm->clear();
$proxy = $this->dm->getReference(Album::class, $document->getId());
$this->assertInstanceOf(GhostObjectInterface::class, $proxy);
$metadata = $this->dm->getClassMetadata(Album::class);
$metadata->setFieldValue($proxy, 'name', 'nevermind');
$this->dm->flush();
$this->dm->clear();
$proxy = $this->dm->getReference(Album::class, $document->getId());
$this->assertInstanceOf(GhostObjectInterface::class, $proxy);
$this->assertEquals('nevermind', $proxy->getName());
}
public function testSetCustomRepositoryClass()
{
$cm = new ClassMetadata('Doctrine\ODM\MongoDB\Tests\Mapping\ClassMetadataTest');
$cm->namespace = 'Doctrine\ODM\MongoDB\Tests\Mapping';
$cm->setCustomRepositoryClass('TestCustomRepositoryClass');
$this->assertEquals('TestCustomRepositoryClass', $cm->customRepositoryClassName);
$cm->setCustomRepositoryClass('Doctrine\ODM\MongoDB\Tests\Mapping\TestCustomRepositoryClass');
$this->assertEquals('Doctrine\ODM\MongoDB\Tests\Mapping\TestCustomRepositoryClass', $cm->customRepositoryClassName);
}
public function testEmbeddedAssociationsAlwaysCascade()
{
$class = $this->dm->getClassMetadata(EmbeddedAssociationsCascadeTest::class);
$this->assertTrue($class->fieldMappings['address']['isCascadeRemove']);
$this->assertTrue($class->fieldMappings['address']['isCascadePersist']);
$this->assertTrue($class->fieldMappings['address']['isCascadeRefresh']);
$this->assertTrue($class->fieldMappings['address']['isCascadeMerge']);
$this->assertTrue($class->fieldMappings['address']['isCascadeDetach']);
$this->assertTrue($class->fieldMappings['addresses']['isCascadeRemove']);
$this->assertTrue($class->fieldMappings['addresses']['isCascadePersist']);
$this->assertTrue($class->fieldMappings['addresses']['isCascadeRefresh']);
$this->assertTrue($class->fieldMappings['addresses']['isCascadeMerge']);
$this->assertTrue($class->fieldMappings['addresses']['isCascadeDetach']);
}
public function testEmbedWithCascadeThrowsMappingException()
{
$class = new ClassMetadata(EmbedWithCascadeTest::class);
$this->expectException(MappingException::class);
$this->expectExceptionMessage(
'Cascade on Doctrine\ODM\MongoDB\Tests\Mapping\EmbedWithCascadeTest::address is not allowed.'
);
$class->mapOneEmbedded([
'fieldName' => 'address',
'targetDocument' => Address::class,
'cascade' => 'all',
]);
}
public function testInvokeLifecycleCallbacksShouldRequireInstanceOfClass()
{
$class = $this->dm->getClassMetadata(User::class);
$document = new stdClass();
$this->assertInstanceOf('\stdClass', $document);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Expected document class "Documents\User"; found: "stdClass"');
$class->invokeLifecycleCallbacks(Events::prePersist, $document);
}
public function testInvokeLifecycleCallbacksAllowsInstanceOfClass()
{
$class = $this->dm->getClassMetadata(User::class);
$document = new SpecialUser();
$this->assertInstanceOf(SpecialUser::class, $document);
$class->invokeLifecycleCallbacks(Events::prePersist, $document);
}
public function testInvokeLifecycleCallbacksShouldAllowProxyOfExactClass()
{
$document = new User();
$this->dm->persist($document);
$this->dm->flush();
$this->dm->clear();
$class = $this->dm->getClassMetadata(User::class);
$proxy = $this->dm->getReference(User::class, $document->getId());
$this->assertInstanceOf(User::class, $proxy);
$class->invokeLifecycleCallbacks(Events::prePersist, $proxy);
}
public function testSimpleReferenceRequiresTargetDocument()
{
$cm = new ClassMetadata('stdClass');
$this->expectException(MappingException::class);
$this->expectExceptionMessage('Target document must be specified for identifier reference: stdClass::assoc');
$cm->mapField([
'fieldName' => 'assoc',
'reference' => true,
'type' => 'one',
'storeAs' => ClassMetadata::REFERENCE_STORE_AS_ID,
]);
}
public function testSimpleAsStringReferenceRequiresTargetDocument()
{
$cm = new ClassMetadata('stdClass');
$this->expectException(MappingException::class);
$this->expectExceptionMessage('Target document must be specified for identifier reference: stdClass::assoc');
$cm->mapField([
'fieldName' => 'assoc',
'reference' => true,
'type' => 'one',
'storeAs' => ClassMetadata::REFERENCE_STORE_AS_ID,
]);
}
/**
* @dataProvider provideRepositoryMethodCanNotBeCombinedWithSkipLimitAndSort
*/
public function testRepositoryMethodCanNotBeCombinedWithSkipLimitAndSort($prop, $value)
{
$cm = new ClassMetadata('stdClass');
$this->expectException(MappingException::class);
$this->expectExceptionMessage(
'\'repositoryMethod\' used on \'assoc\' in class \'stdClass\' can not be combined with skip, ' .
'limit or sort.'
);
$cm->mapField([
'fieldName' => 'assoc',
'reference' => true,
'type' => 'many',
'targetDocument' => 'stdClass',
'repositoryMethod' => 'fetch',
$prop => $value,
]);
}
public function provideRepositoryMethodCanNotBeCombinedWithSkipLimitAndSort()
{
yield ['skip', 5];
yield ['limit', 5];
yield ['sort', ['time' => 1]];
}
public function testStoreAsIdReferenceRequiresTargetDocument()
{
$cm = new ClassMetadata('stdClass');
$this->expectException(MappingException::class);
$this->expectExceptionMessage('Target document must be specified for identifier reference: stdClass::assoc');
$cm->mapField([
'fieldName' => 'assoc',
'reference' => true,
'type' => 'one',
'storeAs' => ClassMetadata::REFERENCE_STORE_AS_ID,
]);
}
public function testAtomicCollectionUpdateUsageInEmbeddedDocument()
{
$object = new class {
public $many;
};
$cm = new ClassMetadata(get_class($object));
$cm->isEmbeddedDocument = true;
$this->expectException(MappingException::class);
$this->expectExceptionMessage('atomicSet collection strategy can be used only in top level document, used in');
$cm->mapField([
'fieldName' => 'many',
'reference' => true,
'type' => 'many',
'strategy' => ClassMetadata::STORAGE_STRATEGY_ATOMIC_SET,
]);
}
public function testDefaultStorageStrategyOfEmbeddedDocumentFields()
{
$object = new class {
public $many;
};
$cm = new ClassMetadata(get_class($object));
$cm->isEmbeddedDocument = true;
$mapping = $cm->mapField([
'fieldName' => 'many',
'type' => 'many',
]);
self::assertEquals(CollectionHelper::DEFAULT_STRATEGY, $mapping['strategy']);
}
/**
* @dataProvider provideOwningAndInversedRefsNeedTargetDocument
*/
public function testOwningAndInversedRefsNeedTargetDocument($config)
{
$config = array_merge($config, [
'fieldName' => 'many',
'reference' => true,
'strategy' => ClassMetadata::STORAGE_STRATEGY_ATOMIC_SET,
]);
$cm = new ClassMetadata('stdClass');
$cm->isEmbeddedDocument = true;
$this->expectException(MappingException::class);
$cm->mapField($config);
}
public function provideOwningAndInversedRefsNeedTargetDocument()
{
return [
[['type' => 'one', 'mappedBy' => 'post']],
[['type' => 'one', 'inversedBy' => 'post']],
[['type' => 'many', 'mappedBy' => 'post']],
[['type' => 'many', 'inversedBy' => 'post']],
];
}
public function testAddInheritedAssociationMapping()
{
$cm = new ClassMetadata('stdClass');
$mapping = [
'fieldName' => 'assoc',
'reference' => true,
'type' => 'one',
'storeAs' => ClassMetadata::REFERENCE_STORE_AS_ID,
];
$cm->addInheritedAssociationMapping($mapping);
$expected = ['assoc' => $mapping];
$this->assertEquals($expected, $cm->associationMappings);
}
public function testIdFieldsTypeMustNotBeOverridden()
{
$cm = new ClassMetadata('stdClass');
$cm->setIdentifier('id');
$this->expectException(MappingException::class);
$this->expectExceptionMessage('stdClass::id was declared an identifier and must stay this way.');
$cm->mapField([
'fieldName' => 'id',
'type' => 'string',
]);
}
public function testReferenceManySortMustNotBeUsedWithNonSetCollectionStrategy()
{
$cm = new ClassMetadata('stdClass');
$this->expectException(MappingException::class);
$this->expectExceptionMessage(
'ReferenceMany\'s sort can not be used with addToSet and pushAll strategies, ' .
'pushAll used in stdClass::ref'
);
$cm->mapField([
'fieldName' => 'ref',
'reference' => true,
'strategy' => ClassMetadata::STORAGE_STRATEGY_PUSH_ALL,
'type' => 'many',
'sort' => ['foo' => 1],
]);
}
public function testSetShardKeyForClassWithoutInheritance()
{
$cm = new ClassMetadata('stdClass');
$cm->setShardKey(['id' => 'asc']);
$shardKey = $cm->getShardKey();
$this->assertEquals(['id' => 1], $shardKey['keys']);
}
public function testSetShardKeyForClassWithSingleCollectionInheritance()
{
$cm = new ClassMetadata('stdClass');
$cm->inheritanceType = ClassMetadata::INHERITANCE_TYPE_SINGLE_COLLECTION;
$cm->setShardKey(['id' => 'asc']);
$shardKey = $cm->getShardKey();
$this->assertEquals(['id' => 1], $shardKey['keys']);
}
public function testSetShardKeyForClassWithSingleCollectionInheritanceWhichAlreadyHasIt()
{
$cm = new ClassMetadata('stdClass');
$cm->setShardKey(['id' => 'asc']);
$cm->inheritanceType = ClassMetadata::INHERITANCE_TYPE_SINGLE_COLLECTION;
$this->expectException(MappingException::class);
$this->expectExceptionMessage('Shard key overriding in subclass is forbidden for single collection inheritance');
$cm->setShardKey(['foo' => 'asc']);
}
public function testSetShardKeyForClassWithCollPerClassInheritance()
{
$cm = new ClassMetadata('stdClass');
$cm->inheritanceType = ClassMetadata::INHERITANCE_TYPE_COLLECTION_PER_CLASS;
$cm->setShardKey(['id' => 'asc']);
$shardKey = $cm->getShardKey();
$this->assertEquals(['id' => 1], $shardKey['keys']);
}
public function testIsNotShardedIfThereIsNoShardKey()
{
$cm = new ClassMetadata('stdClass');
$this->assertFalse($cm->isSharded());
}
public function testIsShardedIfThereIsAShardKey()
{
$cm = new ClassMetadata('stdClass');
$cm->setShardKey(['id' => 'asc']);
$this->assertTrue($cm->isSharded());
}
public function testEmbeddedDocumentCantHaveShardKey()
{
$cm = new ClassMetadata('stdClass');
$cm->isEmbeddedDocument = true;
$this->expectException(MappingException::class);
$this->expectExceptionMessage('Embedded document can\'t have shard key: stdClass');
$cm->setShardKey(['id' => 'asc']);
}
public function testNoIncrementFieldsAllowedInShardKey()
{
$object = new class {
public $inc;
};
$cm = new ClassMetadata(get_class($object));
$cm->mapField([
'fieldName' => 'inc',
'type' => 'int',
'strategy' => ClassMetadata::STORAGE_STRATEGY_INCREMENT,
]);
$this->expectException(MappingException::class);
$this->expectExceptionMessage('Only fields using the SET strategy can be used in the shard key');
$cm->setShardKey(['inc' => 1]);
}
public function testNoCollectionsInShardKey()
{
$object = new class {
public $collection;
};
$cm = new ClassMetadata(get_class($object));
$cm->mapField([
'fieldName' => 'collection',
'type' => 'collection',
]);
$this->expectException(MappingException::class);
$this->expectExceptionMessage('No multikey indexes are allowed in the shard key');
$cm->setShardKey(['collection' => 1]);
}
public function testNoEmbedManyInShardKey()
{
$object = new class {
public $embedMany;
};
$cm = new ClassMetadata(get_class($object));
$cm->mapManyEmbedded(['fieldName' => 'embedMany']);
$this->expectException(MappingException::class);
$this->expectExceptionMessage('No multikey indexes are allowed in the shard key');
$cm->setShardKey(['embedMany' => 1]);
}
public function testNoReferenceManyInShardKey()
{
$object = new class {
public $referenceMany;
};
$cm = new ClassMetadata(get_class($object));
$cm->mapManyEmbedded(['fieldName' => 'referenceMany']);
$this->expectException(MappingException::class);
$this->expectExceptionMessage('No multikey indexes are allowed in the shard key');
$cm->setShardKey(['referenceMany' => 1]);
}
public function testArbitraryFieldInGridFSFileThrowsException(): void
{
$object = new class {
public $contentType;
};
$cm = new ClassMetadata(get_class($object));
$cm->isFile = true;
$this->expectException(MappingException::class);
$this->expectExceptionMessageMatches("#^Field 'contentType' in class '.+' is not a valid field for GridFS documents. You should move it to an embedded metadata document.$#");
$cm->mapField(['type' => 'string', 'fieldName' => 'contentType']);
}
}
class TestCustomRepositoryClass extends DocumentRepository
{
}
class EmbedWithCascadeTest
{
public $address;
}
/** @ODM\Document */
class EmbeddedAssociationsCascadeTest
{
/** @ODM\Id */
public $id;
/** @ODM\EmbedOne(targetDocument=Documents\Address::class) */
public $address;
/** @ODM\EmbedOne(targetDocument=Documents\Address::class) */
public $addresses;
}