Skip to content

Commit

Permalink
Fix failing tests around positional replacements.
Browse files Browse the repository at this point in the history
Both named and positional placeholders should be replaced correctly.
Positional arguments with double digit values should also be handled
correctly.
  • Loading branch information
markstory committed May 8, 2015
1 parent bec7304 commit 5b475a1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/Database/Log/QueryLogger.php
Expand Up @@ -71,9 +71,8 @@ protected function _interpolate($query)

$keys = [];
$limit = is_int(key($params)) ? 1 : -1;
$params = array_reverse($params);
foreach ($params as $key => $param) {
$keys[] = is_string($key) ? "/:$key/" : '/[?]/';
$keys[] = is_string($key) ? "/:$key\b/" : '/[?]/';
}

return preg_replace($keys, $params, $query->query, $limit);
Expand Down
18 changes: 18 additions & 0 deletions tests/TestCase/Database/Log/QueryLoggerTest.php
Expand Up @@ -102,6 +102,24 @@ public function testStringInterpolation3()
$this->assertEquals($expected, (string)$query);
}

/**
* Tests that named placeholders
*
* @return void
*/
public function testStringInterpolationNamed()
{
$logger = $this->getMock('\Cake\Database\Log\QueryLogger', ['_log']);
$query = new LoggedQuery;
$query->query = 'SELECT a FROM b where a = :p1 AND b = :p11 AND c = :p20 AND d = :p2';
$query->params = ['p11' => 'test', 'p1' => 'string', 'p2' => 3, 'p20' => 5];

$logger->expects($this->once())->method('_log')->with($query);
$logger->log($query);
$expected = "SELECT a FROM b where a = 'string' AND b = 'test' AND c = 5 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
Expand Down

0 comments on commit 5b475a1

Please sign in to comment.