ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
MongoDBFormatterTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the Monolog package.
5  *
6  * (c) Jordi Boggiano <j.boggiano@seld.be>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
12 namespace Monolog\Formatter;
13 
14 use Monolog\Logger;
15 
20 {
21  public function setUp()
22  {
23  if (!class_exists('MongoDate')) {
24  $this->markTestSkipped('mongo extension not installed');
25  }
26  }
27 
28  public function constructArgumentProvider()
29  {
30  return array(
31  array(1, true, 1, true),
32  array(0, false, 0, false),
33  );
34  }
35 
44  public function testConstruct($traceDepth, $traceAsString, $expectedTraceDepth, $expectedTraceAsString)
45  {
46  $formatter = new MongoDBFormatter($traceDepth, $traceAsString);
47 
48  $reflTrace = new \ReflectionProperty($formatter, 'exceptionTraceAsString');
49  $reflTrace->setAccessible(true);
50  $this->assertEquals($expectedTraceAsString, $reflTrace->getValue($formatter));
51 
52  $reflDepth = new\ReflectionProperty($formatter, 'maxNestingLevel');
53  $reflDepth->setAccessible(true);
54  $this->assertEquals($expectedTraceDepth, $reflDepth->getValue($formatter));
55  }
56 
57  public function testSimpleFormat()
58  {
59  $record = array(
60  'message' => 'some log message',
61  'context' => array(),
62  'level' => Logger::WARNING,
63  'level_name' => Logger::getLevelName(Logger::WARNING),
64  'channel' => 'test',
65  'datetime' => new \DateTime('2014-02-01 00:00:00'),
66  'extra' => array(),
67  );
68 
69  $formatter = new MongoDBFormatter();
70  $formattedRecord = $formatter->format($record);
71 
72  $this->assertCount(7, $formattedRecord);
73  $this->assertEquals('some log message', $formattedRecord['message']);
74  $this->assertEquals(array(), $formattedRecord['context']);
75  $this->assertEquals(Logger::WARNING, $formattedRecord['level']);
76  $this->assertEquals(Logger::getLevelName(Logger::WARNING), $formattedRecord['level_name']);
77  $this->assertEquals('test', $formattedRecord['channel']);
78  $this->assertInstanceOf('\MongoDate', $formattedRecord['datetime']);
79  $this->assertEquals('0.00000000 1391212800', $formattedRecord['datetime']->__toString());
80  $this->assertEquals(array(), $formattedRecord['extra']);
81  }
82 
83  public function testRecursiveFormat()
84  {
85  $someObject = new \stdClass();
86  $someObject->foo = 'something';
87  $someObject->bar = 'stuff';
88 
89  $record = array(
90  'message' => 'some log message',
91  'context' => array(
92  'stuff' => new \DateTime('2014-02-01 02:31:33'),
93  'some_object' => $someObject,
94  'context_string' => 'some string',
95  'context_int' => 123456,
96  'except' => new \Exception('exception message', 987),
97  ),
98  'level' => Logger::WARNING,
99  'level_name' => Logger::getLevelName(Logger::WARNING),
100  'channel' => 'test',
101  'datetime' => new \DateTime('2014-02-01 00:00:00'),
102  'extra' => array(),
103  );
104 
105  $formatter = new MongoDBFormatter();
106  $formattedRecord = $formatter->format($record);
107 
108  $this->assertCount(5, $formattedRecord['context']);
109  $this->assertInstanceOf('\MongoDate', $formattedRecord['context']['stuff']);
110  $this->assertEquals('0.00000000 1391221893', $formattedRecord['context']['stuff']->__toString());
111  $this->assertEquals(
112  array(
113  'foo' => 'something',
114  'bar' => 'stuff',
115  'class' => 'stdClass',
116  ),
117  $formattedRecord['context']['some_object']
118  );
119  $this->assertEquals('some string', $formattedRecord['context']['context_string']);
120  $this->assertEquals(123456, $formattedRecord['context']['context_int']);
121 
122  $this->assertCount(5, $formattedRecord['context']['except']);
123  $this->assertEquals('exception message', $formattedRecord['context']['except']['message']);
124  $this->assertEquals(987, $formattedRecord['context']['except']['code']);
125  $this->assertInternalType('string', $formattedRecord['context']['except']['file']);
126  $this->assertInternalType('integer', $formattedRecord['context']['except']['code']);
127  $this->assertInternalType('string', $formattedRecord['context']['except']['trace']);
128  $this->assertEquals('Exception', $formattedRecord['context']['except']['class']);
129  }
130 
131  public function testFormatDepthArray()
132  {
133  $record = array(
134  'message' => 'some log message',
135  'context' => array(
136  'nest2' => array(
137  'property' => 'anything',
138  'nest3' => array(
139  'nest4' => 'value',
140  'property' => 'nothing',
141  ),
142  ),
143  ),
144  'level' => Logger::WARNING,
145  'level_name' => Logger::getLevelName(Logger::WARNING),
146  'channel' => 'test',
147  'datetime' => new \DateTime('2014-02-01 00:00:00'),
148  'extra' => array(),
149  );
150 
151  $formatter = new MongoDBFormatter(2);
152  $formattedResult = $formatter->format($record);
153 
154  $this->assertEquals(
155  array(
156  'nest2' => array(
157  'property' => 'anything',
158  'nest3' => '[...]',
159  ),
160  ),
161  $formattedResult['context']
162  );
163  }
164 
166  {
167  $record = array(
168  'message' => 'some log message',
169  'context' => array(
170  'nest2' => array(
171  'property' => 'something',
172  'nest3' => array(
173  'property' => 'anything',
174  'nest4' => array(
175  'property' => 'nothing',
176  ),
177  ),
178  ),
179  ),
180  'level' => Logger::WARNING,
181  'level_name' => Logger::getLevelName(Logger::WARNING),
182  'channel' => 'test',
183  'datetime' => new \DateTime('2014-02-01 00:00:00'),
184  'extra' => array(),
185  );
186 
187  $formatter = new MongoDBFormatter(0);
188  $formattedResult = $formatter->format($record);
189 
190  $this->assertEquals(
191  array(
192  'nest2' => array(
193  'property' => 'something',
194  'nest3' => array(
195  'property' => 'anything',
196  'nest4' => array(
197  'property' => 'nothing',
198  ),
199  ),
200  ),
201  ),
202  $formattedResult['context']
203  );
204  }
205 
206  public function testFormatDepthObjects()
207  {
208  $someObject = new \stdClass();
209  $someObject->property = 'anything';
210  $someObject->nest3 = new \stdClass();
211  $someObject->nest3->property = 'nothing';
212  $someObject->nest3->nest4 = 'invisible';
213 
214  $record = array(
215  'message' => 'some log message',
216  'context' => array(
217  'nest2' => $someObject,
218  ),
219  'level' => Logger::WARNING,
220  'level_name' => Logger::getLevelName(Logger::WARNING),
221  'channel' => 'test',
222  'datetime' => new \DateTime('2014-02-01 00:00:00'),
223  'extra' => array(),
224  );
225 
226  $formatter = new MongoDBFormatter(2, true);
227  $formattedResult = $formatter->format($record);
228 
229  $this->assertEquals(
230  array(
231  'nest2' => array(
232  'property' => 'anything',
233  'nest3' => '[...]',
234  'class' => 'stdClass',
235  ),
236  ),
237  $formattedResult['context']
238  );
239  }
240 
241  public function testFormatDepthException()
242  {
243  $record = array(
244  'message' => 'some log message',
245  'context' => array(
246  'nest2' => new \Exception('exception message', 987),
247  ),
248  'level' => Logger::WARNING,
249  'level_name' => Logger::getLevelName(Logger::WARNING),
250  'channel' => 'test',
251  'datetime' => new \DateTime('2014-02-01 00:00:00'),
252  'extra' => array(),
253  );
254 
255  $formatter = new MongoDBFormatter(2, false);
256  $formattedRecord = $formatter->format($record);
257 
258  $this->assertEquals('exception message', $formattedRecord['context']['nest2']['message']);
259  $this->assertEquals(987, $formattedRecord['context']['nest2']['code']);
260  $this->assertEquals('[...]', $formattedRecord['context']['nest2']['trace']);
261  }
262 }
Formats a record for use with the MongoDBHandler.
const WARNING
Exceptional occurrences that are not errors.
Definition: Logger.php:52
Create styles array
The data for the language used.
static getLevelName($level)
Gets the name of the logging level.
Definition: Logger.php:458
testConstruct($traceDepth, $traceAsString, $expectedTraceDepth, $expectedTraceAsString)