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
|
|
|
for($i = 1; $i <= 200; $i++){ |
132
|
|
|
// Insert some more values for testing |
133
|
|
|
$this->db->insert($this->test_table, array('name' => 'Name '.$i, 'text_field' => 'TextField'.$i, 'number_field' => rand(1, 1000))); |
134
|
|
|
} |
135
|
|
|
$original = $this->db->selectAll($this->test_table, array(), '*', array(), array(0 => 50)); |
136
|
|
|
$this->assertLessThan(51, $this->db->numRows()); |
137
|
|
|
$random = $this->db->selectAll($this->test_table, array(), '*', 'RAND()', array(0 => 50)); |
|
|
|
|
138
|
|
|
$this->assertNotEquals($original, $random); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @covers \DBAL\Database::selectAll |
143
|
|
|
* @covers \DBAL\Database::buildSelectQuery |
144
|
|
|
* @covers \DBAL\Database::executeQuery |
145
|
|
|
* @covers \DBAL\Database::where |
146
|
|
|
* @covers \DBAL\Database::bindValues |
147
|
|
|
* @covers \DBAL\Database::error |
148
|
|
|
* @covers \DBAL\Modifiers\SafeString::makeSafe |
149
|
|
|
*/ |
150
|
|
|
public function testSelectFailure(){ |
151
|
|
|
$this->assertFalse($this->db->selectAll($this->test_table, array('id' => 100))); |
152
|
|
|
$this->assertFalse($this->db->selectAll('unknown_table')); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @covers \DBAL\Database::fetchColumn |
157
|
|
|
* @covers \DBAL\Database::buildSelectQuery |
158
|
|
|
* @covers \DBAL\Database::executeQuery |
159
|
|
|
* @covers \DBAL\Database::where |
160
|
|
|
* @covers \DBAL\Database::bindValues |
161
|
|
|
* @covers \DBAL\Modifiers\SafeString::makeSafe |
162
|
|
|
*/ |
163
|
|
|
public function testFetchColumn(){ |
164
|
|
|
$this->assertEquals('Inigo Montoya', $this->db->fetchColumn($this->test_table, array('id' => 2), '*', 1)); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @covers \DBAL\Database::fetchColumn |
169
|
|
|
* @covers \DBAL\Database::buildSelectQuery |
170
|
|
|
* @covers \DBAL\Database::executeQuery |
171
|
|
|
* @covers \DBAL\Database::where |
172
|
|
|
* @covers \DBAL\Database::bindValues |
173
|
|
|
* @covers \DBAL\Modifiers\SafeString::makeSafe |
174
|
|
|
*/ |
175
|
|
|
public function testFetchColumnFailure(){ |
176
|
|
|
$this->assertFalse($this->db->fetchColumn($this->test_table, array('id' => 2), '*', 6)); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @covers \DBAL\Database::insert |
182
|
|
|
* @covers \DBAL\Database::fields |
183
|
|
|
* @covers \DBAL\Database::numRows |
184
|
|
|
* @covers \DBAL\Database::executeQuery |
185
|
|
|
* @covers \DBAL\Database::bindValues |
186
|
|
|
*/ |
187
|
|
|
public function testInsert(){ |
188
|
|
|
$this->assertTrue($this->db->insert($this->test_table, array('name' => 'Third User', 'text_field' => 'Helloooooo', 'number_field' => rand(1, 1000)))); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @covers \DBAL\Database::insert |
193
|
|
|
* @covers \DBAL\Database::fields |
194
|
|
|
* @covers \DBAL\Database::numRows |
195
|
|
|
* @covers \DBAL\Database::executeQuery |
196
|
|
|
* @covers \DBAL\Database::bindValues |
197
|
|
|
* @covers \DBAL\Database::error |
198
|
|
|
*/ |
199
|
|
|
public function testInsertFailure(){ |
200
|
|
|
$this->assertFalse($this->db->insert($this->test_table, array('id' => 3, 'name' => 'Third User', 'text_field' => NULL, 'number_field' => rand(1, 1000)))); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @covers \DBAL\Database::update |
205
|
|
|
* @covers \DBAL\Database::fields |
206
|
|
|
* @covers \DBAL\Database::numRows |
207
|
|
|
* @covers \DBAL\Database::executeQuery |
208
|
|
|
* @covers \DBAL\Database::where |
209
|
|
|
* @covers \DBAL\Database::bindValues |
210
|
|
|
*/ |
211
|
|
|
public function testUpdate(){ |
212
|
|
|
$this->assertTrue($this->db->update($this->test_table, array('text_field' => 'Altered text', 'number_field' => rand(1, 1000)), array('id' => 1))); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @covers \DBAL\Database::update |
217
|
|
|
* @covers \DBAL\Database::fields |
218
|
|
|
* @covers \DBAL\Database::numRows |
219
|
|
|
* @covers \DBAL\Database::executeQuery |
220
|
|
|
* @covers \DBAL\Database::where |
221
|
|
|
* @covers \DBAL\Database::bindValues |
222
|
|
|
* @covers \DBAL\Database::error |
223
|
|
|
*/ |
224
|
|
|
public function testUpdateFailure(){ |
225
|
|
|
$this->assertFalse($this->db->update($this->test_table, array('number_field' => 256), array('id' => 1))); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @covers \DBAL\Database::delete |
230
|
|
|
* @covers \DBAL\Database::numRows |
231
|
|
|
* @covers \DBAL\Database::formatValues |
232
|
|
|
* @covers \DBAL\Database::executeQuery |
233
|
|
|
* @covers \DBAL\Database::where |
234
|
|
|
* @covers \DBAL\Database::bindValues |
235
|
|
|
* @covers \DBAL\Modifiers\Operators::getOperatorFormat |
236
|
|
|
* @covers \DBAL\Modifiers\Operators::isOperatorValid |
237
|
|
|
* @covers \DBAL\Modifiers\Operators::isOperatorPrepared |
238
|
|
|
* @covers \DBAL\Modifiers\SafeString::makeSafe |
239
|
|
|
*/ |
240
|
|
|
public function testDelete(){ |
241
|
|
|
$this->assertTrue($this->db->delete($this->test_table, array('id' => array('>=', 2)))); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @covers \DBAL\Database::delete |
246
|
|
|
* @covers \DBAL\Database::numRows |
247
|
|
|
* @covers \DBAL\Database::executeQuery |
248
|
|
|
* @covers \DBAL\Database::bindValues |
249
|
|
|
*/ |
250
|
|
|
public function testDeleteFailure(){ |
251
|
|
|
$this->assertFalse($this->db->delete($this->test_table, array('id' => 3))); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @covers \DBAL\Database::count |
256
|
|
|
* @covers \DBAL\Database::executeQuery |
257
|
|
|
*/ |
258
|
|
|
public function testCount(){ |
259
|
|
|
$this->assertEquals(2, $this->db->count($this->test_table)); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @covers \DBAL\Database::lastInsertID |
264
|
|
|
*/ |
265
|
|
|
public function testLastInsertID(){ |
266
|
|
|
$this->testInsert(); |
267
|
|
|
$this->assertEquals(3, $this->db->lastInsertID()); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @covers \DBAL\Database::serverVersion |
272
|
|
|
*/ |
273
|
|
|
public function testServerVersion(){ |
274
|
|
|
$this->assertGreaterThan(5, $this->db->serverVersion()); |
275
|
|
|
$this->assertContains('.', $this->db->serverVersion()); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @covers \DBAL\Database::setCaching |
280
|
|
|
*/ |
281
|
|
|
public function testSetCaching(){ |
282
|
|
|
if (extension_loaded('memcached')) { |
283
|
|
|
$caching = new MemcachedCache(); |
284
|
|
|
$caching->connect('localhost', '11211'); |
285
|
|
|
if(is_object($caching)){ |
286
|
|
|
$this->db->setCaching($caching); |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
$this->assertObjectHasAttribute('sql', $this->db->setCaching('not_a_instance_od_cache_but_should_still_return')); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* @covers \DBAL\Database::truncate |
294
|
|
|
* @covers \DBAL\Database::error |
295
|
|
|
* @covers \DBAL\Database::executeQuery |
296
|
|
|
* @covers \DBAL\Database::bindValues |
297
|
|
|
* @covers \DBAL\Database::numRows |
298
|
|
|
*/ |
299
|
|
|
public function testTruncate(){ |
300
|
|
|
$this->db->truncate($this->test_table); |
301
|
|
|
$this->assertEquals(0, $this->db->count($this->test_table)); |
302
|
|
|
$this->assertFalse($this->db->truncate('any_table')); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @covers \DBAL\Database::setLogLocation |
307
|
|
|
*/ |
308
|
|
|
public function testChangeLogLocation(){ |
309
|
|
|
$this->assertObjectHasAttribute('sql', $this->db->setLogLocation('test/logs/')); |
310
|
|
|
$this->assertObjectHasAttribute('sql', $this->db->setLogLocation(1)); |
311
|
|
|
$this->assertObjectHasAttribute('sql', $this->db->setLogLocation(false)); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
protected function connectToLiveDB(){ |
315
|
|
|
$this->db = new Database($GLOBALS['HOSTNAME'], $GLOBALS['USERNAME'], $GLOBALS['PASSWORD'], $GLOBALS['DATABASE'], '127.0.0.1', false, true, $GLOBALS['DRIVER']); |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
|
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: