Fixed long multibyte parameter logging in DbalLogger:startQuery
This commit is contained in:
parent
4cf06c1560
commit
64a1d3999a
@ -61,11 +61,18 @@ class DbalLogger implements SQLLogger
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// too long string must be shorten
|
// detect if the too long string must be shorten
|
||||||
if (self::MAX_STRING_LENGTH < strlen($params[$index])) {
|
if (function_exists('mb_detect_encoding') && false !== $encoding = mb_detect_encoding($params[$index])) {
|
||||||
$params[$index] = substr($params[$index], self::MAX_STRING_LENGTH - 6).' [...]';
|
if (self::MAX_STRING_LENGTH < mb_strlen($params[$index], $encoding)) {
|
||||||
|
$params[$index] = mb_substr($params[$index], 0, self::MAX_STRING_LENGTH - 6, $encoding).' [...]';
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
if (self::MAX_STRING_LENGTH < strlen($params[$index])) {
|
||||||
|
$params[$index] = substr($params[$index], 0, self::MAX_STRING_LENGTH - 6).' [...]';
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,13 +81,15 @@ class DbalLoggerTest extends \PHPUnit_Framework_TestCase
|
|||||||
->getMock()
|
->getMock()
|
||||||
;
|
;
|
||||||
|
|
||||||
$shortString = str_repeat('a', DbalLogger::MAX_STRING_LENGTH);
|
$testString = 'abc';
|
||||||
$longString = str_repeat('a', DbalLogger::MAX_STRING_LENGTH + 1);
|
|
||||||
|
$shortString = str_pad('', DbalLogger::MAX_STRING_LENGTH, $testString);
|
||||||
|
$longString = str_pad('', DbalLogger::MAX_STRING_LENGTH+1, $testString);
|
||||||
|
|
||||||
$dbalLogger
|
$dbalLogger
|
||||||
->expects($this->once())
|
->expects($this->once())
|
||||||
->method('log')
|
->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(
|
$dbalLogger->startQuery('SQL', array(
|
||||||
@ -95,4 +97,43 @@ class DbalLoggerTest extends \PHPUnit_Framework_TestCase
|
|||||||
'long' => $longString,
|
'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,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user