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