3 namespace Doctrine\Tests\DBAL\Functional;
4 use Doctrine\DBAL\Types\Type;
5 use Doctrine\DBAL\Cache\QueryCacheProfile;
8 require_once __DIR__ . '/../../TestInit.php';
13 class ResultCacheTest extends \Doctrine\Tests\DbalFunctionalTestCase
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'));
18 public function setUp()
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'));
29 $sm = $this->_conn->getSchemaManager();
30 $sm->createTable($table);
31 } catch(\Exception $e) {
34 $this->_conn->executeUpdate('DELETE FROM caching');
35 foreach ($this->expectedResult AS $row) {
36 $this->_conn->insert('caching', $row);
39 $config = $this->_conn->getConfiguration();
40 $config->setSQLLogger($this->sqlLogger = new \Doctrine\DBAL\Logging\DebugStack);
42 $cache = new \Doctrine\Common\Cache\ArrayCache;
43 $config->setResultCacheImpl($cache);
46 public function testCacheFetchAssoc()
48 $this->assertCacheNonCacheSelectSameFetchModeAreEqual($this->expectedResult, \PDO::FETCH_ASSOC);
51 public function testFetchNum()
53 $expectedResult = array();
54 foreach ($this->expectedResult AS $v) {
55 $expectedResult[] = array_values($v);
57 $this->assertCacheNonCacheSelectSameFetchModeAreEqual($expectedResult, \PDO::FETCH_NUM);
60 public function testFetchBoth()
62 $expectedResult = array();
63 foreach ($this->expectedResult AS $v) {
64 $expectedResult[] = array_merge($v, array_values($v));
66 $this->assertCacheNonCacheSelectSameFetchModeAreEqual($expectedResult, \PDO::FETCH_BOTH);
69 public function testFetchColumn()
71 $expectedResult = array();
72 foreach ($this->expectedResult AS $v) {
73 $expectedResult[] = array_shift($v);
75 $this->assertCacheNonCacheSelectSameFetchModeAreEqual($expectedResult, \PDO::FETCH_COLUMN);
78 public function testMixingFetch()
80 $numExpectedResult = array();
81 foreach ($this->expectedResult AS $v) {
82 $numExpectedResult[] = array_values($v);
84 $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey"));
86 $data = $this->hydrateStmt($stmt, \PDO::FETCH_ASSOC);
88 $this->assertEquals($this->expectedResult, $data);
90 $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey"));
92 $data = $this->hydrateStmt($stmt, \PDO::FETCH_NUM);
94 $this->assertEquals($numExpectedResult, $data);
97 public function testIteratorFetch()
99 $this->assertStandardAndIteratorFetchAreEqual(\PDO::FETCH_BOTH);
100 $this->assertStandardAndIteratorFetchAreEqual(\PDO::FETCH_ASSOC);
101 $this->assertStandardAndIteratorFetchAreEqual(\PDO::FETCH_NUM);
104 public function assertStandardAndIteratorFetchAreEqual($fetchMode)
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);
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);
112 $this->assertEquals($data, $data_iterator);
115 public function testDontCloseNoCache()
117 $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey"));
120 while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
124 $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey"));
127 while ($row = $stmt->fetch(\PDO::FETCH_NUM)) {
131 $this->assertEquals(2, count($this->sqlLogger->queries));
134 public function testDontFinishNoCache()
136 $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey"));
138 $row = $stmt->fetch(\PDO::FETCH_ASSOC);
139 $stmt->closeCursor();
141 $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey"));
143 $data = $this->hydrateStmt($stmt, \PDO::FETCH_NUM);
145 $this->assertEquals(2, count($this->sqlLogger->queries));
148 public function assertCacheNonCacheSelectSameFetchModeAreEqual($expectedResult, $fetchMode)
150 $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey"));
152 $this->assertEquals(2, $stmt->columnCount());
153 $data = $this->hydrateStmt($stmt, $fetchMode);
154 $this->assertEquals($expectedResult, $data);
156 $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey"));
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");
164 public function testEmptyResultCache()
166 $stmt = $this->_conn->executeQuery("SELECT * FROM caching WHERE test_int > 500", array(), array(), new QueryCacheProfile(10, "emptycachekey"));
167 $data = $this->hydrateStmt($stmt);
169 $stmt = $this->_conn->executeQuery("SELECT * FROM caching WHERE test_int > 500", array(), array(), new QueryCacheProfile(10, "emptycachekey"));
170 $data = $this->hydrateStmt($stmt);
172 $this->assertEquals(1, count($this->sqlLogger->queries), "just one dbal hit");
175 public function testChangeCacheImpl()
177 $stmt = $this->_conn->executeQuery("SELECT * FROM caching WHERE test_int > 500", array(), array(), new QueryCacheProfile(10, "emptycachekey"));
178 $data = $this->hydrateStmt($stmt);
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);
184 $this->assertEquals(2, count($this->sqlLogger->queries), "two hits");
185 $this->assertEquals(1, count($secondCache->fetch("emptycachekey")));
188 private function hydrateStmt($stmt, $fetchMode = \PDO::FETCH_ASSOC)
191 while ($row = $stmt->fetch($fetchMode)) {
192 $data[] = is_array($row) ? array_change_key_case($row, CASE_LOWER) : $row;
194 $stmt->closeCursor();
198 private function hydrateStmtIterator($stmt, $fetchMode = \PDO::FETCH_ASSOC)
201 $stmt->setFetchMode($fetchMode);
202 foreach ($stmt as $row) {
203 $data[] = is_array($row) ? array_change_key_case($row, CASE_LOWER) : $row;
205 $stmt->closeCursor();