Merge "Add method for inspecting module dependency relations"
[lhc/web/wiklou.git] / tests / phpunit / includes / HooksTest.php
1 <?php
2
3 class HooksTest extends MediaWikiTestCase {
4
5 function setUp() {
6 global $wgHooks;
7 parent::setUp();
8 Hooks::clear( 'MediaWikiHooksTest001' );
9 unset( $wgHooks['MediaWikiHooksTest001'] );
10 }
11
12 public static function provideHooks() {
13 $i = new NothingClass();
14
15 return array(
16 array( 'Object and method', array( $i, 'someNonStatic' ), 'changed-nonstatic', 'changed-nonstatic' ),
17 array( 'Object and no method', array( $i ), 'changed-onevent', 'original' ),
18 array( 'Object and method with data', array( $i, 'someNonStaticWithData', 'data' ), 'data', 'original' ),
19 array( 'Object and static method', array( $i, 'someStatic' ), 'changed-static', 'original' ),
20 array( 'Class::method static call', array( 'NothingClass::someStatic' ), 'changed-static', 'original' ),
21 array( 'Global function', array( 'NothingFunction' ), 'changed-func', 'original' ),
22 array( 'Global function with data', array( 'NothingFunctionData', 'data' ), 'data', 'original' ),
23 array( 'Closure', array( function ( &$foo, $bar ) {
24 $foo = 'changed-closure';
25
26 return true;
27 } ), 'changed-closure', 'original' ),
28 array( 'Closure with data', array( function ( $data, &$foo, $bar ) {
29 $foo = $data;
30
31 return true;
32 }, 'data' ), 'data', 'original' )
33 );
34 }
35
36 /**
37 * @dataProvider provideHooks
38 * @covers ::wfRunHooks
39 */
40 public function testOldStyleHooks( $msg, array $hook, $expectedFoo, $expectedBar ) {
41 global $wgHooks;
42 $foo = $bar = 'original';
43
44 $wgHooks['MediaWikiHooksTest001'][] = $hook;
45 wfRunHooks( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
46
47 $this->assertSame( $expectedFoo, $foo, $msg );
48 $this->assertSame( $expectedBar, $bar, $msg );
49 }
50
51 /**
52 * @dataProvider provideHooks
53 * @covers Hooks::register
54 * @covers Hooks::run
55 */
56 public function testNewStyleHooks( $msg, $hook, $expectedFoo, $expectedBar ) {
57 $foo = $bar = 'original';
58
59 Hooks::register( 'MediaWikiHooksTest001', $hook );
60 Hooks::run( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
61
62 $this->assertSame( $expectedFoo, $foo, $msg );
63 $this->assertSame( $expectedBar, $bar, $msg );
64 }
65
66 /**
67 * @covers Hooks::isRegistered
68 * @covers Hooks::register
69 * @covers Hooks::getHandlers
70 * @covers Hooks::run
71 */
72 public function testNewStyleHookInteraction() {
73 global $wgHooks;
74
75 $a = new NothingClass();
76 $b = new NothingClass();
77
78 $wgHooks['MediaWikiHooksTest001'][] = $a;
79 $this->assertTrue( Hooks::isRegistered( 'MediaWikiHooksTest001' ), 'Hook registered via $wgHooks should be noticed by Hooks::isRegistered' );
80
81 Hooks::register( 'MediaWikiHooksTest001', $b );
82 $this->assertEquals( 2, count( Hooks::getHandlers( 'MediaWikiHooksTest001' ) ), 'Hooks::getHandlers() should return hooks registered via wgHooks as well as Hooks::register' );
83
84 $foo = 'quux';
85 $bar = 'qaax';
86
87 Hooks::run( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
88 $this->assertEquals( 1, $a->calls, 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register' );
89 $this->assertEquals( 1, $b->calls, 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register' );
90 }
91
92 /**
93 * @expectedException MWException
94 * @covers Hooks::run
95 */
96 public function testUncallableFunction() {
97 Hooks::register( 'MediaWikiHooksTest001', 'ThisFunctionDoesntExist' );
98 Hooks::run( 'MediaWikiHooksTest001', array() );
99 }
100
101 /**
102 * @covers Hooks::run
103 */
104 public function testFalseReturn() {
105 Hooks::register( 'MediaWikiHooksTest001', function ( &$foo ) {
106 return false;
107 } );
108 Hooks::register( 'MediaWikiHooksTest001', function ( &$foo ) {
109 $foo = 'test';
110
111 return true;
112 } );
113 $foo = 'original';
114 Hooks::run( 'MediaWikiHooksTest001', array( &$foo ) );
115 $this->assertSame( 'original', $foo, 'Hooks continued processing after a false return.' );
116 }
117
118 /**
119 * @expectedException FatalError
120 * @covers Hooks::run
121 */
122 public function testFatalError() {
123 Hooks::register( 'MediaWikiHooksTest001', function () {
124 return 'test';
125 } );
126 Hooks::run( 'MediaWikiHooksTest001', array() );
127 }
128 }
129
130 function NothingFunction( &$foo, &$bar ) {
131 $foo = 'changed-func';
132
133 return true;
134 }
135
136 function NothingFunctionData( $data, &$foo, &$bar ) {
137 $foo = $data;
138
139 return true;
140 }
141
142 class NothingClass {
143 public $calls = 0;
144
145 public static function someStatic( &$foo, &$bar ) {
146 $foo = 'changed-static';
147
148 return true;
149 }
150
151 public function someNonStatic( &$foo, &$bar ) {
152 $this->calls++;
153 $foo = 'changed-nonstatic';
154 $bar = 'changed-nonstatic';
155
156 return true;
157 }
158
159 public function onMediaWikiHooksTest001( &$foo, &$bar ) {
160 $this->calls++;
161 $foo = 'changed-onevent';
162
163 return true;
164 }
165
166 public function someNonStaticWithData( $data, &$foo, &$bar ) {
167 $this->calls++;
168 $foo = $data;
169
170 return true;
171 }
172 }