Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / dbal / tests / Doctrine / Tests / DBAL / Functional / ResultCacheTest.php
1 <?php
2
3 namespace Doctrine\Tests\DBAL\Functional;
4 use Doctrine\DBAL\Types\Type;
5 use Doctrine\DBAL\Cache\QueryCacheProfile;
6 use PDO;
7
8 require_once __DIR__ . '/../../TestInit.php';
9
10 /**
11  * @group DDC-217
12  */
13 class ResultCacheTest extends \Doctrine\Tests\DbalFunctionalTestCase
14 {
15     private $expectedResult = array(array('test_int' => 100, 'test_string' => 'foo'), array('test_int' => 200, 'test_string' => 'bar'), array('test_int' => 300, 'test_string' => 'baz'));
16     private $sqlLogger;
17
18     public function setUp()
19     {
20         parent::setUp();
21
22         try {
23             /* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */
24             $table = new \Doctrine\DBAL\Schema\Table("caching");
25             $table->addColumn('test_int', 'integer');
26             $table->addColumn('test_string', 'string', array('notnull' => false));
27             $table->setPrimaryKey(array('test_int'));
28
29             $sm = $this->_conn->getSchemaManager();
30             $sm->createTable($table);
31         } catch(\Exception $e) {
32
33         }
34         $this->_conn->executeUpdate('DELETE FROM caching');
35         foreach ($this->expectedResult AS $row) {
36             $this->_conn->insert('caching', $row);
37         }
38
39         $config = $this->_conn->getConfiguration();
40         $config->setSQLLogger($this->sqlLogger = new \Doctrine\DBAL\Logging\DebugStack);
41
42         $cache = new \Doctrine\Common\Cache\ArrayCache;
43         $config->setResultCacheImpl($cache);
44     }
45
46     public function testCacheFetchAssoc()
47     {
48         $this->assertCacheNonCacheSelectSameFetchModeAreEqual($this->expectedResult, \PDO::FETCH_ASSOC);
49     }
50
51     public function testFetchNum()
52     {
53         $expectedResult = array();
54         foreach ($this->expectedResult AS $v) {
55             $expectedResult[] = array_values($v);
56         }
57         $this->assertCacheNonCacheSelectSameFetchModeAreEqual($expectedResult, \PDO::FETCH_NUM);
58     }
59
60     public function testFetchBoth()
61     {
62         $expectedResult = array();
63         foreach ($this->expectedResult AS $v) {
64             $expectedResult[] = array_merge($v, array_values($v));
65         }
66         $this->assertCacheNonCacheSelectSameFetchModeAreEqual($expectedResult, \PDO::FETCH_BOTH);
67     }
68         
69     public function testFetchColumn()
70     {
71         $expectedResult = array();
72         foreach ($this->expectedResult AS $v) {
73             $expectedResult[] = array_shift($v);
74         }
75         $this->assertCacheNonCacheSelectSameFetchModeAreEqual($expectedResult, \PDO::FETCH_COLUMN);
76     }
77
78     public function testMixingFetch()
79     {
80         $numExpectedResult = array();
81         foreach ($this->expectedResult AS $v) {
82             $numExpectedResult[] = array_values($v);
83         }
84         $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey"));
85
86         $data = $this->hydrateStmt($stmt, \PDO::FETCH_ASSOC);
87
88         $this->assertEquals($this->expectedResult, $data);
89
90         $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey"));
91
92         $data = $this->hydrateStmt($stmt, \PDO::FETCH_NUM);
93
94         $this->assertEquals($numExpectedResult, $data);
95     }
96
97     public function testIteratorFetch()
98     {
99         $this->assertStandardAndIteratorFetchAreEqual(\PDO::FETCH_BOTH);
100         $this->assertStandardAndIteratorFetchAreEqual(\PDO::FETCH_ASSOC);
101         $this->assertStandardAndIteratorFetchAreEqual(\PDO::FETCH_NUM);
102     }
103
104     public function assertStandardAndIteratorFetchAreEqual($fetchMode)
105     {
106         $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey"));
107         $data = $this->hydrateStmt($stmt, $fetchMode);
108
109         $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey"));
110         $data_iterator = $this->hydrateStmtIterator($stmt, $fetchMode);
111
112         $this->assertEquals($data, $data_iterator);
113     }
114
115     public function testDontCloseNoCache()
116     {
117         $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey"));
118
119         $data = array();
120         while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
121             $data[] = $row;
122         }
123
124         $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey"));
125
126         $data = array();
127         while ($row = $stmt->fetch(\PDO::FETCH_NUM)) {
128             $data[] = $row;
129         }
130
131         $this->assertEquals(2, count($this->sqlLogger->queries));
132     }
133
134     public function testDontFinishNoCache()
135     {
136         $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey"));
137
138         $row = $stmt->fetch(\PDO::FETCH_ASSOC);
139         $stmt->closeCursor();
140
141         $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey"));
142
143         $data = $this->hydrateStmt($stmt, \PDO::FETCH_NUM);
144
145         $this->assertEquals(2, count($this->sqlLogger->queries));
146     }
147
148     public function assertCacheNonCacheSelectSameFetchModeAreEqual($expectedResult, $fetchMode)
149     {
150         $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey"));
151
152         $this->assertEquals(2, $stmt->columnCount());
153         $data = $this->hydrateStmt($stmt, $fetchMode);
154         $this->assertEquals($expectedResult, $data);
155
156         $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey"));
157
158         $this->assertEquals(2, $stmt->columnCount());
159         $data = $this->hydrateStmt($stmt, $fetchMode);
160         $this->assertEquals($expectedResult, $data);
161         $this->assertEquals(1, count($this->sqlLogger->queries), "just one dbal hit");
162     }
163
164     public function testEmptyResultCache()
165     {
166         $stmt = $this->_conn->executeQuery("SELECT * FROM caching WHERE test_int > 500", array(), array(), new QueryCacheProfile(10, "emptycachekey"));
167         $data = $this->hydrateStmt($stmt);
168
169         $stmt = $this->_conn->executeQuery("SELECT * FROM caching WHERE test_int > 500", array(), array(), new QueryCacheProfile(10, "emptycachekey"));
170         $data = $this->hydrateStmt($stmt);
171
172         $this->assertEquals(1, count($this->sqlLogger->queries), "just one dbal hit");
173     }
174
175     public function testChangeCacheImpl()
176     {
177         $stmt = $this->_conn->executeQuery("SELECT * FROM caching WHERE test_int > 500", array(), array(), new QueryCacheProfile(10, "emptycachekey"));
178         $data = $this->hydrateStmt($stmt);
179
180         $secondCache = new \Doctrine\Common\Cache\ArrayCache;
181         $stmt = $this->_conn->executeQuery("SELECT * FROM caching WHERE test_int > 500", array(), array(), new QueryCacheProfile(10, "emptycachekey", $secondCache));
182         $data = $this->hydrateStmt($stmt);
183
184         $this->assertEquals(2, count($this->sqlLogger->queries), "two hits");
185         $this->assertEquals(1, count($secondCache->fetch("emptycachekey")));
186     }
187
188     private function hydrateStmt($stmt, $fetchMode = \PDO::FETCH_ASSOC)
189     {
190         $data = array();
191         while ($row = $stmt->fetch($fetchMode)) {
192             $data[] = is_array($row) ? array_change_key_case($row, CASE_LOWER) : $row;
193         }
194         $stmt->closeCursor();
195         return $data;
196     }
197
198     private function hydrateStmtIterator($stmt, $fetchMode = \PDO::FETCH_ASSOC)
199     {
200         $data = array();
201         $stmt->setFetchMode($fetchMode);
202         foreach ($stmt as $row) {
203             $data[] = is_array($row) ? array_change_key_case($row, CASE_LOWER) : $row;
204         }
205         $stmt->closeCursor();
206         return $data;
207     }
208 }