| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Logger\Monolog; |
| 4 |
|
| 5 |
use MediaWiki\Logger\Monolog\LogstashFormatter; |
| 6 |
|
| 7 |
class LogstashFormatterTest extends \MediaWikiUnitTestCase { |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
public function testV1( array $record, array $expected, array $notExpected ) { |
| 16 |
$formatter = new LogstashFormatter( 'app', 'system', '', '', LogstashFormatter::V1 ); |
| 17 |
$formatted = json_decode( $formatter->format( $record ), true ); |
| 18 |
foreach ( $expected as $key => $value ) { |
| 19 |
$this->assertArrayHasKey( $key, $formatted ); |
| 20 |
$this->assertSame( $value, $formatted[$key] ); |
| 21 |
} |
| 22 |
foreach ( $notExpected as $key ) { |
| 23 |
$this->assertArrayNotHasKey( $key, $formatted ); |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
public static function provideV1() { |
| 28 |
return [ |
| 29 |
[ |
| 30 |
[ 'extra' => [ 'foo' => 1 ], 'context' => [ 'bar' => 2 ] ], |
| 31 |
[ 'foo' => 1, 'bar' => 2 ], |
| 32 |
[ 'logstash_formatter_key_conflict' ], |
| 33 |
], |
| 34 |
[ |
| 35 |
[ 'extra' => [ 'url' => 1 ], 'context' => [ 'url' => 2 ] ], |
| 36 |
[ 'url' => 1, 'c_url' => 2, 'logstash_formatter_key_conflict' => [ 'url' ] ], |
| 37 |
[], |
| 38 |
], |
| 39 |
[ |
| 40 |
[ 'channel' => 'x', 'context' => [ 'channel' => 'y' ] ], |
| 41 |
[ 'channel' => 'x', 'c_channel' => 'y', |
| 42 |
'logstash_formatter_key_conflict' => [ 'channel' ] ], |
| 43 |
[], |
| 44 |
], |
| 45 |
]; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public function testV1WithPrefix() { |
| 52 |
$formatter = new LogstashFormatter( 'app', 'system', '', 'ctx_', LogstashFormatter::V1 ); |
| 53 |
$record = [ 'extra' => [ 'url' => 1 ], 'context' => [ 'url' => 2 ] ]; |
| 54 |
$formatted = json_decode( $formatter->format( $record ), true ); |
| 55 |
$this->assertArrayHasKey( 'url', $formatted ); |
| 56 |
$this->assertSame( 1, $formatted['url'] ); |
| 57 |
$this->assertArrayHasKey( 'ctx_url', $formatted ); |
| 58 |
$this->assertSame( 2, $formatted['ctx_url'] ); |
| 59 |
$this->assertArrayNotHasKey( 'c_url', $formatted ); |
| 60 |
} |
| 61 |
} |