From 5b475a1f15ec5dca553868316c76c2743ab7041c Mon Sep 17 00:00:00 2001 From: mark_story Date: Fri, 8 May 2015 17:48:33 -0400 Subject: [PATCH] Fix failing tests around positional replacements. Both named and positional placeholders should be replaced correctly. Positional arguments with double digit values should also be handled correctly. --- src/Database/Log/QueryLogger.php | 3 +-- .../TestCase/Database/Log/QueryLoggerTest.php | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Database/Log/QueryLogger.php b/src/Database/Log/QueryLogger.php index 64c739833f1..cc26ef805b3 100644 --- a/src/Database/Log/QueryLogger.php +++ b/src/Database/Log/QueryLogger.php @@ -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); diff --git a/tests/TestCase/Database/Log/QueryLoggerTest.php b/tests/TestCase/Database/Log/QueryLoggerTest.php index 61028f73e62..0ad824fb9e8 100644 --- a/tests/TestCase/Database/Log/QueryLoggerTest.php +++ b/tests/TestCase/Database/Log/QueryLoggerTest.php @@ -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