Skip to content

Commit

Permalink
Correctly log false as 0 for query logger.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Walther Lalk committed Dec 17, 2014
1 parent 6cbb32f commit 07759de
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/Database/Log/QueryLogger.php
Expand Up @@ -59,6 +59,8 @@ protected function _interpolate($query) {
$params = array_map(function ($p) {
if ($p === null) {
return 'NULL';
} elseif (is_bool($p)) {
return $p ? '1' : '0';
}
return is_string($p) ? "'$p'" : $p;
}, $query->params);
Expand Down
12 changes: 6 additions & 6 deletions tests/TestCase/Database/Log/QueryLoggerTest.php
Expand Up @@ -53,12 +53,12 @@ public function tearDown() {
public function testStingInterpolation() {
$logger = $this->getMock('\Cake\Database\Log\QueryLogger', ['_log']);
$query = new LoggedQuery;
$query->query = 'SELECT a FROM b where a = :p1 AND b = :p2 AND c = :p3';
$query->params = ['p1' => 'string', 'p3' => null, 'p2' => 3];
$query->query = 'SELECT a FROM b where a = :p1 AND b = :p2 AND c = :p3 AND d = :p4 AND e = :p5 AND f = :p6';
$query->params = ['p1' => 'string', 'p3' => null, 'p2' => 3, 'p4' => true, 'p5' => false, 'p6' => 0];

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

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

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

Expand Down

0 comments on commit 07759de

Please sign in to comment.