|
|
@@ -16,6 +16,12 @@ namespace Monolog\Formatter;
|
|
|
*/
|
|
|
class NormalizerFormatterTest extends \PHPUnit_Framework_TestCase
|
|
|
{
|
|
|
+ public function tearDown()
|
|
|
+ {
|
|
|
+ \PHPUnit_Framework_Error_Warning::$enabled = true;
|
|
|
+ return parent::tearDown();
|
|
|
+ }
|
|
|
+
|
|
|
public function testFormat()
|
|
|
{
|
|
|
$formatter = new NormalizerFormatter('Y-m-d');
|
|
|
@@ -188,17 +194,100 @@ class NormalizerFormatterTest extends \PHPUnit_Framework_TestCase
|
|
|
*/
|
|
|
public function testThrowsOnInvalidEncoding()
|
|
|
{
|
|
|
+ if (version_compare(PHP_VERSION, '5.5.0', '<')) {
|
|
|
+ // Ignore the warning that will be emitted by PHP <5.5.0
|
|
|
+ \PHPUnit_Framework_Error_Warning::$enabled = false;
|
|
|
+ }
|
|
|
$formatter = new NormalizerFormatter();
|
|
|
$reflMethod = new \ReflectionMethod($formatter, 'toJson');
|
|
|
$reflMethod->setAccessible(true);
|
|
|
|
|
|
- // send an invalid unicode sequence
|
|
|
- $res = $reflMethod->invoke($formatter, array('message' => "\xB1\x31"));
|
|
|
+ // send an invalid unicode sequence as a object that can't be cleaned
|
|
|
+ $record = new \stdClass;
|
|
|
+ $record->message = "\xB1\x31";
|
|
|
+ $res = $reflMethod->invoke($formatter, $record);
|
|
|
if (PHP_VERSION_ID < 50500 && $res === '{"message":null}') {
|
|
|
throw new \RuntimeException('PHP 5.3/5.4 throw a warning and null the value instead of returning false entirely');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public function testConvertsInvalidEncodingAsLatin9()
|
|
|
+ {
|
|
|
+ if (version_compare(PHP_VERSION, '5.5.0', '<')) {
|
|
|
+ // Ignore the warning that will be emitted by PHP <5.5.0
|
|
|
+ \PHPUnit_Framework_Error_Warning::$enabled = false;
|
|
|
+ }
|
|
|
+ $formatter = new NormalizerFormatter();
|
|
|
+ $reflMethod = new \ReflectionMethod($formatter, 'toJson');
|
|
|
+ $reflMethod->setAccessible(true);
|
|
|
+
|
|
|
+ $res = $reflMethod->invoke($formatter, array('message' => "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE"));
|
|
|
+
|
|
|
+ if (version_compare(PHP_VERSION, '5.5.0', '>=')) {
|
|
|
+ $this->assertSame('{"message":"€ŠšŽžŒœŸ"}', $res);
|
|
|
+ } else {
|
|
|
+ // PHP <5.5 does not return false for an element encoding failure,
|
|
|
+ // instead it emits a warning (possibly) and nulls the value.
|
|
|
+ $this->assertSame('{"message":null}', $res);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param mixed $in Input
|
|
|
+ * @param mixed $expect Expected output
|
|
|
+ * @covers Monolog\Formatter\NormalizerFormatter::detectAndCleanUtf8
|
|
|
+ * @dataProvider providesDetectAndCleanUtf8
|
|
|
+ */
|
|
|
+ public function testDetectAndCleanUtf8($in, $expect)
|
|
|
+ {
|
|
|
+ $formatter = new NormalizerFormatter();
|
|
|
+ $formatter->detectAndCleanUtf8($in);
|
|
|
+ $this->assertSame($expect, $in);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function providesDetectAndCleanUtf8()
|
|
|
+ {
|
|
|
+ $obj = new \stdClass;
|
|
|
+ return array(
|
|
|
+ 'null' => array(null, null),
|
|
|
+ 'int' => array(123, 123),
|
|
|
+ 'float' => array(123.45, 123.45),
|
|
|
+ 'bool false' => array(false, false),
|
|
|
+ 'bool true' => array(true, true),
|
|
|
+ 'ascii string' => array('abcdef', 'abcdef'),
|
|
|
+ 'latin9 string' => array("\xB1\x31\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xFF", '±1€ŠšŽžŒœŸÿ'),
|
|
|
+ 'unicode string' => array('¤¦¨´¸¼½¾€ŠšŽžŒœŸ', '¤¦¨´¸¼½¾€ŠšŽžŒœŸ'),
|
|
|
+ 'empty array' => array(array(), array()),
|
|
|
+ 'array' => array(array('abcdef'), array('abcdef')),
|
|
|
+ 'object' => array($obj, $obj),
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param int $code
|
|
|
+ * @param string $msg
|
|
|
+ * @dataProvider providesHandleJsonErrorFailure
|
|
|
+ */
|
|
|
+ public function testHandleJsonErrorFailure($code, $msg)
|
|
|
+ {
|
|
|
+ $formatter = new NormalizerFormatter();
|
|
|
+ $reflMethod = new \ReflectionMethod($formatter, 'handleJsonError');
|
|
|
+ $reflMethod->setAccessible(true);
|
|
|
+
|
|
|
+ $this->setExpectedException('RuntimeException', $msg);
|
|
|
+ $reflMethod->invoke($formatter, $code, 'faked');
|
|
|
+ }
|
|
|
+
|
|
|
+ public function providesHandleJsonErrorFailure()
|
|
|
+ {
|
|
|
+ return array(
|
|
|
+ 'depth' => array(JSON_ERROR_DEPTH, 'Maximum stack depth exceeded'),
|
|
|
+ 'state' => array(JSON_ERROR_STATE_MISMATCH, 'Underflow or the modes mismatch'),
|
|
|
+ 'ctrl' => array(JSON_ERROR_CTRL_CHAR, 'Unexpected control character found'),
|
|
|
+ 'default' => array(-1, 'Unknown error'),
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
public function testExceptionTraceWithArgs()
|
|
|
{
|
|
|
if (defined('HHVM_VERSION')) {
|
|
|
@@ -284,4 +373,4 @@ class TestToStringError
|
|
|
{
|
|
|
throw new \RuntimeException('Could not convert to string');
|
|
|
}
|
|
|
-}
|
|
|
+}
|