Skip to content

Commit 07759de

Browse files
author
Walther Lalk
committed
Correctly log false as 0 for query logger.
The query logger was replacing `false` and `0` values with an empty string. This corrects that by replacing bools and `0` with a php string representation.
1 parent 6cbb32f commit 07759de

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

src/Database/Log/QueryLogger.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ protected function _interpolate($query) {
5959
$params = array_map(function ($p) {
6060
if ($p === null) {
6161
return 'NULL';
62+
} elseif (is_bool($p)) {
63+
return $p ? '1' : '0';
6264
}
6365
return is_string($p) ? "'$p'" : $p;
6466
}, $query->params);

tests/TestCase/Database/Log/QueryLoggerTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ public function tearDown() {
5353
public function testStingInterpolation() {
5454
$logger = $this->getMock('\Cake\Database\Log\QueryLogger', ['_log']);
5555
$query = new LoggedQuery;
56-
$query->query = 'SELECT a FROM b where a = :p1 AND b = :p2 AND c = :p3';
57-
$query->params = ['p1' => 'string', 'p3' => null, 'p2' => 3];
56+
$query->query = 'SELECT a FROM b where a = :p1 AND b = :p2 AND c = :p3 AND d = :p4 AND e = :p5 AND f = :p6';
57+
$query->params = ['p1' => 'string', 'p3' => null, 'p2' => 3, 'p4' => true, 'p5' => false, 'p6' => 0];
5858

5959
$logger->expects($this->once())->method('_log')->with($query);
6060
$logger->log($query);
61-
$expected = "SELECT a FROM b where a = 'string' AND b = 3 AND c = NULL";
61+
$expected = "SELECT a FROM b where a = 'string' AND b = 3 AND c = NULL AND d = 1 AND e = 0 AND f = 0";
6262
$this->assertEquals($expected, (string)$query);
6363
}
6464

@@ -70,12 +70,12 @@ public function testStingInterpolation() {
7070
public function testStingInterpolation2() {
7171
$logger = $this->getMock('\Cake\Database\Log\QueryLogger', ['_log']);
7272
$query = new LoggedQuery;
73-
$query->query = 'SELECT a FROM b where a = ? AND b = ? AND c = ?';
74-
$query->params = ['string', '3', null];
73+
$query->query = 'SELECT a FROM b where a = ? AND b = ? AND c = ? AND d = ? AND e = ? AND f = ?';
74+
$query->params = ['string', '3', null, true, false, 0];
7575

7676
$logger->expects($this->once())->method('_log')->with($query);
7777
$logger->log($query);
78-
$expected = "SELECT a FROM b where a = 'string' AND b = '3' AND c = NULL";
78+
$expected = "SELECT a FROM b where a = 'string' AND b = '3' AND c = NULL AND d = 1 AND e = 0 AND f = 0";
7979
$this->assertEquals($expected, (string)$query);
8080
}
8181

0 commit comments

Comments
 (0)