21 \PHPUnit_Framework_Error_Warning::$enabled =
true;
29 $formatted = $formatter->format(array(
30 'level_name' =>
'ERROR',
44 $this->assertEquals(array(
45 'level_name' =>
'ERROR',
48 'datetime' => date(
'Y-m-d'),
50 'foo' =>
'[object] (Monolog\\Formatter\\TestFooNorm: {"foo":"foo"})',
51 'bar' =>
'[object] (Monolog\\Formatter\\TestBarNorm: bar)',
53 'res' =>
'[resource] (stream)',
68 $e = new \LogicException(
'bar');
69 $e2 = new \RuntimeException(
'foo', 0, $e);
70 $formatted = $formatter->format(array(
74 $this->assertGreaterThan(5, count($formatted[
'exception'][
'trace']));
75 $this->assertTrue(isset($formatted[
'exception'][
'previous']));
76 unset($formatted[
'exception'][
'trace'], $formatted[
'exception'][
'previous']);
78 $this->assertEquals(array(
80 'class' => get_class($e2),
81 'message' => $e2->getMessage(),
82 'code' => $e2->getCode(),
83 'file' => $e2->getFile().
':'.$e2->getLine(),
90 if (!class_exists(
'SoapFault')) {
91 $this->markTestSkipped(
'Requires the soap extension');
95 $e = new \SoapFault(
'foo',
'bar',
'hello',
'world');
96 $formatted = $formatter->format(array(
100 unset($formatted[
'exception'][
'trace']);
102 $this->assertEquals(array(
103 'exception' => array(
104 'class' =>
'SoapFault',
107 'file' => $e->getFile().
':'.$e->getLine(),
108 'faultcode' =>
'foo',
109 'faultactor' =>
'hello',
118 $this->setExpectedException(
'RuntimeException',
'Could not convert to string');
119 $formatter->format(array(
127 $formatted = $formatter->formatBatch(array(
129 'level_name' =>
'CRITICAL',
132 'context' => array(),
137 'level_name' =>
'WARNING',
140 'context' => array(),
145 $this->assertEquals(array(
147 'level_name' =>
'CRITICAL',
150 'context' => array(),
151 'datetime' => date(
'Y-m-d'),
155 'level_name' =>
'WARNING',
158 'context' => array(),
159 'datetime' => date(
'Y-m-d'),
171 $foo = new \stdClass();
172 $bar = new \stdClass();
179 set_error_handler(
function ($level,
$message, $file, $line,
$context) use ($that) {
180 if (error_reporting() & $level) {
181 restore_error_handler();
182 $that->fail(
"$message should not be raised");
187 $reflMethod = new \ReflectionMethod($formatter,
'toJson');
188 $reflMethod->setAccessible(
true);
189 $res = $reflMethod->invoke($formatter, array($foo, $bar),
true);
191 restore_error_handler();
193 $this->assertEquals(@json_encode(array($foo, $bar)),
$res);
199 $x = array(
'foo' =>
'bar');
200 $y = array(
'x' => &
$x);
202 $formatter->format(
$y);
208 $resource =
fopen(__FILE__,
'r');
212 set_error_handler(
function ($level,
$message, $file, $line,
$context) use ($that) {
213 if (error_reporting() & $level) {
214 restore_error_handler();
215 $that->fail(
"$message should not be raised");
220 $reflMethod = new \ReflectionMethod($formatter,
'toJson');
221 $reflMethod->setAccessible(
true);
222 $res = $reflMethod->invoke($formatter, array($resource),
true);
224 restore_error_handler();
226 $this->assertEquals(@json_encode(array($resource)),
$res);
232 $largeArray = range(1, 1000);
234 $res = $formatter->format(array(
235 'level_name' =>
'CRITICAL',
238 'context' => array($largeArray),
243 $this->assertCount(1000,
$res[
'context'][0]);
244 $this->assertArrayNotHasKey(
'...',
$res[
'context'][0]);
250 $largeArray = range(1, 2000);
252 $res = $formatter->format(array(
253 'level_name' =>
'CRITICAL',
256 'context' => array($largeArray),
261 $this->assertCount(1001,
$res[
'context'][0]);
262 $this->assertEquals(
'Over 1000 items (2000 total), aborting normalization',
$res[
'context'][0][
'...']);
270 if (version_compare(PHP_VERSION,
'5.5.0',
'<')) {
272 \PHPUnit_Framework_Error_Warning::$enabled =
false;
275 $reflMethod = new \ReflectionMethod($formatter,
'toJson');
276 $reflMethod->setAccessible(
true);
279 $record = new \stdClass;
280 $record->message =
"\xB1\x31";
281 $res = $reflMethod->invoke($formatter, $record);
282 if (PHP_VERSION_ID < 50500 &&
$res ===
'{"message":null}') {
283 throw new \RuntimeException(
'PHP 5.3/5.4 throw a warning and null the value instead of returning false entirely');
289 if (version_compare(PHP_VERSION,
'5.5.0',
'<')) {
291 \PHPUnit_Framework_Error_Warning::$enabled =
false;
294 $reflMethod = new \ReflectionMethod($formatter,
'toJson');
295 $reflMethod->setAccessible(
true);
297 $res = $reflMethod->invoke($formatter, array(
'message' =>
"\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE"));
299 if (version_compare(PHP_VERSION,
'5.5.0',
'>=')) {
300 $this->assertSame(
'{"message":"€ŠšŽžŒœŸ"}',
$res);
304 $this->assertSame(
'{"message":null}',
$res);
317 $formatter->detectAndCleanUtf8(
$in);
318 $this->assertSame($expect,
$in);
323 $obj = new \stdClass;
326 'null' => array(null, null),
327 'int' => array(123, 123),
328 'float' => array(123.45, 123.45),
329 'bool false' => array(
false,
false),
330 'bool true' => array(
true,
true),
331 'ascii string' => array(
'abcdef',
'abcdef'),
332 'latin9 string' => array(
"\xB1\x31\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xFF",
'±1€ŠšŽžŒœŸÿ'),
333 'unicode string' => array(
'¤¦¨´¸¼½¾€ŠšŽžŒœŸ',
'¤¦¨´¸¼½¾€ŠšŽžŒœŸ'),
334 'empty array' => array(array(), array()),
335 'array' => array(array(
'abcdef'), array(
'abcdef')),
336 'object' => array($obj, $obj),
348 $reflMethod = new \ReflectionMethod($formatter,
'handleJsonError');
349 $reflMethod->setAccessible(
true);
351 $this->setExpectedException(
'RuntimeException', $msg);
352 $reflMethod->invoke($formatter,
$code,
'faked');
358 'depth' => array(JSON_ERROR_DEPTH,
'Maximum stack depth exceeded'),
359 'state' => array(JSON_ERROR_STATE_MISMATCH,
'Underflow or the modes mismatch'),
360 'ctrl' => array(JSON_ERROR_CTRL_CHAR,
'Unexpected control character found'),
361 'default' => array(-1,
'Unknown error'),
367 if (defined(
'HHVM_VERSION')) {
368 $this->markTestSkipped(
'Not supported in HHVM since it detects errors differently');
373 set_error_handler(
function ($errno, $errstr, $errfile, $errline) {
374 throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
379 $resource =
fopen(
'php://memory',
'rw+');
380 fwrite($resource,
'test_resource');
382 $wrappedResource->foo = $resource;
384 array_keys($wrappedResource);
386 restore_error_handler();
390 $record = array(
'context' => array(
'exception' => $e));
391 $result = $formatter->format($record);
394 '%"resource":"\[resource\] \(stream\)"%',
395 $result[
'context'][
'exception'][
'trace'][0]
398 if (version_compare(PHP_VERSION,
'5.5.0',
'>=')) {
399 $pattern =
'%"wrappedResource":"\[object\] \(Monolog\\\\\\\\Formatter\\\\\\\\TestFooNorm: \)"%';
401 $pattern =
'%\\\\"foo\\\\":null%';
407 $result[
'context'][
'exception'][
'trace'][0]
415 call_user_func(array($this,
'throwHelper'), $arg, $dt =
new \
DateTime());
420 $record = array(
'context' => array(
'exception' => $e));
421 $result = $formatter->format($record);
424 '{"function":"throwHelper","class":"Monolog\\\\Formatter\\\\NormalizerFormatterTest","type":"->","args":["[object] (Monolog\\\\Formatter\\\\TestInfoLeak)","'.$dt->format(
'Y-m-d H:i:s').
'"]}',
425 $result[
'context'][
'exception'][
'trace'][0]
431 throw new \RuntimeException(
'Thrown');
455 $this->resource = $resource;
461 fseek($this->resource, 0);
463 return $this->foo .
' - ' . (string) stream_get_contents($this->resource);
471 throw new \RuntimeException(
'Could not convert to string');
479 return 'Sensitive information';
catch(Exception $e) $message
foreach($_POST as $key=> $value) $res
if(php_sapi_name() !='cli') $in