Merge "rdbms: avoid LoadBalancer::getConnection waste when using $groups"
[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 * @covers MediaWiki\Logger\Monolog\LogstashFormatter::formatV1
9 * @param array $record The input record.
10 * @param array $expected Associative array of expected keys and their values.
11 * @param array $notExpected List of keys that should not exist.
12 */
13 public function testV1( array $record, array $expected, array $notExpected ) {
14 $formatter = new LogstashFormatter( 'app', 'system', null, null, LogstashFormatter::V1 );
15 $formatted = json_decode( $formatter->format( $record ), true );
16 foreach ( $expected as $key => $value ) {
17 $this->assertArrayHasKey( $key, $formatted );
18 $this->assertSame( $value, $formatted[$key] );
19 }
20 foreach ( $notExpected as $key ) {
21 $this->assertArrayNotHasKey( $key, $formatted );
22 }
23 }
24
25 public function provideV1() {
26 return [
27 [
28 [ 'extra' => [ 'foo' => 1 ], 'context' => [ 'bar' => 2 ] ],
29 [ 'foo' => 1, 'bar' => 2 ],
30 [ 'logstash_formatter_key_conflict' ],
31 ],
32 [
33 [ 'extra' => [ 'url' => 1 ], 'context' => [ 'url' => 2 ] ],
34 [ 'url' => 1, 'c_url' => 2, 'logstash_formatter_key_conflict' => [ 'url' ] ],
35 [],
36 ],
37 [
38 [ 'channel' => 'x', 'context' => [ 'channel' => 'y' ] ],
39 [ 'channel' => 'x', 'c_channel' => 'y',
40 'logstash_formatter_key_conflict' => [ 'channel' ] ],
41 [],
42 ],
43 ];
44 }
45
46 /**
47 * @covers MediaWiki\Logger\Monolog\LogstashFormatter::formatV1
48 */
49 public function testV1WithPrefix() {
50 $formatter = new LogstashFormatter( 'app', 'system', null, 'ctx_', LogstashFormatter::V1 );
51 $record = [ 'extra' => [ 'url' => 1 ], 'context' => [ 'url' => 2 ] ];
52 $formatted = json_decode( $formatter->format( $record ), true );
53 $this->assertArrayHasKey( 'url', $formatted );
54 $this->assertSame( 1, $formatted['url'] );
55 $this->assertArrayHasKey( 'ctx_url', $formatted );
56 $this->assertSame( 2, $formatted['ctx_url'] );
57 $this->assertArrayNotHasKey( 'c_url', $formatted );
58 }
59 }