Completed
Push — master ( c7757e...39cb21 )
by Luís
16s
created

Doctrine/Tests/DBAL/Functional/ResultCacheTest.php (2 issues)

1
<?php
2
3
namespace Doctrine\Tests\DBAL\Functional;
4
use Doctrine\DBAL\Cache\QueryCacheProfile;
5
use PDO;
6
7
/**
8
 * @group DDC-217
9
 */
10
class ResultCacheTest extends \Doctrine\Tests\DbalFunctionalTestCase
11
{
12
    private $expectedResult = array(array('test_int' => 100, 'test_string' => 'foo'), array('test_int' => 200, 'test_string' => 'bar'), array('test_int' => 300, 'test_string' => 'baz'));
13
    private $sqlLogger;
14
15
    protected function setUp()
16
    {
17
        parent::setUp();
18
19
        $table = new \Doctrine\DBAL\Schema\Table("caching");
20
        $table->addColumn('test_int', 'integer');
21
        $table->addColumn('test_string', 'string', array('notnull' => false));
22
        $table->setPrimaryKey(array('test_int'));
23
24
        $sm = $this->_conn->getSchemaManager();
25
        $sm->createTable($table);
26
27
        foreach ($this->expectedResult as $row) {
28
            $this->_conn->insert('caching', $row);
29
        }
30
31
        $config = $this->_conn->getConfiguration();
32
        $config->setSQLLogger($this->sqlLogger = new \Doctrine\DBAL\Logging\DebugStack);
33
34
        $cache = new \Doctrine\Common\Cache\ArrayCache;
35
        $config->setResultCacheImpl($cache);
36
    }
37
38
    protected function tearDown()
39
    {
40
        $this->_conn->getSchemaManager()->dropTable('caching');
41
42
        parent::tearDown();
43
    }
44
45
    public function testCacheFetchAssoc()
46
    {
47
        self::assertCacheNonCacheSelectSameFetchModeAreEqual($this->expectedResult, \PDO::FETCH_ASSOC);
48
    }
49
50 View Code Duplication
    public function testFetchNum()
51
    {
52
        $expectedResult = array();
53
        foreach ($this->expectedResult as $v) {
54
            $expectedResult[] = array_values($v);
55
        }
56
        self::assertCacheNonCacheSelectSameFetchModeAreEqual($expectedResult, \PDO::FETCH_NUM);
57
    }
58
59 View Code Duplication
    public function testFetchBoth()
60
    {
61
        $expectedResult = array();
62
        foreach ($this->expectedResult as $v) {
63
            $expectedResult[] = array_merge($v, array_values($v));
64
        }
65
        self::assertCacheNonCacheSelectSameFetchModeAreEqual($expectedResult, \PDO::FETCH_BOTH);
66
    }
67
68 View Code Duplication
    public function testFetchColumn()
69
    {
70
        $expectedResult = array();
71
        foreach ($this->expectedResult as $v) {
72
            $expectedResult[] = array_shift($v);
73
        }
74
        self::assertCacheNonCacheSelectSameFetchModeAreEqual($expectedResult, \PDO::FETCH_COLUMN);
75
    }
76
77
    public function testMixingFetch()
78
    {
79
        $numExpectedResult = array();
80
        foreach ($this->expectedResult as $v) {
81
            $numExpectedResult[] = array_values($v);
82
        }
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
        self::assertEquals($this->expectedResult, $data);
88
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
        self::assertEquals($numExpectedResult, $data);
94
    }
95
96
    public function testIteratorFetch()
97
    {
98
        self::assertStandardAndIteratorFetchAreEqual(\PDO::FETCH_BOTH);
99
        self::assertStandardAndIteratorFetchAreEqual(\PDO::FETCH_ASSOC);
100
        self::assertStandardAndIteratorFetchAreEqual(\PDO::FETCH_NUM);
101
    }
102
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
        self::assertEquals($data, $data_iterator);
112
    }
113
114
    public function testDontCloseNoCache()
115
    {
116
        $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey"));
117
118
        $data = array();
119
        while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
120
            $data[] = $row;
121
        }
122
123
        $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey"));
124
125
        $data = array();
126
        while ($row = $stmt->fetch(\PDO::FETCH_NUM)) {
127
            $data[] = $row;
128
        }
129
130
        self::assertEquals(2, count($this->sqlLogger->queries));
131
    }
132
133
    public function testDontFinishNoCache()
134
    {
135
        $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey"));
136
137
        $row = $stmt->fetch(\PDO::FETCH_ASSOC);
138
        $stmt->closeCursor();
139
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
        self::assertEquals(2, count($this->sqlLogger->queries));
145
    }
146
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
        self::assertEquals(2, $stmt->columnCount());
152
        $data = $this->hydrateStmt($stmt, $fetchMode);
153
        self::assertEquals($expectedResult, $data);
154
155
        $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey"));
156
157
        self::assertEquals(2, $stmt->columnCount());
158
        $data = $this->hydrateStmt($stmt, $fetchMode);
159
        self::assertEquals($expectedResult, $data);
160
        self::assertEquals(1, count($this->sqlLogger->queries), "just one dbal hit");
161
    }
162
163
    public function testEmptyResultCache()
164
    {
165
        $stmt = $this->_conn->executeQuery("SELECT * FROM caching WHERE test_int > 500", array(), array(), new QueryCacheProfile(10, "emptycachekey"));
166
        $data = $this->hydrateStmt($stmt);
167
168
        $stmt = $this->_conn->executeQuery("SELECT * FROM caching WHERE test_int > 500", array(), array(), new QueryCacheProfile(10, "emptycachekey"));
169
        $data = $this->hydrateStmt($stmt);
170
171
        self::assertEquals(1, count($this->sqlLogger->queries), "just one dbal hit");
172
    }
173
174
    public function testChangeCacheImpl()
175
    {
176
        $stmt = $this->_conn->executeQuery("SELECT * FROM caching WHERE test_int > 500", array(), array(), new QueryCacheProfile(10, "emptycachekey"));
177
        $data = $this->hydrateStmt($stmt);
178
179
        $secondCache = new \Doctrine\Common\Cache\ArrayCache;
180
        $stmt = $this->_conn->executeQuery("SELECT * FROM caching WHERE test_int > 500", array(), array(), new QueryCacheProfile(10, "emptycachekey", $secondCache));
181
        $data = $this->hydrateStmt($stmt);
182
183
        self::assertEquals(2, count($this->sqlLogger->queries), "two hits");
184
        self::assertEquals(1, count($secondCache->fetch("emptycachekey")));
185
    }
186
187 View Code Duplication
    private function hydrateStmt($stmt, $fetchMode = \PDO::FETCH_ASSOC)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    private function hydrateStmtIterator($stmt, $fetchMode = \PDO::FETCH_ASSOC)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
        $stmt->closeCursor();
205
        return $data;
206
    }
207
}
208