From 64a1d3999af885c2a1537497ba2af58c04fcc19a Mon Sep 17 00:00:00 2001 From: Peter Buri Date: Wed, 27 Mar 2013 19:41:01 +0100 Subject: [PATCH] Fixed long multibyte parameter logging in DbalLogger:startQuery --- .../Bridge/Doctrine/Logger/DbalLogger.php | 15 ++++-- .../Doctrine/Tests/Logger/DbalLoggerTest.php | 47 +++++++++++++++++-- 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/src/Symfony/Bridge/Doctrine/Logger/DbalLogger.php b/src/Symfony/Bridge/Doctrine/Logger/DbalLogger.php index de31b44337dd..cb4e14600507 100644 --- a/src/Symfony/Bridge/Doctrine/Logger/DbalLogger.php +++ b/src/Symfony/Bridge/Doctrine/Logger/DbalLogger.php @@ -61,10 +61,17 @@ public function startQuery($sql, array $params = null, array $types = null) continue; } - // too long string must be shorten - if (self::MAX_STRING_LENGTH < strlen($params[$index])) { - $params[$index] = substr($params[$index], self::MAX_STRING_LENGTH - 6).' [...]'; - continue; + // detect if the too long string must be shorten + if (function_exists('mb_detect_encoding') && false !== $encoding = mb_detect_encoding($params[$index])) { + if (self::MAX_STRING_LENGTH < mb_strlen($params[$index], $encoding)) { + $params[$index] = mb_substr($params[$index], 0, self::MAX_STRING_LENGTH - 6, $encoding).' [...]'; + continue; + } + } else { + if (self::MAX_STRING_LENGTH < strlen($params[$index])) { + $params[$index] = substr($params[$index], 0, self::MAX_STRING_LENGTH - 6).' [...]'; + continue; + } } } } diff --git a/src/Symfony/Bridge/Doctrine/Tests/Logger/DbalLoggerTest.php b/src/Symfony/Bridge/Doctrine/Tests/Logger/DbalLoggerTest.php index b2c855722e82..ad47f339b96e 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Logger/DbalLoggerTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Logger/DbalLoggerTest.php @@ -81,13 +81,15 @@ public function testLogLongString() ->getMock() ; - $shortString = str_repeat('a', DbalLogger::MAX_STRING_LENGTH); - $longString = str_repeat('a', DbalLogger::MAX_STRING_LENGTH + 1); + $testString = 'abc'; + + $shortString = str_pad('', DbalLogger::MAX_STRING_LENGTH, $testString); + $longString = str_pad('', DbalLogger::MAX_STRING_LENGTH+1, $testString); $dbalLogger ->expects($this->once()) ->method('log') - ->with('SQL', array('short' => $shortString, 'long' => substr($longString, DbalLogger::MAX_STRING_LENGTH - 6).' [...]')) + ->with('SQL', array('short' => $shortString, 'long' => substr($longString, 0, DbalLogger::MAX_STRING_LENGTH - 6).' [...]')) ; $dbalLogger->startQuery('SQL', array( @@ -95,4 +97,43 @@ public function testLogLongString() 'long' => $longString, )); } + + public function testLogUTF8LongString() + { + if (!function_exists('mb_detect_encoding')) { + $this->markTestSkipped('Testing log shortening of utf8 charsets requires the mb_detect_encoding() function.'); + } + + $logger = $this->getMock('Symfony\\Component\\HttpKernel\\Log\\LoggerInterface'); + + $dbalLogger = $this + ->getMockBuilder('Symfony\\Bridge\\Doctrine\\Logger\\DbalLogger') + ->setConstructorArgs(array($logger, null)) + ->setMethods(array('log')) + ->getMock() + ; + + $testStringArray = array('é', 'á', 'ű', 'ő', 'ú', 'ö', 'ü', 'ó', 'í'); + $testStringCount = count($testStringArray); + + $shortString = ''; + $longString = ''; + for($i=1; $i<=DbalLogger::MAX_STRING_LENGTH; $i++) { + $shortString .= $testStringArray[$i % $testStringCount]; + $longString .= $testStringArray[$i % $testStringCount]; + } + $longString .= $testStringArray[$i % $testStringCount]; + + $dbalLogger + ->expects($this->once()) + ->method('log') + ->with('SQL', array('short' => $shortString, 'long' => mb_substr($longString, 0, DbalLogger::MAX_STRING_LENGTH - 6, mb_detect_encoding($longString)).' [...]')) + ; + + $dbalLogger->startQuery('SQL', array( + 'short' => $shortString, + 'long' => $longString, + )); + } + }