|
1
|
|
|
<?php |
|
2
|
|
|
namespace DBAL\Tests; |
|
3
|
|
|
|
|
4
|
|
|
use PHPUnit\Framework\TestCase; |
|
5
|
|
|
use DBAL\Database; |
|
6
|
|
|
|
|
7
|
|
|
class DatabaseTest extends TestCase{ |
|
8
|
|
|
public static $db; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @covers \DBAL\Database::__construct |
|
12
|
|
|
* @covers \DBAL\Database::connectToServer |
|
13
|
|
|
* @covers \DBAL\Database::isConnected |
|
14
|
|
|
*/ |
|
15
|
|
|
public function setUp(){ |
|
16
|
|
|
$this->connectToLiveDB(); |
|
17
|
|
|
if(!self::$db->isConnected()){ |
|
18
|
|
|
$this->markTestSkipped( |
|
19
|
|
|
'No local database connection is available' |
|
20
|
|
|
); |
|
21
|
|
|
} |
|
22
|
|
|
else{ |
|
23
|
|
|
self::$db->query('DROP TABLE IF EXISTS `test_table`; |
|
24
|
|
|
CREATE TABLE `test_table` ( |
|
25
|
|
|
`id` int(11) NOT NULL AUTO_INCREMENT, |
|
26
|
|
|
`name` varchar(255) NOT NULL, |
|
27
|
|
|
`text_field` text NOT NULL, |
|
28
|
|
|
`number_field` int(11) NOT NULL, |
|
29
|
|
|
PRIMARY KEY (`id`) |
|
30
|
|
|
); |
|
31
|
|
|
|
|
32
|
|
|
INSERT INTO `test_table` (`id`, `name`, `text_field`, `number_field`) VALUES |
|
33
|
|
|
(1, "My Name", "Hello World", 256), |
|
34
|
|
|
(2, "Inigo Montoya", "You killed my father, prepare to die", 320);'); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @covers \DBAL\Database::__destruct |
|
40
|
|
|
* @covers \DBAL\Database::closeDatabase |
|
41
|
|
|
*/ |
|
42
|
|
|
public static function tearDownAfterClass(){ |
|
43
|
|
|
self::$db = null; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @covers \DBAL\Database::connectToServer |
|
48
|
|
|
* @covers \DBAL\Database::isConnected |
|
49
|
|
|
*/ |
|
50
|
|
|
public function testConnect(){ |
|
51
|
|
|
$this->assertTrue(self::$db->isConnected()); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @covers \DBAL\Database::__construct |
|
56
|
|
|
* @covers \DBAL\Database::connectToServer |
|
57
|
|
|
* @covers \DBAL\Database::isConnected |
|
58
|
|
|
* @covers \DBAL\Database::error |
|
59
|
|
|
*/ |
|
60
|
|
|
public function testConnectFailure(){ |
|
61
|
|
|
$db = new Database('localhost', 'wrong_username', 'incorrect_password', 'non_existent_db'); |
|
62
|
|
|
$this->assertFalse($db->isConnected()); |
|
63
|
|
|
$this->connectToLiveDB(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @covers \DBAL\Database::query |
|
68
|
|
|
*/ |
|
69
|
|
|
public function testQuery(){ |
|
70
|
|
|
// Insert a couple of test vales |
|
71
|
|
|
self::$db->insert('test_table', array('name' => 'My Name', 'text_field' => 'Hello World', 'number_field' => rand(1, 1000))); |
|
72
|
|
|
self::$db->insert('test_table', array('name' => 'Inigo Montoya', 'text_field' => 'You killed my father, prepare to die', 'number_field' => rand(1, 1000))); |
|
73
|
|
|
$query = self::$db->query("SELECT * FROM `test_table` WHERE `id` = ?", array(1)); |
|
74
|
|
|
$this->assertArrayHasKey(0, $query); |
|
75
|
|
|
$this->assertCount(1, $query); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @covers \DBAL\Database::select |
|
80
|
|
|
* @covers \DBAL\Database::selectAll |
|
81
|
|
|
* @covers \DBAL\Database::buildSelectQuery |
|
82
|
|
|
* @covers \DBAL\Database::orderBy |
|
83
|
|
|
* @covers \DBAL\Database::formatValues |
|
84
|
|
|
* @covers \DBAL\Database::executeQuery |
|
85
|
|
|
*/ |
|
86
|
|
|
public function testSelect(){ |
|
87
|
|
|
$simpleSelect = self::$db->select('test_table', array('id' => array('>', 1)), '*', array('id' => 'ASC')); |
|
88
|
|
|
$this->assertArrayHasKey('name', $simpleSelect); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @covers \DBAL\Database::selectAll |
|
93
|
|
|
* @covers \DBAL\Database::buildSelectQuery |
|
94
|
|
|
* @covers \DBAL\Database::numRows |
|
95
|
|
|
* @covers \DBAL\Database::rowCount |
|
96
|
|
|
* @covers \DBAL\Database::limit |
|
97
|
|
|
* @covers \DBAL\Database::executeQuery |
|
98
|
|
|
*/ |
|
99
|
|
|
public function testSelectAll(){ |
|
100
|
|
|
$selectAll = self::$db->selectAll('test_table'); |
|
101
|
|
|
$this->assertGreaterThan(1, self::$db->numRows()); |
|
102
|
|
|
$this->assertArrayHasKey('id', $selectAll[0]); |
|
103
|
|
|
self::$db->selectAll('test_table', array(), '*', array(), 1); |
|
104
|
|
|
$this->assertEquals(1, self::$db->rowCount()); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @covers \DBAL\Database::selectAll |
|
109
|
|
|
* @covers \DBAL\Database::buildSelectQuery |
|
110
|
|
|
* @covers \DBAL\Database::executeQuery |
|
111
|
|
|
*/ |
|
112
|
|
|
public function testSelectFailure(){ |
|
113
|
|
|
$this->assertFalse(self::$db->selectAll('test_table', array('id' => 100))); |
|
114
|
|
|
$this->assertFalse(self::$db->selectAll('unknown_table')); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @covers \DBAL\Database::fetchColumn |
|
119
|
|
|
* @covers \DBAL\Database::buildSelectQuery |
|
120
|
|
|
* @covers \DBAL\Database::executeQuery |
|
121
|
|
|
*/ |
|
122
|
|
|
public function testFetchColumn(){ |
|
123
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @covers \DBAL\Database::fetchColumn |
|
128
|
|
|
* @covers \DBAL\Database::buildSelectQuery |
|
129
|
|
|
* @covers \DBAL\Database::executeQuery |
|
130
|
|
|
*/ |
|
131
|
|
|
public function testFetchColumnFailure(){ |
|
132
|
|
|
|
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @covers \DBAL\Database::insert |
|
138
|
|
|
* @covers \DBAL\Database::numRows |
|
139
|
|
|
* @covers \DBAL\Database::executeQuery |
|
140
|
|
|
*/ |
|
141
|
|
|
public function testInsert(){ |
|
142
|
|
|
$this->assertTrue(self::$db->insert('test_table', array('name' => 'Third User', 'text_field' => 'Helloooooo', 'number_field' => rand(1, 1000)))); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @covers \DBAL\Database::insert |
|
147
|
|
|
* @covers \DBAL\Database::numRows |
|
148
|
|
|
* @covers \DBAL\Database::executeQuery |
|
149
|
|
|
*/ |
|
150
|
|
|
public function testInsertFailure(){ |
|
151
|
|
|
$this->assertFalse(self::$db->insert('test_table', array('id' => 3, 'name' => 'Third User', 'text_field' => NULL, 'number_field' => rand(1, 1000)))); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @covers \DBAL\Database::update |
|
156
|
|
|
* @covers \DBAL\Database::numRows |
|
157
|
|
|
* @covers \DBAL\Database::executeQuery |
|
158
|
|
|
*/ |
|
159
|
|
|
public function testUpdate(){ |
|
160
|
|
|
$this->assertTrue(self::$db->update('test_table', array('text_field' => 'Altered text', 'number_field' => rand(1, 1000)), array('id' => 1))); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @covers \DBAL\Database::update |
|
165
|
|
|
* @covers \DBAL\Database::numRows |
|
166
|
|
|
* @covers \DBAL\Database::executeQuery |
|
167
|
|
|
*/ |
|
168
|
|
|
public function testUpdateFailure(){ |
|
169
|
|
|
$this->assertFalse(self::$db->update('test_table', array('number_field' => 256), array('id' => 1))); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @covers \DBAL\Database::delete |
|
174
|
|
|
* @covers \DBAL\Database::numRows |
|
175
|
|
|
* @covers \DBAL\Database::formatValues |
|
176
|
|
|
* @covers \DBAL\Database::executeQuery |
|
177
|
|
|
*/ |
|
178
|
|
|
public function testDelete(){ |
|
179
|
|
|
$this->assertTrue(self::$db->delete('test_table', array('id' => array('>=', 2)))); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @covers \DBAL\Database::delete |
|
184
|
|
|
* @covers \DBAL\Database::numRows |
|
185
|
|
|
* @covers \DBAL\Database::executeQuery |
|
186
|
|
|
*/ |
|
187
|
|
|
public function testDeleteFailure(){ |
|
188
|
|
|
$this->assertFalse(self::$db->delete('test_table', array('id' => 3))); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @covers \DBAL\Database::count |
|
193
|
|
|
* @covers \DBAL\Database::executeQuery |
|
194
|
|
|
*/ |
|
195
|
|
|
public function testCount(){ |
|
196
|
|
|
$this->assertEquals(2, self::$db->count('test_table')); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
public function testFulltextIndex(){ |
|
200
|
|
|
$this->markTestIncomplete( |
|
201
|
|
|
'This test has not been implemented yet.' |
|
202
|
|
|
); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* @covers \DBAL\Database::lastInsertID |
|
207
|
|
|
*/ |
|
208
|
|
|
public function testLastInsertID(){ |
|
209
|
|
|
$this->testInsert(); |
|
210
|
|
|
$this->assertEquals(3, self::$db->lastInsertID()); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @covers \DBAL\Database::setCaching |
|
215
|
|
|
*/ |
|
216
|
|
|
public function testSetCaching(){ |
|
217
|
|
|
$this->markTestIncomplete( |
|
218
|
|
|
'This test has not been implemented yet.' |
|
219
|
|
|
); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
protected function connectToLiveDB(){ |
|
223
|
|
|
self::$db = new Database($GLOBALS['HOSTNAME'], $GLOBALS['USERNAME'], $GLOBALS['PASSWORD'], $GLOBALS['DATABASE'], false, false, true, $GLOBALS['DRIVER']); |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|