Completed
Push — master ( a0071b...e33605 )
by Michael
12s
created

ORM/Functional/StandardEntityPersisterTest.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Functional;
6
7
use Doctrine\ORM\PersistentCollection;
8
use Doctrine\Tests\Models\ECommerce\ECommerceCart;
9
use Doctrine\Tests\Models\ECommerce\ECommerceCustomer;
10
use Doctrine\Tests\Models\ECommerce\ECommerceFeature;
11
use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
12
use Doctrine\Tests\OrmFunctionalTestCase;
13
14
/**
15
 * Tests capabilities of the persister.
16
 * @author Giorgio Sironi <[email protected]>
17
 */
18
class StandardEntityPersisterTest extends OrmFunctionalTestCase
19
{
20
    protected function setUp()
21
    {
22
        $this->useModelSet('ecommerce');
23
        parent::setUp();
24
    }
25
26
    public function testAcceptsForeignKeysAsCriteria()
27
    {
28
        $customer = new ECommerceCustomer();
29
        $customer->setName('John Doe');
30
        $cart = new ECommerceCart();
31
        $cart->setPayment('Credit card');
32
        $customer->setCart($cart);
33
        $this->em->persist($customer);
34
        $this->em->flush();
35
        $this->em->clear();
36
        $cardId = $cart->getId();
37
        unset($cart);
38
39
        $class = $this->em->getClassMetadata(ECommerceCart::class);
40
41
        $persister = $this->em->getUnitOfWork()->getEntityPersister(ECommerceCart::class);
42
        $newCart = new ECommerceCart();
43
        $this->em->getUnitOfWork()->registerManaged($newCart, ['id' => $cardId], []);
44
        $persister->load(['customer_id' => $customer->getId()], $newCart, $class->getProperty('customer'));
45
        self::assertEquals('Credit card', $newCart->getPayment());
46
    }
47
48
    /**
49
     * Ticket #2478 from Damon Jones (dljones)
50
     */
51
    public function testAddPersistRetrieve()
52
    {
53
        $f1 = new ECommerceFeature;
54
        $f1->setDescription('AC-3');
55
56
        $f2 = new ECommerceFeature;
57
        $f2->setDescription('DTS');
58
59
        $p = new ECommerceProduct;
60
        $p->addFeature($f1);
61
        $p->addFeature($f2);
62
        $this->em->persist($p);
63
64
        $this->em->flush();
65
66
        self::assertCount(2, $p->getFeatures());
67
        self::assertInstanceOf(PersistentCollection::class, $p->getFeatures());
68
69
        $q = $this->em->createQuery(
70
            'SELECT p, f
71
               FROM Doctrine\Tests\Models\ECommerce\ECommerceProduct p
72
               JOIN p.features f'
73
        );
74
75
        $res = $q->getResult();
0 ignored issues
show
The assignment to $res is dead and can be removed.
Loading history...
76
77
        self::assertCount(2, $p->getFeatures());
78
        self::assertInstanceOf(PersistentCollection::class, $p->getFeatures());
79
80
        // Check that the features are the same instances still
81
        foreach ($p->getFeatures() as $feature) {
82
            if ($feature->getDescription() == 'AC-3') {
83
                self::assertSame($feature, $f1);
84
            } else {
85
                self::assertSame($feature, $f2);
86
            }
87
        }
88
89
        // Now we test how Hydrator affects IdentityMap
90
        // (change from ArrayCollection to PersistentCollection)
91
        $f3 = new ECommerceFeature();
92
        $f3->setDescription('XVID');
93
        $p->addFeature($f3);
94
95
        // Now we persist the Feature #3
96
        $this->em->persist($p);
97
        $this->em->flush();
98
99
        $q = $this->em->createQuery(
100
            'SELECT p, f
101
               FROM Doctrine\Tests\Models\ECommerce\ECommerceProduct p
102
               JOIN p.features f'
103
        );
104
105
        $res = $q->getResult();
106
107
        // Persisted Product now must have 3 Feature items
108
        self::assertCount(3, $res[0]->getFeatures());
109
    }
110
}
111