Jeroen De Dauw has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/50042


Change subject: Adding MessageReporter interface
......................................................................

Adding MessageReporter interface

We do not have a nice message reporting interface yet, while
many places in both core and extensions have use for it.

The simple ObservableMessageReporter implementation also provided
can be used to easily hook up existing classes that allow reporting
messages though do not match the interface (for instance
Maintenance->output or Logger::log).

Change-Id: Ia93dae25b283b6c65988eef60ddc3f22c5f44715
---
M includes/AutoLoader.php
A includes/MessageReporter.php
A tests/phpunit/includes/ObservableMessageReporterTest.php
3 files changed, 321 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/42/50042/1

diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php
index 23cf411..6f724e7 100644
--- a/includes/AutoLoader.php
+++ b/includes/AutoLoader.php
@@ -172,6 +172,8 @@
        'MediaWiki_I18N' => 'includes/SkinTemplate.php',
        'Message' => 'includes/Message.php',
        'MessageBlobStore' => 'includes/MessageBlobStore.php',
+       'MessageReporter' => 'includes/MessageReporter.php',
+       'ObservableMessageReporter' => 'includes/MessageReporter.php',
        'MimeMagic' => 'includes/MimeMagic.php',
        'MWException' => 'includes/Exception.php',
        'MWExceptionHandler' => 'includes/Exception.php',
diff --git a/includes/MessageReporter.php b/includes/MessageReporter.php
new file mode 100644
index 0000000..8663e29
--- /dev/null
+++ b/includes/MessageReporter.php
@@ -0,0 +1,120 @@
+<?php
+
+/**
+ * Interface for objects that can report messages.
+ *
+ * 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
+ *
+ * @since 1.21
+ * @file
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < jeroended...@gmail.com >
+ */
+interface MessageReporter {
+
+       /**
+        * Report the provided message.
+        *
+        * @since 1.21
+        *
+        * @param string $message
+        */
+       public function reportMessage( $message );
+
+}
+
+/**
+ * Message reporter that reports messages by passing them along to all
+ * registered handlers.
+ *
+ * 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
+ *
+ * @since 1.21
+ * @file
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < jeroended...@gmail.com >
+ */
+class ObservableMessageReporter implements MessageReporter {
+
+       /**
+        * @since 1.21
+        *
+        * @var MessageReporter[]
+        */
+       protected $reporters = array();
+
+       /**
+        * @since 1.21
+        *
+        * @var callable[]
+        */
+       protected $callbacks = array();
+
+       /**
+        * @see MessageReporter::report
+        *
+        * @since 1.21
+        *
+        * @param string $message
+        */
+       public function reportMessage( $message ) {
+               foreach ( $this->reporters as $reporter ) {
+                       $reporter->reportMessage( $message );
+               }
+
+               foreach ( $this->callbacks as $callback ) {
+                       call_user_func( $callback, $message );
+               }
+       }
+
+       /**
+        * Register a new message reporter.
+        *
+        * @since 1.21
+        *
+        * @param MessageReporter $reporter
+        */
+       public function registerMessageReporter( MessageReporter $reporter ) {
+               $this->reporters[] = $reporter;
+       }
+
+       /**
+        * Register a callback as message reporter.
+        *
+        * @since 1.21
+        *
+        * @param callable $handler
+        */
+       public function registerReporterCallback( $handler ) {
+               $this->callbacks[] = $handler;
+       }
+
+}
diff --git a/tests/phpunit/includes/ObservableMessageReporterTest.php 
b/tests/phpunit/includes/ObservableMessageReporterTest.php
new file mode 100644
index 0000000..f16d191
--- /dev/null
+++ b/tests/phpunit/includes/ObservableMessageReporterTest.php
@@ -0,0 +1,199 @@
+<?php
+
+/**
+ * Tests for the ObservableMessageReporter class.
+ *
+ * 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
+ * @since 1.21
+ *
+ * @ingroup Test
+ *
+ * @group MessageReporter
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < jeroended...@gmail.com >
+ */
+class ObservableMessageReporterTest extends MessageReporterTest {
+
+       /**
+        * @return MessageReporter[]
+        */
+       public function getInstances() {
+               $instances = array();
+
+               $instances[] = new ObservableMessageReporter();
+
+               $reporter = new ObservableMessageReporter();
+               $reporter->registerMessageReporter( new 
ObservableMessageReporter() );
+               $callback0 = function( $string ) {};
+               $callback1 = function( $string ) {};
+               $instances[] = $reporter;
+
+               $reporter = clone $reporter;
+               $reporter->registerReporterCallback( $callback0 );
+               $reporter->registerReporterCallback( $callback1 );
+               $instances[] = $reporter;
+
+               return $instances;
+       }
+
+       /**
+        * @dataProvider reportMessageProvider
+        *
+        * @param string $message
+        */
+       public function testCallbackInvocation( $message ) {
+               $callCount = 0;
+               $asserter = array( $this, 'assertEquals' );
+
+               $callback0 = function( $actual ) use ( $message, &$callCount, 
$asserter ) {
+                       $callCount += 1;
+                       call_user_func( $asserter, $message, $actual );
+               };
+
+               $callback1 = function( $actual ) use ( $message, &$callCount, 
$asserter ) {
+                       $callCount += 1;
+                       call_user_func( $asserter, $message, $actual );
+               };
+
+               $reporter = new ObservableMessageReporter();
+               $reporter->registerReporterCallback( $callback0 );
+               $reporter->registerReporterCallback( $callback1 );
+
+               $reporter->reportMessage( $message );
+
+               $this->assertEquals( 2, $callCount );
+
+               $reporter->reportMessage( $message );
+
+               $this->assertEquals( 4, $callCount );
+       }
+
+       /**
+        * @dataProvider reportMessageProvider
+        *
+        * @param string $message
+        */
+       public function testReporterInvocation( $message ) {
+               $callCount = 0;
+               $asserter = array( $this, 'assertEquals' );
+
+               $callback0 = function( $actual ) use ( $message, &$callCount, 
$asserter ) {
+                       $callCount += 1;
+                       call_user_func( $asserter, $message, $actual );
+               };
+
+               $callback1 = function( $actual ) use ( $message, &$callCount, 
$asserter ) {
+                       $callCount += 1;
+                       call_user_func( $asserter, $message, $actual );
+               };
+
+               $reporter0 = new ObservableMessageReporter();
+               $reporter0->registerReporterCallback( $callback0 );
+
+               $reporter1 = new ObservableMessageReporter();
+               $reporter1->registerReporterCallback( $callback1 );
+
+               $reporter = new ObservableMessageReporter();
+               $reporter->registerMessageReporter( $reporter0 );
+               $reporter->registerMessageReporter( $reporter1 );
+
+               $reporter->reportMessage( $message );
+
+               $this->assertEquals( 2, $callCount );
+
+               $reporter->reportMessage( $message );
+
+               $this->assertEquals( 4, $callCount );
+       }
+
+}
+
+/**
+ * Tests for the MessageReporter interface.
+ *
+ * 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
+ * @since 1.21
+ *
+ * @ingroup Test
+ *
+ * @group MessageReporter
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < jeroended...@gmail.com >
+ */
+abstract class MessageReporterTest extends MediaWikiTestCase {
+
+       /**
+        * @return MessageReporter[]
+        */
+       public abstract function getInstances();
+
+       /**
+        * Message provider, includes edge cases and random tests
+        *
+        * @return array
+        */
+       public function reportMessageProvider() {
+               $messages = array();
+
+               $messages[] = '';
+               $messages[] = '  ';
+
+               foreach ( array_merge( range( 1, 100 ), array( 1000, 10000 ) ) 
as $length ) {
+                       $string = array();
+
+                       for ( $position = 0; $position < $length; $position++ ) 
{
+                               $string[] = chr( mt_rand( 32, 126 ) );
+                       }
+
+                       $messages[] = implode( '', $string );
+               }
+
+               return $this->arrayWrap( $messages );
+       }
+
+       /**
+        * @dataProvider reportMessageProvider
+        *
+        * @param string $message
+        */
+       public function testReportMessage( $message ) {
+               foreach ( $this->getInstances() as $reporter ) {
+                       $reporter->reportMessage( $message );
+                       $reporter->reportMessage( $message );
+                       $this->assertTrue( true );
+               }
+       }
+
+}
\ No newline at end of file

-- 
To view, visit https://gerrit.wikimedia.org/r/50042
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia93dae25b283b6c65988eef60ddc3f22c5f44715
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Jeroen De Dauw <jeroended...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to