Passed
Push — master ( c290d5...c7c9c7 )
by Aimeos
02:21
created

DBTest::testDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 17
rs 9.9332
cc 1
nc 1
nop 0
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\Base\Cache;
11
12
13
class DBTest extends \PHPUnit\Framework\TestCase
14
{
15
	private static $conn;
16
	private static $dbm;
17
	private $config;
18
	private $object;
19
20
21
	public static function setUpBeforeClass() : void
22
	{
23
		self::$dbm = \TestHelper::getDBManager();
24
25
		if( !( self::$dbm instanceof \Aimeos\Base\DB\Manager\DBAL ) ) {
26
			return;
27
		}
28
29
		self::$conn = self::$dbm->acquire();
30
31
		$schema = new \Doctrine\DBAL\Schema\Schema();
32
33
		$cacheTable = $schema->createTable( 'mw_cache_test' );
34
		$cacheTable->addColumn( 'id', 'string', array( 'length' => 255 ) );
35
		$cacheTable->addColumn( 'expire', 'datetime', array( 'notnull' => false ) );
36
		$cacheTable->addColumn( 'value', 'text', array( 'length' => 0xffff ) );
37
		$cacheTable->setPrimaryKey( array( 'id' ) );
38
		$cacheTable->addIndex( array( 'expire' ) );
39
40
		foreach( $schema->toSQL( self::$conn->getRawObject()->getDatabasePlatform() ) as $sql ) {
41
			self::$conn->create( $sql )->execute()->finish();
42
		}
43
44
		$schema = new \Doctrine\DBAL\Schema\Schema();
45
46
		$tagTable = $schema->createTable( 'mw_cache_tag_test' );
47
		$tagTable->addColumn( 'tid', 'string', array( 'length' => 255 ) );
48
		$tagTable->addColumn( 'tname', 'string', array( 'length' => 255 ) );
49
		$tagTable->addUniqueIndex( array( 'tid', 'tname' ) );
50
		$tagTable->addForeignKeyConstraint( 'mw_cache_test', array( 'tid' ), array( 'id' ), array( 'onDelete' => 'CASCADE' ) );
51
52
		foreach( $schema->toSQL( self::$conn->getRawObject()->getDatabasePlatform() ) as $sql ) {
53
			self::$conn->create( $sql )->execute()->finish();
54
		}
55
	}
56
57
58
	public static function tearDownAfterClass() : void
59
	{
60
		if( self::$dbm instanceof \Aimeos\Base\DB\Manager\DBAL )
61
		{
62
			$conn = self::$dbm->acquire();
63
64
			$conn->create( 'DROP TABLE "mw_cache_tag_test"' )->execute()->finish();
65
			$conn->create( 'DROP TABLE "mw_cache_test"' )->execute()->finish();
66
67
			self::$dbm->release( self::$conn );
68
		}
69
	}
70
71
72
	protected function setUp() : void
73
	{
74
		if( !( self::$dbm instanceof \Aimeos\Base\DB\Manager\DBAL ) ) {
75
			$this->markTestSkipped( 'No DBAL database manager configured' );
76
		}
77
78
79
		$sql = 'INSERT INTO "mw_cache_test" ("id", "expire", "value") VALUES (\'t:1\', NULL, \'test 1\')';
80
		self::$conn->create( $sql )->execute()->finish();
81
82
		$sql = 'INSERT INTO "mw_cache_test" ("id", "expire", "value") VALUES (\'t:2\', \'2000-01-01 00:00:00\', \'test 2\')';
83
		self::$conn->create( $sql )->execute()->finish();
84
85
		$sql = 'INSERT INTO "mw_cache_tag_test" ("tid", "tname") VALUES (\'t:1\', \'tag:1\')';
86
		self::$conn->create( $sql )->execute()->finish();
87
88
89
		$this->config = [
90
			'cleanup' => '
91
				DELETE FROM "mw_cache_test" WHERE "expire" < ?
92
			',
93
			'clear' => '
94
				DELETE FROM "mw_cache_test"
95
			',
96
			'delete' => '
97
				DELETE FROM "mw_cache_test" WHERE "id" IN (?)
98
			',
99
			'deletebytag' => '
100
				DELETE FROM "mw_cache_test" WHERE "id" IN (
101
					SELECT "tid" FROM "mw_cache_tag_test" WHERE "tname" IN (?)
102
				)
103
			',
104
			'get' => '
105
				SELECT "id", "value", "expire" FROM "mw_cache_test"
106
				WHERE ( "expire" >= ? OR "expire" IS NULL ) AND "id" IN (?)
107
			',
108
			'set' => '
109
				INSERT INTO "mw_cache_test" ( "id", "expire", "value" ) VALUES ( ?, ?, ? )
110
			',
111
			'settag' => '
112
				INSERT INTO "mw_cache_tag_test" ( "tid", "tname" ) VALUES ( ?, ? )
113
			',
114
		];
115
116
		$this->object = new \Aimeos\Base\Cache\DB( $this->config, self::$conn );
117
	}
118
119
120
	public function tearDown() : void
121
	{
122
		if( self::$dbm instanceof \Aimeos\Base\DB\Manager\DBAL )
123
		{
124
			self::$conn->create( 'DELETE FROM "mw_cache_tag_test"' )->execute()->finish();
125
			self::$conn->create( 'DELETE FROM "mw_cache_test"' )->execute()->finish();
126
		}
127
	}
128
129
130
	public function testCleanup()
131
	{
132
		$this->assertTrue( $this->object->cleanup() );
133
134
		$conn = self::$dbm->acquire();
135
		$result = $conn->create( 'SELECT "id" FROM "mw_cache_test"' )->execute();
136
		self::$dbm->release( $conn );
137
138
		$this->assertEquals( array( 'id' => 't:1' ), $result->fetch() );
139
		$this->assertNull( $result->fetch() );
140
	}
141
142
143
	public function testDelete()
144
	{
145
		$this->assertTrue( $this->object->delete( 't:1' ) );
146
147
		$conn = self::$dbm->acquire();
148
		$row = $conn->create( 'SELECT * FROM "mw_cache_tag_test"' )->execute()->fetch();
149
		self::$dbm->release( $conn );
150
151
		$this->assertNull( $row );
152
153
154
		$conn = self::$dbm->acquire();
155
		$result = $conn->create( 'SELECT "id" FROM "mw_cache_test"' )->execute();
156
		self::$dbm->release( $conn );
157
158
		$this->assertEquals( array( 'id' => 't:2' ), $result->fetch() );
159
		$this->assertNull( $result->fetch() );
160
	}
161
162
163
	public function testDeleteMultiple()
164
	{
165
		$this->assertTrue( $this->object->deleteMultiple( array( 't:1', 't:2' ) ) );
166
167
		$conn = self::$dbm->acquire();
168
		$row = $conn->create( 'SELECT * FROM "mw_cache_test"' )->execute()->fetch();
169
		self::$dbm->release( $conn );
170
171
		$this->assertNull( $row );
172
	}
173
174
175
	public function testDeleteByTags()
176
	{
177
		$this->assertTrue( $this->object->deleteByTags( array( 'tag:1' ) ) );
178
179
		$conn = self::$dbm->acquire();
180
		$row = $conn->create( 'SELECT * FROM "mw_cache_tag_test"' )->execute()->fetch();
181
		self::$dbm->release( $conn );
182
183
		$this->assertNull( $row );
184
185
186
		$conn = self::$dbm->acquire();
187
		$result = $conn->create( 'SELECT "id" FROM "mw_cache_test"' )->execute();
188
		self::$dbm->release( $conn );
189
190
		$this->assertEquals( array( 'id' => 't:2' ), $result->fetch() );
191
		$this->assertNull( $result->fetch() );
192
	}
193
194
195
	public function testClear()
196
	{
197
		$this->assertTrue( $this->object->clear() );
198
199
		$conn = self::$dbm->acquire();
200
		$row = $conn->create( 'SELECT * FROM "mw_cache_tag_test"' )->execute()->fetch();
201
		self::$dbm->release( $conn );
202
203
		$this->assertNull( $row );
204
205
206
		$conn = self::$dbm->acquire();
207
		$row = $conn->create( 'SELECT "id" FROM "mw_cache_test"' )->execute()->fetch();
208
		self::$dbm->release( $conn );
209
210
		$this->assertNull( $row );
211
	}
212
213
214
	public function testGet()
215
	{
216
		$this->assertEquals( 'test 1', $this->object->get( 't:1' ) );
217
	}
218
219
220
	public function testGetExpired()
221
	{
222
		$this->assertEquals( null, $this->object->get( 't:2' ) );
223
	}
224
225
226
	public function testGetMultiple()
227
	{
228
		$this->assertEquals( array( 't:1' => 'test 1', 't:2' => null ), $this->object->getMultiple( array( 't:1', 't:2' ) ) );
229
	}
230
231
232
	public function testHas()
233
	{
234
		$this->assertTrue( $this->object->has( 't:1' ) );
235
	}
236
237
238
	public function testSet()
239
	{
240
		$this->assertTrue( $this->object->set( 't:3', 'test 3', '2100-01-01 00:00:00', ['tag:2', 'tag:3'] ) );
241
242
243
		$conn = self::$dbm->acquire();
244
		$result = $conn->create( 'SELECT "tname" FROM "mw_cache_tag_test" WHERE "tid" = \'t:3\' ORDER BY "tname"' )->execute();
245
		self::$dbm->release( $conn );
246
247
		$this->assertEquals( array( 'tname' => 'tag:2' ), $result->fetch() );
248
		$this->assertEquals( array( 'tname' => 'tag:3' ), $result->fetch() );
249
		$this->assertNull( $result->fetch() );
250
251
252
		$conn = self::$dbm->acquire();
253
		$result = $conn->create( 'SELECT * FROM "mw_cache_test" WHERE "id" = \'t:3\'' )->execute();
254
		self::$dbm->release( $conn );
255
256
		$row = $result->fetch();
257
258
		$this->assertEquals( 't:3', $row['id'] );
259
		$this->assertEquals( 'test 3', $row['value'] );
260
		$this->assertEquals( '2100-01-01 00:00:00', substr( $row['expire'], 0, 19 ) );
261
		$this->assertNull( $result->fetch() );
262
	}
263
264
265
	public function testSetMultiple()
266
	{
267
		$pairs = ['t:3' => 'test 3', 't:2' => 'test 4'];
268
269
		$this->assertTrue( $this->object->setMultiple( $pairs, '2100-01-01 00:00:00', ['tag:2', 'tag:3'] ) );
270
271
272
		$conn = self::$dbm->acquire();
273
		$result = $conn->create( 'SELECT "tname" FROM "mw_cache_tag_test" WHERE "tid" = \'t:3\' ORDER BY "tname"' )->execute();
274
		self::$dbm->release( $conn );
275
276
		$this->assertEquals( array( 'tname' => 'tag:2' ), $result->fetch() );
277
		$this->assertEquals( array( 'tname' => 'tag:3' ), $result->fetch() );
278
		$this->assertNull( $result->fetch() );
279
280
281
		$conn = self::$dbm->acquire();
282
		$result = $conn->create( 'SELECT "tname" FROM "mw_cache_tag_test" WHERE "tid" = \'t:2\' ORDER BY "tname"' )->execute();
283
		self::$dbm->release( $conn );
284
285
		$this->assertEquals( array( 'tname' => 'tag:2' ), $result->fetch() );
286
		$this->assertEquals( array( 'tname' => 'tag:3' ), $result->fetch() );
287
		$this->assertNull( $result->fetch() );
288
289
290
		$conn = self::$dbm->acquire();
291
		$result = $conn->create( 'SELECT * FROM "mw_cache_test" WHERE "id" = \'t:3\'' )->execute();
292
		self::$dbm->release( $conn );
293
294
		$row = $result->fetch();
295
296
		$this->assertEquals( 't:3', $row['id'] );
297
		$this->assertEquals( 'test 3', $row['value'] );
298
		$this->assertEquals( '2100-01-01 00:00:00', substr( $row['expire'], 0, 19 ) );
299
		$this->assertNull( $result->fetch() );
300
301
302
		$conn = self::$dbm->acquire();
303
		$result = $conn->create( 'SELECT * FROM "mw_cache_test" WHERE "id" = \'t:2\'' )->execute();
304
		self::$dbm->release( $conn );
305
306
		$row = $result->fetch();
307
308
		$this->assertEquals( 't:2', $row['id'] );
309
		$this->assertEquals( 'test 4', $row['value'] );
310
		$this->assertEquals( '2100-01-01 00:00:00', substr( $row['expire'], 0, 19 ) );
311
		$this->assertNull( $result->fetch() );
312
	}
313
}
314