Completed
Push — master ( 815faa...197710 )
by Aimeos
07:55
created

DBTest::testClear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2014
6
 * @copyright Aimeos (aimeos.org), 2015-2016
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()
21
	{
22
		self::$dbm = \TestHelperMw::getDBManager();
23
24
		if( !( self::$dbm instanceof \Aimeos\MW\DB\Manager\DBAL ) ) {
25
			return;
26
		}
27
28
		$schema = new \Doctrine\DBAL\Schema\Schema();
29
30
		$cacheTable = $schema->createTable( 'mw_cache_test' );
31
		$cacheTable->addColumn( 'id', 'string', array( 'length' => 255 ) );
32
		$cacheTable->addColumn( 'siteid', 'integer', array( 'notnull' => false ) );
33
		$cacheTable->addColumn( 'expire', 'datetime', array( 'notnull' => false ) );
34
		$cacheTable->addColumn( 'value', 'text', array( 'length' => 0xffff ) );
35
		$cacheTable->addUniqueIndex( array( 'id', 'siteid' ) );
36
		$cacheTable->addIndex( array( 'expire' ) );
37
38
		$tagTable = $schema->createTable( 'mw_cache_tag_test' );
39
		$tagTable->addColumn( 'tid', 'string', array( 'length' => 255 ) );
40
		$tagTable->addColumn( 'tsiteid', 'integer', array( 'notnull' => false ) );
41
		$tagTable->addColumn( 'tname', 'string', array( 'length' => 255 ) );
42
		$tagTable->addUniqueIndex( array( 'tid', 'tsiteid', 'tname' ) );
43
		$tagTable->addForeignKeyConstraint( 'mw_cache_test', array( 'tid', 'tsiteid' ), array( 'id', 'siteid' ), array( 'onDelete' => 'CASCADE' ) );
44
45
46
		$conn = self::$dbm->acquire();
47
48
		foreach( $schema->toSQL( $conn->getRawObject()->getDatabasePlatform() ) as $sql ) {
49
			$conn->create( $sql )->execute()->finish();
50
		}
51
52
		self::$dbm->release( $conn );
53
	}
54
55
56
	public static function tearDownAfterClass()
57
	{
58
		if( self::$dbm instanceof \Aimeos\MW\DB\Manager\DBAL )
59
		{
60
			$conn = self::$dbm->acquire();
61
62
			$conn->create( 'DROP TABLE "mw_cache_tag_test"' )->execute()->finish();
63
			$conn->create( 'DROP TABLE "mw_cache_test"' )->execute()->finish();
64
65
			self::$dbm->release( $conn );
66
		}
67
	}
68
69
70
	protected function setUp()
71
	{
72
		if( !( self::$dbm instanceof \Aimeos\MW\DB\Manager\DBAL ) ) {
73
			$this->markTestSkipped( 'No DBAL database manager configured' );
74
		}
75
76
77
		$conn = self::$dbm->acquire();
78
79
		$sql = 'INSERT INTO "mw_cache_test" ("id", "siteid", "expire", "value") VALUES (\'t:1\', 1, NULL, \'test 1\')';
80
		$conn->create( $sql )->execute()->finish();
81
82
		$sql = 'INSERT INTO "mw_cache_test" ("id", "siteid", "expire", "value") VALUES (\'t:2\', 1, \'2000-01-01 00:00:00\', \'test 2\')';
83
		$conn->create( $sql )->execute()->finish();
84
85
		$sql = 'INSERT INTO "mw_cache_tag_test" ("tid", "tsiteid", "tname") VALUES (\'t:1\', 1, \'tag:1\')';
86
		$conn->create( $sql )->execute()->finish();
87
88
		self::$dbm->release( $conn );
89
90
91
		$this->config = array( 'siteid' => 1 );
92
93
		$this->config['search'] = array(
94
			'cache.id' => array( 'label' => 'Cache ID', 'code' => 'cache.id', 'internalcode' => 'id', 'type' => 'string', 'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR ),
95
			'cache.siteid' => array( 'label' => 'Cache site ID', 'code' => 'cache.siteid', 'internalcode' => 'siteid', 'type' => 'integer', 'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT ),
96
			'cache.value' => array( 'label' => 'Cached value', 'code' => 'cache.value', 'internalcode' => 'value', 'type' => 'string', 'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR ),
97
			'cache.expire' => array( 'label' => 'Cache expiration date', 'code' => 'cache.expire', 'internalcode' => 'expire', 'type' => 'datetime', 'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR ),
98
			'cache.tag.name' => array( 'label' => 'Cache tag name', 'code' => 'cache.tag.name', 'internalcode' => 'tname', 'type' => 'string', 'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR ),
99
		);
100
101
		$this->config['sql'] = array(
102
			'delete' => '
103
				DELETE FROM "mw_cache_test" WHERE "siteid" = ? AND :cond
104
			',
105
			'deletebytag' => '
106
				DELETE FROM "mw_cache_test" WHERE "siteid" = ? AND id IN (
107
					SELECT "tid" FROM "mw_cache_tag_test" WHERE "tsiteid" = ? AND :cond
108
				)
109
			',
110
			'get' => '
111
				SELECT "id", "value", "expire" FROM "mw_cache_test" WHERE "siteid" = ? AND :cond
112
			',
113
			'getbytag' => '
114
				SELECT "id", "value", "expire" FROM "mw_cache_test"
115
				JOIN "mw_cache_tag_test" ON "tid" = "id"
116
				WHERE siteid = ? AND tsiteid = ? AND :cond
117
			',
118
			'set' => '
119
				INSERT INTO "mw_cache_test" ( "id", "siteid", "expire", "value" ) VALUES ( ?, ?, ?, ? )
120
			',
121
			'settag' => '
122
				INSERT INTO "mw_cache_tag_test" ( "tid", "tsiteid", "tname" ) VALUES ( ?, ?, ? )
123
			',
124
		);
125
126
		$this->object = new \Aimeos\MW\Cache\DB( $this->config, self::$dbm );
127
	}
128
129
130
	public function tearDown()
131
	{
132
		if( self::$dbm instanceof \Aimeos\MW\DB\Manager\DBAL )
133
		{
134
			$conn = self::$dbm->acquire();
135
136
			$conn->create( 'DELETE FROM "mw_cache_tag_test"' )->execute()->finish();
137
			$conn->create( 'DELETE FROM "mw_cache_test"' )->execute()->finish();
138
139
			self::$dbm->release( $conn );
140
		}
141
	}
142
143
144
	public function testConstructorNoConfig()
145
	{
146
		$this->setExpectedException( '\\Aimeos\\MW\\Cache\\Exception' );
147
		new \Aimeos\MW\Cache\DB( array(), self::$dbm );
148
	}
149
150
151
	public function testConstructorNoSql()
152
	{
153
		$config = $this->config;
154
		unset( $config['sql'] );
155
156
		$this->setExpectedException( '\\Aimeos\\MW\\Cache\\Exception' );
157
		new \Aimeos\MW\Cache\DB( $config, self::$dbm );
158
	}
159
160
161
	public function testConstructorNoSearch()
162
	{
163
		$config = $this->config;
164
		unset( $config['search'] );
165
166
		$this->setExpectedException( '\\Aimeos\\MW\\Cache\\Exception' );
167
		new \Aimeos\MW\Cache\DB( $config, self::$dbm );
168
	}
169
170
171
	public function testConstructorIncompleteSql()
172
	{
173
		$config = $this->config;
174
		unset( $config['sql']['delete'] );
175
176
		$this->setExpectedException( '\\Aimeos\\MW\\Cache\\Exception' );
177
		new \Aimeos\MW\Cache\DB( $config, self::$dbm );
178
	}
179
180
181
	public function testConstructorIncompleteSearch()
182
	{
183
		$config = $this->config;
184
		unset( $config['search']['cache.id'] );
185
186
		$this->setExpectedException( '\\Aimeos\\MW\\Cache\\Exception' );
187
		new \Aimeos\MW\Cache\DB( $config, self::$dbm );
188
	}
189
190
191
	public function testCleanup()
192
	{
193
		$this->object->cleanup();
194
195
196
		$conn = self::$dbm->acquire();
197
		$result = $conn->create( 'SELECT "id" FROM "mw_cache_test"' )->execute();
198
		self::$dbm->release( $conn );
199
200
		$this->assertEquals( array( 'id' => 't:1' ), $result->fetch() );
201
		$this->assertFalse( $result->fetch() );
202
	}
203
204
205
	public function testDelete()
206
	{
207
		$this->object->delete( 't:1' );
208
209
		$conn = self::$dbm->acquire();
210
		$row = $conn->create( 'SELECT * FROM "mw_cache_tag_test"' )->execute()->fetch();
211
		self::$dbm->release( $conn );
212
213
		$this->assertFalse( $row );
214
215
216
		$conn = self::$dbm->acquire();
217
		$result = $conn->create( 'SELECT "id" FROM "mw_cache_test"' )->execute();
218
		self::$dbm->release( $conn );
219
220
		$this->assertEquals( array( 'id' => 't:2' ), $result->fetch() );
221
		$this->assertFalse( $result->fetch() );
222
	}
223
224
225
	public function testDeleteMultiple()
226
	{
227
		$this->object->deleteMultiple( array( 't:1', 't:2' ) );
0 ignored issues
show
Documentation introduced by
array('t:1', 't:2') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a object<Aimeos\MW\Cache\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
228
229
		$conn = self::$dbm->acquire();
230
		$row = $conn->create( 'SELECT * FROM "mw_cache_test"' )->execute()->fetch();
231
		self::$dbm->release( $conn );
232
233
		$this->assertFalse( $row );
234
	}
235
236
237
	public function testDeleteByTags()
238
	{
239
		$this->object->deleteByTags( array( 'tag:1' ) );
240
241
		$conn = self::$dbm->acquire();
242
		$row = $conn->create( 'SELECT * FROM "mw_cache_tag_test"' )->execute()->fetch();
243
		self::$dbm->release( $conn );
244
245
		$this->assertFalse( $row );
246
247
248
		$conn = self::$dbm->acquire();
249
		$result = $conn->create( 'SELECT "id" FROM "mw_cache_test"' )->execute();
250
		self::$dbm->release( $conn );
251
252
		$this->assertEquals( array( 'id' => 't:2' ), $result->fetch() );
253
		$this->assertFalse( $result->fetch() );
254
	}
255
256
257
	public function testClear()
258
	{
259
		$this->object->clear();
260
261
		$conn = self::$dbm->acquire();
262
		$row = $conn->create( 'SELECT * FROM "mw_cache_tag_test"' )->execute()->fetch();
263
		self::$dbm->release( $conn );
264
265
		$this->assertFalse( $row );
266
267
268
		$conn = self::$dbm->acquire();
269
		$row = $conn->create( 'SELECT "id" FROM "mw_cache_test"' )->execute()->fetch();
270
		self::$dbm->release( $conn );
271
272
		$this->assertFalse( $row );
273
	}
274
275
276
	public function testGet()
277
	{
278
		$this->assertEquals( 'test 1', $this->object->get( 't:1' ) );
279
	}
280
281
282
	public function testGetExpired()
283
	{
284
		$this->assertEquals( null, $this->object->get( 't:2' ) );
285
	}
286
287
288
	public function testGetMultiple()
289
	{
290
		$this->assertEquals( array( 't:1' => 'test 1', 't:2' => null ), $this->object->getMultiple( array( 't:1', 't:2' ) ) );
0 ignored issues
show
Documentation introduced by
array('t:1', 't:2') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a object<Aimeos\MW\Cache\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
291
	}
292
293
294
	public function testGetMultipleByTags()
295
	{
296
		$this->assertEquals( array( 't:1' => 'test 1' ), $this->object->getMultipleByTags( array( 'tag:1' ) ) );
297
	}
298
299
300
	public function testSet()
301
	{
302
		$this->object->set( 't:3', 'test 3', '2100-01-01 00:00:00', array( 'tag:2', 'tag:3' ) );
303
304
305
		$conn = self::$dbm->acquire();
306
		$result = $conn->create( 'SELECT "tname" FROM "mw_cache_tag_test" WHERE "tid" = \'t:3\' ORDER BY "tname"' )->execute();
307
		self::$dbm->release( $conn );
308
309
		$this->assertEquals( array( 'tname' => 'tag:2' ), $result->fetch() );
310
		$this->assertEquals( array( 'tname' => 'tag:3' ), $result->fetch() );
311
		$this->assertFalse( $result->fetch() );
312
313
314
		$conn = self::$dbm->acquire();
315
		$result = $conn->create( 'SELECT * FROM "mw_cache_test" WHERE "id" = \'t:3\'' )->execute();
316
		self::$dbm->release( $conn );
317
318
		$expected = array(
319
			'expire' => '2100-01-01 00:00:00',
320
			'id' => 't:3',
321
			'siteid' => 1,
322
			'value' => 'test 3',
323
		);
324
		$this->assertEquals( $expected, $result->fetch() );
325
		$this->assertFalse( $result->fetch() );
326
	}
327
328
329
	public function testSetMultiple()
330
	{
331
		$pairs = array( 't:3' => 'test 3', 't:2' => 'test 4' );
332
		$tags = array( 't:3' => array( 'tag:2', 'tag:3' ), 't:2' => array( 'tag:4' ) );
333
		$expires = array( 't:3' => '2100-01-01 00:00:00' );
334
335
		$this->object->setMultiple( $pairs, $expires, $tags );
0 ignored issues
show
Documentation introduced by
$pairs is of type array<string,string,{"t:...tring","t:2":"string"}>, but the function expects a object<Aimeos\MW\Cache\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
336
337
338
		$conn = self::$dbm->acquire();
339
		$result = $conn->create( 'SELECT "tname" FROM "mw_cache_tag_test" WHERE "tid" = \'t:3\' ORDER BY "tname"' )->execute();
340
		self::$dbm->release( $conn );
341
342
		$this->assertEquals( array( 'tname' => 'tag:2' ), $result->fetch() );
343
		$this->assertEquals( array( 'tname' => 'tag:3' ), $result->fetch() );
344
		$this->assertFalse( $result->fetch() );
345
346
347
		$conn = self::$dbm->acquire();
348
		$result = $conn->create( 'SELECT "tname" FROM "mw_cache_tag_test" WHERE "tid" = \'t:2\'' )->execute();
349
		self::$dbm->release( $conn );
350
351
		$this->assertEquals( array( 'tname' => 'tag:4' ), $result->fetch() );
352
		$this->assertFalse( $result->fetch() );
353
354
355
		$conn = self::$dbm->acquire();
356
		$result = $conn->create( 'SELECT * FROM "mw_cache_test" WHERE "id" = \'t:3\'' )->execute();
357
		self::$dbm->release( $conn );
358
359
		$expected = array(
360
			'expire' => '2100-01-01 00:00:00',
361
			'id' => 't:3',
362
			'siteid' => 1,
363
			'value' => 'test 3',
364
		);
365
		$this->assertEquals( $expected, $result->fetch() );
366
		$this->assertFalse( $result->fetch() );
367
368
369
		$conn = self::$dbm->acquire();
370
		$result = $conn->create( 'SELECT * FROM "mw_cache_test" WHERE "id" = \'t:2\'' )->execute();
371
		self::$dbm->release( $conn );
372
373
		$expected = array(
374
			'expire' => null,
375
			'id' => 't:2',
376
			'siteid' => 1,
377
			'value' => 'test 4',
378
		);
379
		$this->assertEquals( $expected, $result->fetch() );
380
		$this->assertFalse( $result->fetch() );
381
	}
382
383
384
	public function testSetException()
385
	{
386
		$this->setExpectedException( '\\Aimeos\\MW\\Cache\\Exception' );
387
		$this->object->set( array(), '' );
388
	}
389
390
}
391