Skip to content

Commit

Permalink
Fixed long multibyte parameter logging in DbalLogger:startQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Buri committed Mar 27, 2013
1 parent 4cf06c1 commit 64a1d39
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
15 changes: 11 additions & 4 deletions src/Symfony/Bridge/Doctrine/Logger/DbalLogger.php
Expand Up @@ -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;
}
}
}
}
Expand Down
47 changes: 44 additions & 3 deletions src/Symfony/Bridge/Doctrine/Tests/Logger/DbalLoggerTest.php
Expand Up @@ -81,18 +81,59 @@ 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(
'short' => $shortString,
'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,
));
}

}

0 comments on commit 64a1d39

Please sign in to comment.