ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Monolog\Formatter\NormalizerFormatterTest Class Reference

@covers Monolog\Formatter\NormalizerFormatter More...

+ Inheritance diagram for Monolog\Formatter\NormalizerFormatterTest:
+ Collaboration diagram for Monolog\Formatter\NormalizerFormatterTest:

Public Member Functions

 tearDown ()
 
 testFormat ()
 
 testFormatExceptions ()
 
 testFormatSoapFaultException ()
 
 testFormatToStringExceptionHandle ()
 
 testBatchFormat ()
 
 testIgnoresRecursiveObjectReferences ()
 Test issue #137. More...
 
 testIgnoresInvalidTypes ()
 
 testThrowsOnInvalidEncoding ()
 @expectedException RuntimeException More...
 
 testConvertsInvalidEncodingAsLatin9 ()
 
 testDetectAndCleanUtf8 ($in, $expect)
 
 providesDetectAndCleanUtf8 ()
 
 testHandleJsonErrorFailure ($code, $msg)
 
 providesHandleJsonErrorFailure ()
 
 testExceptionTraceWithArgs ()
 

Detailed Description

@covers Monolog\Formatter\NormalizerFormatter

Definition at line 17 of file NormalizerFormatterTest.php.

Member Function Documentation

◆ providesDetectAndCleanUtf8()

Monolog\Formatter\NormalizerFormatterTest::providesDetectAndCleanUtf8 ( )

Definition at line 276 of file NormalizerFormatterTest.php.

277 {
278 $obj = new \stdClass;
279
280 return array(
281 'null' => array(null, null),
282 'int' => array(123, 123),
283 'float' => array(123.45, 123.45),
284 'bool false' => array(false, false),
285 'bool true' => array(true, true),
286 'ascii string' => array('abcdef', 'abcdef'),
287 'latin9 string' => array("\xB1\x31\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xFF", '±1€ŠšŽžŒœŸÿ'),
288 'unicode string' => array('¤¦¨´¸¼½¾€ŠšŽžŒœŸ', '¤¦¨´¸¼½¾€ŠšŽžŒœŸ'),
289 'empty array' => array(array(), array()),
290 'array' => array(array('abcdef'), array('abcdef')),
291 'object' => array($obj, $obj),
292 );
293 }

◆ providesHandleJsonErrorFailure()

Monolog\Formatter\NormalizerFormatterTest::providesHandleJsonErrorFailure ( )

Definition at line 310 of file NormalizerFormatterTest.php.

311 {
312 return array(
313 'depth' => array(JSON_ERROR_DEPTH, 'Maximum stack depth exceeded'),
314 'state' => array(JSON_ERROR_STATE_MISMATCH, 'Underflow or the modes mismatch'),
315 'ctrl' => array(JSON_ERROR_CTRL_CHAR, 'Unexpected control character found'),
316 'default' => array(-1, 'Unknown error'),
317 );
318 }

◆ tearDown()

Monolog\Formatter\NormalizerFormatterTest::tearDown ( )

Definition at line 19 of file NormalizerFormatterTest.php.

20 {
21 \PHPUnit_Framework_Error_Warning::$enabled = true;
22
23 return parent::tearDown();
24 }

◆ testBatchFormat()

Monolog\Formatter\NormalizerFormatterTest::testBatchFormat ( )

Definition at line 124 of file NormalizerFormatterTest.php.

125 {
126 $formatter = new NormalizerFormatter('Y-m-d');
127 $formatted = $formatter->formatBatch(array(
128 array(
129 'level_name' => 'CRITICAL',
130 'channel' => 'test',
131 'message' => 'bar',
132 'context' => array(),
133 'datetime' => new \DateTime,
134 'extra' => array(),
135 ),
136 array(
137 'level_name' => 'WARNING',
138 'channel' => 'log',
139 'message' => 'foo',
140 'context' => array(),
141 'datetime' => new \DateTime,
142 'extra' => array(),
143 ),
144 ));
145 $this->assertEquals(array(
146 array(
147 'level_name' => 'CRITICAL',
148 'channel' => 'test',
149 'message' => 'bar',
150 'context' => array(),
151 'datetime' => date('Y-m-d'),
152 'extra' => array(),
153 ),
154 array(
155 'level_name' => 'WARNING',
156 'channel' => 'log',
157 'message' => 'foo',
158 'context' => array(),
159 'datetime' => date('Y-m-d'),
160 'extra' => array(),
161 ),
162 ), $formatted);
163 }
date( 'd-M-Y', $objPHPExcel->getProperties() ->getCreated())

References date.

◆ testConvertsInvalidEncodingAsLatin9()

Monolog\Formatter\NormalizerFormatterTest::testConvertsInvalidEncodingAsLatin9 ( )

Definition at line 242 of file NormalizerFormatterTest.php.

243 {
244 if (version_compare(PHP_VERSION, '5.5.0', '<')) {
245 // Ignore the warning that will be emitted by PHP <5.5.0
246 \PHPUnit_Framework_Error_Warning::$enabled = false;
247 }
248 $formatter = new NormalizerFormatter();
249 $reflMethod = new \ReflectionMethod($formatter, 'toJson');
250 $reflMethod->setAccessible(true);
251
252 $res = $reflMethod->invoke($formatter, array('message' => "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE"));
253
254 if (version_compare(PHP_VERSION, '5.5.0', '>=')) {
255 $this->assertSame('{"message":"€ŠšŽžŒœŸ"}', $res);
256 } else {
257 // PHP <5.5 does not return false for an element encoding failure,
258 // instead it emits a warning (possibly) and nulls the value.
259 $this->assertSame('{"message":null}', $res);
260 }
261 }
foreach($_POST as $key=> $value) $res

References $res.

◆ testDetectAndCleanUtf8()

Monolog\Formatter\NormalizerFormatterTest::testDetectAndCleanUtf8 (   $in,
  $expect 
)
Parameters
mixed$inInput
mixed$expectExpected output @covers Monolog\Formatter\NormalizerFormatter::detectAndCleanUtf8 @dataProvider providesDetectAndCleanUtf8

Definition at line 269 of file NormalizerFormatterTest.php.

270 {
271 $formatter = new NormalizerFormatter();
272 $formatter->detectAndCleanUtf8($in);
273 $this->assertSame($expect, $in);
274 }
if(php_sapi_name() !='cli') $in
Definition: Utf8Test.php:37

References $in.

◆ testExceptionTraceWithArgs()

Monolog\Formatter\NormalizerFormatterTest::testExceptionTraceWithArgs ( )

Definition at line 320 of file NormalizerFormatterTest.php.

321 {
322 if (defined('HHVM_VERSION')) {
323 $this->markTestSkipped('Not supported in HHVM since it detects errors differently');
324 }
325
326 // This happens i.e. in React promises or Guzzle streams where stream wrappers are registered
327 // and no file or line are included in the trace because it's treated as internal function
328 set_error_handler(function ($errno, $errstr, $errfile, $errline) {
329 throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
330 });
331
332 try {
333 // This will contain $resource and $wrappedResource as arguments in the trace item
334 $resource = fopen('php://memory', 'rw+');
335 fwrite($resource, 'test_resource');
336 $wrappedResource = new TestFooNorm;
337 $wrappedResource->foo = $resource;
338 // Just do something stupid with a resource/wrapped resource as argument
339 array_keys($wrappedResource);
340 } catch (\Exception $e) {
341 restore_error_handler();
342 }
343
344 $formatter = new NormalizerFormatter();
345 $record = array('context' => array('exception' => $e));
346 $result = $formatter->format($record);
347
348 $this->assertRegExp(
349 '%"resource":"\[resource\] \‍(stream\‍)"%',
350 $result['context']['exception']['trace'][0]
351 );
352
353 if (version_compare(PHP_VERSION, '5.5.0', '>=')) {
354 $pattern = '%"wrappedResource":"\[object\] \‍(Monolog\\\\\\\\Formatter\\\\\\\\TestFooNorm: \‍)"%';
355 } else {
356 $pattern = '%\\\\"foo\\\\":null%';
357 }
358
359 // Tests that the wrapped resource is ignored while encoding, only works for PHP <= 5.4
360 $this->assertRegExp(
361 $pattern,
362 $result['context']['exception']['trace'][0]
363 );
364 }
$result
defined( 'APPLICATION_ENV')||define( 'APPLICATION_ENV'
Definition: bootstrap.php:27

References $result, and defined.

◆ testFormat()

Monolog\Formatter\NormalizerFormatterTest::testFormat ( )

Definition at line 26 of file NormalizerFormatterTest.php.

27 {
28 $formatter = new NormalizerFormatter('Y-m-d');
29 $formatted = $formatter->format(array(
30 'level_name' => 'ERROR',
31 'channel' => 'meh',
32 'message' => 'foo',
33 'datetime' => new \DateTime,
34 'extra' => array('foo' => new TestFooNorm, 'bar' => new TestBarNorm, 'baz' => array(), 'res' => fopen('php://memory', 'rb')),
35 'context' => array(
36 'foo' => 'bar',
37 'baz' => 'qux',
38 'inf' => INF,
39 '-inf' => -INF,
40 'nan' => acos(4),
41 ),
42 ));
43
44 $this->assertEquals(array(
45 'level_name' => 'ERROR',
46 'channel' => 'meh',
47 'message' => 'foo',
48 'datetime' => date('Y-m-d'),
49 'extra' => array(
50 'foo' => '[object] (Monolog\\Formatter\\TestFooNorm: {"foo":"foo"})',
51 'bar' => '[object] (Monolog\\Formatter\\TestBarNorm: bar)',
52 'baz' => array(),
53 'res' => '[resource] (stream)',
54 ),
55 'context' => array(
56 'foo' => 'bar',
57 'baz' => 'qux',
58 'inf' => 'INF',
59 '-inf' => '-INF',
60 'nan' => 'NaN',
61 ),
62 ), $formatted);
63 }

References date.

◆ testFormatExceptions()

Monolog\Formatter\NormalizerFormatterTest::testFormatExceptions ( )

Definition at line 65 of file NormalizerFormatterTest.php.

66 {
67 $formatter = new NormalizerFormatter('Y-m-d');
68 $e = new \LogicException('bar');
69 $e2 = new \RuntimeException('foo', 0, $e);
70 $formatted = $formatter->format(array(
71 'exception' => $e2,
72 ));
73
74 $this->assertGreaterThan(5, count($formatted['exception']['trace']));
75 $this->assertTrue(isset($formatted['exception']['previous']));
76 unset($formatted['exception']['trace'], $formatted['exception']['previous']);
77
78 $this->assertEquals(array(
79 'exception' => array(
80 'class' => get_class($e2),
81 'message' => $e2->getMessage(),
82 'code' => $e2->getCode(),
83 'file' => $e2->getFile().':'.$e2->getLine(),
84 ),
85 ), $formatted);
86 }

◆ testFormatSoapFaultException()

Monolog\Formatter\NormalizerFormatterTest::testFormatSoapFaultException ( )

Definition at line 88 of file NormalizerFormatterTest.php.

89 {
90 if (!class_exists('SoapFault')) {
91 $this->markTestSkipped('Requires the soap extension');
92 }
93
94 $formatter = new NormalizerFormatter('Y-m-d');
95 $e = new \SoapFault('foo', 'bar', 'hello', 'world');
96 $formatted = $formatter->format(array(
97 'exception' => $e,
98 ));
99
100 unset($formatted['exception']['trace']);
101
102 $this->assertEquals(array(
103 'exception' => array(
104 'class' => 'SoapFault',
105 'message' => 'bar',
106 'code' => 0,
107 'file' => $e->getFile().':'.$e->getLine(),
108 'faultcode' => 'foo',
109 'faultactor' => 'hello',
110 'detail' => 'world',
111 ),
112 ), $formatted);
113 }

◆ testFormatToStringExceptionHandle()

Monolog\Formatter\NormalizerFormatterTest::testFormatToStringExceptionHandle ( )

Definition at line 115 of file NormalizerFormatterTest.php.

116 {
117 $formatter = new NormalizerFormatter('Y-m-d');
118 $this->setExpectedException('RuntimeException', 'Could not convert to string');
119 $formatter->format(array(
120 'myObject' => new TestToStringError(),
121 ));
122 }

◆ testHandleJsonErrorFailure()

Monolog\Formatter\NormalizerFormatterTest::testHandleJsonErrorFailure (   $code,
  $msg 
)
Parameters
int$code
string$msg@dataProvider providesHandleJsonErrorFailure

Definition at line 300 of file NormalizerFormatterTest.php.

301 {
302 $formatter = new NormalizerFormatter();
303 $reflMethod = new \ReflectionMethod($formatter, 'handleJsonError');
304 $reflMethod->setAccessible(true);
305
306 $this->setExpectedException('RuntimeException', $msg);
307 $reflMethod->invoke($formatter, $code, 'faked');
308 }
$code
Definition: example_050.php:99

References $code.

◆ testIgnoresInvalidTypes()

Monolog\Formatter\NormalizerFormatterTest::testIgnoresInvalidTypes ( )

Definition at line 196 of file NormalizerFormatterTest.php.

197 {
198 // set up the recursion
199 $resource = fopen(__FILE__, 'r');
200
201 // set an error handler to assert that the error is not raised anymore
202 $that = $this;
203 set_error_handler(function ($level, $message, $file, $line, $context) use ($that) {
204 if (error_reporting() & $level) {
205 restore_error_handler();
206 $that->fail("$message should not be raised");
207 }
208 });
209
210 $formatter = new NormalizerFormatter();
211 $reflMethod = new \ReflectionMethod($formatter, 'toJson');
212 $reflMethod->setAccessible(true);
213 $res = $reflMethod->invoke($formatter, array($resource), true);
214
215 restore_error_handler();
216
217 $this->assertEquals(@json_encode(array($resource)), $res);
218 }
catch(Exception $e) $message
if(!file_exists("$old.txt")) if( $old===$new) if(file_exists("$new.txt")) $file

References $file, $message, and $res.

◆ testIgnoresRecursiveObjectReferences()

Monolog\Formatter\NormalizerFormatterTest::testIgnoresRecursiveObjectReferences ( )

Test issue #137.

Definition at line 168 of file NormalizerFormatterTest.php.

169 {
170 // set up the recursion
171 $foo = new \stdClass();
172 $bar = new \stdClass();
173
174 $foo->bar = $bar;
175 $bar->foo = $foo;
176
177 // set an error handler to assert that the error is not raised anymore
178 $that = $this;
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");
183 }
184 });
185
186 $formatter = new NormalizerFormatter();
187 $reflMethod = new \ReflectionMethod($formatter, 'toJson');
188 $reflMethod->setAccessible(true);
189 $res = $reflMethod->invoke($formatter, array($foo, $bar), true);
190
191 restore_error_handler();
192
193 $this->assertEquals(@json_encode(array($foo, $bar)), $res);
194 }

References $file, $message, and $res.

◆ testThrowsOnInvalidEncoding()

Monolog\Formatter\NormalizerFormatterTest::testThrowsOnInvalidEncoding ( )

@expectedException RuntimeException

Definition at line 223 of file NormalizerFormatterTest.php.

224 {
225 if (version_compare(PHP_VERSION, '5.5.0', '<')) {
226 // Ignore the warning that will be emitted by PHP <5.5.0
227 \PHPUnit_Framework_Error_Warning::$enabled = false;
228 }
229 $formatter = new NormalizerFormatter();
230 $reflMethod = new \ReflectionMethod($formatter, 'toJson');
231 $reflMethod->setAccessible(true);
232
233 // send an invalid unicode sequence as a object that can't be cleaned
234 $record = new \stdClass;
235 $record->message = "\xB1\x31";
236 $res = $reflMethod->invoke($formatter, $record);
237 if (PHP_VERSION_ID < 50500 && $res === '{"message":null}') {
238 throw new \RuntimeException('PHP 5.3/5.4 throw a warning and null the value instead of returning false entirely');
239 }
240 }

References $res.


The documentation for this class was generated from the following file: