ResultCacheTest.php 7.79 KB
Newer Older
1 2 3
<?php

namespace Doctrine\Tests\DBAL\Functional;
4
use Doctrine\DBAL\Cache\QueryCacheProfile;
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
use PDO;

require_once __DIR__ . '/../../TestInit.php';

/**
 * @group DDC-217
 */
class ResultCacheTest extends \Doctrine\Tests\DbalFunctionalTestCase
{
    private $expectedResult = array(array('test_int' => 100, 'test_string' => 'foo'), array('test_int' => 200, 'test_string' => 'bar'), array('test_int' => 300, 'test_string' => 'baz'));
    private $sqlLogger;

    public function setUp()
    {
        parent::setUp();

        try {
            /* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */
            $table = new \Doctrine\DBAL\Schema\Table("caching");
            $table->addColumn('test_int', 'integer');
            $table->addColumn('test_string', 'string', array('notnull' => false));
26
            $table->setPrimaryKey(array('test_int'));
27 28 29 30 31 32 33

            $sm = $this->_conn->getSchemaManager();
            $sm->createTable($table);
        } catch(\Exception $e) {

        }
        $this->_conn->executeUpdate('DELETE FROM caching');
jeroendedauw's avatar
jeroendedauw committed
34
        foreach ($this->expectedResult as $row) {
35 36 37 38 39
            $this->_conn->insert('caching', $row);
        }

        $config = $this->_conn->getConfiguration();
        $config->setSQLLogger($this->sqlLogger = new \Doctrine\DBAL\Logging\DebugStack);
40 41 42

        $cache = new \Doctrine\Common\Cache\ArrayCache;
        $config->setResultCacheImpl($cache);
43 44 45 46 47 48 49 50 51 52
    }

    public function testCacheFetchAssoc()
    {
        $this->assertCacheNonCacheSelectSameFetchModeAreEqual($this->expectedResult, \PDO::FETCH_ASSOC);
    }

    public function testFetchNum()
    {
        $expectedResult = array();
jeroendedauw's avatar
jeroendedauw committed
53
        foreach ($this->expectedResult as $v) {
54 55 56 57 58 59 60 61
            $expectedResult[] = array_values($v);
        }
        $this->assertCacheNonCacheSelectSameFetchModeAreEqual($expectedResult, \PDO::FETCH_NUM);
    }

    public function testFetchBoth()
    {
        $expectedResult = array();
jeroendedauw's avatar
jeroendedauw committed
62
        foreach ($this->expectedResult as $v) {
63 64 65 66
            $expectedResult[] = array_merge($v, array_values($v));
        }
        $this->assertCacheNonCacheSelectSameFetchModeAreEqual($expectedResult, \PDO::FETCH_BOTH);
    }
67 68 69 70
	
    public function testFetchColumn()
    {
        $expectedResult = array();
jeroendedauw's avatar
jeroendedauw committed
71
        foreach ($this->expectedResult as $v) {
72 73 74 75
            $expectedResult[] = array_shift($v);
        }
        $this->assertCacheNonCacheSelectSameFetchModeAreEqual($expectedResult, \PDO::FETCH_COLUMN);
    }
76 77 78 79

    public function testMixingFetch()
    {
        $numExpectedResult = array();
jeroendedauw's avatar
jeroendedauw committed
80
        foreach ($this->expectedResult as $v) {
81 82
            $numExpectedResult[] = array_values($v);
        }
83
        $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey"));
84

85
        $data = $this->hydrateStmt($stmt, \PDO::FETCH_ASSOC);
86 87 88

        $this->assertEquals($this->expectedResult, $data);

89
        $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey"));
90

91
        $data = $this->hydrateStmt($stmt, \PDO::FETCH_NUM);
92 93 94 95

        $this->assertEquals($numExpectedResult, $data);
    }

96 97 98 99 100 101 102
    public function testIteratorFetch()
    {
        $this->assertStandardAndIteratorFetchAreEqual(\PDO::FETCH_BOTH);
        $this->assertStandardAndIteratorFetchAreEqual(\PDO::FETCH_ASSOC);
        $this->assertStandardAndIteratorFetchAreEqual(\PDO::FETCH_NUM);
    }

103
    public function assertStandardAndIteratorFetchAreEqual($fetchMode)
104
    {
105
        $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey"));
106
        $data = $this->hydrateStmt($stmt, $fetchMode);
107

108
        $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey"));
109
        $data_iterator = $this->hydrateStmtIterator($stmt, $fetchMode);
110 111 112 113

        $this->assertEquals($data, $data_iterator);
    }

114 115
    public function testDontCloseNoCache()
    {
116
        $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey"));
117 118 119 120 121 122

        $data = array();
        while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
            $data[] = $row;
        }

123
        $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey"));
124 125 126 127 128 129 130 131 132 133 134

        $data = array();
        while ($row = $stmt->fetch(\PDO::FETCH_NUM)) {
            $data[] = $row;
        }

        $this->assertEquals(2, count($this->sqlLogger->queries));
    }

    public function testDontFinishNoCache()
    {
135
        $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey"));
136 137 138 139

        $row = $stmt->fetch(\PDO::FETCH_ASSOC);
        $stmt->closeCursor();

140
        $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey"));
141

142
        $data = $this->hydrateStmt($stmt, \PDO::FETCH_NUM);
143 144 145 146

        $this->assertEquals(2, count($this->sqlLogger->queries));
    }

147
    public function assertCacheNonCacheSelectSameFetchModeAreEqual($expectedResult, $fetchMode)
148
    {
149
        $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey"));
150 151

        $this->assertEquals(2, $stmt->columnCount());
152
        $data = $this->hydrateStmt($stmt, $fetchMode);
153 154
        $this->assertEquals($expectedResult, $data);

155
        $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey"));
156 157

        $this->assertEquals(2, $stmt->columnCount());
158
        $data = $this->hydrateStmt($stmt, $fetchMode);
159 160 161 162 163 164 165 166 167 168 169 170 171 172
        $this->assertEquals($expectedResult, $data);
        $this->assertEquals(1, count($this->sqlLogger->queries), "just one dbal hit");
    }

    public function testEmptyResultCache()
    {
        $stmt = $this->_conn->executeQuery("SELECT * FROM caching WHERE test_int > 500", array(), array(), new QueryCacheProfile(10, "emptycachekey"));
        $data = $this->hydrateStmt($stmt);

        $stmt = $this->_conn->executeQuery("SELECT * FROM caching WHERE test_int > 500", array(), array(), new QueryCacheProfile(10, "emptycachekey"));
        $data = $this->hydrateStmt($stmt);

        $this->assertEquals(1, count($this->sqlLogger->queries), "just one dbal hit");
    }
173

174 175 176 177 178 179 180 181 182 183 184 185 186
    public function testChangeCacheImpl()
    {
        $stmt = $this->_conn->executeQuery("SELECT * FROM caching WHERE test_int > 500", array(), array(), new QueryCacheProfile(10, "emptycachekey"));
        $data = $this->hydrateStmt($stmt);

        $secondCache = new \Doctrine\Common\Cache\ArrayCache;
        $stmt = $this->_conn->executeQuery("SELECT * FROM caching WHERE test_int > 500", array(), array(), new QueryCacheProfile(10, "emptycachekey", $secondCache));
        $data = $this->hydrateStmt($stmt);

        $this->assertEquals(2, count($this->sqlLogger->queries), "two hits");
        $this->assertEquals(1, count($secondCache->fetch("emptycachekey")));
    }

187
    private function hydrateStmt($stmt, $fetchMode = \PDO::FETCH_ASSOC)
188
    {
189
        $data = array();
190
        while ($row = $stmt->fetch($fetchMode)) {
191
            $data[] = is_array($row) ? array_change_key_case($row, CASE_LOWER) : $row;
192 193
        }
        $stmt->closeCursor();
194
        return $data;
195
    }
196

197
    private function hydrateStmtIterator($stmt, $fetchMode = \PDO::FETCH_ASSOC)
198 199
    {
        $data = array();
200
        $stmt->setFetchMode($fetchMode);
201
        foreach ($stmt as $row) {
202
            $data[] = is_array($row) ? array_change_key_case($row, CASE_LOWER) : $row;
203 204 205 206
        }
        $stmt->closeCursor();
        return $data;
    }
207
}