3 class HooksTest
extends MediaWikiTestCase
{
8 Hooks
::clear( 'MediaWikiHooksTest001' );
9 unset( $wgHooks['MediaWikiHooksTest001'] );
12 public static function provideHooks() {
13 $i = new NothingClass();
18 array( $i, 'someNonStatic' ),
22 array( 'Object and no method', array( $i ), 'changed-onevent', 'original' ),
24 'Object and method with data',
25 array( $i, 'someNonStaticWithData', 'data' ),
29 array( 'Object and static method', array( $i, 'someStatic' ), 'changed-static', 'original' ),
31 'Class::method static call',
32 array( 'NothingClass::someStatic' ),
36 array( 'Global function', array( 'NothingFunction' ), 'changed-func', 'original' ),
37 array( 'Global function with data', array( 'NothingFunctionData', 'data' ), 'data', 'original' ),
38 array( 'Closure', array( function ( &$foo, $bar ) {
39 $foo = 'changed-closure';
42 } ), 'changed-closure', 'original' ),
43 array( 'Closure with data', array( function ( $data, &$foo, $bar ) {
47 }, 'data' ), 'data', 'original' )
52 * @dataProvider provideHooks
53 * @covers ::wfRunHooks
55 public function testOldStyleHooks( $msg, array $hook, $expectedFoo, $expectedBar ) {
57 $foo = $bar = 'original';
59 $wgHooks['MediaWikiHooksTest001'][] = $hook;
60 wfRunHooks( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
62 $this->assertSame( $expectedFoo, $foo, $msg );
63 $this->assertSame( $expectedBar, $bar, $msg );
67 * @dataProvider provideHooks
68 * @covers Hooks::register
71 public function testNewStyleHooks( $msg, $hook, $expectedFoo, $expectedBar ) {
72 $foo = $bar = 'original';
74 Hooks
::register( 'MediaWikiHooksTest001', $hook );
75 Hooks
::run( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
77 $this->assertSame( $expectedFoo, $foo, $msg );
78 $this->assertSame( $expectedBar, $bar, $msg );
82 * @covers Hooks::isRegistered
83 * @covers Hooks::register
84 * @covers Hooks::getHandlers
87 public function testNewStyleHookInteraction() {
90 $a = new NothingClass();
91 $b = new NothingClass();
93 $wgHooks['MediaWikiHooksTest001'][] = $a;
95 Hooks
::isRegistered( 'MediaWikiHooksTest001' ),
96 'Hook registered via $wgHooks should be noticed by Hooks::isRegistered'
99 Hooks
::register( 'MediaWikiHooksTest001', $b );
102 count( Hooks
::getHandlers( 'MediaWikiHooksTest001' ) ),
103 'Hooks::getHandlers() should return hooks registered via wgHooks as well as Hooks::register'
109 Hooks
::run( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
113 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register'
118 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register'
123 * @expectedException MWException
126 public function testUncallableFunction() {
127 Hooks
::register( 'MediaWikiHooksTest001', 'ThisFunctionDoesntExist' );
128 Hooks
::run( 'MediaWikiHooksTest001', array() );
134 public function testFalseReturn() {
135 Hooks
::register( 'MediaWikiHooksTest001', function ( &$foo ) {
138 Hooks
::register( 'MediaWikiHooksTest001', function ( &$foo ) {
144 Hooks
::run( 'MediaWikiHooksTest001', array( &$foo ) );
145 $this->assertSame( 'original', $foo, 'Hooks continued processing after a false return.' );
149 * @expectedException FatalError
152 public function testFatalError() {
153 Hooks
::register( 'MediaWikiHooksTest001', function () {
156 Hooks
::run( 'MediaWikiHooksTest001', array() );
160 function NothingFunction( &$foo, &$bar ) {
161 $foo = 'changed-func';
166 function NothingFunctionData( $data, &$foo, &$bar ) {
175 public static function someStatic( &$foo, &$bar ) {
176 $foo = 'changed-static';
181 public function someNonStatic( &$foo, &$bar ) {
183 $foo = 'changed-nonstatic';
184 $bar = 'changed-nonstatic';
189 public function onMediaWikiHooksTest001( &$foo, &$bar ) {
191 $foo = 'changed-onevent';
196 public function someNonStaticWithData( $data, &$foo, &$bar ) {