Move ResultWrapper subclasses to Rdbms
[mediawiki.git] / tests / phpunit / includes / HooksTest.php
blob527e12927ab1516e363684b26026b64d771396b4
1 <?php
3 class HooksTest extends MediaWikiTestCase {
5 function setUp() {
6 global $wgHooks;
7 parent::setUp();
8 Hooks::clear( 'MediaWikiHooksTest001' );
9 unset( $wgHooks['MediaWikiHooksTest001'] );
12 public static function provideHooks() {
13 $i = new NothingClass();
15 return [
17 'Object and method',
18 [ $i, 'someNonStatic' ],
19 'changed-nonstatic',
20 'changed-nonstatic'
22 [ 'Object and no method', [ $i ], 'changed-onevent', 'original' ],
24 'Object and method with data',
25 [ $i, 'someNonStaticWithData', 'data' ],
26 'data',
27 'original'
29 [ 'Object and static method', [ $i, 'someStatic' ], 'changed-static', 'original' ],
31 'Class::method static call',
32 [ 'NothingClass::someStatic' ],
33 'changed-static',
34 'original'
36 [ 'Global function', [ 'NothingFunction' ], 'changed-func', 'original' ],
37 [ 'Global function with data', [ 'NothingFunctionData', 'data' ], 'data', 'original' ],
38 [ 'Closure', [ function ( &$foo, $bar ) {
39 $foo = 'changed-closure';
41 return true;
42 } ], 'changed-closure', 'original' ],
43 [ 'Closure with data', [ function ( $data, &$foo, $bar ) {
44 $foo = $data;
46 return true;
47 }, 'data' ], 'data', 'original' ]
51 /**
52 * @dataProvider provideHooks
53 * @covers ::wfRunHooks
55 public function testOldStyleHooks( $msg, array $hook, $expectedFoo, $expectedBar ) {
56 global $wgHooks;
57 $foo = $bar = 'original';
59 $wgHooks['MediaWikiHooksTest001'][] = $hook;
60 wfRunHooks( 'MediaWikiHooksTest001', [ &$foo, &$bar ] );
62 $this->assertSame( $expectedFoo, $foo, $msg );
63 $this->assertSame( $expectedBar, $bar, $msg );
66 /**
67 * @dataProvider provideHooks
68 * @covers Hooks::register
69 * @covers Hooks::run
71 public function testNewStyleHooks( $msg, $hook, $expectedFoo, $expectedBar ) {
72 $foo = $bar = 'original';
74 Hooks::register( 'MediaWikiHooksTest001', $hook );
75 Hooks::run( 'MediaWikiHooksTest001', [ &$foo, &$bar ] );
77 $this->assertSame( $expectedFoo, $foo, $msg );
78 $this->assertSame( $expectedBar, $bar, $msg );
81 /**
82 * @covers Hooks::isRegistered
83 * @covers Hooks::register
84 * @covers Hooks::getHandlers
85 * @covers Hooks::run
87 public function testNewStyleHookInteraction() {
88 global $wgHooks;
90 $a = new NothingClass();
91 $b = new NothingClass();
93 $wgHooks['MediaWikiHooksTest001'][] = $a;
94 $this->assertTrue(
95 Hooks::isRegistered( 'MediaWikiHooksTest001' ),
96 'Hook registered via $wgHooks should be noticed by Hooks::isRegistered'
99 Hooks::register( 'MediaWikiHooksTest001', $b );
100 $this->assertEquals(
102 count( Hooks::getHandlers( 'MediaWikiHooksTest001' ) ),
103 'Hooks::getHandlers() should return hooks registered via wgHooks as well as Hooks::register'
106 $foo = 'quux';
107 $bar = 'qaax';
109 Hooks::run( 'MediaWikiHooksTest001', [ &$foo, &$bar ] );
110 $this->assertEquals(
112 $a->calls,
113 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register'
115 $this->assertEquals(
117 $b->calls,
118 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register'
123 * @expectedException MWException
124 * @covers Hooks::run
126 public function testUncallableFunction() {
127 Hooks::register( 'MediaWikiHooksTest001', 'ThisFunctionDoesntExist' );
128 Hooks::run( 'MediaWikiHooksTest001', [] );
132 * @covers Hooks::run
134 public function testFalseReturn() {
135 Hooks::register( 'MediaWikiHooksTest001', function ( &$foo ) {
136 return false;
137 } );
138 Hooks::register( 'MediaWikiHooksTest001', function ( &$foo ) {
139 $foo = 'test';
141 return true;
142 } );
143 $foo = 'original';
144 Hooks::run( 'MediaWikiHooksTest001', [ &$foo ] );
145 $this->assertSame( 'original', $foo, 'Hooks continued processing after a false return.' );
149 * @expectedException FatalError
150 * @covers Hooks::run
152 public function testFatalError() {
153 Hooks::register( 'MediaWikiHooksTest001', function () {
154 return 'test';
155 } );
156 Hooks::run( 'MediaWikiHooksTest001', [] );
160 function NothingFunction( &$foo, &$bar ) {
161 $foo = 'changed-func';
163 return true;
166 function NothingFunctionData( $data, &$foo, &$bar ) {
167 $foo = $data;
169 return true;
172 class NothingClass {
173 public $calls = 0;
175 public static function someStatic( &$foo, &$bar ) {
176 $foo = 'changed-static';
178 return true;
181 public function someNonStatic( &$foo, &$bar ) {
182 $this->calls++;
183 $foo = 'changed-nonstatic';
184 $bar = 'changed-nonstatic';
186 return true;
189 public function onMediaWikiHooksTest001( &$foo, &$bar ) {
190 $this->calls++;
191 $foo = 'changed-onevent';
193 return true;
196 public function someNonStaticWithData( $data, &$foo, &$bar ) {
197 $this->calls++;
198 $foo = $data;
200 return true;