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