8086b4bf2303ef55321eff153bbf9b66802b6150
[lhc/web/wiklou.git] / tests / phpunit / includes / debug / logger / monolog / LogstashFormatterTest.php
1 <?php
2
3 namespace MediaWiki\Logger\Monolog;
4
5 class LogstashFormatterTest extends \PHPUnit_Framework_TestCase {
6 /**
7 * @dataProvider provideV1
8 * @param array $record The input record.
9 * @param array $expected Associative array of expected keys and their values.
10 * @param array $notExpected List of keys that should not exist.
11 */
12 public function testV1( array $record, array $expected, array $notExpected ) {
13 $formatter = new LogstashFormatter( 'app', 'system', null, null, LogstashFormatter::V1 );
14 $formatted = json_decode( $formatter->format( $record ), true );
15 foreach ( $expected as $key => $value ) {
16 $this->assertArrayHasKey( $key, $formatted );
17 $this->assertSame( $value, $formatted[$key] );
18 }
19 foreach ( $notExpected as $key ) {
20 $this->assertArrayNotHasKey( $key, $formatted );
21 }
22 }
23
24 public function provideV1() {
25 return [
26 [
27 [ 'extra' => [ 'foo' => 1 ], 'context' => [ 'bar' => 2 ] ],
28 [ 'foo' => 1, 'bar' => 2 ],
29 [ 'logstash_formatter_key_conflict' ],
30 ],
31 [
32 [ 'extra' => [ 'url' => 1 ], 'context' => [ 'url' => 2 ] ],
33 [ 'url' => 1, 'c_url' => 2, 'logstash_formatter_key_conflict' => [ 'url' ] ],
34 [],
35 ],
36 [
37 [ 'channel' => 'x', 'context' => [ 'channel' => 'y' ] ],
38 [ 'channel' => 'x', 'c_channel' => 'y',
39 'logstash_formatter_key_conflict' => [ 'channel' ] ],
40 [],
41 ],
42 ];
43 }
44
45 public function testV1WithPrefix() {
46 $formatter = new LogstashFormatter( 'app', 'system', null, 'ctx_', LogstashFormatter::V1 );
47 $record = [ 'extra' => [ 'url' => 1 ], 'context' => [ 'url' => 2 ] ];
48 $formatted = json_decode( $formatter->format( $record ), true );
49 $this->assertArrayHasKey( 'url', $formatted );
50 $this->assertSame( 1, $formatted['url'] );
51 $this->assertArrayHasKey( 'ctx_url', $formatted );
52 $this->assertSame( 2, $formatted['ctx_url'] );
53 $this->assertArrayNotHasKey( 'c_url', $formatted );
54 }
55 }