Complete test coverage of Hooks class
authorKunal Mehta <legoktm@member.fsf.org>
Sun, 27 May 2018 06:38:28 +0000 (23:38 -0700)
committerKunal Mehta <legoktm@member.fsf.org>
Sun, 27 May 2018 06:38:28 +0000 (23:38 -0700)
Change-Id: I9e720c44e6d6c9c5d726a8a147e07ee9b638913f

includes/Hooks.php
tests/phpunit/includes/HooksTest.php

index c22dc97..3a33c2d 100644 (file)
@@ -62,6 +62,7 @@ class Hooks {
         *
         * @since 1.21
         * @throws MWException If not in testing mode.
         *
         * @since 1.21
         * @throws MWException If not in testing mode.
+        * @codeCoverageIgnore
         */
        public static function clear( $name ) {
                if ( !defined( 'MW_PHPUNIT_TEST' ) && !defined( 'MW_PARSER_TEST' ) ) {
         */
        public static function clear( $name ) {
                if ( !defined( 'MW_PHPUNIT_TEST' ) && !defined( 'MW_PARSER_TEST' ) ) {
index efe92ec..c1efc7f 100644 (file)
@@ -33,6 +33,12 @@ class HooksTest extends MediaWikiTestCase {
                                'changed-static',
                                'original'
                        ],
                                'changed-static',
                                'original'
                        ],
+                       [
+                               'Class::method static call as array',
+                               [ [ 'NothingClass::someStatic' ] ],
+                               'changed-static',
+                               'original'
+                       ],
                        [ 'Global function', [ 'NothingFunction' ], 'changed-func', 'original' ],
                        [ 'Global function with data', [ 'NothingFunctionData', 'data' ], 'data', 'original' ],
                        [ 'Closure', [ function ( &$foo, $bar ) {
                        [ 'Global function', [ 'NothingFunction' ], 'changed-func', 'original' ],
                        [ 'Global function with data', [ 'NothingFunctionData', 'data' ], 'data', 'original' ],
                        [ 'Closure', [ function ( &$foo, $bar ) {
@@ -81,10 +87,50 @@ class HooksTest extends MediaWikiTestCase {
                $this->assertSame( $expectedBar, $bar, $msg );
        }
 
                $this->assertSame( $expectedBar, $bar, $msg );
        }
 
+       /**
+        * @covers Hooks::getHandlers
+        */
+       public function testGetHandlers() {
+               global $wgHooks;
+
+               $this->assertSame(
+                       [],
+                       Hooks::getHandlers( 'MediaWikiHooksTest001' ),
+                       'No hooks registered'
+               );
+
+               $a = new NothingClass();
+               $b = new NothingClass();
+
+               $wgHooks['MediaWikiHooksTest001'][] = $a;
+
+               $this->assertSame(
+                       [ $a ],
+                       Hooks::getHandlers( 'MediaWikiHooksTest001' ),
+                       'Hook registered by $wgHooks'
+               );
+
+               Hooks::register( 'MediaWikiHooksTest001', $b );
+               $this->assertSame(
+                       [ $b, $a ],
+                       Hooks::getHandlers( 'MediaWikiHooksTest001' ),
+                       'Hooks::getHandlers() should return hooks registered via wgHooks as well as Hooks::register'
+               );
+
+               Hooks::clear( 'MediaWikiHooksTest001' );
+               unset( $wgHooks['MediaWikiHooksTest001'] );
+
+               Hooks::register( 'MediaWikiHooksTest001', $b );
+               $this->assertSame(
+                       [ $b ],
+                       Hooks::getHandlers( 'MediaWikiHooksTest001' ),
+                       'Hook registered by Hook::register'
+               );
+       }
+
        /**
         * @covers Hooks::isRegistered
         * @covers Hooks::register
        /**
         * @covers Hooks::isRegistered
         * @covers Hooks::register
-        * @covers Hooks::getHandlers
         * @covers Hooks::run
         * @covers Hooks::callHook
         */
         * @covers Hooks::run
         * @covers Hooks::callHook
         */
@@ -151,6 +197,56 @@ class HooksTest extends MediaWikiTestCase {
                $this->assertSame( 'original', $foo, 'Hooks abort after a false return.' );
        }
 
                $this->assertSame( 'original', $foo, 'Hooks abort after a false return.' );
        }
 
+       /**
+        * @covers Hooks::run
+        */
+       public function testNullReturn() {
+               Hooks::register( 'MediaWikiHooksTest001', function ( &$foo ) {
+                       return;
+               } );
+               Hooks::register( 'MediaWikiHooksTest001', function ( &$foo ) {
+                       $foo = 'test';
+
+                       return true;
+               } );
+               $foo = 'original';
+               Hooks::run( 'MediaWikiHooksTest001', [ &$foo ] );
+               $this->assertSame( 'test', $foo, 'Hooks continue after a null return.' );
+       }
+
+       /**
+        * @covers Hooks::callHook
+        */
+       public function testCallHook_FalseHook() {
+               Hooks::register( 'MediaWikiHooksTest001', false );
+               Hooks::register( 'MediaWikiHooksTest001', function ( &$foo ) {
+                       $foo = 'test';
+
+                       return true;
+               } );
+               $foo = 'original';
+               Hooks::run( 'MediaWikiHooksTest001', [ &$foo ] );
+               $this->assertSame( 'test', $foo, 'Hooks that are falsey are skipped.' );
+       }
+
+       /**
+        * @covers Hooks::callHook
+        * @expectedException MWException
+        */
+       public function testCallHook_UnknownDatatype() {
+               Hooks::register( 'MediaWikiHooksTest001', 12345 );
+               Hooks::run( 'MediaWikiHooksTest001' );
+       }
+
+       /**
+        * @covers Hooks::callHook
+        * @expectedException PHPUnit_Framework_Error_Deprecated
+        */
+       public function testCallHook_Deprecated() {
+               Hooks::register( 'MediaWikiHooksTest001', 'NothingClass::someStatic' );
+               Hooks::run( 'MediaWikiHooksTest001', [], '1.31' );
+       }
+
        /**
         * @covers Hooks::runWithoutAbort
         * @covers Hooks::callHook
        /**
         * @covers Hooks::runWithoutAbort
         * @covers Hooks::callHook