Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / StandardEntityPersisterTest.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional;
4
5 use Doctrine\Tests\Models\ECommerce\ECommerceCart,
6     Doctrine\Tests\Models\ECommerce\ECommerceFeature,
7     Doctrine\Tests\Models\ECommerce\ECommerceCustomer,
8     Doctrine\Tests\Models\ECommerce\ECommerceProduct;
9
10 use Doctrine\ORM\Mapping\AssociationMapping;
11
12 require_once __DIR__ . '/../../TestInit.php';
13
14 /**
15  * Tests capabilities of the persister.
16  * @author Giorgio Sironi <piccoloprincipeazzurro@gmail.com>
17  */
18 class StandardEntityPersisterTest extends \Doctrine\Tests\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('Doctrine\Tests\Models\ECommerce\ECommerceCart');
40
41         $persister = $this->_em->getUnitOfWork()->getEntityPersister('Doctrine\Tests\Models\ECommerce\ECommerceCart');
42         $newCart = new ECommerceCart();
43         $this->_em->getUnitOfWork()->registerManaged($newCart, array('id' => $cardId), array());
44         $persister->load(array('customer_id' => $customer->getId()), $newCart, $class->associationMappings['customer']);
45         $this->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         $this->assertEquals(2, count($p->getFeatures()));
67         $this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $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();
76
77         $this->assertEquals(2, count($p->getFeatures()));
78         $this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $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                 $this->assertTrue($feature === $f1);
84             } else {
85                 $this->assertTrue($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         $this->assertEquals(3, count($res[0]->getFeatures()));
109     }
110 }