1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-11 15:44:34 +02:00

Resolve json encoding errors issue globally, refs #137

This commit is contained in:
Jordi Boggiano
2013-01-06 20:14:14 +01:00
parent 271c396964
commit 63e3bfdf7e
5 changed files with 71 additions and 52 deletions

View File

@@ -89,6 +89,61 @@ class NormalizerFormatterTest extends \PHPUnit_Framework_TestCase
),
), $formatted);
}
/**
* Test issue #137
*/
public function testIgnoresRecursiveObjectReferences()
{
// set up the recursion
$foo = new \stdClass();
$bar = new \stdClass();
$foo->bar = $bar;
$bar->foo = $foo;
// set an error handler to assert that the error is not raised anymore
$that = $this;
set_error_handler(function ($level, $message, $file, $line, $context) use ($that) {
if (error_reporting() & $level) {
restore_error_handler();
$that->fail("$message should not be raised");
}
});
$formatter = new NormalizerFormatter();
$reflMethod = new \ReflectionMethod($formatter, 'toJson');
$reflMethod->setAccessible(true);
$res = $reflMethod->invoke($formatter, array($foo, $bar), true);
restore_error_handler();
$this->assertEquals(@json_encode(array($foo, $bar)), $res);
}
public function testIgnoresInvalidTypes()
{
// set up the recursion
$resource = fopen(__FILE__, 'r');
// set an error handler to assert that the error is not raised anymore
$that = $this;
set_error_handler(function ($level, $message, $file, $line, $context) use ($that) {
if (error_reporting() & $level) {
restore_error_handler();
$that->fail("$message should not be raised");
}
});
$formatter = new NormalizerFormatter();
$reflMethod = new \ReflectionMethod($formatter, 'toJson');
$reflMethod->setAccessible(true);
$res = $reflMethod->invoke($formatter, array($resource), true);
restore_error_handler();
$this->assertEquals(@json_encode(array($resource)), $res);
}
}
class TestFooNorm

View File

@@ -108,41 +108,4 @@ class WildfireFormatterTest extends \PHPUnit_Framework_TestCase
$wildfire->formatBatch(array($record));
}
/**
* Test issue #137 (https://github.com/Seldaek/monolog/pull/137)
*/
public function testFormatWithObjectsInContext()
{
// Set up the recursion
$foo = new \stdClass();
$bar = new \stdClass();
$foo->bar = $bar;
$bar->foo = $foo;
$record = array(
'message' => "foo",
'level' => 300,
'channel' => 'foo',
'context' => array(
'stack' => array(
array($foo),
array($bar),
),
),
'extra' => array(),
);
// Set an error handler to assert that the error is not raised anymore
$that = $this;
set_error_handler(function ($level, $message, $file, $line, $context) use ($that) {
$that->fail("$message should not be raised anymore");
});
$wildfire = new WildfireFormatter();
$wildfire->format($record);
restore_error_handler();
}
}