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:
@@ -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
|
||||
|
@@ -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();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user