Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / ProxiesLikeEntitiesTest.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional;
4
5 use Doctrine\Common\Util\ClassUtils,
6     Doctrine\Tests\Models\CMS\CmsUser,
7     Doctrine\Tests\Proxies\__CG__\Doctrine\Tests\Models\CMS\CmsUser as Proxy;
8
9 /**
10  * Test that Doctrine ORM correctly works with proxy instances exactly like with ordinary Entities
11  *
12  * The test considers two possible cases:
13  *  a) __initialized__ = true and no identifier set in proxy
14  *  b) __initialized__ = false and identifier set in proxy and in property
15  * @todo All other cases would cause lazy loading issues
16  */
17 class ProxiesLikeEntitiesTest extends \Doctrine\Tests\OrmFunctionalTestCase
18 {
19     /**
20      * @var CmsUser
21      */
22     protected $user;
23
24     protected function setUp()
25     {
26         parent::setUp();
27         try {
28             $this->_schemaTool->createSchema(array(
29                 $this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsUser'),
30             ));
31         } catch (\Exception $e) {
32         }
33         $this->user = new CmsUser();
34         $this->user->username = 'ocramius';
35         $this->user->name = 'Marco';
36         $this->_em->persist($this->user);
37         $this->_em->flush();
38         $this->_em->clear();
39     }
40
41     /**
42      * Verifies that a proxy can be successfully persisted and updated
43      */
44     public function testPersistUpdate()
45     {
46         // Considering case (a)
47         $persister = $this->_em->getUnitOfWork()->getEntityPersister('Doctrine\Tests\Models\CMS\CmsUser');
48         $proxy = new Proxy($persister, array());
49         $proxy->__isInitialized__ = true;
50         $proxy->username = 'ocra';
51         $proxy->name = 'Marco';
52         $this->_em->persist($proxy);
53         $this->_em->flush();
54         $this->assertNotNull($proxy->getId());
55         $proxy->name = 'Marco Pivetta';
56         $this
57             ->_em
58             ->getUnitOfWork()
59             ->computeChangeSet($this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsUser'), $proxy);
60         $this->assertNotEmpty($this->_em->getUnitOfWork()->getEntityChangeSet($proxy));
61         $this->assertEquals('Marco Pivetta', $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $proxy->getId())->name);
62         $this->_em->remove($proxy);
63         $this->_em->flush();
64     }
65
66     public function testEntityWithIdentifier()
67     {
68         // Considering case (b)
69         $persister = $this->_em->getUnitOfWork()->getEntityPersister('Doctrine\Tests\Models\CMS\CmsUser');
70         $uninitializedProxy = new Proxy($persister, array('id' => $this->user->getId()));
71         $uninitializedProxy->id = $this->user->getId();
72         $uninitializedProxy->username = 'ocra';
73         $uninitializedProxy->name = 'Marco Pivetta';
74         $this->_em->persist($uninitializedProxy);
75         $this->_em->flush();
76         $this->assertEquals($this->user->getId(), $uninitializedProxy->getId());
77         $this->_em->remove($uninitializedProxy);
78         $this->_em->flush();
79     }
80
81     /**
82      * Verifying that proxies can be used without problems as query parameters
83      */
84     public function testProxyAsDqlParameterPersist()
85     {
86         $persister = $this->_em->getUnitOfWork()->getEntityPersister('Doctrine\Tests\Models\CMS\CmsUser');
87         $proxy = new Proxy($persister, array('id' => $this->user->getId()));
88         $proxy->id = $this->user->getId();
89         $result = $this
90             ->_em
91             ->createQuery('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u = ?1')
92             ->setParameter(1, $proxy)
93             ->getSingleResult();
94         $this->assertSame($this->user->getId(), $result->getId());
95         $this->_em->remove($proxy);
96         $this->_em->flush();
97     }
98
99     /**
100      * Verifying that proxies can be used without problems as query parameters
101      */
102     public function testFindWithProxyName()
103     {
104         $result = $this
105             ->_em
106             ->find('Doctrine\Tests\Proxies\__CG__\Doctrine\Tests\Models\CMS\CmsUser', $this->user->getId());
107         $this->assertSame($this->user->getId(), $result->getId());
108         $this->_em->clear();
109         $result = $this
110             ->_em
111             ->getReference('Doctrine\Tests\Proxies\__CG__\Doctrine\Tests\Models\CMS\CmsUser', $this->user->getId());
112         $this->assertSame($this->user->getId(), $result->getId());
113         $this->_em->clear();
114         $result = $this
115             ->_em
116             ->getRepository('Doctrine\Tests\Proxies\__CG__\Doctrine\Tests\Models\CMS\CmsUser')
117             ->findOneBy(array('username' => $this->user->username));
118         $this->assertSame($this->user->getId(), $result->getId());
119         $this->_em->clear();
120         $result = $this
121             ->_em
122             ->createQuery('SELECT u FROM Doctrine\Tests\Proxies\__CG__\Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1')
123             ->setParameter(1, $this->user->getId())
124             ->getSingleResult();
125         $this->assertSame($this->user->getId(), $result->getId());
126         $this->_em->clear();
127     }
128
129     protected function tearDown()
130     {
131         $this->_em->createQuery('DELETE FROM Doctrine\Tests\Models\CMS\CmsUser u')->execute();
132     }
133 }