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