forked from propelorm/Propel
/
PropelPDOTest.php
670 lines (524 loc) · 29.5 KB
/
PropelPDOTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
<?php
/**
* This file is part of the Propel package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
require_once dirname(__FILE__) . '/../../../tools/helpers/bookstore/BookstoreTestBase.php';
/**
* Test for PropelPDO subclass.
*
* @package runtime.connection
*/
class PropelPDOTest extends PHPUnit_Framework_TestCase
{
public function testSetAttribute()
{
$con = Propel::getConnection(BookPeer::DATABASE_NAME);
$this->assertFalse($con->getAttribute(PropelPDO::PROPEL_ATTR_CACHE_PREPARES));
$con->setAttribute(PropelPDO::PROPEL_ATTR_CACHE_PREPARES, true);
$this->assertTrue($con->getAttribute(PropelPDO::PROPEL_ATTR_CACHE_PREPARES));
$con->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
$this->assertEquals(PDO::CASE_LOWER, $con->getAttribute(PDO::ATTR_CASE));
}
public function testCommitBeforeFetch()
{
$con = Propel::getConnection(BookPeer::DATABASE_NAME);
AuthorPeer::doDeleteAll($con);
$a = new Author();
$a->setFirstName('Test');
$a->setLastName('User');
$a->save($con);
$con->beginTransaction();
$stmt = $con->prepare('SELECT author.FIRST_NAME, author.LAST_NAME FROM author');
$stmt->execute();
$con->commit();
$authorArr = array(0 => 'Test', 1 => 'User');
$i = 0;
try {
$row = $stmt->fetch( PDO::FETCH_NUM );
$stmt->closeCursor();
$this->assertEquals($authorArr, $row, 'PDO driver supports calling $stmt->fetch after the transaction has been closed');
} catch (PDOException $e) {
$this->fail("PDO driver does not support calling \$stmt->fetch after the transaction has been closed.\nFails with error ".$e->getMessage());
}
}
public function testCommitAfterFetch()
{
$con = Propel::getConnection(BookPeer::DATABASE_NAME);
AuthorPeer::doDeleteAll($con);
$a = new Author();
$a->setFirstName('Test');
$a->setLastName('User');
$a->save($con);
$con->beginTransaction();
$stmt = $con->prepare('SELECT author.FIRST_NAME, author.LAST_NAME FROM author');
$stmt->execute();
$authorArr = array(0 => 'Test', 1 => 'User');
$i = 0;
$row = $stmt->fetch( PDO::FETCH_NUM );
$stmt->closeCursor();
$con->commit();
$this->assertEquals($authorArr, $row, 'PDO driver supports calling $stmt->fetch before the transaction has been closed');
}
public function testNestedTransactionCommit()
{
$con = Propel::getConnection(BookPeer::DATABASE_NAME);
$driver = $con->getAttribute(PDO::ATTR_DRIVER_NAME);
$this->assertEquals(0, $con->getNestedTransactionCount(), 'nested transaction is equal to 0 before transaction');
$this->assertFalse($con->isInTransaction(), 'PropelPDO is not in transaction by default');
$con->beginTransaction();
$this->assertEquals(1, $con->getNestedTransactionCount(), 'nested transaction is incremented after main transaction begin');
$this->assertTrue($con->isInTransaction(), 'PropelPDO is in transaction after main transaction begin');
try {
$a = new Author();
$a->setFirstName('Test');
$a->setLastName('User');
$a->save($con);
$authorId = $a->getId();
$this->assertNotNull($authorId, "Expected valid new author ID");
$con->beginTransaction();
$this->assertEquals(2, $con->getNestedTransactionCount(), 'nested transaction is incremented after nested transaction begin');
$this->assertTrue($con->isInTransaction(), 'PropelPDO is in transaction after nested transaction begin');
try {
$a2 = new Author();
$a2->setFirstName('Test2');
$a2->setLastName('User2');
$a2->save($con);
$authorId2 = $a2->getId();
$this->assertNotNull($authorId2, "Expected valid new author ID");
$con->commit();
$this->assertEquals(1, $con->getNestedTransactionCount(), 'nested transaction decremented after nested transaction commit');
$this->assertTrue($con->isInTransaction(), 'PropelPDO is in transaction after main transaction commit');
} catch (Exception $e) {
$con->rollBack();
throw $e;
}
$con->commit();
$this->assertEquals(0, $con->getNestedTransactionCount(), 'nested transaction decremented after main transaction commit');
$this->assertFalse($con->isInTransaction(), 'PropelPDO is not in transaction after main transaction commit');
} catch (Exception $e) {
$con->rollBack();
}
AuthorPeer::clearInstancePool();
$at = AuthorPeer::retrieveByPK($authorId);
$this->assertNotNull($at, "Committed transaction is persisted in database");
$at2 = AuthorPeer::retrieveByPK($authorId2);
$this->assertNotNull($at2, "Committed transaction is persisted in database");
}
/**
* @link http://trac.propelorm.org/ticket/699
*/
public function testNestedTransactionRollBackRethrow()
{
$con = Propel::getConnection(BookPeer::DATABASE_NAME);
$driver = $con->getAttribute(PDO::ATTR_DRIVER_NAME);
$con->beginTransaction();
try {
$a = new Author();
$a->setFirstName('Test');
$a->setLastName('User');
$a->save($con);
$authorId = $a->getId();
$this->assertNotNull($authorId, "Expected valid new author ID");
$con->beginTransaction();
$this->assertEquals(2, $con->getNestedTransactionCount(), 'nested transaction is incremented after nested transaction begin');
$this->assertTrue($con->isInTransaction(), 'PropelPDO is in transaction after nested transaction begin');
try {
$con->exec('INVALID SQL');
$this->fail("Expected exception on invalid SQL");
} catch (PDOException $x) {
$con->rollBack();
$this->assertEquals(1, $con->getNestedTransactionCount(), 'nested transaction decremented after nested transaction rollback');
$this->assertTrue($con->isInTransaction(), 'PropelPDO is in transaction after main transaction rollback');
throw $x;
}
$con->commit();
} catch (Exception $x) {
$con->rollBack();
}
AuthorPeer::clearInstancePool();
$at = AuthorPeer::retrieveByPK($authorId);
$this->assertNull($at, "Rolled back transaction is not persisted in database");
}
/**
* @link http://trac.propelorm.org/ticket/699
*/
public function testNestedTransactionRollBackSwallow()
{
$con = Propel::getConnection(BookPeer::DATABASE_NAME);
$driver = $con->getAttribute(PDO::ATTR_DRIVER_NAME);
$con->beginTransaction();
try {
$a = new Author();
$a->setFirstName('Test');
$a->setLastName('User');
$a->save($con);
$authorId = $a->getId();
$this->assertNotNull($authorId, "Expected valid new author ID");
$con->beginTransaction();
try {
$a2 = new Author();
$a2->setFirstName('Test2');
$a2->setLastName('User2');
$a2->save($con);
$authorId2 = $a2->getId();
$this->assertNotNull($authorId2, "Expected valid new author ID");
$con->exec('INVALID SQL');
$this->fail("Expected exception on invalid SQL");
} catch (PDOException $e) {
$con->rollBack();
// NO RETHROW
}
$a3 = new Author();
$a3->setFirstName('Test2');
$a3->setLastName('User2');
$a3->save($con);
$authorId3 = $a3->getId();
$this->assertNotNull($authorId3, "Expected valid new author ID");
$con->commit();
$this->fail("Commit fails after a nested rollback");
} catch (PropelException $e) {
$this->assertTrue(true, "Commit fails after a nested rollback");
$con->rollback();
}
AuthorPeer::clearInstancePool();
$at = AuthorPeer::retrieveByPK($authorId);
$this->assertNull($at, "Rolled back transaction is not persisted in database");
$at2 = AuthorPeer::retrieveByPK($authorId2);
$this->assertNull($at2, "Rolled back transaction is not persisted in database");
$at3 = AuthorPeer::retrieveByPK($authorId3);
$this->assertNull($at3, "Rolled back nested transaction is not persisted in database");
}
public function testNestedTransactionForceRollBack()
{
$con = Propel::getConnection(BookPeer::DATABASE_NAME);
$driver = $con->getAttribute(PDO::ATTR_DRIVER_NAME);
// main transaction
$con->beginTransaction();
$a = new Author();
$a->setFirstName('Test');
$a->setLastName('User');
$a->save($con);
$authorId = $a->getId();
// nested transaction
$con->beginTransaction();
$a2 = new Author();
$a2->setFirstName('Test2');
$a2->setLastName('User2');
$a2->save($con);
$authorId2 = $a2->getId();
// force rollback
$con->forceRollback();
$this->assertEquals(0, $con->getNestedTransactionCount(), 'nested transaction is null after nested transaction forced rollback');
$this->assertFalse($con->isInTransaction(), 'PropelPDO is not in transaction after nested transaction force rollback');
AuthorPeer::clearInstancePool();
$at = AuthorPeer::retrieveByPK($authorId);
$this->assertNull($at, "Rolled back transaction is not persisted in database");
$at2 = AuthorPeer::retrieveByPK($authorId2);
$this->assertNull($at2, "Forced Rolled back nested transaction is not persisted in database");
}
public function testLatestQuery()
{
$con = Propel::getConnection(BookPeer::DATABASE_NAME);
$con->setLastExecutedQuery(123);
$this->assertEquals(123, $con->getLastExecutedQuery(), 'PropelPDO has getter and setter for last executed query');
}
public function testLatestQueryMoreThanTenArgs()
{
$con = Propel::getConnection(BookPeer::DATABASE_NAME);
$c = new Criteria();
$c->add(BookPeer::ID, array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Criteria::IN);
$books = BookPeer::doSelect($c, $con);
$expected = "SELECT book.id, book.title, book.isbn, book.price, book.publisher_id, book.author_id FROM `book` WHERE book.id IN (1,1,1,1,1,1,1,1,1,1,1,1)";
$this->assertEquals($expected, $con->getLastExecutedQuery(), 'PropelPDO correctly replaces arguments in queries');
}
public function testQueryCount()
{
$con = Propel::getConnection(BookPeer::DATABASE_NAME);
$count = $con->getQueryCount();
$con->incrementQueryCount();
$this->assertEquals($count + 1, $con->getQueryCount(), 'PropelPDO has getter and incrementer for query count');
}
public function testUseDebug()
{
$con = Propel::getConnection(BookPeer::DATABASE_NAME);
$con->useDebug(false);
$this->assertEquals(array('PDOStatement'), $con->getAttribute(PDO::ATTR_STATEMENT_CLASS), 'Statement is PDOStatement when debug is false');
$con->useDebug(true);
$this->assertEquals(array('DebugPDOStatement', array($con)), $con->getAttribute(PDO::ATTR_STATEMENT_CLASS), 'statement is DebugPDOStatement when debug is true');
}
public function testDebugLatestQuery()
{
$con = Propel::getConnection(BookPeer::DATABASE_NAME);
$c = new Criteria();
$c->add(BookPeer::TITLE, 'Harry%s', Criteria::LIKE);
$con->useDebug(false);
$this->assertEquals('', $con->getLastExecutedQuery(), 'PropelPDO reinitializes the latest query when debug is set to false');
$books = BookPeer::doSelect($c, $con);
$this->assertEquals('', $con->getLastExecutedQuery(), 'PropelPDO does not update the last executed query when useLogging is false');
$con->useDebug(true);
$books = BookPeer::doSelect($c, $con);
$latestExecutedQuery = "SELECT book.id, book.title, book.isbn, book.price, book.publisher_id, book.author_id FROM `book` WHERE book.title LIKE 'Harry%s'";
if (!Propel::getDB(BookPeer::DATABASE_NAME)->useQuoteIdentifier()) {
$latestExecutedQuery = str_replace('`', '', $latestExecutedQuery);
}
$this->assertEquals($latestExecutedQuery, $con->getLastExecutedQuery(), 'PropelPDO updates the last executed query when useLogging is true');
BookPeer::doDeleteAll($con);
$latestExecutedQuery = "DELETE FROM `book`";
$this->assertEquals($latestExecutedQuery, $con->getLastExecutedQuery(), 'PropelPDO updates the last executed query on delete operations');
$sql = 'DELETE FROM book WHERE 1=1';
$con->exec($sql);
$this->assertEquals($sql, $con->getLastExecutedQuery(), 'PropelPDO updates the last executed query on exec operations');
$sql = 'DELETE FROM book WHERE 2=2';
$con->query($sql);
$this->assertEquals($sql, $con->getLastExecutedQuery(), 'PropelPDO updates the last executed query on query operations');
$stmt = $con->prepare('DELETE FROM book WHERE 1=:p1');
$stmt->bindValue(':p1', '2');
$stmt->execute();
$this->assertEquals("DELETE FROM book WHERE 1='2'", $con->getLastExecutedQuery(), 'PropelPDO updates the last executed query on prapared statements');
$con->useDebug(false);
$this->assertEquals('', $con->getLastExecutedQuery(), 'PropelPDO reinitializes the latest query when debug is set to false');
$con->useDebug(true);
}
public function testDebugQueryCount()
{
$con = Propel::getConnection(BookPeer::DATABASE_NAME);
$c = new Criteria();
$c->add(BookPeer::TITLE, 'Harry%s', Criteria::LIKE);
$con->useDebug(false);
$this->assertEquals(0, $con->getQueryCount(), 'PropelPDO does not update the query count when useLogging is false');
$books = BookPeer::doSelect($c, $con);
$this->assertEquals(0, $con->getQueryCount(), 'PropelPDO does not update the query count when useLogging is false');
$con->useDebug(true);
$books = BookPeer::doSelect($c, $con);
$this->assertEquals(1, $con->getQueryCount(), 'PropelPDO updates the query count when useLogging is true');
BookPeer::doDeleteAll($con);
$this->assertEquals(2, $con->getQueryCount(), 'PropelPDO updates the query count on delete operations');
$sql = 'DELETE FROM book WHERE 1=1';
$con->exec($sql);
$this->assertEquals(3, $con->getQueryCount(), 'PropelPDO updates the query count on exec operations');
$sql = 'DELETE FROM book WHERE 2=2';
$con->query($sql);
$this->assertEquals(4, $con->getQueryCount(), 'PropelPDO updates the query count on query operations');
$stmt = $con->prepare('DELETE FROM book WHERE 1=:p1');
$stmt->bindValue(':p1', '2');
$stmt->execute();
$this->assertEquals(5, $con->getQueryCount(), 'PropelPDO updates the query count on prapared statements');
$con->useDebug(false);
$this->assertEquals(0, $con->getQueryCount(), 'PropelPDO reinitializes the query count when debug is set to false');
$con->useDebug(true);
}
public function testDebugLog()
{
$con = Propel::getConnection(BookPeer::DATABASE_NAME);
$config = Propel::getConfiguration(PropelConfiguration::TYPE_OBJECT);
// save data to return to normal state after test
$logger = $con->getLogger();
$testLog = new myLogger();
$con->setLogger($testLog);
$logEverything = array('PropelPDO::exec', 'PropelPDO::query', 'PropelPDO::beginTransaction', 'PropelPDO::commit', 'PropelPDO::rollBack', 'DebugPDOStatement::execute');
Propel::getConfiguration(PropelConfiguration::TYPE_OBJECT)->setParameter("debugpdo.logging.methods", $logEverything, false);
$con->useDebug(true);
// test transaction log
$con->beginTransaction();
$this->assertEquals('log: Begin transaction', $testLog->latestMessage, 'PropelPDO logs begin transation in debug mode');
$con->commit();
$this->assertEquals('log: Commit transaction', $testLog->latestMessage, 'PropelPDO logs commit transation in debug mode');
$con->beginTransaction();
$con->rollBack();
$this->assertEquals('log: Rollback transaction', $testLog->latestMessage, 'PropelPDO logs rollback transation in debug mode');
$con->beginTransaction();
$testLog->latestMessage = '';
$con->beginTransaction();
$this->assertEquals('', $testLog->latestMessage, 'PropelPDO does not log nested begin transation in debug mode');
$con->commit();
$this->assertEquals('', $testLog->latestMessage, 'PropelPDO does not log nested commit transation in debug mode');
$con->beginTransaction();
$con->rollBack();
$this->assertEquals('', $testLog->latestMessage, 'PropelPDO does not log nested rollback transation in debug mode');
$con->rollback();
// test query log
$con->beginTransaction();
$c = new Criteria();
$c->add(BookPeer::TITLE, 'Harry%s', Criteria::LIKE);
$books = BookPeer::doSelect($c, $con);
$latestExecutedQuery = "SELECT book.id, book.title, book.isbn, book.price, book.publisher_id, book.author_id FROM `book` WHERE book.title LIKE 'Harry%s'";
$this->assertEquals('log: ' . $latestExecutedQuery, $testLog->latestMessage, 'PropelPDO logs queries and populates bound parameters in debug mode');
BookPeer::doDeleteAll($con);
$latestExecutedQuery = "DELETE FROM `book`";
$this->assertEquals('log: ' . $latestExecutedQuery, $testLog->latestMessage, 'PropelPDO logs deletion queries in debug mode');
$latestExecutedQuery = 'DELETE FROM book WHERE 1=1';
$con->exec($latestExecutedQuery);
$this->assertEquals('log: ' . $latestExecutedQuery, $testLog->latestMessage, 'PropelPDO logs exec queries in debug mode');
$con->commit();
// return to normal state after test
$con->setLogger($logger);
$config->setParameter("debugpdo.logging.methods", array('PropelPDO::exec', 'PropelPDO::query', 'DebugPDOStatement::execute'));
}
/**
* Testing if string values will be quoted correctly by DebugPDOStatement::getExecutedQueryString
*/
public function testDebugExecutedQueryStringValue()
{
/**
* @var DebugPDO $con
*/
$con = Propel::getConnection(BookPeer::DATABASE_NAME);
// different method must all result in this given querystring, using a string value
$bindParamStringValue = "%Harry%";
$expectedQuery = "SELECT book.id FROM `book` WHERE book.title LIKE '{$bindParamStringValue}'";
// simple statement without params
$prepStmt = $con->prepare($expectedQuery);
$prepStmt->execute();
$this->assertEquals($expectedQuery, $con->getLastExecutedQuery(), 'DebugPDO failed to quote prepared statement on execute properly');
// statement with named placeholder
$prepStmt = $con->prepare("SELECT book.id FROM `book` WHERE book.title LIKE :p1");
$prepStmt->bindValue(':p1', '%Harry%'); // bind value variant
$prepStmt->execute();
$this->assertEquals($expectedQuery, $con->getLastExecutedQuery(), 'DebugPDO failed to quote prepared statement on execute properly');
$prepStmt->bindParam(':p1', $bindParamStringValue); // bind param variant
$prepStmt->execute();
$this->assertEquals($expectedQuery, $con->getLastExecutedQuery(), 'DebugPDO failed to quote prepared statement on execute properly');
// passing params directly
$prepStmt->execute(array(':p1' => '%Harry%'));
$this->assertEquals($expectedQuery, $con->getLastExecutedQuery(), 'DebugPDO failed to quote prepared statement on execute properly');
// statement with named placeholder, this one won't get substituted
$expectedNotSubstitutedQuery = "SELECT book.id FROM `book` WHERE book.title LIKE :name";
$prepStmt = $con->prepare($expectedNotSubstitutedQuery);
$prepStmt->bindValue(':name', '%Harry%'); // bind value variant
$prepStmt->execute();
$this->assertEquals($expectedNotSubstitutedQuery, $con->getLastExecutedQuery(), 'DebugPDO failed to quote prepared statement on execute properly');
$prepStmt->bindParam(':name', $bindParamStringValue); // bind param variant
$prepStmt->execute();
$this->assertEquals($expectedNotSubstitutedQuery, $con->getLastExecutedQuery(), 'DebugPDO failed to quote prepared statement on execute properly');
// passing params directly
$prepStmt->execute(array(':name' => '%Harry%'));
$this->assertEquals($expectedNotSubstitutedQuery, $con->getLastExecutedQuery(), 'DebugPDO failed to quote prepared statement on execute properly');
// statement with positional placeholder, this one won't get substituted either
$expectedNotSubstitutedQuery = "SELECT book.id FROM `book` WHERE book.title LIKE ?";
$prepStmt = $con->prepare($expectedNotSubstitutedQuery);
$prepStmt->bindValue(1, '%Harry%');
$prepStmt->execute();
$this->assertEquals($expectedNotSubstitutedQuery, $con->getLastExecutedQuery(), 'DebugPDO failed to quote prepared statement on execute properly');
$prepStmt->bindParam(1, $bindParamStringValue);
$prepStmt->execute();
$this->assertEquals($expectedNotSubstitutedQuery, $con->getLastExecutedQuery(), 'DebugPDO failed to quote prepared statement on execute properly');
// passing params directly
$prepStmt->execute(array('%Harry%'));
$this->assertEquals($expectedNotSubstitutedQuery, $con->getLastExecutedQuery(), 'DebugPDO failed to quote prepared statement on execute properly');
}
/**
* Testing if integer values will be quoted correctly by DebugPDOStatement::getExecutedQueryString
*/
public function testDebugExecutedQueryIntegerValue() {
/**
* @var DebugPDO $con
*/
$con = Propel::getConnection(BookPeer::DATABASE_NAME);
// different method must all result in this given querystring, using an integer value
$bindParamIntegerValue = 123;
$expectedQuery = "SELECT book.title FROM `book` WHERE book.id = {$bindParamIntegerValue}";
// simple statement without params
$prepStmt = $con->prepare($expectedQuery);
$prepStmt->execute();
$this->assertEquals($expectedQuery, $con->getLastExecutedQuery(), 'DebugPDO failed to quote prepared statement on execute properly');
// statement with named placeholder
$prepStmt = $con->prepare("SELECT book.title FROM `book` WHERE book.id = :p1");
$prepStmt->bindValue(':p1', 123); // bind value variant
$prepStmt->execute();
$this->assertEquals($expectedQuery, $con->getLastExecutedQuery(), 'DebugPDO failed to quote prepared statement on execute properly');
$prepStmt->bindParam(':p1', $bindParamIntegerValue); // bind param variant
$prepStmt->execute();
$this->assertEquals($expectedQuery, $con->getLastExecutedQuery(), 'DebugPDO failed to quote prepared statement on execute properly');
// passing params directly
$prepStmt->execute(array(':p1' => 123));
$this->assertEquals($expectedQuery, $con->getLastExecutedQuery(), 'DebugPDO failed to quote prepared statement on execute properly');
// statement with named placeholder, this one won't get substituted
$expectedNotSubstitutedQuery = "SELECT book.title FROM `book` WHERE book.id = :name";
$prepStmt = $con->prepare($expectedNotSubstitutedQuery);
$prepStmt->bindValue(':name', 123); // bind value variant
$prepStmt->execute();
$this->assertEquals($expectedNotSubstitutedQuery, $con->getLastExecutedQuery(), 'DebugPDO failed to quote prepared statement on execute properly');
$prepStmt->bindParam(':name', $bindParamIntegerValue); // bind param variant
$prepStmt->execute();
$this->assertEquals($expectedNotSubstitutedQuery, $con->getLastExecutedQuery(), 'DebugPDO failed to quote prepared statement on execute properly');
// passing params directly
$prepStmt->execute(array(':name' => 123));
$this->assertEquals($expectedNotSubstitutedQuery, $con->getLastExecutedQuery(), 'DebugPDO failed to quote prepared statement on execute properly');
// statement with positional placeholder, this one won't get substituted either
$expectedNotSubstitutedQuery = "SELECT book.title FROM `book` WHERE book.id = ?";
$prepStmt = $con->prepare($expectedNotSubstitutedQuery);
$prepStmt->bindValue(1, 123);
$prepStmt->execute();
$this->assertEquals($expectedNotSubstitutedQuery, $con->getLastExecutedQuery(), 'DebugPDO failed to quote prepared statement on execute properly');
$prepStmt->bindParam(1, $bindParamIntegerValue);
$prepStmt->execute();
$this->assertEquals($expectedNotSubstitutedQuery, $con->getLastExecutedQuery(), 'DebugPDO failed to quote prepared statement on execute properly');
// passing params directly
$prepStmt->execute(array(123));
$this->assertEquals($expectedNotSubstitutedQuery, $con->getLastExecutedQuery(), 'DebugPDO failed to quote prepared statement on execute properly');
}
/**
* Testing if numeric values will be quoted correctly by DebugPDOStatement::getExecutedQueryString
* Numeric values sometimes will get handled differently, since there are numeric values which are non-integer
*/
public function testDebugExecutedQueryNumericValue() {
/**
* @var DebugPDO $con
*/
$con = Propel::getConnection(BookPeer::DATABASE_NAME);
// different method must all result in this given querystring, using an integer value
$bindParamNumericValue = 0002000;
$expectedQuery = "SELECT book.title FROM `book` WHERE book.id = {$bindParamNumericValue}";
// simple statement without params
$prepStmt = $con->prepare($expectedQuery);
$prepStmt->execute();
$this->assertEquals($expectedQuery, $con->getLastExecutedQuery(), 'DebugPDO failed to quote prepared statement on execute properly');
// statement with named placeholder
$prepStmt = $con->prepare("SELECT book.title FROM `book` WHERE book.id = :p1");
$prepStmt->bindValue(':p1', 0002000); // bind value variant
$prepStmt->execute();
$this->assertEquals($expectedQuery, $con->getLastExecutedQuery(), 'DebugPDO failed to quote prepared statement on execute properly');
$prepStmt->bindParam(':p1', $bindParamNumericValue); // bind param variant
$prepStmt->execute();
$this->assertEquals($expectedQuery, $con->getLastExecutedQuery(), 'DebugPDO failed to quote prepared statement on execute properly');
// passing params directly
$prepStmt->execute(array(':p1' => 0002000));
$this->assertEquals($expectedQuery, $con->getLastExecutedQuery(), 'DebugPDO failed to quote prepared statement on execute properly');
// statement with named placeholder, this one won't get substituted
$expectedNotSubstitutedQuery = "SELECT book.title FROM `book` WHERE book.id = :name";
$prepStmt = $con->prepare($expectedNotSubstitutedQuery);
$prepStmt->bindValue(':name', 0002000); // bind value variant
$prepStmt->execute();
$this->assertEquals($expectedNotSubstitutedQuery, $con->getLastExecutedQuery(), 'DebugPDO failed to quote prepared statement on execute properly');
$prepStmt->bindParam(':name', $bindParamNumericValue); // bind param variant
$prepStmt->execute();
$this->assertEquals($expectedNotSubstitutedQuery, $con->getLastExecutedQuery(), 'DebugPDO failed to quote prepared statement on execute properly');
// passing params directly
$prepStmt->execute(array(':name' => 0002000));
$this->assertEquals($expectedNotSubstitutedQuery, $con->getLastExecutedQuery(), 'DebugPDO failed to quote prepared statement on execute properly');
// statement with positional placeholder, this one won't get substituted either
$expectedNotSubstitutedQuery = "SELECT book.title FROM `book` WHERE book.id = ?";
$prepStmt = $con->prepare($expectedNotSubstitutedQuery);
$prepStmt->bindValue(1, 0002000);
$prepStmt->execute();
$this->assertEquals($expectedNotSubstitutedQuery, $con->getLastExecutedQuery(), 'DebugPDO failed to quote prepared statement on execute properly');
$prepStmt->bindParam(1, $bindParamNumericValue);
$prepStmt->execute();
$this->assertEquals($expectedNotSubstitutedQuery, $con->getLastExecutedQuery(), 'DebugPDO failed to quote prepared statement on execute properly');
// passing params directly
$prepStmt->execute(array(0002000));
$this->assertEquals($expectedNotSubstitutedQuery, $con->getLastExecutedQuery(), 'DebugPDO failed to quote prepared statement on execute properly');
}
}
class myLogger
{
public $latestMessage = '';
public function __call($method, $arguments)
{
$this->latestMessage = $method . ': ' . array_shift($arguments);
}
}