MediaWiki REL1_31
LogstashFormatterTest.php
Go to the documentation of this file.
1<?php
2
4
5class LogstashFormatterTest extends \PHPUnit\Framework\TestCase {
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}
testV1(array $record, array $expected, array $notExpected)
provideV1
LogstashFormatter squashes the base message array and the context and extras subarrays into one.