summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/debug/logger
diff options
context:
space:
mode:
Diffstat (limited to 'tests/phpunit/includes/debug/logger')
-rw-r--r--tests/phpunit/includes/debug/logger/LegacyLoggerTest.php175
-rw-r--r--tests/phpunit/includes/debug/logger/MonologSpiTest.php136
-rw-r--r--tests/phpunit/includes/debug/logger/monolog/AvroFormatterTest.php64
-rw-r--r--tests/phpunit/includes/debug/logger/monolog/KafkaHandlerTest.php207
-rw-r--r--tests/phpunit/includes/debug/logger/monolog/LineFormatterTest.php75
5 files changed, 657 insertions, 0 deletions
diff --git a/tests/phpunit/includes/debug/logger/LegacyLoggerTest.php b/tests/phpunit/includes/debug/logger/LegacyLoggerTest.php
new file mode 100644
index 00000000..1b3ce2ca
--- /dev/null
+++ b/tests/phpunit/includes/debug/logger/LegacyLoggerTest.php
@@ -0,0 +1,175 @@
+<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
+namespace MediaWiki\Logger;
+
+use MediaWikiTestCase;
+use Psr\Log\LogLevel;
+
+class LegacyLoggerTest extends MediaWikiTestCase {
+
+ /**
+ * @covers LegacyLogger::interpolate
+ * @dataProvider provideInterpolate
+ */
+ public function testInterpolate( $message, $context, $expect ) {
+ $this->assertEquals(
+ $expect, LegacyLogger::interpolate( $message, $context ) );
+ }
+
+ public function provideInterpolate() {
+ $e = new \Exception( 'boom!' );
+ $d = new \DateTime();
+ return array(
+ array(
+ 'no-op',
+ array(),
+ 'no-op',
+ ),
+ array(
+ 'Hello {world}!',
+ array(
+ 'world' => 'World',
+ ),
+ 'Hello World!',
+ ),
+ array(
+ '{greeting} {user}',
+ array(
+ 'greeting' => 'Goodnight',
+ 'user' => 'Moon',
+ ),
+ 'Goodnight Moon',
+ ),
+ array(
+ 'Oops {key_not_set}',
+ array(),
+ 'Oops {key_not_set}',
+ ),
+ array(
+ '{ not interpolated }',
+ array(
+ 'not interpolated' => 'This should NOT show up in the message',
+ ),
+ '{ not interpolated }',
+ ),
+ array(
+ '{null}',
+ array(
+ 'null' => null,
+ ),
+ '[Null]',
+ ),
+ array(
+ '{bool}',
+ array(
+ 'bool' => true,
+ ),
+ 'true',
+ ),
+ array(
+ '{float}',
+ array(
+ 'float' => 1.23,
+ ),
+ '1.23',
+ ),
+ array(
+ '{array}',
+ array(
+ 'array' => array( 1, 2, 3 ),
+ ),
+ '[Array(3)]',
+ ),
+ array(
+ '{exception}',
+ array(
+ 'exception' => $e,
+ ),
+ '[Exception ' . get_class( $e ) . '( ' .
+ $e->getFile() . ':' . $e->getLine() . ') ' .
+ $e->getMessage() . ']',
+ ),
+ array(
+ '{datetime}',
+ array(
+ 'datetime' => $d,
+ ),
+ $d->format( 'c' ),
+ ),
+ array(
+ '{object}',
+ array(
+ 'object' => new \stdClass,
+ ),
+ '[Object stdClass]',
+ ),
+ );
+ }
+
+ /**
+ * @covers LegacyLogger::shouldEmit
+ * @dataProvider provideShouldEmit
+ */
+ public function testShouldEmit( $level, $config, $expected ) {
+ $this->setMwGlobals( 'wgDebugLogGroups', array( 'fakechannel' => $config ) );
+ $this->assertEquals(
+ $expected,
+ LegacyLogger::shouldEmit( 'fakechannel', 'some message', $level, array() )
+ );
+ }
+
+ public static function provideShouldEmit() {
+ $dest = array( 'destination' => 'foobar' );
+ $tests = array(
+ array(
+ LogLevel::DEBUG,
+ $dest,
+ true
+ ),
+ array(
+ LogLevel::WARNING,
+ $dest + array( 'level' => LogLevel::INFO ),
+ true,
+ ),
+ array(
+ LogLevel::INFO,
+ $dest + array( 'level' => LogLevel::CRITICAL ),
+ false,
+ ),
+ );
+
+ if ( class_exists( '\Monolog\Logger' ) ) {
+ $tests[] = array(
+ \Monolog\Logger::INFO,
+ $dest + array( 'level' => LogLevel::INFO ),
+ true,
+ );
+ $tests[] = array(
+ \Monolog\Logger::WARNING,
+ $dest + array( 'level' => LogLevel::EMERGENCY ),
+ false,
+ );
+ }
+
+ return $tests;
+ }
+
+}
diff --git a/tests/phpunit/includes/debug/logger/MonologSpiTest.php b/tests/phpunit/includes/debug/logger/MonologSpiTest.php
new file mode 100644
index 00000000..aa0a54ff
--- /dev/null
+++ b/tests/phpunit/includes/debug/logger/MonologSpiTest.php
@@ -0,0 +1,136 @@
+<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
+namespace MediaWiki\Logger;
+
+use MediaWikiTestCase;
+use TestingAccessWrapper;
+
+class MonologSpiTest extends MediaWikiTestCase {
+
+ /**
+ * @covers MonologSpi::mergeConfig
+ */
+ public function testMergeConfig() {
+ $base = array(
+ 'loggers' => array(
+ '@default' => array(
+ 'processors' => array( 'constructor' ),
+ 'handlers' => array( 'constructor' ),
+ ),
+ ),
+ 'processors' => array(
+ 'constructor' => array(
+ 'class' => 'constructor',
+ ),
+ ),
+ 'handlers' => array(
+ 'constructor' => array(
+ 'class' => 'constructor',
+ 'formatter' => 'constructor',
+ ),
+ ),
+ 'formatters' => array(
+ 'constructor' => array(
+ 'class' => 'constructor',
+ ),
+ ),
+ );
+
+ $fixture = new MonologSpi( $base );
+ $this->assertSame(
+ $base,
+ TestingAccessWrapper::newFromObject( $fixture )->config
+ );
+
+ $fixture->mergeConfig( array(
+ 'loggers' => array(
+ 'merged' => array(
+ 'processors' => array( 'merged' ),
+ 'handlers' => array( 'merged' ),
+ ),
+ ),
+ 'processors' => array(
+ 'merged' => array(
+ 'class' => 'merged',
+ ),
+ ),
+ 'magic' => array(
+ 'idkfa' => array( 'xyzzy' ),
+ ),
+ 'handlers' => array(
+ 'merged' => array(
+ 'class' => 'merged',
+ 'formatter' => 'merged',
+ ),
+ ),
+ 'formatters' => array(
+ 'merged' => array(
+ 'class' => 'merged',
+ ),
+ ),
+ ) );
+ $this->assertSame(
+ array(
+ 'loggers' => array(
+ '@default' => array(
+ 'processors' => array( 'constructor' ),
+ 'handlers' => array( 'constructor' ),
+ ),
+ 'merged' => array(
+ 'processors' => array( 'merged' ),
+ 'handlers' => array( 'merged' ),
+ ),
+ ),
+ 'processors' => array(
+ 'constructor' => array(
+ 'class' => 'constructor',
+ ),
+ 'merged' => array(
+ 'class' => 'merged',
+ ),
+ ),
+ 'handlers' => array(
+ 'constructor' => array(
+ 'class' => 'constructor',
+ 'formatter' => 'constructor',
+ ),
+ 'merged' => array(
+ 'class' => 'merged',
+ 'formatter' => 'merged',
+ ),
+ ),
+ 'formatters' => array(
+ 'constructor' => array(
+ 'class' => 'constructor',
+ ),
+ 'merged' => array(
+ 'class' => 'merged',
+ ),
+ ),
+ 'magic' => array(
+ 'idkfa' => array( 'xyzzy' ),
+ ),
+ ),
+ TestingAccessWrapper::newFromObject( $fixture )->config
+ );
+ }
+
+}
diff --git a/tests/phpunit/includes/debug/logger/monolog/AvroFormatterTest.php b/tests/phpunit/includes/debug/logger/monolog/AvroFormatterTest.php
new file mode 100644
index 00000000..44242ed2
--- /dev/null
+++ b/tests/phpunit/includes/debug/logger/monolog/AvroFormatterTest.php
@@ -0,0 +1,64 @@
+<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
+namespace MediaWiki\Logger\Monolog;
+
+use MediaWikiTestCase;
+use PHPUnit_Framework_Error_Notice;
+
+class AvroFormatterTest extends MediaWikiTestCase {
+
+ protected function setUp() {
+ if ( !class_exists( 'AvroStringIO' ) ) {
+ $this->markTestSkipped( 'Avro is required for the AvroFormatterTest' );
+ }
+ parent::setUp();
+ }
+
+ public function testSchemaNotAvailable() {
+ $formatter = new AvroFormatter( array() );
+ $this->setExpectedException( 'PHPUnit_Framework_Error_Notice', "The schema for channel 'marty' is not available" );
+ $formatter->format( array( 'channel' => 'marty' ) );
+ }
+
+ public function testSchemaNotAvailableReturnValue() {
+ $formatter = new AvroFormatter( array() );
+ $noticeEnabled = PHPUnit_Framework_Error_Notice::$enabled;
+ // disable conversion of notices
+ PHPUnit_Framework_Error_Notice::$enabled = false;
+ // have to keep the user notice from being output
+ wfSuppressWarnings();
+ $res = $formatter->format( array( 'channel' => 'marty' ) );
+ wfRestoreWarnings();
+ PHPUnit_Framework_Error_Notice::$enabled = $noticeEnabled;
+ $this->assertNull( $res );
+ }
+
+ public function testDoesSomethingWhenSchemaAvailable() {
+ $formatter = new AvroFormatter( array( 'string' => array( 'type' => 'string' ) ) );
+ $res = $formatter->format( array(
+ 'channel' => 'string',
+ 'context' => 'better to be',
+ ) );
+ $this->assertNotNull( $res );
+ // basically just tell us if avro changes its string encoding
+ $this->assertEquals( base64_decode( 'GGJldHRlciB0byBiZQ==' ), $res );
+ }
+}
diff --git a/tests/phpunit/includes/debug/logger/monolog/KafkaHandlerTest.php b/tests/phpunit/includes/debug/logger/monolog/KafkaHandlerTest.php
new file mode 100644
index 00000000..090f439e
--- /dev/null
+++ b/tests/phpunit/includes/debug/logger/monolog/KafkaHandlerTest.php
@@ -0,0 +1,207 @@
+<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
+namespace MediaWiki\Logger\Monolog;
+
+use MediaWikiTestCase;
+use Monolog\Logger;
+
+// not available in the version of phpunit mw uses, so copied into repo
+require_once __DIR__ . '/../../../ConsecutiveParametersMatcher.php';
+
+class KafkaHandlerTest extends MediaWikiTestCase {
+
+ protected function setUp() {
+ if ( !class_exists( 'Monolog\Handler\AbstractProcessingHandler' )
+ || !class_exists( 'Kafka\Produce' )
+ ) {
+ $this->markTestSkipped( 'Monolog and Kafka are required for the KafkaHandlerTest' );
+ }
+
+ parent::setUp();
+ }
+
+ public function topicNamingProvider() {
+ return array(
+ array( array(), 'monolog_foo' ),
+ array( array( 'alias' => array( 'foo' => 'bar' ) ), 'bar' )
+ );
+ }
+
+ /**
+ * @dataProvider topicNamingProvider
+ */
+ public function testTopicNaming( $options, $expect ) {
+ $produce = $this->getMockBuilder( 'Kafka\Produce' )
+ ->disableOriginalConstructor()
+ ->getMock();
+ $produce->expects($this->any())
+ ->method('getAvailablePartitions')
+ ->will($this->returnValue( array( 'A' ) ) );
+ $produce->expects($this->once())
+ ->method( 'setMessages' )
+ ->with( $expect, $this->anything(), $this->anything() );
+
+ $handler = new KafkaHandler( $produce, $options );
+ $handler->handle( array(
+ 'channel' => 'foo',
+ 'level' => Logger::EMERGENCY,
+ 'extra' => array(),
+ ) );
+ }
+
+ public function swallowsExceptionsWhenRequested() {
+ return array(
+ // defaults to false
+ array( array(), true ),
+ // also try false explicitly
+ array( array( 'swallowExceptions' => false ), true ),
+ // turn it on
+ array( array( 'swallowExceptions' => true ), false ),
+ );
+ }
+
+ /**
+ * @dataProvider swallowsExceptionsWhenRequested
+ */
+ public function testGetAvailablePartitionsException( $options, $expectException ) {
+ $produce = $this->getMockBuilder( 'Kafka\Produce' )
+ ->disableOriginalConstructor()
+ ->getMock();
+ $produce->expects( $this->any() )
+ ->method( 'getAvailablePartitions' )
+ ->will( $this->throwException( new \Kafka\Exception ) );
+
+ if ( $expectException ) {
+ $this->setExpectedException( 'Kafka\Exception' );
+ }
+
+ $handler = new KafkaHandler( $produce, $options );
+ $handler->handle( array(
+ 'channel' => 'foo',
+ 'level' => Logger::EMERGENCY,
+ 'extra' => array(),
+ ) );
+
+ if ( !$expectException ) {
+ $this->assertTrue( true, 'no exception was thrown' );
+ }
+ }
+
+ /**
+ * @dataProvider swallowsExceptionsWhenRequested
+ */
+ public function testSendException( $options, $expectException ) {
+ $produce = $this->getMockBuilder( 'Kafka\Produce' )
+ ->disableOriginalConstructor()
+ ->getMock();
+ $produce->expects( $this->any() )
+ ->method( 'getAvailablePartitions' )
+ ->will( $this->returnValue( array( 'A' ) ) );
+ $produce->expects( $this->any() )
+ ->method( 'send' )
+ ->will( $this->throwException( new \Kafka\Exception ) );
+
+ if ( $expectException ) {
+ $this->setExpectedException( 'Kafka\Exception' );
+ }
+
+ $handler = new KafkaHandler( $produce, $options );
+ $handler->handle( array(
+ 'channel' => 'foo',
+ 'level' => Logger::EMERGENCY,
+ 'extra' => array(),
+ ) );
+
+ if ( !$expectException ) {
+ $this->assertTrue( true, 'no exception was thrown' );
+ }
+ }
+
+ public function testHandlesNullFormatterResult() {
+ $produce = $this->getMockBuilder( 'Kafka\Produce' )
+ ->disableOriginalConstructor()
+ ->getMock();
+ $produce->expects( $this->any() )
+ ->method( 'getAvailablePartitions' )
+ ->will( $this->returnValue( array( 'A' ) ) );
+ $mockMethod = $produce->expects( $this->exactly( 2 ) )
+ ->method( 'setMessages' );
+ // evil hax
+ \TestingAccessWrapper::newFromObject( $mockMethod )->matcher->parametersMatcher =
+ new \PHPUnit_Framework_MockObject_Matcher_ConsecutiveParameters( array(
+ array( $this->anything(), $this->anything(), array( 'words' ) ),
+ array( $this->anything(), $this->anything(), array( 'lines' ) )
+ ) );
+
+ $formatter = $this->getMock( 'Monolog\Formatter\FormatterInterface' );
+ $formatter->expects( $this->any() )
+ ->method( 'format' )
+ ->will( $this->onConsecutiveCalls( 'words', null, 'lines' ) );
+
+ $handler = new KafkaHandler( $produce, array() );
+ $handler->setFormatter( $formatter );
+ for ( $i = 0; $i < 3; ++$i ) {
+ $handler->handle( array(
+ 'channel' => 'foo',
+ 'level' => Logger::EMERGENCY,
+ 'extra' => array(),
+ ) );
+ }
+ }
+
+
+ public function testBatchHandlesNullFormatterResult() {
+ $produce = $this->getMockBuilder( 'Kafka\Produce' )
+ ->disableOriginalConstructor()
+ ->getMock();
+ $produce->expects( $this->any() )
+ ->method( 'getAvailablePartitions' )
+ ->will( $this->returnValue( array( 'A' ) ) );
+ $produce->expects( $this->once() )
+ ->method( 'setMessages' )
+ ->with( $this->anything(), $this->anything(), array( 'words', 'lines' ) );
+
+ $formatter = $this->getMock( 'Monolog\Formatter\FormatterInterface' );
+ $formatter->expects( $this->any() )
+ ->method( 'format' )
+ ->will( $this->onConsecutiveCalls( 'words', null, 'lines' ) );
+
+ $handler = new KafkaHandler( $produce, array() );
+ $handler->setFormatter( $formatter );
+ $handler->handleBatch( array(
+ array(
+ 'channel' => 'foo',
+ 'level' => Logger::EMERGENCY,
+ 'extra' => array(),
+ ),
+ array(
+ 'channel' => 'foo',
+ 'level' => Logger::EMERGENCY,
+ 'extra' => array(),
+ ),
+ array(
+ 'channel' => 'foo',
+ 'level' => Logger::EMERGENCY,
+ 'extra' => array(),
+ ),
+ ) );
+ }
+}
diff --git a/tests/phpunit/includes/debug/logger/monolog/LineFormatterTest.php b/tests/phpunit/includes/debug/logger/monolog/LineFormatterTest.php
new file mode 100644
index 00000000..be23c4a2
--- /dev/null
+++ b/tests/phpunit/includes/debug/logger/monolog/LineFormatterTest.php
@@ -0,0 +1,75 @@
+<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
+namespace MediaWiki\Logger\Monolog;
+
+use InvalidArgumentException;
+use LengthException;
+use LogicException;
+use MediaWikiTestCase;
+use TestingAccessWrapper;
+
+class LineFormatterTest extends MediaWikiTestCase {
+
+ protected function setUp() {
+ if ( !class_exists( 'Monolog\Formatter\LineFormatter' ) ) {
+ $this->markTestSkipped( 'This test requires monolog to be installed' );
+ }
+ parent::setUp();
+ }
+
+ /**
+ * @covers LineFormatter::normalizeException
+ */
+ public function testNormalizeExceptionNoTrace() {
+ $fixture = new LineFormatter();
+ $fixture->includeStacktraces( false );
+ $fixture = TestingAccessWrapper::newFromObject( $fixture );
+ $boom = new InvalidArgumentException( 'boom', 0,
+ new LengthException( 'too long', 0,
+ new LogicException( 'Spock wuz here' )
+ )
+ );
+ $out = $fixture->normalizeException( $boom );
+ $this->assertContains( "\n[Exception InvalidArgumentException]", $out );
+ $this->assertContains( "\nCaused by: [Exception LengthException]", $out );
+ $this->assertContains( "\nCaused by: [Exception LogicException]", $out );
+ $this->assertNotContains( "\n #0", $out );
+ }
+
+ /**
+ * @covers LineFormatter::normalizeException
+ */
+ public function testNormalizeExceptionTrace() {
+ $fixture = new LineFormatter();
+ $fixture->includeStacktraces( true );
+ $fixture = TestingAccessWrapper::newFromObject( $fixture );
+ $boom = new InvalidArgumentException( 'boom', 0,
+ new LengthException( 'too long', 0,
+ new LogicException( 'Spock wuz here' )
+ )
+ );
+ $out = $fixture->normalizeException( $boom );
+ $this->assertContains( "\n[Exception InvalidArgumentException]", $out );
+ $this->assertContains( "\nCaused by: [Exception LengthException]", $out );
+ $this->assertContains( "\nCaused by: [Exception LogicException]", $out );
+ $this->assertContains( "\n #0", $out );
+ }
+}