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

MysqlTest::testSetMultiple()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 22
nc 1
nop 0
dl 0
loc 32
rs 8.8571
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 MysqlTest extends \PHPUnit_Framework_TestCase
14
{
15
	private $dbm;
16
	private $config;
17
	private $object;
18
19
20
	protected function setUp()
21
	{
22
		if( \TestHelperMw::getConfig()->get( 'resource/db/adapter', false ) !== 'mysql' ) {
23
			$this->markTestSkipped( 'No MySQL database configured' );
24
		}
25
26
27
		$this->config = array( 'siteid' => 1 );
28
29
		$this->config['search'] = array(
30
			'cache.id' => array( 'label' => 'Cache ID', 'code' => 'cache.id', 'internalcode' => 'id', 'type' => 'string', 'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR ),
31
			'cache.siteid' => array( 'label' => 'Cache site ID', 'code' => 'cache.siteid', 'internalcode' => 'siteid', 'type' => 'integer', 'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT ),
32
			'cache.value' => array( 'label' => 'Cached value', 'code' => 'cache.value', 'internalcode' => 'value', 'type' => 'string', 'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR ),
33
			'cache.expire' => array( 'label' => 'Cache expiration date', 'code' => 'cache.expire', 'internalcode' => 'expire', 'type' => 'datetime', 'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR ),
34
			'cache.tag.name' => array( 'label' => 'Cache tag name', 'code' => 'cache.tag.name', 'internalcode' => 'tname', 'type' => 'string', 'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR ),
35
		);
36
37
		$this->config['sql'] = array(
38
			'delete' => '
39
				DELETE FROM "mw_cache_test" WHERE "siteid" = ? AND :cond
40
			',
41
			'deletebytag' => '
42
				DELETE FROM "mw_cache_test" WHERE "siteid" = ? AND id IN (
43
					SELECT "tid" FROM "mw_cache_tag_test" WHERE "tsiteid" = ? AND :cond
44
				)
45
			',
46
			'get' => '
47
				SELECT "id", "value", "expire" FROM "mw_cache_test" WHERE "siteid" = ? AND :cond
48
			',
49
			'getbytag' => '
50
				SELECT "id", "value", "expire" FROM "mw_cache_test"
51
				JOIN "mw_cache_tag_test" ON "tid" = "id"
52
				WHERE siteid = ? AND tsiteid = ? AND :cond
53
			',
54
			'set' => '
55
				REPLACE INTO "mw_cache_test" ( "id", "siteid", "expire", "value" ) VALUES ( ?, ?, ?, ? )
56
			',
57
			'settag' => '
58
				REPLACE INTO "mw_cache_tag_test" ( "tid", "tsiteid", "tname" ) VALUES  :tuples
59
			',
60
		);
61
62
63
		$this->dbm = \TestHelperMw::getDBManager();
64
		$conn = $this->dbm->acquire();
65
66
67
		$sql = 'DROP TABLE IF EXISTS "mw_cache_tag_test"';
68
		$conn->create( $sql )->execute()->finish();
69
70
		$sql = 'DROP TABLE IF EXISTS "mw_cache_test"';
71
		$conn->create( $sql )->execute()->finish();
72
73
		$sql = '
74
			CREATE TABLE IF NOT EXISTS "mw_cache_test" (
75
				"id" VARCHAR(255) NOT NULL,
76
				"siteid" INTEGER NULL,
77
				"expire" DATETIME NULL,
78
				"value" MEDIUMTEXT NOT NULL,
79
				KEY ("expire"),
80
				CONSTRAINT UNIQUE KEY ("id", "siteid")
81
			);
82
		';
83
		$conn->create( $sql )->execute()->finish();
84
85
		$sql = '
86
			CREATE TABLE IF NOT EXISTS "mw_cache_tag_test" (
87
				"tid" VARCHAR(255) NOT NULL,
88
				"tsiteid" INTEGER NULL,
89
				"tname" VARCHAR(255) NOT NULL,
90
				CONSTRAINT UNIQUE ("tid", "tsiteid", "tname"),
91
				CONSTRAINT FOREIGN KEY ("tid") REFERENCES "mw_cache_test" ("id") ON DELETE CASCADE
92
			);
93
		';
94
		$conn->create( $sql )->execute()->finish();
95
96
97
		$sql = 'INSERT INTO "mw_cache_test" ("id", "siteid", "expire", "value") VALUES (\'t:1\', 1, NULL, \'test 1\')';
98
		$conn->create( $sql )->execute()->finish();
99
100
		$sql = 'INSERT INTO "mw_cache_tag_test" ("tid", "tsiteid", "tname") VALUES (\'t:1\', 1, \'tag:1\')';
101
		$conn->create( $sql )->execute()->finish();
102
103
104
		$this->dbm->release( $conn );
105
106
107
		$this->object = new \Aimeos\MW\Cache\Mysql( $this->config, $this->dbm );
108
	}
109
110
111
	protected function tearDown()
112
	{
113
		if( \TestHelperMw::getConfig()->get( 'resource/db/adapter', false ) === 'mysql' )
114
		{
115
			$this->dbm = \TestHelperMw::getDBManager();
116
			$conn = $this->dbm->acquire();
117
118
			$conn->create( 'DROP TABLE "mw_cache_tag_test"' )->execute()->finish();
119
			$conn->create( 'DROP TABLE "mw_cache_test"' )->execute()->finish();
120
121
			$this->dbm->release( $conn );
122
		}
123
	}
124
125
126
	public function testSetMultiple()
127
	{
128
		$pairs = array( 't:1' => 'test 2' );
129
		$tags = array( 't:1' => array( 'tag:1', 'tag:2', 'tag:3' ) );
130
		$expires = array( 't:1' => '2100-00-00 00:00:00' );
131
132
		$this->object->setMultiple( $pairs, $expires, $tags );
0 ignored issues
show
Documentation introduced by
$pairs is of type array<string,string,{"t: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...
133
134
135
		$conn = $this->dbm->acquire();
136
		$result = $conn->create( 'SELECT "tname" FROM "mw_cache_tag_test" WHERE "tid" = \'t:1\' ORDER BY "tname"' )->execute();
137
		$this->dbm->release( $conn );
138
139
		$this->assertEquals( array( 'tname' => 'tag:1' ), $result->fetch() );
140
		$this->assertEquals( array( 'tname' => 'tag:2' ), $result->fetch() );
141
		$this->assertEquals( array( 'tname' => 'tag:3' ), $result->fetch() );
142
		$this->assertFalse( $result->fetch() );
143
144
145
		$conn = $this->dbm->acquire();
146
		$result = $conn->create( 'SELECT * FROM "mw_cache_test" WHERE "id" = \'t:1\'' )->execute();
147
		$this->dbm->release( $conn );
148
149
		$expected = array(
150
			'expire' => '2100-00-00 00:00:00',
151
			'id' => 't:1',
152
			'siteid' => 1,
153
			'value' => 'test 2',
154
		);
155
		$this->assertEquals( $expected, $result->fetch() );
156
		$this->assertFalse( $result->fetch() );
157
	}
158
}
159