Completed
Push — master ( e3cefb...81922a )
by Marco
61:50 queued 11s
created

ResultCacheTest::testFetchAllColumn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 14
rs 10
cc 1
nc 1
nop 0
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\Driver\ResultStatement;
8
use Doctrine\DBAL\FetchMode;
9
use Doctrine\DBAL\Logging\DebugStack;
10
use Doctrine\DBAL\Schema\Table;
11
use Doctrine\Tests\DbalFunctionalTestCase;
12
use const CASE_LOWER;
13
use function array_change_key_case;
14
use function array_merge;
15
use function array_shift;
16
use function array_values;
17
use function is_array;
18
19
/**
20
 * @group DDC-217
21
 */
22
class ResultCacheTest extends DbalFunctionalTestCase
23
{
24
    /** @var array<int, array<int, int|string>> */
25
    private $expectedResult = [['test_int' => 100, 'test_string' => 'foo'], ['test_int' => 200, 'test_string' => 'bar'], ['test_int' => 300, 'test_string' => 'baz']];
26
27
    /** @var DebugStack */
28
    private $sqlLogger;
29
30
    protected function setUp() : void
31
    {
32
        parent::setUp();
33
34
        $table = new Table('caching');
35
        $table->addColumn('test_int', 'integer');
36
        $table->addColumn('test_string', 'string', ['notnull' => false]);
37
        $table->setPrimaryKey(['test_int']);
38
39
        $sm = $this->connection->getSchemaManager();
40
        $sm->createTable($table);
41
42
        foreach ($this->expectedResult as $row) {
43
            $this->connection->insert('caching', $row);
44
        }
45
46
        $config                                = $this->connection->getConfiguration();
47
        $config->setSQLLogger($this->sqlLogger = new DebugStack());
48
49
        $cache = new ArrayCache();
50
        $config->setResultCacheImpl($cache);
51
    }
52
53
    protected function tearDown() : void
54
    {
55
        $this->connection->getSchemaManager()->dropTable('caching');
56
57
        parent::tearDown();
58
    }
59
60
    public function testCacheFetchAssoc() : void
61
    {
62
        $this->assertCacheNonCacheSelectSameFetchModeAreEqual(
63
            $this->expectedResult,
64
            FetchMode::ASSOCIATIVE
65
        );
66
    }
67
68
    public function testFetchNum() : void
69
    {
70
        $expectedResult = [];
71
        foreach ($this->expectedResult as $v) {
72
            $expectedResult[] = array_values($v);
73
        }
74
75
        $this->assertCacheNonCacheSelectSameFetchModeAreEqual($expectedResult, FetchMode::NUMERIC);
76
    }
77
78
    public function testFetchBoth() : void
79
    {
80
        $expectedResult = [];
81
        foreach ($this->expectedResult as $v) {
82
            $expectedResult[] = array_merge($v, array_values($v));
83
        }
84
85
        $this->assertCacheNonCacheSelectSameFetchModeAreEqual($expectedResult, FetchMode::MIXED);
86
    }
87
88
    public function testFetchColumn() : void
89
    {
90
        $expectedResult = [];
91
        foreach ($this->expectedResult as $v) {
92
            $expectedResult[] = array_shift($v);
93
        }
94
95
        $this->assertCacheNonCacheSelectSameFetchModeAreEqual($expectedResult, FetchMode::COLUMN);
96
    }
97
98
    public function testMixingFetch() : void
99
    {
100
        $numExpectedResult = [];
101
        foreach ($this->expectedResult as $v) {
102
            $numExpectedResult[] = array_values($v);
103
        }
104
        $stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey'));
105
106
        $data = $this->hydrateStmt($stmt, FetchMode::ASSOCIATIVE);
107
108
        self::assertEquals($this->expectedResult, $data);
109
110
        $stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey'));
111
112
        $data = $this->hydrateStmt($stmt, FetchMode::NUMERIC);
113
114
        self::assertEquals($numExpectedResult, $data);
115
    }
116
117
    public function testIteratorFetch() : void
118
    {
119
        self::assertStandardAndIteratorFetchAreEqual(FetchMode::MIXED);
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\DBAL\Func...IteratorFetchAreEqual() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

119
        self::/** @scrutinizer ignore-call */ 
120
              assertStandardAndIteratorFetchAreEqual(FetchMode::MIXED);
Loading history...
120
        self::assertStandardAndIteratorFetchAreEqual(FetchMode::ASSOCIATIVE);
121
        self::assertStandardAndIteratorFetchAreEqual(FetchMode::NUMERIC);
122
    }
123
124
    private function assertStandardAndIteratorFetchAreEqual(int $fetchMode) : void
125
    {
126
        $stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey'));
127
        $data = $this->hydrateStmt($stmt, $fetchMode);
128
129
        $stmt          = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey'));
130
        $data_iterator = $this->hydrateStmtIterator($stmt, $fetchMode);
131
132
        self::assertEquals($data, $data_iterator);
133
    }
134
135
    public function testDontCloseNoCache() : void
136
    {
137
        $stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey'));
138
139
        $data = [];
140
141
        while ($row = $stmt->fetch(FetchMode::ASSOCIATIVE)) {
142
            $data[] = $row;
143
        }
144
145
        $stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey'));
146
147
        $data = [];
148
149
        while ($row = $stmt->fetch(FetchMode::NUMERIC)) {
150
            $data[] = $row;
151
        }
152
153
        self::assertCount(2, $this->sqlLogger->queries);
154
    }
155
156
    public function testDontFinishNoCache() : void
157
    {
158
        $stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey'));
159
160
        $stmt->fetch(FetchMode::ASSOCIATIVE);
161
        $stmt->closeCursor();
162
163
        $stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey'));
164
165
        $this->hydrateStmt($stmt, FetchMode::NUMERIC);
166
167
        self::assertCount(2, $this->sqlLogger->queries);
168
    }
169
170
    public function testFetchAllAndFinishSavesCache() : void
171
    {
172
        $layerCache = new ArrayCache();
173
        $stmt       = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(10, 'testcachekey', $layerCache));
174
        $stmt->fetchAll();
175
        $stmt->closeCursor();
176
177
        self::assertCount(1, $layerCache->fetch('testcachekey'));
0 ignored issues
show
Bug introduced by
It seems like $layerCache->fetch('testcachekey') can also be of type false; however, parameter $haystack of PHPUnit\Framework\Assert::assertCount() does only seem to accept Countable|iterable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

177
        self::assertCount(1, /** @scrutinizer ignore-type */ $layerCache->fetch('testcachekey'));
Loading history...
178
    }
179
180
    public function testFetchAllColumn() : void
181
    {
182
        $query = $this->connection->getDatabasePlatform()
183
            ->getDummySelectSQL('1');
184
185
        $qcp = new QueryCacheProfile(0, 0, new ArrayCache());
186
187
        $stmt = $this->connection->executeCacheQuery($query, [], [], $qcp);
188
        $stmt->fetchAll(FetchMode::COLUMN);
189
        $stmt->closeCursor();
190
191
        $stmt = $this->connection->executeCacheQuery($query, [], [], $qcp);
192
193
        self::assertEquals([1], $stmt->fetchAll(FetchMode::COLUMN));
194
    }
195
196
    /**
197
     * @param array<int, array<int, int|string>> $expectedResult
198
     */
199
    private function assertCacheNonCacheSelectSameFetchModeAreEqual(array $expectedResult, int $fetchMode) : void
200
    {
201
        $stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey'));
202
203
        self::assertEquals(2, $stmt->columnCount());
204
        $data = $this->hydrateStmt($stmt, $fetchMode);
205
        self::assertEquals($expectedResult, $data);
206
207
        $stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey'));
208
209
        self::assertEquals(2, $stmt->columnCount());
210
        $data = $this->hydrateStmt($stmt, $fetchMode);
211
        self::assertEquals($expectedResult, $data);
212
        self::assertCount(1, $this->sqlLogger->queries, 'just one dbal hit');
213
    }
214
215
    public function testEmptyResultCache() : void
216
    {
217
        $stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(10, 'emptycachekey'));
218
        $data = $this->hydrateStmt($stmt);
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
219
220
        $stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(10, 'emptycachekey'));
221
        $data = $this->hydrateStmt($stmt);
222
223
        self::assertCount(1, $this->sqlLogger->queries, 'just one dbal hit');
224
    }
225
226
    public function testChangeCacheImpl() : void
227
    {
228
        $stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(10, 'emptycachekey'));
229
        $data = $this->hydrateStmt($stmt);
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
230
231
        $secondCache = new ArrayCache();
232
        $stmt        = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(10, 'emptycachekey', $secondCache));
233
        $data        = $this->hydrateStmt($stmt);
234
235
        self::assertCount(2, $this->sqlLogger->queries, 'two hits');
236
        self::assertCount(1, $secondCache->fetch('emptycachekey'));
0 ignored issues
show
Bug introduced by
It seems like $secondCache->fetch('emptycachekey') can also be of type false; however, parameter $haystack of PHPUnit\Framework\Assert::assertCount() does only seem to accept Countable|iterable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

236
        self::assertCount(1, /** @scrutinizer ignore-type */ $secondCache->fetch('emptycachekey'));
Loading history...
237
    }
238
239
    /**
240
     * @return array<int, mixed>
241
     */
242
    private function hydrateStmt(ResultStatement $stmt, int $fetchMode = FetchMode::ASSOCIATIVE) : array
243
    {
244
        $data = [];
245
        while ($row = $stmt->fetch($fetchMode)) {
246
            $data[] = is_array($row) ? array_change_key_case($row, CASE_LOWER) : $row;
247
        }
248
        $stmt->closeCursor();
249
250
        return $data;
251
    }
252
253
    /**
254
     * @return array<int, mixed>
255
     */
256
    private function hydrateStmtIterator(ResultStatement $stmt, int $fetchMode = FetchMode::ASSOCIATIVE) : array
257
    {
258
        $data = [];
259
        $stmt->setFetchMode($fetchMode);
260
        foreach ($stmt as $row) {
261
            $data[] = is_array($row) ? array_change_key_case($row, CASE_LOWER) : $row;
262
        }
263
        $stmt->closeCursor();
264
265
        return $data;
266
    }
267
}
268