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::orderBy |
96
|
|
|
* @covers \DBAL\Database::formatValues |
97
|
|
|
* @covers \DBAL\Database::executeQuery |
98
|
|
|
* @covers \DBAL\Database::bindValues |
99
|
|
|
* @covers \DBAL\Modifiers\Operators::getOperatorFormat |
100
|
|
|
* @covers \DBAL\Modifiers\Operators::isOperatorValid |
101
|
|
|
* @covers \DBAL\Modifiers\Operators::isOperatorPrepared |
102
|
|
|
* @covers \DBAL\Modifiers\SafeString::makeSafe |
103
|
|
|
*/ |
104
|
|
|
public function testSelect(){ |
105
|
|
|
$simpleSelect = $this->db->select($this->test_table, array('id' => array('>', 1)), '*', array('id' => 'ASC')); |
106
|
|
|
$this->assertArrayHasKey('name', $simpleSelect); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @covers \DBAL\Database::selectAll |
111
|
|
|
* @covers \DBAL\Database::buildSelectQuery |
112
|
|
|
* @covers \DBAL\Database::numRows |
113
|
|
|
* @covers \DBAL\Database::rowCount |
114
|
|
|
* @covers \DBAL\Database::limit |
115
|
|
|
* @covers \DBAL\Database::executeQuery |
116
|
|
|
* @covers \DBAL\Database::bindValues |
117
|
|
|
* @covers \DBAL\Modifiers\SafeString::makeSafe |
118
|
|
|
*/ |
119
|
|
|
public function testSelectAll(){ |
120
|
|
|
$selectAll = $this->db->selectAll($this->test_table); |
121
|
|
|
$this->assertGreaterThan(1, $this->db->numRows()); |
122
|
|
|
$this->assertArrayHasKey('id', $selectAll[0]); |
123
|
|
|
$this->db->selectAll($this->test_table, array(), '*', array(), 1); |
124
|
|
|
$this->assertEquals(1, $this->db->rowCount()); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @covers \DBAL\Database::selectAll |
129
|
|
|
* @covers \DBAL\Database::buildSelectQuery |
130
|
|
|
* @covers \DBAL\Database::executeQuery |
131
|
|
|
* @covers \DBAL\Database::bindValues |
132
|
|
|
* @covers \DBAL\Modifiers\SafeString::makeSafe |
133
|
|
|
*/ |
134
|
|
|
public function testSelectFailure(){ |
135
|
|
|
$this->assertFalse($this->db->selectAll($this->test_table, array('id' => 100))); |
136
|
|
|
$this->assertFalse($this->db->selectAll('unknown_table')); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @covers \DBAL\Database::fetchColumn |
141
|
|
|
* @covers \DBAL\Database::buildSelectQuery |
142
|
|
|
* @covers \DBAL\Database::executeQuery |
143
|
|
|
* @covers \DBAL\Database::bindValues |
144
|
|
|
* @covers \DBAL\Modifiers\SafeString::makeSafe |
145
|
|
|
*/ |
146
|
|
|
public function testFetchColumn(){ |
147
|
|
|
$this->assertEquals('Inigo Montoya', $this->db->fetchColumn($this->test_table, array('id' => 2), '*', 1)); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @covers \DBAL\Database::fetchColumn |
152
|
|
|
* @covers \DBAL\Database::buildSelectQuery |
153
|
|
|
* @covers \DBAL\Database::executeQuery |
154
|
|
|
* @covers \DBAL\Database::bindValues |
155
|
|
|
* @covers \DBAL\Modifiers\SafeString::makeSafe |
156
|
|
|
*/ |
157
|
|
|
public function testFetchColumnFailure(){ |
158
|
|
|
$this->assertFalse($this->db->fetchColumn($this->test_table, array('id' => 2), '*', 6)); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @covers \DBAL\Database::insert |
164
|
|
|
* @covers \DBAL\Database::numRows |
165
|
|
|
* @covers \DBAL\Database::executeQuery |
166
|
|
|
* @covers \DBAL\Database::bindValues |
167
|
|
|
*/ |
168
|
|
|
public function testInsert(){ |
169
|
|
|
$this->assertTrue($this->db->insert($this->test_table, array('name' => 'Third User', 'text_field' => 'Helloooooo', 'number_field' => rand(1, 1000)))); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @covers \DBAL\Database::insert |
174
|
|
|
* @covers \DBAL\Database::numRows |
175
|
|
|
* @covers \DBAL\Database::executeQuery |
176
|
|
|
* @covers \DBAL\Database::bindValues |
177
|
|
|
* @covers \DBAL\Database::writeQueryToLog |
178
|
|
|
*/ |
179
|
|
|
public function testInsertFailure(){ |
180
|
|
|
$this->assertFalse($this->db->insert($this->test_table, array('id' => 3, 'name' => 'Third User', 'text_field' => NULL, 'number_field' => rand(1, 1000)))); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @covers \DBAL\Database::update |
185
|
|
|
* @covers \DBAL\Database::numRows |
186
|
|
|
* @covers \DBAL\Database::executeQuery |
187
|
|
|
* @covers \DBAL\Database::bindValues |
188
|
|
|
*/ |
189
|
|
|
public function testUpdate(){ |
190
|
|
|
$this->assertTrue($this->db->update($this->test_table, array('text_field' => 'Altered text', 'number_field' => rand(1, 1000)), array('id' => 1))); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @covers \DBAL\Database::update |
195
|
|
|
* @covers \DBAL\Database::numRows |
196
|
|
|
* @covers \DBAL\Database::executeQuery |
197
|
|
|
* @covers \DBAL\Database::bindValues |
198
|
|
|
*/ |
199
|
|
|
public function testUpdateFailure(){ |
200
|
|
|
$this->assertFalse($this->db->update($this->test_table, array('number_field' => 256), array('id' => 1))); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @covers \DBAL\Database::delete |
205
|
|
|
* @covers \DBAL\Database::numRows |
206
|
|
|
* @covers \DBAL\Database::formatValues |
207
|
|
|
* @covers \DBAL\Database::executeQuery |
208
|
|
|
* @covers \DBAL\Database::bindValues |
209
|
|
|
* @covers \DBAL\Modifiers\Operators::getOperatorFormat |
210
|
|
|
* @covers \DBAL\Modifiers\Operators::isOperatorValid |
211
|
|
|
* @covers \DBAL\Modifiers\Operators::isOperatorPrepared |
212
|
|
|
* @covers \DBAL\Modifiers\SafeString::makeSafe |
213
|
|
|
*/ |
214
|
|
|
public function testDelete(){ |
215
|
|
|
$this->assertTrue($this->db->delete($this->test_table, array('id' => array('>=', 2)))); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @covers \DBAL\Database::delete |
220
|
|
|
* @covers \DBAL\Database::numRows |
221
|
|
|
* @covers \DBAL\Database::executeQuery |
222
|
|
|
* @covers \DBAL\Database::bindValues |
223
|
|
|
*/ |
224
|
|
|
public function testDeleteFailure(){ |
225
|
|
|
$this->assertFalse($this->db->delete($this->test_table, array('id' => 3))); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @covers \DBAL\Database::count |
230
|
|
|
* @covers \DBAL\Database::executeQuery |
231
|
|
|
*/ |
232
|
|
|
public function testCount(){ |
233
|
|
|
$this->assertEquals(2, $this->db->count($this->test_table)); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @covers \DBAL\Database::lastInsertID |
238
|
|
|
*/ |
239
|
|
|
public function testLastInsertID(){ |
240
|
|
|
$this->testInsert(); |
241
|
|
|
$this->assertEquals(3, $this->db->lastInsertID()); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @covers \DBAL\Database::serverVersion |
246
|
|
|
*/ |
247
|
|
|
public function testServerVersion(){ |
248
|
|
|
$this->assertGreaterThan(5, $this->db->serverVersion()); |
249
|
|
|
$this->assertContains('.', $this->db->serverVersion()); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @covers \DBAL\Database::setCaching |
254
|
|
|
*/ |
255
|
|
|
public function testSetCaching(){ |
256
|
|
|
if (extension_loaded('memcached')) { |
257
|
|
|
$caching = new MemcachedCache(); |
258
|
|
|
$caching->connect('localhost', '11211'); |
259
|
|
|
if(is_object($caching)){ |
260
|
|
|
$this->db->setCaching($caching); |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
else{ |
264
|
|
|
$this->markTestSkipped('Memcached is not avaiable'); |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
protected function connectToLiveDB(){ |
269
|
|
|
$this->db = new Database($GLOBALS['HOSTNAME'], $GLOBALS['USERNAME'], $GLOBALS['PASSWORD'], $GLOBALS['DATABASE'], false, false, true, $GLOBALS['DRIVER']); |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.