|
1
|
|
|
<?php |
|
2
|
|
|
use Ubiquity\db\Database; |
|
3
|
|
|
use Ubiquity\cache\database\TableCache; |
|
4
|
|
|
use Ubiquity\exceptions\CacheException; |
|
5
|
|
|
|
|
6
|
|
|
require_once 'Ubiquity/db/Database.php'; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Database test case. |
|
10
|
|
|
* |
|
11
|
|
|
* @covers \Ubiquity\db\Database |
|
12
|
|
|
* |
|
13
|
|
|
*/ |
|
14
|
|
|
class DatabaseTest extends \Codeception\Test\Unit { |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* |
|
18
|
|
|
* @var Database |
|
19
|
|
|
*/ |
|
20
|
|
|
private $database; |
|
21
|
|
|
private $db_server; |
|
22
|
|
|
const DB_NAME = 'messagerie'; |
|
23
|
|
|
const DB_TYPE = 'mysql'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Prepares the environment before running a test. |
|
27
|
|
|
*/ |
|
28
|
|
|
protected function _before() { |
|
29
|
|
|
if (! defined ( 'ROOT' )) { |
|
30
|
|
|
define ( 'ROOT', __DIR__ ); |
|
31
|
|
|
define ( 'DS', DIRECTORY_SEPARATOR ); |
|
32
|
|
|
} |
|
33
|
|
|
$ip = getenv ( 'SERVICE_MYSQL_IP' ); |
|
34
|
|
|
if ($ip === false) { |
|
35
|
|
|
$ip = '127.0.0.1'; |
|
36
|
|
|
} |
|
37
|
|
|
$this->db_server = $ip; |
|
38
|
|
|
$this->database = new Database ( self::DB_TYPE, self::DB_NAME, $this->db_server ); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Cleans up the environment after running a test. |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function _after() { |
|
45
|
|
|
$this->database = null; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
protected function beforeQuery() { |
|
49
|
|
|
if (! $this->database->isConnected ()) |
|
50
|
|
|
$this->database->connect (); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Tests Database->__construct() |
|
55
|
|
|
* |
|
56
|
|
|
* @covers \Ubiquity\db\Database::<private> |
|
57
|
|
|
*/ |
|
58
|
|
|
public function test__construct() { |
|
59
|
|
|
$this->assertEquals ( self::DB_NAME, $this->database->getDbName () ); |
|
60
|
|
|
$this->assertEquals ( '3306', $this->database->getPort () ); |
|
61
|
|
|
$db = new Database ( self::DB_TYPE, self::DB_NAME, $this->db_server, 3306, 'root', '', [ "quote" => "`" ], TableCache::class ); |
|
62
|
|
|
$this->assertTrue ( $db->connect () ); |
|
63
|
|
|
$db = new Database ( self::DB_TYPE, self::DB_NAME, $this->db_server, 3306, 'root', '', [ "quote" => "`" ], function () { |
|
|
|
|
|
|
64
|
|
|
return new TableCache (); |
|
65
|
|
|
} ); |
|
66
|
|
|
$this->assertTrue ( $db->connect () ); |
|
67
|
|
|
$this->expectException ( CacheException::class ); |
|
68
|
|
|
$db = new Database ( self::DB_TYPE, self::DB_NAME, $this->db_server, 3306, 'root', '', [ "quote" => "`" ], "notExistingClass" ); |
|
69
|
|
|
$this->assertTrue ( $db->connect () ); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Tests Database->connect() |
|
74
|
|
|
*/ |
|
75
|
|
|
public function testConnect() { |
|
76
|
|
|
$this->assertFalse ( $this->database->isConnected () ); |
|
77
|
|
|
$this->assertFalse ( $this->database->ping () ); |
|
78
|
|
|
$this->assertTrue ( $this->database->connect () ); |
|
79
|
|
|
$this->assertTrue ( $this->database->isConnected () ); |
|
80
|
|
|
$this->assertTrue ( $this->database->ping () ); |
|
81
|
|
|
$this->database->setUser ( 'nobody' ); |
|
82
|
|
|
$this->assertFalse ( $this->database->connect () ); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Tests Database->setters |
|
87
|
|
|
*/ |
|
88
|
|
|
public function testSetters() { |
|
89
|
|
|
$this->database->setDbName ( 'test' ); |
|
90
|
|
|
$this->assertEquals ( 'test', $this->database->getDbName () ); |
|
91
|
|
|
$this->database->setDbType ( 'mongo' ); |
|
92
|
|
|
$this->assertEquals ( 'mongo', $this->database->getDbType () ); |
|
93
|
|
|
$options = [ "a" => true,"b" => "test" ]; |
|
94
|
|
|
$this->database->setOptions ( $options ); |
|
95
|
|
|
$this->assertEquals ( $options, $this->database->getOptions () ); |
|
96
|
|
|
$this->database->setPassword ( 'password' ); |
|
97
|
|
|
$this->assertEquals ( 'password', $this->database->getPassword () ); |
|
98
|
|
|
$this->database->setPort ( 3307 ); |
|
99
|
|
|
$this->assertEquals ( 3307, $this->database->getPort () ); |
|
100
|
|
|
$this->database->setServerName ( 'local' ); |
|
101
|
|
|
$this->assertEquals ( 'local', $this->database->getServerName () ); |
|
102
|
|
|
$this->database->setUser ( 'user' ); |
|
103
|
|
|
$this->assertEquals ( 'user', $this->database->getUser () ); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Tests Database->getDSN() |
|
108
|
|
|
*/ |
|
109
|
|
|
public function testGetDSN() { |
|
110
|
|
|
$db = new Database ( self::DB_TYPE, "dbname" ); |
|
111
|
|
|
$dsn = $db->getDSN (); |
|
112
|
|
|
$this->assertEquals ( self::DB_TYPE . ':dbname=dbname;host=127.0.0.1;charset=UTF8;port=3306', $dsn ); |
|
113
|
|
|
$db->setDbType ( "mongo" ); |
|
114
|
|
|
$this->assertEquals ( 'mongo:dbname=dbname;host=127.0.0.1;charset=UTF8;port=3306', $db->getDSN () ); |
|
115
|
|
|
$db->setServerName ( "localhost" ); |
|
116
|
|
|
$this->assertEquals ( 'mongo:dbname=dbname;host=localhost;charset=UTF8;port=3306', $db->getDSN () ); |
|
117
|
|
|
$db->setPort ( 23 ); |
|
118
|
|
|
$this->assertEquals ( 'mongo:dbname=dbname;host=localhost;charset=UTF8;port=23', $db->getDSN () ); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Tests Database->query() |
|
123
|
|
|
*/ |
|
124
|
|
|
public function testQuery() { |
|
125
|
|
|
$this->beforeQuery (); |
|
126
|
|
|
$this->assertNotFalse ( $this->database->query ( "SELECT 1" ) ); |
|
127
|
|
|
$st = $this->database->query ( "SELECT * from `user` limit 0,1" ); |
|
128
|
|
|
$this->assertInstanceOf ( PDOStatement::class, $st ); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Tests Database->prepareAndExecute() |
|
133
|
|
|
*/ |
|
134
|
|
|
public function testPrepareAndExecute() { |
|
135
|
|
|
$this->beforeQuery (); |
|
136
|
|
|
$response = $this->database->prepareAndExecute ( "user", "WHERE `email`='[email protected]'", [ "email","firstname" ] ); |
|
137
|
|
|
$this->assertEquals ( sizeof ( $response ), 1 ); |
|
138
|
|
|
$row = current ( $response ); |
|
139
|
|
|
$this->assertEquals ( "[email protected]", $row ['email'] ); |
|
140
|
|
|
$this->assertEquals ( "Benjamin", $row ['firstname'] ); |
|
141
|
|
|
$this->assertArrayNotHasKey ( 'lastname', $row ); |
|
142
|
|
|
$this->expectException ( Error::class ); |
|
143
|
|
|
$this->database->prepareAndExecute ( "users", "WHERE `email`='[email protected]'", [ "email","firstname" ], null, true ); |
|
144
|
|
|
$db = new Database ( self::DB_TYPE, self::DB_NAME, $this->db_server, 3306, 'root', '', [ "quote" => "`" ], TableCache::class ); |
|
145
|
|
|
$db->connect (); |
|
146
|
|
|
$response = $db->prepareAndExecute ( "user", "WHERE `email`='[email protected]'", [ "email","firstname" ], null, true ); |
|
147
|
|
|
$this->assertEquals ( sizeof ( $response ), 1 ); |
|
148
|
|
|
$row = current ( $response ); |
|
149
|
|
|
$this->assertEquals ( "[email protected]", $row ['email'] ); |
|
150
|
|
|
$response = $db->prepareAndExecute ( "user", "WHERE `email`= ?", [ "email","firstname" ], [ '[email protected]' ], true ); |
|
151
|
|
|
$this->assertEquals ( sizeof ( $response ), 1 ); |
|
152
|
|
|
$row = current ( $response ); |
|
153
|
|
|
$this->assertEquals ( "[email protected]", $row ['email'] ); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Tests Database->prepareAndFetchAll() |
|
158
|
|
|
*/ |
|
159
|
|
|
public function testPrepareAndFetchAll() { |
|
160
|
|
|
$this->beforeQuery (); |
|
161
|
|
|
$this->assertNotFalse ( $this->database->prepareAndFetchAll ( "SELECT 1" ) ); |
|
162
|
|
|
$resp = $this->database->prepareAndFetchAll ( "select * from `user`" ); |
|
163
|
|
|
$this->assertEquals ( 101, sizeof ( $resp ) ); |
|
164
|
|
|
$row = current ( $resp ); |
|
165
|
|
|
$this->assertEquals ( 7, sizeof ( $row ) ); |
|
166
|
|
|
$resp = $this->database->prepareAndFetchAll ( "select * from `user` where email= ?", [ "[email protected]" ] ); |
|
167
|
|
|
$row = current ( $resp ); |
|
168
|
|
|
$this->assertEquals ( "[email protected]", $row ['email'] ); |
|
169
|
|
|
$this->assertEquals ( "Benjamin", $row ['firstname'] ); |
|
170
|
|
|
$resp = $this->database->prepareAndFetchAll ( "select * from `user` where email= ? and firstname= ?", [ "[email protected]","Benjamin" ] ); |
|
171
|
|
|
$row = current ( $resp ); |
|
172
|
|
|
$this->assertEquals ( "[email protected]", $row ['email'] ); |
|
173
|
|
|
$this->assertEquals ( "Benjamin", $row ['firstname'] ); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Tests Database->prepareAndFetchAllColumn() |
|
178
|
|
|
*/ |
|
179
|
|
|
public function testPrepareAndFetchAllColumn() { |
|
180
|
|
|
$this->beforeQuery (); |
|
181
|
|
|
$this->assertNotFalse ( $this->database->prepareAndFetchAllColumn ( "SELECT 1" ) ); |
|
|
|
|
|
|
182
|
|
|
$resp = $this->database->prepareAndFetchAllColumn ( "select * from `user`" ); |
|
183
|
|
|
$this->assertEquals ( 101, sizeof ( $resp ) ); |
|
184
|
|
|
$resp = $this->database->prepareAndFetchAllColumn ( "select email from `user` where email= ?", [ "[email protected]" ] ); |
|
185
|
|
|
$row = current ( $resp ); |
|
|
|
|
|
|
186
|
|
|
$this->assertEquals ( "[email protected]", $row ); |
|
187
|
|
|
$resp = $this->database->prepareAndFetchAllColumn ( "select * from `user` where email= ? and firstname= ?", [ "[email protected]","Benjamin" ], 3 ); |
|
188
|
|
|
$row = current ( $resp ); |
|
189
|
|
|
$this->assertEquals ( "[email protected]", $row ); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Tests Database->prepareAndFetchColumn() |
|
194
|
|
|
*/ |
|
195
|
|
|
public function testPrepareAndFetchColumn() { |
|
196
|
|
|
$this->beforeQuery (); |
|
197
|
|
|
$result = $this->database->prepareAndFetchColumn ( "select email from `user` where email='[email protected]'" ); |
|
198
|
|
|
$this->assertEquals ( '[email protected]', $result ); |
|
199
|
|
|
$result = $this->database->prepareAndFetchColumn ( "select email from `user` limit 0,1" ); |
|
200
|
|
|
$this->assertEquals ( '[email protected]', $result ); |
|
201
|
|
|
$result = $this->database->prepareAndFetchColumn ( "select * from `user` limit 0,1", null, 3 ); |
|
202
|
|
|
$this->assertEquals ( '[email protected]', $result ); |
|
203
|
|
|
$result = $this->database->prepareAndFetchColumn ( "select * from `user` where `email`= ?", [ '[email protected]' ], 3 ); |
|
204
|
|
|
$this->assertEquals ( '[email protected]', $result ); |
|
205
|
|
|
$result = $this->database->prepareAndFetchColumn ( "select email from `user` where `email`= ?", [ '[email protected]' ] ); |
|
206
|
|
|
$this->assertEquals ( '[email protected]', $result ); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Tests Database->execute() |
|
211
|
|
|
*/ |
|
212
|
|
|
public function testExecute() { |
|
213
|
|
|
$this->beforeQuery (); |
|
214
|
|
|
$this->assertEquals ( 0, $this->database->execute ( "DELETE FROM organization where 1=2" ) ); |
|
215
|
|
|
$this->assertEquals ( 1, $this->database->execute ( "INSERT INTO organization(`name`,`domain`,`aliases`) VALUES('name','domain','aliases')" ) ); |
|
216
|
|
|
$this->assertEquals ( 1, $this->database->execute ( "DELETE FROM organization where `name`='name'" ) ); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Tests Database->setServerName() |
|
221
|
|
|
*/ |
|
222
|
|
|
public function testSetServerName() { |
|
223
|
|
|
$this->assertEquals ( $this->db_server, $this->database->getServerName () ); |
|
224
|
|
|
$this->database->setServerName ( 'localhost' ); |
|
225
|
|
|
$this->assertEquals ( 'localhost', $this->database->getServerName () ); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Tests Database->prepareStatement() |
|
230
|
|
|
*/ |
|
231
|
|
|
public function testPrepareStatement() { |
|
232
|
|
|
$this->beforeQuery (); |
|
233
|
|
|
$st = $this->database->prepareStatement ( "SELECT 1" ); |
|
234
|
|
|
$this->assertNotNull ( $st ); |
|
235
|
|
|
$st = $this->database->prepareStatement ( "SELECT * from `user` limit 0,1" ); |
|
236
|
|
|
$this->assertInstanceOf ( PDOStatement::class, $st ); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Tests Database->bindValueFromStatement() |
|
241
|
|
|
*/ |
|
242
|
|
|
public function testBindValueFromStatement() { |
|
243
|
|
|
$this->beforeQuery (); |
|
244
|
|
|
$st = $this->database->prepareStatement ( "SELECT * from `user` where email= :email limit 0,1" ); |
|
245
|
|
|
$this->assertTrue ( $this->database->bindValueFromStatement ( $st, ':email', '[email protected]' ) ); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* Tests Database->lastInserId() |
|
250
|
|
|
*/ |
|
251
|
|
|
public function testLastInserId() { |
|
252
|
|
|
$this->beforeQuery (); |
|
253
|
|
|
$this->assertEquals ( 1, $this->database->execute ( "INSERT INTO organization(`name`,`domain`,`aliases`) VALUES('name','domain','aliases')" ) ); |
|
254
|
|
|
$id = $this->database->lastInserId (); |
|
255
|
|
|
$this->assertNotNull ( $id ); |
|
256
|
|
|
$this->assertEquals ( 1, $this->database->execute ( "DELETE FROM organization where `id`=" . $id ) ); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* Tests Database->getTablesName() |
|
261
|
|
|
*/ |
|
262
|
|
|
public function testGetTablesName() { |
|
263
|
|
|
$this->beforeQuery (); |
|
264
|
|
|
$tables = $this->database->getTablesName (); |
|
265
|
|
|
$this->assertEquals ( 7, sizeof ( $tables ) ); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* Tests Database->count() |
|
270
|
|
|
*/ |
|
271
|
|
|
public function testCount() { |
|
272
|
|
|
$this->beforeQuery (); |
|
273
|
|
|
$this->assertEquals ( 101, $this->database->count ( "user" ) ); |
|
274
|
|
|
$this->assertEquals ( 1, $this->database->count ( "user", "`email`='[email protected]'" ) ); |
|
275
|
|
|
$this->assertEquals ( 0, $this->database->count ( "user", "1=2" ) ); |
|
276
|
|
|
$this->assertEquals ( 101, $this->database->count ( "user", "1=1" ) ); |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* Tests Database->queryColumn() |
|
281
|
|
|
*/ |
|
282
|
|
|
public function testQueryColumn() { |
|
283
|
|
|
$this->beforeQuery (); |
|
284
|
|
|
$result = $this->database->queryColumn ( "select email from `user` where email='[email protected]'" ); |
|
285
|
|
|
$this->assertEquals ( '[email protected]', $result ); |
|
286
|
|
|
$result = $this->database->queryColumn ( "select email from `user` limit 0,1" ); |
|
287
|
|
|
$this->assertEquals ( '[email protected]', $result ); |
|
288
|
|
|
$result = $this->database->queryColumn ( "select * from `user` limit 0,1", 3 ); |
|
289
|
|
|
$this->assertEquals ( '[email protected]', $result ); |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* Tests Database->fetchAll() |
|
294
|
|
|
*/ |
|
295
|
|
|
public function testFetchAll() { |
|
296
|
|
|
$this->beforeQuery (); |
|
297
|
|
|
$result = $this->database->fetchAll ( "select * from `user`" ); |
|
298
|
|
|
$this->assertEquals ( 101, sizeof ( $result ) ); |
|
299
|
|
|
$row = current ( $result ); |
|
300
|
|
|
$this->assertEquals ( $row ['email'], '[email protected]' ); |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* Tests Database->isConnected() |
|
305
|
|
|
*/ |
|
306
|
|
|
public function testIsConnected() { |
|
307
|
|
|
$this->assertFalse ( $this->database->isConnected () ); |
|
308
|
|
|
$this->assertFalse ( $this->database->ping () ); |
|
309
|
|
|
$this->beforeQuery (); |
|
310
|
|
|
$this->assertTrue ( $this->database->isConnected () ); |
|
311
|
|
|
$this->assertTrue ( $this->database->ping () ); |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
/** |
|
315
|
|
|
* Tests Database->setDbType() |
|
316
|
|
|
*/ |
|
317
|
|
|
public function testSetDbType() { |
|
318
|
|
|
$this->assertEquals ( self::DB_TYPE, $this->database->getDbType () ); |
|
319
|
|
|
$this->database->setDbType ( 'mongo' ); |
|
320
|
|
|
$this->assertEquals ( 'mongo', $this->database->getDbType () ); |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
/** |
|
324
|
|
|
* Tests Database->ping() |
|
325
|
|
|
*/ |
|
326
|
|
|
public function testPing() { |
|
327
|
|
|
$this->assertFalse ( $this->database->ping () ); |
|
328
|
|
|
$this->beforeQuery (); |
|
329
|
|
|
$this->assertTrue ( $this->database->ping () ); |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
/** |
|
333
|
|
|
* Tests Database->getPdoObject() |
|
334
|
|
|
*/ |
|
335
|
|
|
public function testGetPdoObject() { |
|
336
|
|
|
$this->assertNull ( $this->database->getPdoObject () ); |
|
337
|
|
|
$this->beforeQuery (); |
|
338
|
|
|
$pdoo = $this->database->getPdoObject (); |
|
339
|
|
|
$this->assertNotNull ( $pdoo ); |
|
340
|
|
|
$this->assertInstanceOf ( PDO::class, $pdoo ); |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
/** |
|
344
|
|
|
* Tests Database::getAvailableDrivers() |
|
345
|
|
|
*/ |
|
346
|
|
|
public function testGetAvailableDrivers() { |
|
347
|
|
|
$drivers = Database::getAvailableDrivers (); |
|
348
|
|
|
$this->assertTrue ( sizeof ( $drivers ) > 0 ); |
|
349
|
|
|
} |
|
350
|
|
|
} |
|
351
|
|
|
|
|
352
|
|
|
|