Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / HydrationCacheTest.php
1 <?php
2 namespace Doctrine\Tests\ORM\Functional;
3
4 use Doctrine\Tests\OrmFunctionalTestCase;
5 use Doctrine\Tests\Models\Cms\CmsUser;
6 use Doctrine\DBAL\Cache\QueryCacheProfile;
7 use Doctrine\Common\Cache\ArrayCache;
8
9 /**
10  * @group DDC-1766
11  */
12 class HydrationCacheTest extends OrmFunctionalTestCase
13 {
14     public function setUp()
15     {
16         $this->useModelSet('cms');
17
18         parent::setUp();
19
20         $user = new CmsUser;
21         $user->name = "Benjamin";
22         $user->username = "beberlei";
23         $user->status = 'active';
24
25         $this->_em->persist($user);
26         $this->_em->flush();
27         $this->_em->clear();
28     }
29
30     public function testHydrationCache()
31     {
32         $cache = new ArrayCache();
33         $dql = "SELECT u FROM Doctrine\Tests\Models\Cms\CmsUser u";
34
35         $users = $this->_em->createQuery($dql)
36                       ->setHydrationCacheProfile(new QueryCacheProfile(null, null, $cache))
37                       ->getResult();
38
39         $c = $this->getCurrentQueryCount();
40         $users = $this->_em->createQuery($dql)
41                       ->setHydrationCacheProfile(new QueryCacheProfile(null, null, $cache))
42                       ->getResult();
43
44         $this->assertEquals($c, $this->getCurrentQueryCount(), "Should not execute query. Its cached!");
45
46         $users = $this->_em->createQuery($dql)
47                       ->setHydrationCacheProfile(new QueryCacheProfile(null, null, $cache))
48                       ->getArrayResult();
49
50         $this->assertEquals($c + 1, $this->getCurrentQueryCount(), "Hydration is part of cache key.");
51
52         $users = $this->_em->createQuery($dql)
53                       ->setHydrationCacheProfile(new QueryCacheProfile(null, null, $cache))
54                       ->getArrayResult();
55
56         $this->assertEquals($c + 1, $this->getCurrentQueryCount(), "Hydration now cached");
57
58         $users = $this->_em->createQuery($dql)
59                       ->setHydrationCacheProfile(new QueryCacheProfile(null, 'cachekey', $cache))
60                       ->getArrayResult();
61
62         $this->assertTrue($cache->contains('cachekey'), 'Explicit cache key');
63
64         $users = $this->_em->createQuery($dql)
65                       ->setHydrationCacheProfile(new QueryCacheProfile(null, 'cachekey', $cache))
66                       ->getArrayResult();
67         $this->assertEquals($c + 2, $this->getCurrentQueryCount(), "Hydration now cached");
68     }
69
70     public function testHydrationParametersSerialization()
71     {
72         $cache = new ArrayCache();
73
74         $dql   = "SELECT u FROM Doctrine\Tests\Models\Cms\CmsUser u WHERE u.id = ?1";
75         $query = $this->_em->createQuery($dql)
76             ->setParameter(1, $userId = 1)
77             ->setHydrationCacheProfile(new QueryCacheProfile(null, null, $cache));
78
79         $query->getResult();
80
81         $c = $this->getCurrentQueryCount();
82
83         $query->getResult();
84
85         $this->assertEquals($c, $this->getCurrentQueryCount(), "Should not execute query. Its cached!");
86     }
87 }
88