1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, https://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Metaways Infosystems GmbH, 2014 |
6
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2022 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
namespace Aimeos\MW\Cache; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class DBTest extends \PHPUnit\Framework\TestCase |
14
|
|
|
{ |
15
|
|
|
private static $dbm; |
16
|
|
|
private $config; |
17
|
|
|
private $object; |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
public static function setUpBeforeClass() : void |
21
|
|
|
{ |
22
|
|
|
self::$dbm = \TestHelper::getDBManager(); |
23
|
|
|
|
24
|
|
|
if( !( self::$dbm instanceof \Aimeos\Base\DB\Manager\DBAL ) ) { |
25
|
|
|
return; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
$conn = self::$dbm->acquire(); |
29
|
|
|
|
30
|
|
|
$schema = new \Doctrine\DBAL\Schema\Schema(); |
31
|
|
|
|
32
|
|
|
$cacheTable = $schema->createTable( 'mw_cache_test' ); |
33
|
|
|
$cacheTable->addColumn( 'id', 'string', array( 'length' => 255 ) ); |
34
|
|
|
$cacheTable->addColumn( 'expire', 'datetime', array( 'notnull' => false ) ); |
35
|
|
|
$cacheTable->addColumn( 'value', 'text', array( 'length' => 0xffff ) ); |
36
|
|
|
$cacheTable->setPrimaryKey( array( 'id' ) ); |
37
|
|
|
$cacheTable->addIndex( array( 'expire' ) ); |
38
|
|
|
|
39
|
|
|
foreach( $schema->toSQL( $conn->getRawObject()->getDatabasePlatform() ) as $sql ) { |
40
|
|
|
$conn->create( $sql )->execute()->finish(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$schema = new \Doctrine\DBAL\Schema\Schema(); |
44
|
|
|
|
45
|
|
|
$tagTable = $schema->createTable( 'mw_cache_tag_test' ); |
46
|
|
|
$tagTable->addColumn( 'tid', 'string', array( 'length' => 255 ) ); |
47
|
|
|
$tagTable->addColumn( 'tname', 'string', array( 'length' => 255 ) ); |
48
|
|
|
$tagTable->addUniqueIndex( array( 'tid', 'tname' ) ); |
49
|
|
|
$tagTable->addForeignKeyConstraint( 'mw_cache_test', array( 'tid' ), array( 'id' ), array( 'onDelete' => 'CASCADE' ) ); |
50
|
|
|
|
51
|
|
|
foreach( $schema->toSQL( $conn->getRawObject()->getDatabasePlatform() ) as $sql ) { |
52
|
|
|
$conn->create( $sql )->execute()->finish(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
self::$dbm->release( $conn ); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
public static function tearDownAfterClass() : void |
60
|
|
|
{ |
61
|
|
|
if( self::$dbm instanceof \Aimeos\Base\DB\Manager\DBAL ) |
62
|
|
|
{ |
63
|
|
|
$conn = self::$dbm->acquire(); |
64
|
|
|
|
65
|
|
|
$conn->create( 'DROP TABLE "mw_cache_tag_test"' )->execute()->finish(); |
66
|
|
|
$conn->create( 'DROP TABLE "mw_cache_test"' )->execute()->finish(); |
67
|
|
|
|
68
|
|
|
self::$dbm->release( $conn ); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
protected function setUp() : void |
74
|
|
|
{ |
75
|
|
|
if( !( self::$dbm instanceof \Aimeos\Base\DB\Manager\DBAL ) ) { |
76
|
|
|
$this->markTestSkipped( 'No DBAL database manager configured' ); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
$conn = self::$dbm->acquire(); |
81
|
|
|
|
82
|
|
|
$sql = 'INSERT INTO "mw_cache_test" ("id", "expire", "value") VALUES (\'t:1\', NULL, \'test 1\')'; |
83
|
|
|
$conn->create( $sql )->execute()->finish(); |
84
|
|
|
|
85
|
|
|
$sql = 'INSERT INTO "mw_cache_test" ("id", "expire", "value") VALUES (\'t:2\', \'2000-01-01 00:00:00\', \'test 2\')'; |
86
|
|
|
$conn->create( $sql )->execute()->finish(); |
87
|
|
|
|
88
|
|
|
$sql = 'INSERT INTO "mw_cache_tag_test" ("tid", "tname") VALUES (\'t:1\', \'tag:1\')'; |
89
|
|
|
$conn->create( $sql )->execute()->finish(); |
90
|
|
|
|
91
|
|
|
self::$dbm->release( $conn ); |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
$this->config = []; |
95
|
|
|
|
96
|
|
|
$this->config['search'] = array( |
97
|
|
|
'cache.id' => array( 'label' => 'Cache ID', 'code' => 'cache.id', 'internalcode' => 'id', 'type' => 'string', 'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR ), |
98
|
|
|
'cache.value' => array( 'label' => 'Cached value', 'code' => 'cache.value', 'internalcode' => 'value', 'type' => 'string', 'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR ), |
99
|
|
|
'cache.expire' => array( 'label' => 'Cache expiration date', 'code' => 'cache.expire', 'internalcode' => 'expire', 'type' => 'datetime', 'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR ), |
100
|
|
|
'cache.tag.name' => array( 'label' => 'Cache tag name', 'code' => 'cache.tag.name', 'internalcode' => 'tname', 'type' => 'string', 'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR ), |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
$this->config['sql'] = array( |
104
|
|
|
'delete' => ' |
105
|
|
|
DELETE FROM "mw_cache_test" WHERE :cond |
106
|
|
|
', |
107
|
|
|
'deletebytag' => ' |
108
|
|
|
DELETE FROM "mw_cache_test" WHERE id IN ( |
109
|
|
|
SELECT "tid" FROM "mw_cache_tag_test" WHERE :cond |
110
|
|
|
) |
111
|
|
|
', |
112
|
|
|
'get' => ' |
113
|
|
|
SELECT "id", "value", "expire" FROM "mw_cache_test" WHERE :cond |
114
|
|
|
', |
115
|
|
|
'getbytag' => ' |
116
|
|
|
SELECT "id", "value", "expire" FROM "mw_cache_test" |
117
|
|
|
JOIN "mw_cache_tag_test" ON "tid" = "id" |
118
|
|
|
WHERE :cond |
119
|
|
|
', |
120
|
|
|
'set' => ' |
121
|
|
|
INSERT INTO "mw_cache_test" ( "id", "expire", "value" ) VALUES ( ?, ?, ? ) |
122
|
|
|
', |
123
|
|
|
'settag' => ' |
124
|
|
|
INSERT INTO "mw_cache_tag_test" ( "tid", "tname" ) VALUES ( ?, ? ) |
125
|
|
|
', |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
$this->object = new \Aimeos\MW\Cache\DB( $this->config, self::$dbm ); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
public function tearDown() : void |
133
|
|
|
{ |
134
|
|
|
if( self::$dbm instanceof \Aimeos\Base\DB\Manager\DBAL ) |
135
|
|
|
{ |
136
|
|
|
$conn = self::$dbm->acquire(); |
137
|
|
|
|
138
|
|
|
$conn->create( 'DELETE FROM "mw_cache_tag_test"' )->execute()->finish(); |
139
|
|
|
$conn->create( 'DELETE FROM "mw_cache_test"' )->execute()->finish(); |
140
|
|
|
|
141
|
|
|
self::$dbm->release( $conn ); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
|
146
|
|
|
public function testConstructorNoConfig() |
147
|
|
|
{ |
148
|
|
|
$this->expectException( \Aimeos\MW\Cache\Exception::class ); |
|
|
|
|
149
|
|
|
new \Aimeos\MW\Cache\DB( [], self::$dbm ); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
|
153
|
|
|
public function testConstructorNoSql() |
154
|
|
|
{ |
155
|
|
|
$config = $this->config; |
156
|
|
|
unset( $config['sql'] ); |
157
|
|
|
|
158
|
|
|
$this->expectException( \Aimeos\MW\Cache\Exception::class ); |
159
|
|
|
new \Aimeos\MW\Cache\DB( $config, self::$dbm ); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
|
163
|
|
|
public function testConstructorNoSearch() |
164
|
|
|
{ |
165
|
|
|
$config = $this->config; |
166
|
|
|
unset( $config['search'] ); |
167
|
|
|
|
168
|
|
|
$this->expectException( \Aimeos\MW\Cache\Exception::class ); |
169
|
|
|
new \Aimeos\MW\Cache\DB( $config, self::$dbm ); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
public function testConstructorIncompleteSql() |
174
|
|
|
{ |
175
|
|
|
$config = $this->config; |
176
|
|
|
unset( $config['sql']['delete'] ); |
177
|
|
|
|
178
|
|
|
$this->expectException( \Aimeos\MW\Cache\Exception::class ); |
179
|
|
|
new \Aimeos\MW\Cache\DB( $config, self::$dbm ); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
|
183
|
|
|
public function testConstructorIncompleteSearch() |
184
|
|
|
{ |
185
|
|
|
$config = $this->config; |
186
|
|
|
unset( $config['search']['cache.id'] ); |
187
|
|
|
|
188
|
|
|
$this->expectException( \Aimeos\MW\Cache\Exception::class ); |
189
|
|
|
new \Aimeos\MW\Cache\DB( $config, self::$dbm ); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
|
193
|
|
|
public function testCleanup() |
194
|
|
|
{ |
195
|
|
|
$this->assertTrue( $this->object->cleanup() ); |
196
|
|
|
|
197
|
|
|
$conn = self::$dbm->acquire(); |
198
|
|
|
$result = $conn->create( 'SELECT "id" FROM "mw_cache_test"' )->execute(); |
199
|
|
|
self::$dbm->release( $conn ); |
200
|
|
|
|
201
|
|
|
$this->assertEquals( array( 'id' => 't:1' ), $result->fetch() ); |
202
|
|
|
$this->assertNull( $result->fetch() ); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
|
206
|
|
|
public function testDelete() |
207
|
|
|
{ |
208
|
|
|
$this->assertTrue( $this->object->delete( 't:1' ) ); |
209
|
|
|
|
210
|
|
|
$conn = self::$dbm->acquire(); |
211
|
|
|
$row = $conn->create( 'SELECT * FROM "mw_cache_tag_test"' )->execute()->fetch(); |
212
|
|
|
self::$dbm->release( $conn ); |
213
|
|
|
|
214
|
|
|
$this->assertNull( $row ); |
215
|
|
|
|
216
|
|
|
|
217
|
|
|
$conn = self::$dbm->acquire(); |
218
|
|
|
$result = $conn->create( 'SELECT "id" FROM "mw_cache_test"' )->execute(); |
219
|
|
|
self::$dbm->release( $conn ); |
220
|
|
|
|
221
|
|
|
$this->assertEquals( array( 'id' => 't:2' ), $result->fetch() ); |
222
|
|
|
$this->assertNull( $result->fetch() ); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
|
226
|
|
|
public function testDeleteMultiple() |
227
|
|
|
{ |
228
|
|
|
$this->assertTrue( $this->object->deleteMultiple( array( 't:1', 't:2' ) ) ); |
229
|
|
|
|
230
|
|
|
$conn = self::$dbm->acquire(); |
231
|
|
|
$row = $conn->create( 'SELECT * FROM "mw_cache_test"' )->execute()->fetch(); |
232
|
|
|
self::$dbm->release( $conn ); |
233
|
|
|
|
234
|
|
|
$this->assertNull( $row ); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
|
238
|
|
|
public function testDeleteByTags() |
239
|
|
|
{ |
240
|
|
|
$this->assertTrue( $this->object->deleteByTags( array( 'tag:1' ) ) ); |
241
|
|
|
|
242
|
|
|
$conn = self::$dbm->acquire(); |
243
|
|
|
$row = $conn->create( 'SELECT * FROM "mw_cache_tag_test"' )->execute()->fetch(); |
244
|
|
|
self::$dbm->release( $conn ); |
245
|
|
|
|
246
|
|
|
$this->assertNull( $row ); |
247
|
|
|
|
248
|
|
|
|
249
|
|
|
$conn = self::$dbm->acquire(); |
250
|
|
|
$result = $conn->create( 'SELECT "id" FROM "mw_cache_test"' )->execute(); |
251
|
|
|
self::$dbm->release( $conn ); |
252
|
|
|
|
253
|
|
|
$this->assertEquals( array( 'id' => 't:2' ), $result->fetch() ); |
254
|
|
|
$this->assertNull( $result->fetch() ); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
|
258
|
|
|
public function testClear() |
259
|
|
|
{ |
260
|
|
|
$this->assertTrue( $this->object->clear() ); |
261
|
|
|
|
262
|
|
|
$conn = self::$dbm->acquire(); |
263
|
|
|
$row = $conn->create( 'SELECT * FROM "mw_cache_tag_test"' )->execute()->fetch(); |
264
|
|
|
self::$dbm->release( $conn ); |
265
|
|
|
|
266
|
|
|
$this->assertNull( $row ); |
267
|
|
|
|
268
|
|
|
|
269
|
|
|
$conn = self::$dbm->acquire(); |
270
|
|
|
$row = $conn->create( 'SELECT "id" FROM "mw_cache_test"' )->execute()->fetch(); |
271
|
|
|
self::$dbm->release( $conn ); |
272
|
|
|
|
273
|
|
|
$this->assertNull( $row ); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
|
277
|
|
|
public function testGet() |
278
|
|
|
{ |
279
|
|
|
$this->assertEquals( 'test 1', $this->object->get( 't:1' ) ); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
|
283
|
|
|
public function testGetExpired() |
284
|
|
|
{ |
285
|
|
|
$this->assertEquals( null, $this->object->get( 't:2' ) ); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
|
289
|
|
|
public function testGetMultiple() |
290
|
|
|
{ |
291
|
|
|
$this->assertEquals( array( 't:1' => 'test 1', 't:2' => null ), $this->object->getMultiple( array( 't:1', 't:2' ) ) ); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
|
295
|
|
|
public function testHas() |
296
|
|
|
{ |
297
|
|
|
$this->assertTrue( $this->object->has( 't:1' ) ); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
|
301
|
|
|
public function testSet() |
302
|
|
|
{ |
303
|
|
|
$this->assertTrue( $this->object->set( 't:3', 'test 3', '2100-01-01 00:00:00', ['tag:2', 'tag:3'] ) ); |
304
|
|
|
|
305
|
|
|
|
306
|
|
|
$conn = self::$dbm->acquire(); |
307
|
|
|
$result = $conn->create( 'SELECT "tname" FROM "mw_cache_tag_test" WHERE "tid" = \'t:3\' ORDER BY "tname"' )->execute(); |
308
|
|
|
self::$dbm->release( $conn ); |
309
|
|
|
|
310
|
|
|
$this->assertEquals( array( 'tname' => 'tag:2' ), $result->fetch() ); |
311
|
|
|
$this->assertEquals( array( 'tname' => 'tag:3' ), $result->fetch() ); |
312
|
|
|
$this->assertNull( $result->fetch() ); |
313
|
|
|
|
314
|
|
|
|
315
|
|
|
$conn = self::$dbm->acquire(); |
316
|
|
|
$result = $conn->create( 'SELECT * FROM "mw_cache_test" WHERE "id" = \'t:3\'' )->execute(); |
317
|
|
|
self::$dbm->release( $conn ); |
318
|
|
|
|
319
|
|
|
$row = $result->fetch(); |
320
|
|
|
|
321
|
|
|
$this->assertEquals( 't:3', $row['id'] ); |
322
|
|
|
$this->assertEquals( 'test 3', $row['value'] ); |
323
|
|
|
$this->assertEquals( '2100-01-01 00:00:00', substr( $row['expire'], 0, 19 ) ); |
324
|
|
|
$this->assertNull( $result->fetch() ); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
|
328
|
|
|
public function testSetMultiple() |
329
|
|
|
{ |
330
|
|
|
$pairs = ['t:3' => 'test 3', 't:2' => 'test 4']; |
331
|
|
|
|
332
|
|
|
$this->assertTrue( $this->object->setMultiple( $pairs, '2100-01-01 00:00:00', ['tag:2', 'tag:3'] ) ); |
333
|
|
|
|
334
|
|
|
|
335
|
|
|
$conn = self::$dbm->acquire(); |
336
|
|
|
$result = $conn->create( 'SELECT "tname" FROM "mw_cache_tag_test" WHERE "tid" = \'t:3\' ORDER BY "tname"' )->execute(); |
337
|
|
|
self::$dbm->release( $conn ); |
338
|
|
|
|
339
|
|
|
$this->assertEquals( array( 'tname' => 'tag:2' ), $result->fetch() ); |
340
|
|
|
$this->assertEquals( array( 'tname' => 'tag:3' ), $result->fetch() ); |
341
|
|
|
$this->assertNull( $result->fetch() ); |
342
|
|
|
|
343
|
|
|
|
344
|
|
|
$conn = self::$dbm->acquire(); |
345
|
|
|
$result = $conn->create( 'SELECT "tname" FROM "mw_cache_tag_test" WHERE "tid" = \'t:2\' ORDER BY "tname"' )->execute(); |
346
|
|
|
self::$dbm->release( $conn ); |
347
|
|
|
|
348
|
|
|
$this->assertEquals( array( 'tname' => 'tag:2' ), $result->fetch() ); |
349
|
|
|
$this->assertEquals( array( 'tname' => 'tag:3' ), $result->fetch() ); |
350
|
|
|
$this->assertNull( $result->fetch() ); |
351
|
|
|
|
352
|
|
|
|
353
|
|
|
$conn = self::$dbm->acquire(); |
354
|
|
|
$result = $conn->create( 'SELECT * FROM "mw_cache_test" WHERE "id" = \'t:3\'' )->execute(); |
355
|
|
|
self::$dbm->release( $conn ); |
356
|
|
|
|
357
|
|
|
$row = $result->fetch(); |
358
|
|
|
|
359
|
|
|
$this->assertEquals( 't:3', $row['id'] ); |
360
|
|
|
$this->assertEquals( 'test 3', $row['value'] ); |
361
|
|
|
$this->assertEquals( '2100-01-01 00:00:00', substr( $row['expire'], 0, 19 ) ); |
362
|
|
|
$this->assertNull( $result->fetch() ); |
363
|
|
|
|
364
|
|
|
|
365
|
|
|
$conn = self::$dbm->acquire(); |
366
|
|
|
$result = $conn->create( 'SELECT * FROM "mw_cache_test" WHERE "id" = \'t:2\'' )->execute(); |
367
|
|
|
self::$dbm->release( $conn ); |
368
|
|
|
|
369
|
|
|
$row = $result->fetch(); |
370
|
|
|
|
371
|
|
|
$this->assertEquals( 't:2', $row['id'] ); |
372
|
|
|
$this->assertEquals( 'test 4', $row['value'] ); |
373
|
|
|
$this->assertEquals( '2100-01-01 00:00:00', substr( $row['expire'], 0, 19 ) ); |
374
|
|
|
$this->assertNull( $result->fetch() ); |
375
|
|
|
} |
376
|
|
|
} |
377
|
|
|
|