Skip to content
Permalink
Browse files

fixed a bug in QueryLogger where using the same named placeholder mul…

…tiple times resulted in only the first time being replaced
  • Loading branch information...
ramiroaraujo committed Mar 25, 2015
1 parent 0c4fa69 commit c66826eeccddec4ad7adb5b03198328fe6538028
Showing with 20 additions and 1 deletion.
  1. +2 −1 src/Database/Log/QueryLogger.php
  2. +18 −0 tests/TestCase/Database/Log/QueryLoggerTest.php
@@ -70,10 +70,11 @@ protected function _interpolate($query)
}, $query->params);
$keys = [];
$limit = is_int(key($params)) ? 1 : -1;
foreach ($params as $key => $param) {
$keys[] = is_string($key) ? "/:$key/" : '/[?]/';
}
return preg_replace($keys, $params, $query->query, 1);
return preg_replace($keys, $params, $query->query, $limit);
}
}
@@ -84,6 +84,24 @@ public function testStringInterpolation2()
$this->assertEquals($expected, (string)$query);
}
/**
* Tests that repeated placeholders are correctly replaced
*
* @return void
*/
public function testStringInterpolation3()
{
$logger = $this->getMock('\Cake\Database\Log\QueryLogger', ['_log']);
$query = new LoggedQuery;
$query->query = 'SELECT a FROM b where a = :p1 AND b = :p1 AND c = :p2 AND d = :p2';
$query->params = ['p1' => 'string', 'p2' => 3];
$logger->expects($this->once())->method('_log')->with($query);
$logger->log($query);
$expected = "SELECT a FROM b where a = 'string' AND b = 'string' AND c = 3 AND d = 3";
$this->assertEquals($expected, (string)$query);
}
/**
* Tests that the logged query object is passed to the built-in logger using
* the correct scope

0 comments on commit c66826e

Please sign in to comment.
You can’t perform that action at this time.