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\Exception\InvalidQueryException; |
14
|
|
|
use Drone\Db\Driver\MySQL; |
15
|
|
|
use PHPUnit\Framework\TestCase; |
16
|
|
|
use Symfony\Component\Dotenv\Dotenv; |
17
|
|
|
|
18
|
|
|
class MySQLTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Database parameters |
22
|
|
|
*/ |
23
|
|
|
private $options = [ |
24
|
|
|
"dbhost" => "localhost", |
25
|
|
|
"dbuser" => "root", |
26
|
|
|
"dbpass" => "", |
27
|
|
|
"dbname" => "test", |
28
|
|
|
"dbchar" => "utf8", |
29
|
|
|
"dbport" => "3306", |
30
|
|
|
"auto_connect" => false, |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
public function setUp() |
34
|
|
|
{ |
35
|
|
|
parent::setUp(); |
36
|
|
|
|
37
|
|
|
$dotenv = new Dotenv(); |
38
|
|
|
$dotenv->load(__DIR__.'/../../../.env.testing'); |
39
|
|
|
|
40
|
|
|
$this->options['dbhost'] = $_ENV['DB_HOST']; |
41
|
|
|
$this->options['dbuser'] = $_ENV['DB_USER']; |
42
|
|
|
$this->options['dbpass'] = $_ENV['DB_PASS']; |
43
|
|
|
$this->options['dbname'] = $_ENV['DB_NAME']; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/* |
47
|
|
|
|-------------------------------------------------------------------------- |
48
|
|
|
| Establishing connections |
49
|
|
|
|-------------------------------------------------------------------------- |
50
|
|
|
| |
51
|
|
|
| The following tests are related to the connection methods and its |
52
|
|
|
| exceptions and returned values. |
53
|
|
|
| |
54
|
|
|
*/ |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Tests if we can connect to the database server |
58
|
|
|
* |
59
|
|
|
* @return null |
60
|
|
|
*/ |
61
|
|
|
public function testCanEstablishConnection() |
62
|
|
|
{ |
63
|
|
|
$conn = new MySQL($this->options); |
64
|
|
|
|
65
|
|
|
$mysqliObject = $conn->connect(); |
66
|
|
|
|
67
|
|
|
$this->assertInstanceOf('\mysqli', $mysqliObject); |
68
|
|
|
$this->assertTrue($conn->isConnected()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Tests if we can disconnect from the database server |
73
|
|
|
* |
74
|
|
|
* @return null |
75
|
|
|
*/ |
76
|
|
|
public function testCanDownConnection() |
77
|
|
|
{ |
78
|
|
|
$conn = new MySQL($this->options); |
79
|
|
|
|
80
|
|
|
$conn->connect(); |
81
|
|
|
$result = $conn->disconnect(); |
82
|
|
|
|
83
|
|
|
$this->assertNotTrue($conn->isConnected()); |
84
|
|
|
$this->assertTrue($result); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Tests if we can disconnect from server when there is not a connection established |
89
|
|
|
* |
90
|
|
|
* @expectedException LogicException |
91
|
|
|
*/ |
92
|
|
|
public function testCannotDisconnectWhenNotConnected() |
93
|
|
|
{ |
94
|
|
|
$conn = new MySQL($this->options); |
95
|
|
|
|
96
|
|
|
try { |
97
|
|
|
$conn->disconnect(); |
98
|
|
|
var_dump($conn); |
|
|
|
|
99
|
|
|
} catch (\Exception $e) { |
100
|
|
|
$this->assertNotTrue($conn->isConnected()); |
101
|
|
|
throw $e; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Tests if we can reconnect to the database server |
107
|
|
|
* |
108
|
|
|
* @return null |
109
|
|
|
*/ |
110
|
|
|
public function testCanEstablishConnectionAgain() |
111
|
|
|
{ |
112
|
|
|
$conn = new MySQL($this->options); |
113
|
|
|
|
114
|
|
|
$conn->connect(); |
115
|
|
|
$mysqliObject = $conn->reconnect(); |
116
|
|
|
|
117
|
|
|
$this->assertInstanceOf('\mysqli', $mysqliObject); |
118
|
|
|
$this->assertTrue($conn->isConnected()); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Tests if we can reconnect to the database server when there is not a connection established |
123
|
|
|
* |
124
|
|
|
* @expectedException LogicException |
125
|
|
|
*/ |
126
|
|
|
public function testCannotEstablishReconnection() |
127
|
|
|
{ |
128
|
|
|
$conn = new MySQL($this->options); |
129
|
|
|
|
130
|
|
|
try { |
131
|
|
|
$conn->reconnect(); |
132
|
|
|
} catch (\Exception $e) { |
133
|
|
|
$this->assertNotTrue($conn->isConnected()); |
134
|
|
|
throw $e; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Tests if a failed connection throws a ConnectionException |
140
|
|
|
* |
141
|
|
|
* @expectedException Drone\Db\Driver\Exception\ConnectionException |
142
|
|
|
*/ |
143
|
|
|
public function testCannotEstablishConnection() |
144
|
|
|
{ |
145
|
|
|
$options = $this->options; |
146
|
|
|
$options["dbhost"] = 'myserver'; // this server does not exists |
147
|
|
|
|
148
|
|
|
$conn = new MySQL($options); |
149
|
|
|
|
150
|
|
|
try { |
151
|
|
|
$conn->connect(); |
152
|
|
|
} catch (\Exception $e) { |
153
|
|
|
$this->assertNotTrue($conn->isConnected()); |
154
|
|
|
throw $e; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/* |
159
|
|
|
|-------------------------------------------------------------------------- |
160
|
|
|
| Querying and Transactions |
161
|
|
|
|-------------------------------------------------------------------------- |
162
|
|
|
| |
163
|
|
|
| The following tests are related to query and transaction operations and its |
164
|
|
|
| exceptions and returned values. |
165
|
|
|
| |
166
|
|
|
*/ |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Tests if we can execute DDL statements |
170
|
|
|
* |
171
|
|
|
* @return null |
172
|
|
|
*/ |
173
|
|
|
public function testCanExecuteDLLStatement() |
174
|
|
|
{ |
175
|
|
|
$options = $this->options; |
176
|
|
|
$options["auto_connect"] = true; |
177
|
|
|
|
178
|
|
|
$conn = new MySQL($options); |
179
|
|
|
$sql = "CREATE TABLE MYTABLE (ID INTEGER(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, DESCRIPTION VARCHAR(100))"; |
180
|
|
|
$result = $conn->execute($sql); |
181
|
|
|
|
182
|
|
|
# mysqli |
183
|
|
|
$this->assertTrue(is_object($result)); |
184
|
|
|
|
185
|
|
|
# properties modified by execute() method |
186
|
|
|
$this->assertEquals(0, $conn->getNumRows()); |
187
|
|
|
$this->assertEquals(0, $conn->getNumFields()); |
188
|
|
|
$this->assertEquals(0, $conn->getRowsAffected()); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Tests if we can execute DML statements |
193
|
|
|
* |
194
|
|
|
* @return null |
195
|
|
|
*/ |
196
|
|
|
public function testCanExecuteDMLStatement() |
197
|
|
|
{ |
198
|
|
|
$options = $this->options; |
199
|
|
|
$options["auto_connect"] = true; |
200
|
|
|
|
201
|
|
|
$conn = new MySQL($options); |
202
|
|
|
$sql = "INSERT INTO MYTABLE (DESCRIPTION) VALUES ('Hello world!')"; |
203
|
|
|
$result = $conn->execute($sql); |
204
|
|
|
|
205
|
|
|
$this->assertTrue(is_object($result)); |
206
|
|
|
|
207
|
|
|
# properties modified by execute() method |
208
|
|
|
$this->assertEquals(0, $conn->getNumRows()); |
209
|
|
|
$this->assertEquals(0, $conn->getNumFields()); |
210
|
|
|
$this->assertEquals(1, $conn->getRowsAffected()); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Tests if we can execute a prepared DML statement |
215
|
|
|
* |
216
|
|
|
* @return null |
217
|
|
|
*/ |
218
|
|
|
public function testCanExecutePreparedDMLStatement() |
219
|
|
|
{ |
220
|
|
|
$options = $this->options; |
221
|
|
|
$options["auto_connect"] = true; |
222
|
|
|
|
223
|
|
|
$conn = new MySQL($options); |
224
|
|
|
$sql = "UPDATE MYTABLE SET DESCRIPTION = ? WHERE ID = ?"; |
225
|
|
|
$result = $conn->execute($sql, ["Bonjour le monde!", 1]); |
226
|
|
|
|
227
|
|
|
$this->assertTrue(is_object($result)); |
228
|
|
|
|
229
|
|
|
# properties modified by execute() method |
230
|
|
|
$this->assertEquals(0, $conn->getNumRows()); |
231
|
|
|
$this->assertEquals(0, $conn->getNumFields()); |
232
|
|
|
$this->assertEquals(1, $conn->getRowsAffected()); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Tests if a wrong query execution throws an InvalidQueryException |
237
|
|
|
* |
238
|
|
|
* @expectedException Drone\Db\Driver\Exception\InvalidQueryException |
239
|
|
|
*/ |
240
|
|
|
public function testGettingInvalidQueryException() |
241
|
|
|
{ |
242
|
|
|
$options = $this->options; |
243
|
|
|
$options["auto_connect"] = true; |
244
|
|
|
|
245
|
|
|
$conn = new MySQL($options); |
246
|
|
|
|
247
|
|
|
$sql = "INSERT INTO MYTABLE (DESCRIPTION, WRONG) VALUES ('Hello world!')"; |
248
|
|
|
$conn->execute($sql); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Tests getting results without a query execution |
253
|
|
|
* |
254
|
|
|
* @expectedException LogicException |
255
|
|
|
*/ |
256
|
|
|
public function testGettingResults() |
257
|
|
|
{ |
258
|
|
|
$options = $this->options; |
259
|
|
|
$options["auto_connect"] = true; |
260
|
|
|
|
261
|
|
|
$conn = new MySQL($options); |
262
|
|
|
$conn->getArrayResult(); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Tests getting results |
267
|
|
|
* |
268
|
|
|
* @return null |
269
|
|
|
*/ |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Tests if we can commit transactions |
273
|
|
|
* |
274
|
|
|
* @return null |
275
|
|
|
*/ |
276
|
|
|
public function testCommitBehavior() |
277
|
|
|
{ |
278
|
|
|
$options = $this->options; |
279
|
|
|
$options["auto_connect"] = true; |
280
|
|
|
|
281
|
|
|
$conn = new MySQL($options); |
282
|
|
|
$conn->autocommit(false); |
283
|
|
|
|
284
|
|
|
$sql = "INSERT INTO MYTABLE (DESCRIPTION) VALUES ('COMMIT_ROW_1')"; |
285
|
|
|
$conn->execute($sql); |
286
|
|
|
|
287
|
|
|
$sql = "SELECT * FROM MYTABLE WHERE DESCRIPTION = 'COMMIT_ROW_1'"; |
288
|
|
|
$conn->execute($sql); |
289
|
|
|
$rowcount = count($conn->getArrayResult()); |
290
|
|
|
|
291
|
|
|
$this->assertTrue(($rowcount === 1)); # the row is available for now |
292
|
|
|
|
293
|
|
|
# properties modified by execute() method |
294
|
|
|
$this->assertEquals(1, $conn->getNumRows()); |
295
|
|
|
$this->assertEquals(2, $conn->getNumFields()); |
296
|
|
|
$this->assertEquals(0, $conn->getRowsAffected()); # nothing affected (autocommit = false) |
297
|
|
|
|
298
|
|
|
$this->assertTrue($conn->commit()); |
299
|
|
|
|
300
|
|
|
# now let's to verify if the record exists after commit |
301
|
|
|
$sql = "SELECT * FROM MYTABLE WHERE DESCRIPTION = 'COMMIT_ROW_1'"; |
302
|
|
|
$conn->execute($sql); |
303
|
|
|
$rowcount = count($conn->getArrayResult()); |
304
|
|
|
|
305
|
|
|
$this->assertTrue(($rowcount === 1)); # the row is available |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Tests if we can rollback transactions |
310
|
|
|
* |
311
|
|
|
* @return null |
312
|
|
|
*/ |
313
|
|
|
public function testRollbackBehavior() |
314
|
|
|
{ |
315
|
|
|
$options = $this->options; |
316
|
|
|
$options["auto_connect"] = true; |
317
|
|
|
|
318
|
|
|
$conn = new MySQL($options); |
319
|
|
|
$conn->autocommit(false); |
320
|
|
|
|
321
|
|
|
$sql = "INSERT INTO MYTABLE (DESCRIPTION) VALUES ('ROLLBACK_ROW_1')"; |
322
|
|
|
$conn->execute($sql); |
323
|
|
|
|
324
|
|
|
$sql = "SELECT * FROM MYTABLE WHERE DESCRIPTION = 'ROLLBACK_ROW_1'"; |
325
|
|
|
$conn->execute($sql); |
326
|
|
|
$rowcount = count($conn->getArrayResult()); |
327
|
|
|
|
328
|
|
|
$this->assertTrue(($rowcount === 1)); # the row is available for now |
329
|
|
|
|
330
|
|
|
# properties modified by execute() method |
331
|
|
|
$this->assertEquals(1, $conn->getNumRows()); |
332
|
|
|
$this->assertEquals(2, $conn->getNumFields()); |
333
|
|
|
$this->assertEquals(0, $conn->getRowsAffected()); # nothing affected (autocommit = false) |
334
|
|
|
|
335
|
|
|
$this->assertTrue($conn->rollback()); |
336
|
|
|
|
337
|
|
|
# now let's to verify if the record exists after commit |
338
|
|
|
$sql = "SELECT * FROM MYTABLE WHERE DESCRIPTION = 'ROLLBACK_ROW_1'"; |
339
|
|
|
$conn->execute($sql); |
340
|
|
|
$rowcount = count($conn->getArrayResult()); |
341
|
|
|
|
342
|
|
|
$this->assertNotTrue(($rowcount === 1)); # the row is not available |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* Tests if we can do a transaction with commiting changes |
347
|
|
|
* |
348
|
|
|
* @return null |
349
|
|
|
*/ |
350
|
|
|
public function testTransactionConfirmation() |
351
|
|
|
{ |
352
|
|
|
$options = $this->options; |
353
|
|
|
$options["auto_connect"] = true; |
354
|
|
|
|
355
|
|
|
$conn = new MySQL($options); |
356
|
|
|
$conn->autocommit(false); |
357
|
|
|
|
358
|
|
|
$sql = "INSERT INTO MYTABLE (DESCRIPTION) VALUES ('COMMIT_ROW_TRANSACTION_1')"; |
359
|
|
|
$conn->execute($sql); |
360
|
|
|
|
361
|
|
|
$sql = "INSERT INTO MYTABLE (DESCRIPTION) VALUES ('COMMIT_ROW_TRANSACTION_2')"; |
362
|
|
|
$conn->execute($sql); |
363
|
|
|
|
364
|
|
|
$sql = "INSERT INTO MYTABLE (DESCRIPTION) VALUES ('COMMIT_ROW_TRANSACTION_3')"; |
365
|
|
|
$conn->execute($sql); |
366
|
|
|
|
367
|
|
|
$sql = "INSERT INTO MYTABLE (DESCRIPTION) VALUES ('COMMIT_ROW_TRANSACTION_4')"; |
368
|
|
|
$conn->execute($sql); |
369
|
|
|
|
370
|
|
|
$sql = "SELECT * FROM MYTABLE WHERE DESCRIPTION LIKE 'COMMIT_ROW_TRANSACTION_%'"; |
371
|
|
|
$conn->execute($sql); |
372
|
|
|
$rowcount = count($conn->getArrayResult()); |
373
|
|
|
|
374
|
|
|
$this->assertTrue(($rowcount === 4)); # the rows are available for now |
375
|
|
|
|
376
|
|
|
# properties modified by execute() method |
377
|
|
|
$this->assertEquals(4, $conn->getNumRows()); |
378
|
|
|
$this->assertEquals(2, $conn->getNumFields()); |
379
|
|
|
$this->assertEquals(0, $conn->getRowsAffected()); # nothing affected (autocommit = false) |
380
|
|
|
|
381
|
|
|
$this->assertTrue($conn->commit()); |
382
|
|
|
|
383
|
|
|
# now let's to verify if the record exists after commit |
384
|
|
|
$sql = "SELECT * FROM MYTABLE WHERE DESCRIPTION LIKE 'COMMIT_ROW_TRANSACTION_%'"; |
385
|
|
|
$conn->execute($sql); |
386
|
|
|
$rowcount = count($conn->getArrayResult()); |
387
|
|
|
|
388
|
|
|
$this->assertTrue(($rowcount === 4)); # the rows are available |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* Tests if we can do a transaction with reverting changes |
393
|
|
|
* |
394
|
|
|
* @return null |
395
|
|
|
*/ |
396
|
|
|
public function testTransactionReversion() |
397
|
|
|
{ |
398
|
|
|
$options = $this->options; |
399
|
|
|
$options["auto_connect"] = true; |
400
|
|
|
|
401
|
|
|
$conn = new MySQL($options); |
402
|
|
|
$conn->autocommit(false); |
403
|
|
|
|
404
|
|
|
$sql = "INSERT INTO MYTABLE (DESCRIPTION) VALUES ('ROLLBACK_ROW_TRANSACTION_1')"; |
405
|
|
|
$conn->execute($sql); |
406
|
|
|
|
407
|
|
|
$sql = "INSERT INTO MYTABLE (DESCRIPTION) VALUES ('ROLLBACK_ROW_TRANSACTION_2')"; |
408
|
|
|
$conn->execute($sql); |
409
|
|
|
|
410
|
|
|
$sql = "INSERT INTO MYTABLE (DESCRIPTION) VALUES ('ROLLBACK_ROW_TRANSACTION_3')"; |
411
|
|
|
$conn->execute($sql); |
412
|
|
|
|
413
|
|
|
$sql = "INSERT INTO MYTABLE (DESCRIPTION) VALUES ('ROLLBACK_ROW_TRANSACTION_4')"; |
414
|
|
|
$conn->execute($sql); |
415
|
|
|
|
416
|
|
|
$sql = "SELECT * FROM MYTABLE WHERE DESCRIPTION LIKE 'ROLLBACK_ROW_TRANSACTION_%'"; |
417
|
|
|
$conn->execute($sql); |
418
|
|
|
$rowcount = count($conn->getArrayResult()); |
419
|
|
|
|
420
|
|
|
$this->assertTrue(($rowcount === 4)); # the rows are available for now |
421
|
|
|
|
422
|
|
|
# properties modified by execute() method |
423
|
|
|
$this->assertEquals(4, $conn->getNumRows()); |
424
|
|
|
$this->assertEquals(2, $conn->getNumFields()); |
425
|
|
|
$this->assertEquals(0, $conn->getRowsAffected()); # nothing affected (autocommit = false) |
426
|
|
|
|
427
|
|
|
$this->assertTrue($conn->rollback()); |
428
|
|
|
|
429
|
|
|
# now let's to verify if the record exists after commit |
430
|
|
|
$sql = "SELECT * FROM MYTABLE WHERE DESCRIPTION LIKE 'ROLLBACK_ROW_TRANSACTION_%'"; |
431
|
|
|
$conn->execute($sql); |
432
|
|
|
$rowcount = count($conn->getArrayResult()); |
433
|
|
|
|
434
|
|
|
$this->assertNotTrue(($rowcount === 4)); # the rows are not available |
435
|
|
|
$this->assertTrue(($rowcount === 0)); |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* Tests if we can do a transaction with the shortcut method |
440
|
|
|
* |
441
|
|
|
* @return null |
442
|
|
|
*/ |
443
|
|
|
public function testTransactionConfirmationShortcut() |
444
|
|
|
{ |
445
|
|
|
$options = $this->options; |
446
|
|
|
$options["auto_connect"] = true; |
447
|
|
|
|
448
|
|
|
$conn = new MySQL($options); |
449
|
|
|
|
450
|
|
|
# not necessary! |
451
|
|
|
# $conn->autocommit(false); |
452
|
|
|
|
453
|
|
|
# starts the transaction |
454
|
|
|
$conn->beginTransaction(); |
455
|
|
|
|
456
|
|
|
$sql = "INSERT INTO MYTABLE (DESCRIPTION) VALUES ('TRANSACTION_SHORTCUT_1')"; |
457
|
|
|
$conn->execute($sql); |
458
|
|
|
|
459
|
|
|
$sql = "INSERT INTO MYTABLE (DESCRIPTION) VALUES ('TRANSACTION_SHORTCUT_1')"; |
460
|
|
|
$conn->execute($sql); |
461
|
|
|
|
462
|
|
|
$sql = "SELECT * FROM MYTABLE WHERE DESCRIPTION LIKE 'TRANSACTION_SHORTCUT_%'"; |
463
|
|
|
$conn->execute($sql); |
464
|
|
|
$rowcount = count($conn->getArrayResult()); |
465
|
|
|
|
466
|
|
|
$this->assertTrue(($rowcount === 2)); # the rows are available for now |
467
|
|
|
|
468
|
|
|
# properties modified by execute() method |
469
|
|
|
$this->assertEquals(2, $conn->getNumRows()); |
470
|
|
|
$this->assertEquals(2, $conn->getNumFields()); |
471
|
|
|
$this->assertEquals(0, $conn->getRowsAffected()); # nothing affected (autocommit = false) |
472
|
|
|
|
473
|
|
|
# ends the transaction |
474
|
|
|
$conn->endTransaction(); |
475
|
|
|
|
476
|
|
|
# now let's to verify if the record exists after endTransaction() |
477
|
|
|
$sql = "SELECT * FROM MYTABLE WHERE DESCRIPTION LIKE 'TRANSACTION_SHORTCUT_%'"; |
478
|
|
|
$conn->execute($sql); |
479
|
|
|
$rowcount = count($conn->getArrayResult()); |
480
|
|
|
|
481
|
|
|
$this->assertTrue(($rowcount === 2)); # the row is available |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
/** |
485
|
|
|
* Tests if we can do a transaction with reverting changes |
486
|
|
|
* |
487
|
|
|
* @return null |
488
|
|
|
*/ |
489
|
|
|
public function testTransactionReversionShortcut() |
490
|
|
|
{ |
491
|
|
|
$options = $this->options; |
492
|
|
|
$options["auto_connect"] = true; |
493
|
|
|
|
494
|
|
|
$conn = new MySQL($options); |
495
|
|
|
|
496
|
|
|
# not necessary! |
497
|
|
|
# $conn->autocommit(false); |
498
|
|
|
|
499
|
|
|
# starts the transaction |
500
|
|
|
$conn->beginTransaction(); |
501
|
|
|
|
502
|
|
|
try { |
503
|
|
|
$sql = "INSERT INTO MYTABLE (DESCRIPTION) VALUES ('TRANS_SHORTCUT_1')"; |
504
|
|
|
$conn->execute($sql); |
505
|
|
|
|
506
|
|
|
$sql = "INSERT INTO MYTABLE (DESCRIPTION, WRONG) VALUES ('TRANS_SHORTCUT_2')"; |
507
|
|
|
$conn->execute($sql); # this throws the exception |
508
|
|
|
} catch (InvalidQueryException $e) { |
509
|
|
|
#·not necessary! |
510
|
|
|
# $this->assertTrue($conn->rollback()); |
511
|
|
|
} |
512
|
|
|
|
513
|
|
|
# ends the transaction |
514
|
|
|
$conn->endTransaction(); |
515
|
|
|
|
516
|
|
|
# now let's to verify if the record exists after endTransaction() |
517
|
|
|
$sql = "SELECT * FROM MYTABLE WHERE DESCRIPTION LIKE 'TRANS_SHORTCUT_%'"; |
518
|
|
|
$conn->execute($sql); |
519
|
|
|
$rowcount = count($conn->getArrayResult()); |
520
|
|
|
|
521
|
|
|
$this->assertNotTrue(($rowcount === 0)); # the rows are not available |
522
|
|
|
|
523
|
|
|
# remove all work |
524
|
|
|
$conn->execute("DROP TABLE MYTABLE"); |
525
|
|
|
} |
526
|
|
|
} |
527
|
|
|
|