1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* DronePHP (http://www.dronephp.com) |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/Pleets/DronePHP |
6
|
|
|
* @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org) |
7
|
|
|
* @license http://www.dronephp.com/license |
8
|
|
|
* @author Darío Rivera <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace DroneTest\Util; |
12
|
|
|
|
13
|
|
|
use Drone\Db\Driver\MySQL; |
14
|
|
|
use Drone\Db\Driver\Exception\ConnectionException; |
15
|
|
|
use PHPUnit\Framework\TestCase; |
16
|
|
|
|
17
|
|
|
class MySQLTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Database parameters |
21
|
|
|
*/ |
22
|
|
|
private $options = [ |
23
|
|
|
"dbhost" => "localhost", |
24
|
|
|
"dbuser" => "root", |
25
|
|
|
"dbpass" => "", |
26
|
|
|
"dbname" => "test", |
27
|
|
|
"dbchar" => "utf8", |
28
|
|
|
"dbport" => "3306", |
29
|
|
|
"auto_connect" => false |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
/* |
33
|
|
|
|-------------------------------------------------------------------------- |
34
|
|
|
| Stablishing connections |
35
|
|
|
|-------------------------------------------------------------------------- |
36
|
|
|
| |
37
|
|
|
| The following tests are related to the connection methods and its |
38
|
|
|
| exceptions and returned values. |
39
|
|
|
| |
40
|
|
|
*/ |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Tests if we can connect to the database server |
44
|
|
|
* |
45
|
|
|
* @return null |
46
|
|
|
*/ |
47
|
|
|
public function testCanStablishConnection() |
48
|
|
|
{ |
49
|
|
|
$conn = new MySQL($this->options); |
50
|
|
|
|
51
|
|
|
$mysqliObject = $conn->connect(); |
52
|
|
|
|
53
|
|
|
$this->assertInstanceOf('\mysqli', $mysqliObject); |
54
|
|
|
$this->assertTrue($conn->isConnected()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Tests if we can disconnect from the database server |
59
|
|
|
* |
60
|
|
|
* @return null |
61
|
|
|
*/ |
62
|
|
|
public function testCanDownConnection() |
63
|
|
|
{ |
64
|
|
|
$conn = new MySQL($this->options); |
65
|
|
|
|
66
|
|
|
$conn->connect(); |
67
|
|
|
$result = $conn->disconnect(); |
68
|
|
|
|
69
|
|
|
$this->assertNotTrue($conn->isConnected()); |
70
|
|
|
$this->assertTrue($result); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Tests if we can disconnect from server when there is not a connection stablished |
75
|
|
|
* |
76
|
|
|
* @return null |
77
|
|
|
*/ |
78
|
|
|
public function testCannotDisconectWhenNotConnected() |
79
|
|
|
{ |
80
|
|
|
$conn = new MySQL($this->options); |
81
|
|
|
|
82
|
|
|
$errorObject = null; |
83
|
|
|
|
84
|
|
|
try { |
85
|
|
|
$conn->disconnect(); |
86
|
|
|
} |
87
|
|
|
catch (\Exception $e) |
88
|
|
|
{ |
89
|
|
|
$errorObject = ($e instanceof \LogicException); |
90
|
|
|
} |
91
|
|
|
finally |
92
|
|
|
{ |
93
|
|
|
$this->assertNotTrue($conn->isConnected()); |
94
|
|
|
$this->assertTrue($errorObject, $e->getMessage()); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Tests if we can reconnect to the database server |
100
|
|
|
* |
101
|
|
|
* @return null |
102
|
|
|
*/ |
103
|
|
|
public function testCanStablishConnectionAgain() |
104
|
|
|
{ |
105
|
|
|
$conn = new MySQL($this->options); |
106
|
|
|
|
107
|
|
|
$conn->connect(); |
108
|
|
|
$mysqliObject = $conn->reconnect(); |
109
|
|
|
|
110
|
|
|
$this->assertInstanceOf('\mysqli', $mysqliObject); |
111
|
|
|
$this->assertTrue($conn->isConnected()); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Tests if we can reconnect to the database server when there is not a connection stablished |
116
|
|
|
* |
117
|
|
|
* @return null |
118
|
|
|
*/ |
119
|
|
|
public function testCannotStablishReconnection() |
120
|
|
|
{ |
121
|
|
|
$conn = new MySQL($this->options); |
122
|
|
|
|
123
|
|
|
$errorObject = null; |
124
|
|
|
|
125
|
|
|
try { |
126
|
|
|
$conn->reconnect(); |
127
|
|
|
} |
128
|
|
|
catch (\Exception $e) |
129
|
|
|
{ |
130
|
|
|
$errorObject = ($e instanceof \LogicException); |
131
|
|
|
} |
132
|
|
|
finally |
133
|
|
|
{ |
134
|
|
|
$this->assertTrue($errorObject, $e->getMessage()); |
|
|
|
|
135
|
|
|
$this->assertNotTrue($conn->isConnected()); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Tests if a failed connection throws a ConnectionException |
141
|
|
|
* |
142
|
|
|
* @return null |
143
|
|
|
*/ |
144
|
|
|
public function testCannotStablishConnection() |
145
|
|
|
{ |
146
|
|
|
$options = $this->options; |
147
|
|
|
$options["dbhost"] = 'myserver'; // this server does not exists |
148
|
|
|
|
149
|
|
|
$conn = new MySQL($options); |
150
|
|
|
|
151
|
|
|
$mysqliObject = $errorObject = null; |
|
|
|
|
152
|
|
|
|
153
|
|
|
$message = "No exception"; |
154
|
|
|
|
155
|
|
|
try { |
156
|
|
|
$mysqliObject = $conn->connect(); |
157
|
|
|
} |
158
|
|
|
catch (\Exception $e) |
159
|
|
|
{ |
160
|
|
|
$errorObject = ($e instanceof ConnectionException); |
161
|
|
|
$message = $e->getMessage(); |
162
|
|
|
} |
163
|
|
|
finally |
164
|
|
|
{ |
165
|
|
|
$this->assertTrue($errorObject, $message); |
166
|
|
|
$this->assertNotTrue($conn->isConnected()); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/* |
171
|
|
|
|-------------------------------------------------------------------------- |
172
|
|
|
| Quering and Transactions |
173
|
|
|
|-------------------------------------------------------------------------- |
174
|
|
|
| |
175
|
|
|
| The following tests are related to query and transaction operations and its |
176
|
|
|
| exceptions and returned values. |
177
|
|
|
| |
178
|
|
|
*/ |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Tests if we can execute DDL statements |
182
|
|
|
* |
183
|
|
|
* @return null |
184
|
|
|
*/ |
185
|
|
|
public function testCanExecuteDLLStatement() |
186
|
|
|
{ |
187
|
|
|
$options = $this->options; |
188
|
|
|
$options["auto_connect"] = true; |
189
|
|
|
|
190
|
|
|
$conn = new MySQL($options); |
191
|
|
|
$sql = "CREATE TABLE MYTABLE (ID INTEGER(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, DESCRIPTION VARCHAR(100))"; |
192
|
|
|
$result = $conn->execute($sql); |
193
|
|
|
|
194
|
|
|
$this->assertTrue(is_object($result)); |
195
|
|
|
|
196
|
|
|
# properties modified by execute() method |
197
|
|
|
$this->assertEquals(0, $conn->getNumRows()); |
198
|
|
|
$this->assertEquals(0, $conn->getNumFields()); |
199
|
|
|
$this->assertEquals(0, $conn->getRowsAffected()); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Tests if we can execute DML statements |
204
|
|
|
* |
205
|
|
|
* @return null |
206
|
|
|
*/ |
207
|
|
|
public function testCanExecuteDMLStatement() |
208
|
|
|
{ |
209
|
|
|
$options = $this->options; |
210
|
|
|
$options["auto_connect"] = true; |
211
|
|
|
|
212
|
|
|
$conn = new MySQL($options); |
213
|
|
|
$sql = "INSERT INTO MYTABLE (DESCRIPTION) VALUES ('Hello world!')"; |
214
|
|
|
$result = $conn->execute($sql); |
215
|
|
|
|
216
|
|
|
$this->assertTrue(is_object($result)); |
217
|
|
|
|
218
|
|
|
# properties modified by execute() method |
219
|
|
|
$this->assertEquals(0, $conn->getNumRows()); |
220
|
|
|
$this->assertEquals(0, $conn->getNumFields()); |
221
|
|
|
$this->assertEquals(1, $conn->getRowsAffected()); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Tests getting results |
226
|
|
|
* |
227
|
|
|
* @return null |
228
|
|
|
*/ |
229
|
|
|
public function testGettingResults() |
230
|
|
|
{ |
231
|
|
|
$options = $this->options; |
232
|
|
|
$options["auto_connect"] = true; |
233
|
|
|
|
234
|
|
|
$conn = new MySQL($options); |
235
|
|
|
$sql = "SELECT * FROM MYTABLE LIMIT 2"; |
236
|
|
|
$result = $conn->execute($sql); |
|
|
|
|
237
|
|
|
|
238
|
|
|
# properties modified by execute() method |
239
|
|
|
$this->assertEquals(1, $conn->getNumRows()); |
240
|
|
|
$this->assertEquals(2, $conn->getNumFields()); |
241
|
|
|
$this->assertEquals(0, $conn->getRowsAffected()); |
242
|
|
|
|
243
|
|
|
$rowset = $conn->getArrayResult(); # array with results |
244
|
|
|
$row = array_shift($rowset); |
245
|
|
|
|
246
|
|
|
$this->assertArrayHasKey("ID", $row); |
247
|
|
|
$this->assertArrayHasKey("DESCRIPTION", $row); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Tests if we can commit transactions |
252
|
|
|
* |
253
|
|
|
* @return null |
254
|
|
|
*/ |
255
|
|
|
public function testCommitBehavior() |
256
|
|
|
{ |
257
|
|
|
$options = $this->options; |
258
|
|
|
$options["auto_connect"] = true; |
259
|
|
|
|
260
|
|
|
$conn = new MySQL($options); |
261
|
|
|
$conn->autocommit(false); |
262
|
|
|
|
263
|
|
|
$sql = "INSERT INTO MYTABLE (DESCRIPTION) VALUES ('COMMIT_ROW_1')"; |
264
|
|
|
$result = $conn->execute($sql); |
|
|
|
|
265
|
|
|
|
266
|
|
|
$sql = "SELECT * FROM MYTABLE WHERE DESCRIPTION = 'COMMIT_ROW_1'"; |
267
|
|
|
$result = $conn->execute($sql); |
268
|
|
|
$rowcount = count($conn->getArrayResult()); |
269
|
|
|
|
270
|
|
|
$this->assertTrue(($rowcount === 1)); # the row is available for now |
271
|
|
|
|
272
|
|
|
# properties modified by execute() method |
273
|
|
|
$this->assertEquals(1, $conn->getNumRows()); |
274
|
|
|
$this->assertEquals(2, $conn->getNumFields()); |
275
|
|
|
$this->assertEquals(0, $conn->getRowsAffected()); # nothing affected (autocommit = false) |
276
|
|
|
|
277
|
|
|
$this->assertTrue($conn->commit()); |
278
|
|
|
|
279
|
|
|
# now let's to verify if the record exists after commit |
280
|
|
|
$sql = "SELECT * FROM MYTABLE WHERE DESCRIPTION = 'COMMIT_ROW_1'"; |
281
|
|
|
$result = $conn->execute($sql); |
282
|
|
|
$rowcount = count($conn->getArrayResult()); |
283
|
|
|
|
284
|
|
|
$this->assertTrue(($rowcount === 1)); # the row is available |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Tests if we can rollback transactions |
289
|
|
|
* |
290
|
|
|
* @return null |
291
|
|
|
*/ |
292
|
|
|
public function testRollbackBehavior() |
293
|
|
|
{ |
294
|
|
|
$options = $this->options; |
295
|
|
|
$options["auto_connect"] = true; |
296
|
|
|
|
297
|
|
|
$conn = new MySQL($options); |
298
|
|
|
$conn->autocommit(false); |
299
|
|
|
|
300
|
|
|
$sql = "INSERT INTO MYTABLE (DESCRIPTION) VALUES ('ROLLBACK_ROW_1')"; |
301
|
|
|
$result = $conn->execute($sql); |
|
|
|
|
302
|
|
|
|
303
|
|
|
$sql = "SELECT * FROM MYTABLE WHERE DESCRIPTION = 'ROLLBACK_ROW_1'"; |
304
|
|
|
$result = $conn->execute($sql); |
305
|
|
|
$rowcount = count($conn->getArrayResult()); |
306
|
|
|
|
307
|
|
|
$this->assertTrue(($rowcount === 1)); # the row is available for now |
308
|
|
|
|
309
|
|
|
# properties modified by execute() method |
310
|
|
|
$this->assertEquals(1, $conn->getNumRows()); |
311
|
|
|
$this->assertEquals(2, $conn->getNumFields()); |
312
|
|
|
$this->assertEquals(0, $conn->getRowsAffected()); # nothing affected (autocommit = false) |
313
|
|
|
|
314
|
|
|
$this->assertTrue($conn->rollback()); |
315
|
|
|
|
316
|
|
|
# now let's to verify if the record exists after commit |
317
|
|
|
$sql = "SELECT * FROM MYTABLE WHERE DESCRIPTION = 'ROLLBACK_ROW_1'"; |
318
|
|
|
$result = $conn->execute($sql); |
319
|
|
|
$rowcount = count($conn->getArrayResult()); |
320
|
|
|
|
321
|
|
|
$this->assertNotTrue(($rowcount === 1)); # the row is not available |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Tests if we can do a transaction with commiting changes |
326
|
|
|
* |
327
|
|
|
* @return null |
328
|
|
|
*/ |
329
|
|
|
public function testTransactionConfirmation() |
330
|
|
|
{ |
331
|
|
|
$options = $this->options; |
332
|
|
|
$options["auto_connect"] = true; |
333
|
|
|
|
334
|
|
|
$conn = new MySQL($options); |
335
|
|
|
$conn->autocommit(false); |
336
|
|
|
|
337
|
|
|
$sql = "INSERT INTO MYTABLE (DESCRIPTION) VALUES ('COMMIT_ROW_TRANSACTION_1')"; |
338
|
|
|
$result = $conn->execute($sql); |
|
|
|
|
339
|
|
|
|
340
|
|
|
$sql = "INSERT INTO MYTABLE (DESCRIPTION) VALUES ('COMMIT_ROW_TRANSACTION_2')"; |
341
|
|
|
$result = $conn->execute($sql); |
342
|
|
|
|
343
|
|
|
$sql = "INSERT INTO MYTABLE (DESCRIPTION) VALUES ('COMMIT_ROW_TRANSACTION_3')"; |
344
|
|
|
$result = $conn->execute($sql); |
345
|
|
|
|
346
|
|
|
$sql = "INSERT INTO MYTABLE (DESCRIPTION) VALUES ('COMMIT_ROW_TRANSACTION_4')"; |
347
|
|
|
$result = $conn->execute($sql); |
348
|
|
|
|
349
|
|
|
$sql = "SELECT * FROM MYTABLE WHERE DESCRIPTION LIKE 'COMMIT_ROW_TRANSACTION_%'"; |
350
|
|
|
$result = $conn->execute($sql); |
351
|
|
|
$rowcount = count($conn->getArrayResult()); |
352
|
|
|
|
353
|
|
|
$this->assertTrue(($rowcount === 4)); # the rows are available for now |
354
|
|
|
|
355
|
|
|
# properties modified by execute() method |
356
|
|
|
$this->assertEquals(4, $conn->getNumRows()); |
357
|
|
|
$this->assertEquals(2, $conn->getNumFields()); |
358
|
|
|
$this->assertEquals(0, $conn->getRowsAffected()); # nothing affected (autocommit = false) |
359
|
|
|
|
360
|
|
|
$this->assertTrue($conn->commit()); |
361
|
|
|
|
362
|
|
|
# now let's to verify if the record exists after commit |
363
|
|
|
$sql = "SELECT * FROM MYTABLE WHERE DESCRIPTION LIKE 'COMMIT_ROW_TRANSACTION_%'"; |
364
|
|
|
$result = $conn->execute($sql); |
365
|
|
|
$rowcount = count($conn->getArrayResult()); |
366
|
|
|
|
367
|
|
|
$this->assertTrue(($rowcount === 4)); # the row is available |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* Tests if we can do a transaction with reverting changes |
372
|
|
|
* |
373
|
|
|
* @return null |
374
|
|
|
*/ |
375
|
|
|
public function testTransactionReversion() |
376
|
|
|
{ |
377
|
|
|
$options = $this->options; |
378
|
|
|
$options["auto_connect"] = true; |
379
|
|
|
|
380
|
|
|
$conn = new MySQL($options); |
381
|
|
|
$conn->autocommit(false); |
382
|
|
|
|
383
|
|
|
$sql = "INSERT INTO MYTABLE (DESCRIPTION) VALUES ('ROLLBACK_ROW_TRANSACTION_1')"; |
384
|
|
|
$result = $conn->execute($sql); |
|
|
|
|
385
|
|
|
|
386
|
|
|
$sql = "INSERT INTO MYTABLE (DESCRIPTION) VALUES ('ROLLBACK_ROW_TRANSACTION_2')"; |
387
|
|
|
$result = $conn->execute($sql); |
388
|
|
|
|
389
|
|
|
$sql = "INSERT INTO MYTABLE (DESCRIPTION) VALUES ('ROLLBACK_ROW_TRANSACTION_3')"; |
390
|
|
|
$result = $conn->execute($sql); |
391
|
|
|
|
392
|
|
|
$sql = "INSERT INTO MYTABLE (DESCRIPTION) VALUES ('ROLLBACK_ROW_TRANSACTION_4')"; |
393
|
|
|
$result = $conn->execute($sql); |
394
|
|
|
|
395
|
|
|
$sql = "SELECT * FROM MYTABLE WHERE DESCRIPTION LIKE 'ROLLBACK_ROW_TRANSACTION_%'"; |
396
|
|
|
$result = $conn->execute($sql); |
397
|
|
|
$rowcount = count($conn->getArrayResult()); |
398
|
|
|
|
399
|
|
|
$this->assertTrue(($rowcount === 4)); # the rows are available for now |
400
|
|
|
|
401
|
|
|
# properties modified by execute() method |
402
|
|
|
$this->assertEquals(4, $conn->getNumRows()); |
403
|
|
|
$this->assertEquals(2, $conn->getNumFields()); |
404
|
|
|
$this->assertEquals(0, $conn->getRowsAffected()); # nothing affected (autocommit = false) |
405
|
|
|
|
406
|
|
|
$this->assertTrue($conn->rollback()); |
407
|
|
|
|
408
|
|
|
# now let's to verify if the record exists after commit |
409
|
|
|
$sql = "SELECT * FROM MYTABLE WHERE DESCRIPTION LIKE 'ROLLBACK_ROW_TRANSACTION_%'"; |
410
|
|
|
$result = $conn->execute($sql); |
411
|
|
|
$rowcount = count($conn->getArrayResult()); |
412
|
|
|
|
413
|
|
|
$this->assertNotTrue(($rowcount === 4)); # the row is available |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* Tests if we can do a transaction with the shortcut method |
418
|
|
|
* |
419
|
|
|
* @return null |
420
|
|
|
*/ |
421
|
|
|
public function testTransactionShortcut() |
422
|
|
|
{ |
423
|
|
|
$options = $this->options; |
424
|
|
|
$options["auto_connect"] = true; |
425
|
|
|
|
426
|
|
|
$conn = new MySQL($options); |
427
|
|
|
|
428
|
|
|
# not necessary |
429
|
|
|
# $conn->autocommit(false); |
430
|
|
|
|
431
|
|
|
# start the transaction |
432
|
|
|
$conn->beginTransaction(); |
433
|
|
|
|
434
|
|
|
$sql = "INSERT INTO MYTABLE (DESCRIPTION) VALUES ('TRANSACTION_SHORTCUT_1')"; |
435
|
|
|
$result = $conn->execute($sql); |
|
|
|
|
436
|
|
|
|
437
|
|
|
$sql = "INSERT INTO MYTABLE (DESCRIPTION) VALUES ('TRANSACTION_SHORTCUT_1')"; |
438
|
|
|
$result = $conn->execute($sql); |
439
|
|
|
|
440
|
|
|
$sql = "SELECT * FROM MYTABLE WHERE DESCRIPTION LIKE 'TRANSACTION_SHORTCUT_%'"; |
441
|
|
|
$result = $conn->execute($sql); |
442
|
|
|
$rowcount = count($conn->getArrayResult()); |
443
|
|
|
|
444
|
|
|
$this->assertTrue(($rowcount === 2)); # the rows are available for now |
445
|
|
|
|
446
|
|
|
# properties modified by execute() method |
447
|
|
|
$this->assertEquals(2, $conn->getNumRows()); |
448
|
|
|
$this->assertEquals(2, $conn->getNumFields()); |
449
|
|
|
$this->assertEquals(0, $conn->getRowsAffected()); # nothing affected (autocommit = false) |
450
|
|
|
|
451
|
|
|
# ends the transaction |
452
|
|
|
$conn->endTransaction(); |
453
|
|
|
|
454
|
|
|
# now let's to verify if the record exists after commit |
455
|
|
|
$sql = "SELECT * FROM MYTABLE WHERE DESCRIPTION LIKE 'TRANSACTION_SHORTCUT_%'"; |
456
|
|
|
$result = $conn->execute($sql); |
457
|
|
|
$rowcount = count($conn->getArrayResult()); |
458
|
|
|
|
459
|
|
|
$this->assertTrue(($rowcount === 2)); # the row is available |
460
|
|
|
} |
461
|
|
|
} |