Failed Conditions
Pull Request — 2.7 (#8138)
by Benjamin
07:27
created

ValueObjectsTest::testEmbeddableIsNotTransient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional;
4
use Doctrine\Common\Reflection\RuntimePublicReflectionProperty;
5
use Doctrine\ORM\Mapping\MappingException;
6
use Doctrine\ORM\Mapping\ReflectionEmbeddedProperty;
7
use Doctrine\ORM\Query\QueryException;
8
use Doctrine\Tests\OrmFunctionalTestCase;
9
10
/**
11
 * @group DDC-93
12
 */
13
class ValueObjectsTest extends OrmFunctionalTestCase
14
{
15
    public function setUp()
16
    {
17
        parent::setUp();
18
19
        try {
20
            $this->_schemaTool->createSchema(
21
                [
22
                $this->_em->getClassMetadata(DDC93Person::class),
23
                $this->_em->getClassMetadata(DDC93Address::class),
24
                $this->_em->getClassMetadata(DDC93Vehicle::class),
25
                $this->_em->getClassMetadata(DDC93Car::class),
26
                $this->_em->getClassMetadata(DDC3027Animal::class),
27
                $this->_em->getClassMetadata(DDC3027Dog::class),
28
                ]
29
            );
30
        } catch(\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
31
        }
32
    }
33
34
    public function testMetadataHasReflectionEmbeddablesAccessible()
35
    {
36
        $classMetadata = $this->_em->getClassMetadata(DDC93Person::class);
37
38
        $this->assertInstanceOf(RuntimePublicReflectionProperty::class, $classMetadata->getReflectionProperty('address'));
39
        $this->assertInstanceOf(ReflectionEmbeddedProperty::class, $classMetadata->getReflectionProperty('address.street'));
40
    }
41
42
    public function testCRUD()
43
    {
44
        $person = new DDC93Person();
45
        $person->name = "Tara";
46
        $person->address = new DDC93Address();
47
        $person->address->street = "United States of Tara Street";
48
        $person->address->zip = "12345";
49
        $person->address->city = "funkytown";
50
        $person->address->country = new DDC93Country('Germany');
51
52
        // 1. check saving value objects works
53
        $this->_em->persist($person);
54
        $this->_em->flush();
55
56
        $this->_em->clear();
57
58
        // 2. check loading value objects works
59
        $person = $this->_em->find(DDC93Person::class, $person->id);
60
61
        $this->assertInstanceOf(DDC93Address::class, $person->address);
62
        $this->assertEquals('United States of Tara Street', $person->address->street);
63
        $this->assertEquals('12345', $person->address->zip);
64
        $this->assertEquals('funkytown', $person->address->city);
65
        $this->assertInstanceOf(DDC93Country::class, $person->address->country);
66
        $this->assertEquals('Germany', $person->address->country->name);
67
68
        // 3. check changing value objects works
69
        $person->address->street = "Street";
70
        $person->address->zip = "54321";
71
        $person->address->city = "another town";
72
        $person->address->country->name = "United States of America";
73
        $this->_em->flush();
74
75
        $this->_em->clear();
76
77
        $person = $this->_em->find(DDC93Person::class, $person->id);
78
79
        $this->assertEquals('Street', $person->address->street);
80
        $this->assertEquals('54321', $person->address->zip);
81
        $this->assertEquals('another town', $person->address->city);
82
        $this->assertEquals('United States of America', $person->address->country->name);
83
84
        // 4. check deleting works
85
        $personId = $person->id;;
86
        $this->_em->remove($person);
87
        $this->_em->flush();
88
89
        $this->assertNull($this->_em->find(DDC93Person::class, $personId));
90
    }
91
92
    public function testLoadDql()
93
    {
94
        for ($i = 0; $i < 3; $i++) {
95
            $person = new DDC93Person();
96
            $person->name = "Donkey Kong$i";
97
            $person->address = new DDC93Address();
98
            $person->address->street = "Tree";
99
            $person->address->zip = "12345";
100
            $person->address->city = "funkytown";
101
            $person->address->country = new DDC93Country('United States of America');
102
103
            $this->_em->persist($person);
104
        }
105
106
        $this->_em->flush();
107
        $this->_em->clear();
108
109
        $dql = "SELECT p FROM " . __NAMESPACE__ . "\DDC93Person p";
110
        $persons = $this->_em->createQuery($dql)->getResult();
111
112
        $this->assertCount(3, $persons);
113
        foreach ($persons as $person) {
114
            $this->assertInstanceOf(DDC93Address::class, $person->address);
115
            $this->assertEquals('Tree', $person->address->street);
116
            $this->assertEquals('12345', $person->address->zip);
117
            $this->assertEquals('funkytown', $person->address->city);
118
            $this->assertInstanceOf(DDC93Country::class, $person->address->country);
119
            $this->assertEquals('United States of America', $person->address->country->name);
120
        }
121
122
        $dql = "SELECT p FROM " . __NAMESPACE__ . "\DDC93Person p";
123
        $persons = $this->_em->createQuery($dql)->getArrayResult();
124
125
        foreach ($persons as $person) {
126
            $this->assertEquals('Tree', $person['address.street']);
127
            $this->assertEquals('12345', $person['address.zip']);
128
            $this->assertEquals('funkytown', $person['address.city']);
129
            $this->assertEquals('United States of America', $person['address.country.name']);
130
        }
131
    }
132
133
    /**
134
     * @group dql
135
     */
136
    public function testDqlOnEmbeddedObjectsField()
137
    {
138
        if ($this->isSecondLevelCacheEnabled) {
139
            $this->markTestSkipped('SLC does not work with UPDATE/DELETE queries through EM.');
140
        }
141
142
        $person = new DDC93Person('Johannes', new DDC93Address('Moo', '12345', 'Karlsruhe', new DDC93Country('Germany')));
143
        $this->_em->persist($person);
144
        $this->_em->flush();
145
146
        // SELECT
147
        $selectDql = "SELECT p FROM " . __NAMESPACE__ ."\\DDC93Person p WHERE p.address.city = :city AND p.address.country.name = :country";
148
        $loadedPerson = $this->_em->createQuery($selectDql)
149
            ->setParameter('city', 'Karlsruhe')
150
            ->setParameter('country', 'Germany')
151
            ->getSingleResult();
152
        $this->assertEquals($person, $loadedPerson);
153
154
        $this->assertNull(
155
            $this->_em->createQuery($selectDql)
156
                ->setParameter('city', 'asdf')
157
                ->setParameter('country', 'Germany')
158
                ->getOneOrNullResult()
159
        );
160
161
        // UPDATE
162
        $updateDql = "UPDATE " . __NAMESPACE__ . "\\DDC93Person p SET p.address.street = :street, p.address.country.name = :country WHERE p.address.city = :city";
163
        $this->_em->createQuery($updateDql)
164
            ->setParameter('street', 'Boo')
165
            ->setParameter('country', 'DE')
166
            ->setParameter('city', 'Karlsruhe')
167
            ->execute();
168
169
        $this->_em->refresh($person);
170
        $this->assertEquals('Boo', $person->address->street);
171
        $this->assertEquals('DE', $person->address->country->name);
172
173
        // DELETE
174
        $this->_em->createQuery("DELETE " . __NAMESPACE__ . "\\DDC93Person p WHERE p.address.city = :city AND p.address.country.name = :country")
175
            ->setParameter('city', 'Karlsruhe')
176
            ->setParameter('country', 'DE')
177
            ->execute();
178
179
        $this->_em->clear();
180
        $this->assertNull($this->_em->find(DDC93Person::class, $person->id));
181
    }
182
183
    public function testPartialDqlOnEmbeddedObjectsField()
184
    {
185
        $person = new DDC93Person('Karl', new DDC93Address('Foo', '12345', 'Gosport', new DDC93Country('England')));
186
        $this->_em->persist($person);
187
        $this->_em->flush();
188
        $this->_em->clear();
189
190
        // Prove that the entity was persisted correctly.
191
        $dql = "SELECT p FROM " . __NAMESPACE__ ."\\DDC93Person p WHERE p.name = :name";
192
193
        $person = $this->_em->createQuery($dql)
194
            ->setParameter('name', 'Karl')
195
            ->getSingleResult();
196
197
        $this->assertEquals('Gosport', $person->address->city);
198
        $this->assertEquals('Foo', $person->address->street);
199
        $this->assertEquals('12345', $person->address->zip);
200
        $this->assertEquals('England', $person->address->country->name);
201
202
        // Clear the EM and prove that the embeddable can be the subject of a partial query.
203
        $this->_em->clear();
204
205
        $dql = "SELECT PARTIAL p.{id,address.city} FROM " . __NAMESPACE__ ."\\DDC93Person p WHERE p.name = :name";
206
207
        $person = $this->_em->createQuery($dql)
208
            ->setParameter('name', 'Karl')
209
            ->getSingleResult();
210
211
        // Selected field must be equal, all other fields must be null.
212
        $this->assertEquals('Gosport', $person->address->city);
213
        $this->assertNull($person->address->street);
214
        $this->assertNull($person->address->zip);
215
        $this->assertNull($person->address->country);
216
        $this->assertNull($person->name);
217
218
        // Clear the EM and prove that the embeddable can be the subject of a partial query regardless of attributes positions.
219
        $this->_em->clear();
220
221
        $dql = "SELECT PARTIAL p.{address.city, id} FROM " . __NAMESPACE__ ."\\DDC93Person p WHERE p.name = :name";
222
223
        $person = $this->_em->createQuery($dql)
224
            ->setParameter('name', 'Karl')
225
            ->getSingleResult();
226
227
        // Selected field must be equal, all other fields must be null.
228
        $this->assertEquals('Gosport', $person->address->city);
229
        $this->assertNull($person->address->street);
230
        $this->assertNull($person->address->zip);
231
        $this->assertNull($person->address->country);
232
        $this->assertNull($person->name);
233
    }
234
235
    public function testDqlWithNonExistentEmbeddableField()
236
    {
237
        $this->expectException(QueryException::class);
238
        $this->expectExceptionMessage('no field or association named address.asdfasdf');
239
240
        $this->_em->createQuery("SELECT p FROM " . __NAMESPACE__ . "\\DDC93Person p WHERE p.address.asdfasdf IS NULL")
241
            ->execute();
242
    }
243
244
    public function testPartialDqlWithNonExistentEmbeddableField()
245
    {
246
        $this->expectException(QueryException::class);
247
        $this->expectExceptionMessage("no mapped field named 'address.asdfasdf'");
248
249
        $this->_em->createQuery("SELECT PARTIAL p.{id,address.asdfasdf} FROM " . __NAMESPACE__ . "\\DDC93Person p")
250
            ->execute();
251
    }
252
253
    public function testEmbeddableWithInheritance()
254
    {
255
        $car = new DDC93Car(new DDC93Address('Foo', '12345', 'Asdf'));
256
        $this->_em->persist($car);
257
        $this->_em->flush();
258
259
        $reloadedCar = $this->_em->find(DDC93Car::class, $car->id);
260
        $this->assertEquals($car, $reloadedCar);
261
    }
262
263
    public function testInlineEmbeddableWithPrefix()
264
    {
265
        $metadata = $this->_em->getClassMetadata(DDC3028PersonWithPrefix::class);
266
267
        $this->assertEquals('foobar_id', $metadata->getColumnName('id.id'));
268
        $this->assertEquals('bloo_foo_id', $metadata->getColumnName('nested.nestedWithPrefix.id'));
269
        $this->assertEquals('bloo_nestedWithEmptyPrefix_id', $metadata->getColumnName('nested.nestedWithEmptyPrefix.id'));
270
        $this->assertEquals('bloo_id', $metadata->getColumnName('nested.nestedWithPrefixFalse.id'));
271
    }
272
273
    public function testInlineEmbeddableEmptyPrefix()
274
    {
275
        $metadata = $this->_em->getClassMetadata(DDC3028PersonEmptyPrefix::class);
276
277
        $this->assertEquals('id_id', $metadata->getColumnName('id.id'));
278
        $this->assertEquals('nested_foo_id', $metadata->getColumnName('nested.nestedWithPrefix.id'));
279
        $this->assertEquals('nested_nestedWithEmptyPrefix_id', $metadata->getColumnName('nested.nestedWithEmptyPrefix.id'));
280
        $this->assertEquals('nested_id', $metadata->getColumnName('nested.nestedWithPrefixFalse.id'));
281
    }
282
283
    public function testInlineEmbeddablePrefixFalse()
284
    {
285
        $expectedColumnName = 'id';
286
287
        $actualColumnName = $this->_em
288
            ->getClassMetadata(DDC3028PersonPrefixFalse::class)
289
            ->getColumnName('id.id');
290
291
        $this->assertEquals($expectedColumnName, $actualColumnName);
292
    }
293
294
    public function testInlineEmbeddableInMappedSuperClass()
295
    {
296
        $isFieldMapped = $this->_em
297
            ->getClassMetadata(DDC3027Dog::class)
298
            ->hasField('address.street');
299
300
        $this->assertTrue($isFieldMapped);
301
    }
302
303
    /**
304
     * @dataProvider getInfiniteEmbeddableNestingData
305
     */
306
    public function testThrowsExceptionOnInfiniteEmbeddableNesting($embeddableClassName, $declaredEmbeddableClassName)
307
    {
308
        $this->expectException(MappingException::class);
309
        $this->expectExceptionMessage(
310
            sprintf(
311
                'Infinite nesting detected for embedded property %s::nested. ' .
312
                'You cannot embed an embeddable from the same type inside an embeddable.',
313
                __NAMESPACE__ . '\\' . $declaredEmbeddableClassName
314
            )
315
        );
316
317
        $this->_schemaTool->createSchema(
318
            [
319
            $this->_em->getClassMetadata(__NAMESPACE__ . '\\' . $embeddableClassName),
320
            ]
321
        );
322
    }
323
324
    public function getInfiniteEmbeddableNestingData()
325
    {
326
        return [
327
            ['DDCInfiniteNestingEmbeddable', 'DDCInfiniteNestingEmbeddable'],
328
            ['DDCNestingEmbeddable1', 'DDCNestingEmbeddable4'],
329
        ];
330
    }
331
}
332
333
334
/**
335
 * @Entity
336
 */
337
class DDC93Person
338
{
339
    /** @Id @GeneratedValue @Column(type="integer") */
340
    public $id;
341
342
    /** @Column(type="string") */
343
    public $name;
344
345
    /** @Embedded(class="DDC93Address") */
346
    public $address;
347
348
    /** @Embedded(class = "DDC93Timestamps") */
349
    public $timestamps;
350
351
    public function __construct($name = null, DDC93Address $address = null)
352
    {
353
        $this->name = $name;
354
        $this->address = $address;
355
        $this->timestamps = new DDC93Timestamps(new \DateTime);
356
    }
357
}
358
359
/**
360
 * @Embeddable
361
 */
362
class DDC93Timestamps
363
{
364
    /** @Column(type = "datetime") */
365
    public $createdAt;
366
367
    public function __construct(\DateTime $createdAt)
368
    {
369
        $this->createdAt = $createdAt;
370
    }
371
}
372
373
/**
374
 * @Entity
375
 *
376
 * @InheritanceType("SINGLE_TABLE")
377
 * @DiscriminatorColumn(name = "t", type = "string", length = 10)
378
 * @DiscriminatorMap({
379
 *     "v" = "Doctrine\Tests\ORM\Functional\DDC93Car",
380
 * })
381
 */
382
abstract class DDC93Vehicle
383
{
384
    /** @Id @GeneratedValue(strategy = "AUTO") @Column(type = "integer") */
385
    public $id;
386
387
    /** @Embedded(class = "DDC93Address") */
388
    public $address;
389
390
    public function __construct(DDC93Address $address)
391
    {
392
        $this->address = $address;
393
    }
394
}
395
396
/**
397
 * @Entity
398
 */
399
class DDC93Car extends DDC93Vehicle
400
{
401
}
402
403
/**
404
 * @Embeddable
405
 */
406
class DDC93Country
407
{
408
    /**
409
     * @Column(type="string", nullable=true)
410
     */
411
    public $name;
412
413
    public function __construct($name = null)
414
    {
415
        $this->name = $name;
416
    }
417
}
418
419
/**
420
 * @Embeddable
421
 */
422
class DDC93Address
423
{
424
    /**
425
     * @Column(type="string")
426
     */
427
    public $street;
428
    /**
429
     * @Column(type="string")
430
     */
431
    public $zip;
432
    /**
433
     * @Column(type="string")
434
     */
435
    public $city;
436
    /** @Embedded(class = "DDC93Country") */
437
    public $country;
438
439
    public function __construct($street = null, $zip = null, $city = null, DDC93Country $country = null)
440
    {
441
        $this->street = $street;
442
        $this->zip = $zip;
443
        $this->city = $city;
444
        $this->country = $country;
445
    }
446
}
447
448
/** @Entity */
449
class DDC93Customer
450
{
451
    /** @Id @GeneratedValue @Column(type="integer") */
452
    private $id;
453
454
    /** @Embedded(class = "DDC93ContactInfo", columnPrefix = "contact_info_") */
455
    private $contactInfo;
0 ignored issues
show
introduced by
The private property $contactInfo is not used, and could be removed.
Loading history...
456
}
457
458
/** @Embeddable */
459
class DDC93ContactInfo
460
{
461
    /**
462
     * @Column(type="string")
463
     */
464
    public $email;
465
    /** @Embedded(class = "DDC93Address") */
466
    public $address;
467
}
468
469
/**
470
 * @Entity
471
 */
472
class DDC3028PersonWithPrefix
473
{
474
    /** @Embedded(class="DDC3028Id", columnPrefix = "foobar_") */
475
    public $id;
476
477
    /** @Embedded(class="DDC3028NestedEmbeddable", columnPrefix = "bloo_") */
478
    public $nested;
479
480
    public function __construct(DDC3028Id $id = null, DDC3028NestedEmbeddable $nested = null)
481
    {
482
        $this->id = $id;
483
        $this->nested = $nested;
484
    }
485
}
486
487
/**
488
 * @Entity
489
 */
490
class DDC3028PersonEmptyPrefix
491
{
492
    /** @Embedded(class="DDC3028Id", columnPrefix = "") */
493
    public $id;
494
495
    /** @Embedded(class="DDC3028NestedEmbeddable", columnPrefix = "") */
496
    public $nested;
497
498
    public function __construct(DDC3028Id $id = null, DDC3028NestedEmbeddable $nested = null)
499
    {
500
        $this->id = $id;
501
        $this->nested = $nested;
502
    }
503
}
504
505
/**
506
 * @Entity
507
 */
508
class DDC3028PersonPrefixFalse
509
{
510
    /** @Embedded(class="DDC3028Id", columnPrefix = false) */
511
    public $id;
512
513
    public function __construct(DDC3028Id $id = null)
514
    {
515
        $this->id = $id;
516
    }
517
}
518
519
/**
520
 * @Embeddable
521
 */
522
class DDC3028Id
523
{
524
    /**
525
     * @Id @Column(type="string")
526
     */
527
    public $id;
528
529
    public function __construct($id = null)
530
    {
531
        $this->id = $id;
532
    }
533
}
534
535
/**
536
 * @Embeddable
537
 */
538
class DDC3028NestedEmbeddable
539
{
540
    /** @Embedded(class="DDC3028Id", columnPrefix = "foo_") */
541
    public $nestedWithPrefix;
542
543
    /** @Embedded(class="DDC3028Id", columnPrefix = "") */
544
    public $nestedWithEmptyPrefix;
545
546
    /** @Embedded(class="DDC3028Id", columnPrefix = false) */
547
    public $nestedWithPrefixFalse;
548
549
    public function __construct(
550
        DDC3028Id $nestedWithPrefix = null,
551
        DDC3028Id $nestedWithEmptyPrefix = null,
552
        DDC3028Id $nestedWithPrefixFalse = null
553
    ) {
554
        $this->nestedWithPrefix = $nestedWithPrefix;
555
        $this->nestedWithEmptyPrefix = $nestedWithEmptyPrefix;
556
        $this->nestedWithPrefixFalse = $nestedWithPrefixFalse;
557
    }
558
}
559
560
/**
561
 * @MappedSuperclass
562
 */
563
abstract class DDC3027Animal
564
{
565
    /** @Id @GeneratedValue(strategy = "AUTO") @Column(type = "integer") */
566
    public $id;
567
568
    /** @Embedded(class = "DDC93Address") */
569
    public $address;
570
}
571
572
/**
573
 * @Entity
574
 */
575
class DDC3027Dog extends DDC3027Animal
576
{
577
}
578
579
/**
580
 * @Embeddable
581
 */
582
class DDCInfiniteNestingEmbeddable
583
{
584
    /** @Embedded(class="DDCInfiniteNestingEmbeddable") */
585
    public $nested;
586
}
587
588
/**
589
 * @Embeddable
590
 */
591
class DDCNestingEmbeddable1
592
{
593
    /** @Embedded(class="DDC3028Id") */
594
    public $id1;
595
596
    /** @Embedded(class="DDC3028Id") */
597
    public $id2;
598
599
    /** @Embedded(class="DDCNestingEmbeddable2") */
600
    public $nested;
601
}
602
603
/**
604
 * @Embeddable
605
 */
606
class DDCNestingEmbeddable2
607
{
608
    /** @Embedded(class="DDC3028Id") */
609
    public $id1;
610
611
    /** @Embedded(class="DDC3028Id") */
612
    public $id2;
613
614
    /** @Embedded(class="DDCNestingEmbeddable3") */
615
    public $nested;
616
}
617
618
/**
619
 * @Embeddable
620
 */
621
class DDCNestingEmbeddable3
622
{
623
    /** @Embedded(class="DDC3028Id") */
624
    public $id1;
625
626
    /** @Embedded(class="DDC3028Id") */
627
    public $id2;
628
629
    /** @Embedded(class="DDCNestingEmbeddable4") */
630
    public $nested;
631
}
632
633
/**
634
 * @Embeddable
635
 */
636
class DDCNestingEmbeddable4
637
{
638
    /** @Embedded(class="DDC3028Id") */
639
    public $id1;
640
641
    /** @Embedded(class="DDC3028Id") */
642
    public $id2;
643
644
    /** @Embedded(class="DDCNestingEmbeddable1") */
645
    public $nested;
646
}
647