1
|
|
|
<?php |
2
|
|
|
namespace DBAL\Tests; |
3
|
|
|
|
4
|
|
|
use PHPUnit\Framework\TestCase; |
5
|
|
|
use DBAL\Database; |
6
|
|
|
use DBAL\Caching\MemcachedCache; |
7
|
|
|
|
8
|
|
|
class DatabaseTest extends TestCase{ |
9
|
|
|
public $db; |
10
|
|
|
|
11
|
|
|
protected $test_table = 'test_table'; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @covers \DBAL\Database::__construct |
15
|
|
|
* @covers \DBAL\Database::connectToServer |
16
|
|
|
* @covers \DBAL\Database::isConnected |
17
|
|
|
*/ |
18
|
|
|
public function setUp(){ |
19
|
|
|
$this->connectToLiveDB(); |
20
|
|
|
if(!$this->db->isConnected()){ |
21
|
|
|
$this->markTestSkipped( |
22
|
|
|
'No local database connection is available' |
23
|
|
|
); |
24
|
|
|
} |
25
|
|
|
else{ |
26
|
|
|
$this->db->query("DROP TABLE IF EXISTS `{$this->test_table}`;"); |
27
|
|
|
$this->db->query("CREATE TABLE `{$this->test_table}` ( |
28
|
|
|
`id` int(11) NOT NULL AUTO_INCREMENT, |
29
|
|
|
`name` varchar(255) NOT NULL, |
30
|
|
|
`text_field` text NOT NULL, |
31
|
|
|
`number_field` int(11) NOT NULL, |
32
|
|
|
PRIMARY KEY (`id`) |
33
|
|
|
);"); |
34
|
|
|
$this->db->insert($this->test_table, array('name' => 'My Name', 'text_field' => 'Hello World', 'number_field' => 256)); |
35
|
|
|
$this->db->insert($this->test_table, array('name' => 'Inigo Montoya', 'text_field' => 'You killed my father, prepare to die', 'number_field' => 320)); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @covers \DBAL\Database::__destruct |
41
|
|
|
* @covers \DBAL\Database::closeDatabase |
42
|
|
|
*/ |
43
|
|
|
public function tearDown(){ |
44
|
|
|
$this->db = null; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @covers \DBAL\Database::connectToServer |
49
|
|
|
* @covers \DBAL\Database::isConnected |
50
|
|
|
*/ |
51
|
|
|
public function testConnect(){ |
52
|
|
|
$this->assertTrue($this->db->isConnected()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @covers \DBAL\Database::__destruct |
57
|
|
|
* @covers \DBAL\Database::closeDatabase |
58
|
|
|
*/ |
59
|
|
|
public function testCloseDatabaseConnection(){ |
60
|
|
|
$this->assertTrue($this->db->isConnected()); |
61
|
|
|
$this->assertObjectHasAttribute('sql', $this->db); |
62
|
|
|
$this->db = null; |
63
|
|
|
$this->assertNull($this->db); |
64
|
|
|
$this->connectToLiveDB(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @covers \DBAL\Database::__construct |
69
|
|
|
* @covers \DBAL\Database::connectToServer |
70
|
|
|
* @covers \DBAL\Database::isConnected |
71
|
|
|
* @covers \DBAL\Database::error |
72
|
|
|
*/ |
73
|
|
|
public function testConnectFailure(){ |
74
|
|
|
$db = new Database('localhost', 'wrong_username', 'incorrect_password', 'non_existent_db'); |
75
|
|
|
$this->assertFalse($db->isConnected()); |
76
|
|
|
$this->connectToLiveDB(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @covers \DBAL\Database::query |
81
|
|
|
*/ |
82
|
|
|
public function testQuery(){ |
83
|
|
|
// Insert a couple of test vales |
84
|
|
|
$this->db->insert($this->test_table, array('name' => 'My Name', 'text_field' => 'Hello World', 'number_field' => rand(1, 1000))); |
85
|
|
|
$this->db->insert($this->test_table, array('name' => 'Inigo Montoya', 'text_field' => 'You killed my father, prepare to die', 'number_field' => rand(1, 1000))); |
86
|
|
|
$query = $this->db->query("SELECT * FROM `test_table` WHERE `id` = ?", array(1)); |
87
|
|
|
$this->assertArrayHasKey(0, $query); |
88
|
|
|
$this->assertCount(1, $query); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @covers \DBAL\Database::select |
93
|
|
|
* @covers \DBAL\Database::selectAll |
94
|
|
|
* @covers \DBAL\Database::buildSelectQuery |
95
|
|
|
* @covers \DBAL\Database::where |
96
|
|
|
* @covers \DBAL\Database::orderBy |
97
|
|
|
* @covers \DBAL\Database::formatValues |
98
|
|
|
* @covers \DBAL\Database::executeQuery |
99
|
|
|
* @covers \DBAL\Database::bindValues |
100
|
|
|
* @covers \DBAL\Modifiers\Operators::getOperatorFormat |
101
|
|
|
* @covers \DBAL\Modifiers\Operators::isOperatorValid |
102
|
|
|
* @covers \DBAL\Modifiers\Operators::isOperatorPrepared |
103
|
|
|
* @covers \DBAL\Modifiers\SafeString::makeSafe |
104
|
|
|
*/ |
105
|
|
|
public function testSelect(){ |
106
|
|
|
$simpleSelect = $this->db->select($this->test_table, array('id' => array('>', 1)), '*', array('id' => 'ASC')); |
107
|
|
|
$this->assertArrayHasKey('name', $simpleSelect); |
108
|
|
|
$this->assertFalse($this->db->select($this->test_table, array('id' => 'IS NULL'), '*', array('id' => 'ASC'))); |
109
|
|
|
$this->assertArrayHasKey('name', $this->db->select($this->test_table, array('id' => 'IS NOT NULL'), array('id', 'name'), array('id' => 'ASC'))); |
110
|
|
|
$between = $this->db->select($this->test_table, array('id' => array('BETWEEN' => array(2, 3))), '*', array('id' => 'ASC')); |
111
|
|
|
$this->assertEquals(2, $between['id']); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @covers \DBAL\Database::selectAll |
116
|
|
|
* @covers \DBAL\Database::buildSelectQuery |
117
|
|
|
* @covers \DBAL\Database::where |
118
|
|
|
* @covers \DBAL\Database::numRows |
119
|
|
|
* @covers \DBAL\Database::rowCount |
120
|
|
|
* @covers \DBAL\Database::limit |
121
|
|
|
* @covers \DBAL\Database::executeQuery |
122
|
|
|
* @covers \DBAL\Database::bindValues |
123
|
|
|
* @covers \DBAL\Modifiers\SafeString::makeSafe |
124
|
|
|
*/ |
125
|
|
|
public function testSelectAll(){ |
126
|
|
|
$selectAll = $this->db->selectAll($this->test_table); |
127
|
|
|
$this->assertGreaterThan(1, $this->db->numRows()); |
128
|
|
|
$this->assertArrayHasKey('id', $selectAll[0]); |
129
|
|
|
$this->db->selectAll($this->test_table, array(), '*', array(), 1); |
130
|
|
|
$this->assertEquals(1, $this->db->rowCount()); |
131
|
|
|
$this->db->selectAll($this->test_table, array(), '*', array(), array(0 => 50)); |
132
|
|
|
$this->assertLessThan(51, $this->db->numRows()); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @covers \DBAL\Database::selectAll |
137
|
|
|
* @covers \DBAL\Database::buildSelectQuery |
138
|
|
|
* @covers \DBAL\Database::executeQuery |
139
|
|
|
* @covers \DBAL\Database::where |
140
|
|
|
* @covers \DBAL\Database::bindValues |
141
|
|
|
* @covers \DBAL\Database::error |
142
|
|
|
* @covers \DBAL\Modifiers\SafeString::makeSafe |
143
|
|
|
*/ |
144
|
|
|
public function testSelectFailure(){ |
145
|
|
|
$this->assertFalse($this->db->selectAll($this->test_table, array('id' => 100))); |
146
|
|
|
$this->assertFalse($this->db->selectAll('unknown_table')); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @covers \DBAL\Database::fetchColumn |
151
|
|
|
* @covers \DBAL\Database::buildSelectQuery |
152
|
|
|
* @covers \DBAL\Database::executeQuery |
153
|
|
|
* @covers \DBAL\Database::where |
154
|
|
|
* @covers \DBAL\Database::bindValues |
155
|
|
|
* @covers \DBAL\Modifiers\SafeString::makeSafe |
156
|
|
|
*/ |
157
|
|
|
public function testFetchColumn(){ |
158
|
|
|
$this->assertEquals('Inigo Montoya', $this->db->fetchColumn($this->test_table, array('id' => 2), '*', 1)); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @covers \DBAL\Database::fetchColumn |
163
|
|
|
* @covers \DBAL\Database::buildSelectQuery |
164
|
|
|
* @covers \DBAL\Database::executeQuery |
165
|
|
|
* @covers \DBAL\Database::where |
166
|
|
|
* @covers \DBAL\Database::bindValues |
167
|
|
|
* @covers \DBAL\Modifiers\SafeString::makeSafe |
168
|
|
|
*/ |
169
|
|
|
public function testFetchColumnFailure(){ |
170
|
|
|
$this->assertFalse($this->db->fetchColumn($this->test_table, array('id' => 2), '*', 6)); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @covers \DBAL\Database::insert |
176
|
|
|
* @covers \DBAL\Database::fields |
177
|
|
|
* @covers \DBAL\Database::numRows |
178
|
|
|
* @covers \DBAL\Database::executeQuery |
179
|
|
|
* @covers \DBAL\Database::bindValues |
180
|
|
|
*/ |
181
|
|
|
public function testInsert(){ |
182
|
|
|
$this->assertTrue($this->db->insert($this->test_table, array('name' => 'Third User', 'text_field' => 'Helloooooo', 'number_field' => rand(1, 1000)))); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @covers \DBAL\Database::insert |
187
|
|
|
* @covers \DBAL\Database::fields |
188
|
|
|
* @covers \DBAL\Database::numRows |
189
|
|
|
* @covers \DBAL\Database::executeQuery |
190
|
|
|
* @covers \DBAL\Database::bindValues |
191
|
|
|
* @covers \DBAL\Database::error |
192
|
|
|
*/ |
193
|
|
|
public function testInsertFailure(){ |
194
|
|
|
$this->assertFalse($this->db->insert($this->test_table, array('id' => 3, 'name' => 'Third User', 'text_field' => NULL, 'number_field' => rand(1, 1000)))); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @covers \DBAL\Database::update |
199
|
|
|
* @covers \DBAL\Database::fields |
200
|
|
|
* @covers \DBAL\Database::numRows |
201
|
|
|
* @covers \DBAL\Database::executeQuery |
202
|
|
|
* @covers \DBAL\Database::where |
203
|
|
|
* @covers \DBAL\Database::bindValues |
204
|
|
|
*/ |
205
|
|
|
public function testUpdate(){ |
206
|
|
|
$this->assertTrue($this->db->update($this->test_table, array('text_field' => 'Altered text', 'number_field' => rand(1, 1000)), array('id' => 1))); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @covers \DBAL\Database::update |
211
|
|
|
* @covers \DBAL\Database::fields |
212
|
|
|
* @covers \DBAL\Database::numRows |
213
|
|
|
* @covers \DBAL\Database::executeQuery |
214
|
|
|
* @covers \DBAL\Database::where |
215
|
|
|
* @covers \DBAL\Database::bindValues |
216
|
|
|
* @covers \DBAL\Database::error |
217
|
|
|
*/ |
218
|
|
|
public function testUpdateFailure(){ |
219
|
|
|
$this->assertFalse($this->db->update($this->test_table, array('number_field' => 256), array('id' => 1))); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @covers \DBAL\Database::delete |
224
|
|
|
* @covers \DBAL\Database::numRows |
225
|
|
|
* @covers \DBAL\Database::formatValues |
226
|
|
|
* @covers \DBAL\Database::executeQuery |
227
|
|
|
* @covers \DBAL\Database::where |
228
|
|
|
* @covers \DBAL\Database::bindValues |
229
|
|
|
* @covers \DBAL\Modifiers\Operators::getOperatorFormat |
230
|
|
|
* @covers \DBAL\Modifiers\Operators::isOperatorValid |
231
|
|
|
* @covers \DBAL\Modifiers\Operators::isOperatorPrepared |
232
|
|
|
* @covers \DBAL\Modifiers\SafeString::makeSafe |
233
|
|
|
*/ |
234
|
|
|
public function testDelete(){ |
235
|
|
|
$this->assertTrue($this->db->delete($this->test_table, array('id' => array('>=', 2)))); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @covers \DBAL\Database::delete |
240
|
|
|
* @covers \DBAL\Database::numRows |
241
|
|
|
* @covers \DBAL\Database::executeQuery |
242
|
|
|
* @covers \DBAL\Database::bindValues |
243
|
|
|
*/ |
244
|
|
|
public function testDeleteFailure(){ |
245
|
|
|
$this->assertFalse($this->db->delete($this->test_table, array('id' => 3))); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @covers \DBAL\Database::count |
250
|
|
|
* @covers \DBAL\Database::executeQuery |
251
|
|
|
*/ |
252
|
|
|
public function testCount(){ |
253
|
|
|
$this->assertEquals(2, $this->db->count($this->test_table)); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @covers \DBAL\Database::lastInsertID |
258
|
|
|
*/ |
259
|
|
|
public function testLastInsertID(){ |
260
|
|
|
$this->testInsert(); |
261
|
|
|
$this->assertEquals(3, $this->db->lastInsertID()); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @covers \DBAL\Database::serverVersion |
266
|
|
|
*/ |
267
|
|
|
public function testServerVersion(){ |
268
|
|
|
$this->assertGreaterThan(5, $this->db->serverVersion()); |
269
|
|
|
$this->assertContains('.', $this->db->serverVersion()); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @covers \DBAL\Database::setCaching |
274
|
|
|
*/ |
275
|
|
|
public function testSetCaching(){ |
276
|
|
|
if (extension_loaded('memcached')) { |
277
|
|
|
$caching = new MemcachedCache(); |
278
|
|
|
$caching->connect('localhost', '11211'); |
279
|
|
|
if(is_object($caching)){ |
280
|
|
|
$this->db->setCaching($caching); |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
$this->assertObjectHasAttribute('sql', $this->db->setCaching('not_a_instance_od_cache_but_should_still_return')); |
|
|
|
|
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @covers \DBAL\Database::truncate |
288
|
|
|
* @covers \DBAL\Database::error |
289
|
|
|
* @covers \DBAL\Database::executeQuery |
290
|
|
|
* @covers \DBAL\Database::bindValues |
291
|
|
|
* @covers \DBAL\Database::numRows |
292
|
|
|
*/ |
293
|
|
|
public function testTruncate(){ |
294
|
|
|
$this->db->truncate($this->test_table); |
295
|
|
|
$this->assertEquals(0, $this->db->count($this->test_table)); |
296
|
|
|
$this->assertFalse($this->db->truncate('any_table')); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
protected function connectToLiveDB(){ |
300
|
|
|
$this->db = new Database($GLOBALS['HOSTNAME'], $GLOBALS['USERNAME'], $GLOBALS['PASSWORD'], $GLOBALS['DATABASE'], false, false, true, $GLOBALS['DRIVER']); |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
|
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: