Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / QueryCacheTest.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional;
4
5 use Doctrine\Tests\Models\CMS\CmsUser;
6 use Doctrine\Common\Cache\ArrayCache;
7
8 require_once __DIR__ . '/../../TestInit.php';
9
10 /**
11  * QueryCacheTest
12  *
13  * @author robo
14  */
15 class QueryCacheTest extends \Doctrine\Tests\OrmFunctionalTestCase
16 {
17     /**
18      * @var \ReflectionProperty
19      */
20     private $cacheDataReflection;
21
22     protected function setUp()
23     {
24         $this->cacheDataReflection = new \ReflectionProperty("Doctrine\Common\Cache\ArrayCache", "data");
25         $this->cacheDataReflection->setAccessible(true);
26
27         $this->useModelSet('cms');
28
29         parent::setUp();
30     }
31
32     /**
33      * @param   ArrayCache $cache
34      * @return  integer
35      */
36     private function getCacheSize(ArrayCache $cache)
37     {
38         return sizeof($this->cacheDataReflection->getValue($cache));
39     }
40
41
42     public function testQueryCache_DependsOnHints()
43     {
44         $query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
45
46         $cache = new ArrayCache();
47         $query->setQueryCacheDriver($cache);
48
49         $query->getResult();
50         $this->assertEquals(2, $this->getCacheSize($cache));
51
52         $query->setHint('foo', 'bar');
53
54         $query->getResult();
55         $this->assertEquals(3, $this->getCacheSize($cache));
56
57         return $query;
58     }
59
60     /**
61      * @param <type> $query
62      * @depends testQueryCache_DependsOnHints
63      */
64     public function testQueryCache_DependsOnFirstResult($query)
65     {
66         $cache = $query->getQueryCacheDriver();
67         $cacheCount = $this->getCacheSize($cache);
68
69         $query->setFirstResult(10);
70         $query->setMaxResults(9999);
71
72         $query->getResult();
73         $this->assertEquals($cacheCount + 1, $this->getCacheSize($cache));
74     }
75
76     /**
77      * @param <type> $query
78      * @depends testQueryCache_DependsOnHints
79      */
80     public function testQueryCache_DependsOnMaxResults($query)
81     {
82         $cache = $query->getQueryCacheDriver();
83         $cacheCount = $this->getCacheSize($cache);
84
85         $query->setMaxResults(10);
86
87         $query->getResult();
88         $this->assertEquals($cacheCount + 1, $this->getCacheSize($cache));
89     }
90
91     /**
92      * @param <type> $query
93      * @depends testQueryCache_DependsOnHints
94      */
95     public function testQueryCache_DependsOnHydrationMode($query)
96     {
97         $cache = $query->getQueryCacheDriver();
98         $cacheCount = $this->getCacheSize($cache);
99
100         $query->getArrayResult();
101         $this->assertEquals($cacheCount + 1, $this->getCacheSize($cache));
102     }
103
104     public function testQueryCache_NoHitSaveParserResult()
105     {
106         $this->_em->getConfiguration()->setQueryCacheImpl(new ArrayCache());
107
108         $query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
109
110         $cache = new \Doctrine\Common\Cache\ArrayCache();
111
112         $query->setQueryCacheDriver($cache);
113
114         $users = $query->getResult();
115
116         $data = $this->cacheDataReflection->getValue($cache);
117         $this->assertEquals(2, count($data));
118
119         $this->assertInstanceOf('Doctrine\ORM\Query\ParserResult', array_pop($data));
120     }
121
122     public function testQueryCache_HitDoesNotSaveParserResult()
123     {
124         $this->_em->getConfiguration()->setQueryCacheImpl(new ArrayCache());
125
126         $query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
127
128         $sqlExecMock = $this->getMock('Doctrine\ORM\Query\Exec\AbstractSqlExecutor', array('execute'));
129         $sqlExecMock->expects($this->once())
130                     ->method('execute')
131                     ->will($this->returnValue( 10 ));
132
133         $parserResultMock = $this->getMock('Doctrine\ORM\Query\ParserResult');
134         $parserResultMock->expects($this->once())
135                          ->method('getSqlExecutor')
136                          ->will($this->returnValue($sqlExecMock));
137
138         $cache = $this->getMock('Doctrine\Common\Cache\CacheProvider',
139                 array('doFetch', 'doContains', 'doSave', 'doDelete', 'doFlush', 'doGetStats'));
140         $cache->expects($this->at(0))->method('doFetch')->will($this->returnValue(1));
141         $cache->expects($this->at(1))
142               ->method('doFetch')
143               ->with($this->isType('string'))
144               ->will($this->returnValue($parserResultMock));
145         $cache->expects($this->never())
146               ->method('doSave');
147
148         $query->setQueryCacheDriver($cache);
149
150         $users = $query->getResult();
151     }
152 }
153