Completed
Push — master ( 43bf95...2d7f23 )
by mw
02:19
created

doTestSmwParserBeforeMagicWordsFinder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 1
1
<?php
2
3
namespace SBL\Tests;
4
5
use SBL\HookRegistry;
6
use SBL\Options;
7
use Title;
8
9
/**
10
 * @covers \SBL\HookRegistry
11
 * @group semantic-breadcrumb-links
12
 *
13
 * @license GNU GPL v2+
14
 * @since 1.0
15
 *
16
 * @author mwjames
17
 */
18
class HookRegistryTest extends \PHPUnit_Framework_TestCase {
19
20
	public function testCanConstruct() {
21
22
		$store = $this->getMockBuilder( '\SMW\Store' )
23
			->disableOriginalConstructor()
24
			->getMockForAbstractClass();
25
26
		$options = $this->getMockBuilder( '\SBL\Options' )
27
			->disableOriginalConstructor()
28
			->getMock();
29
30
		$this->assertInstanceOf(
31
			'\SBL\HookRegistry',
32
			new HookRegistry( $store, new $options )
33
		);
34
	}
35
36
	public function testRegister() {
37
38
		$title = Title::newFromText( __METHOD__ );
39
40
		$outputPage = $this->getMockBuilder( '\OutputPage' )
41
			->disableOriginalConstructor()
42
			->getMock();
43
44
		$outputPage->expects( $this->any() )
45
			->method( 'getTitle' )
46
			->will( $this->returnValue( $title ) );
47
48
		$skin = $this->getMockBuilder( '\Skin' )
49
			->disableOriginalConstructor()
50
			->getMock();
51
52
		$skin->expects( $this->any() )
53
			->method( 'getOutput' )
54
			->will( $this->returnValue( $outputPage ) );
55
56
		$store = $this->getMockBuilder( '\SMW\Store' )
57
			->disableOriginalConstructor()
58
			->getMockForAbstractClass();
59
60
		$configuration = array(
61
			'useSubpageFinderFallback' => false,
62
			'tryToFindClosestDescendant' => false,
63
			'propertySearchPatternByNamespace' => array(),
64
			'breadcrumbTrailStyleClass' => 'foo',
65
			'breadcrumbDividerStyleClass' => 'bar',
66
			'hideSubpageParent' => true,
67
			'enabledSubpageParentAnnotation' => true,
68
			'wgNamespacesWithSubpages' => array()
69
		);
70
71
		$instance = new HookRegistry(
72
			$store,
73
			new Options( $configuration )
74
		);
75
76
		$instance->register();
77
78
		$this->doTestInitProperties( $instance );
79
		$this->doTestSkinTemplateOutputPageBeforeExec( $instance, $skin );
80
		$this->doTestBeforePageDisplay( $instance, $outputPage, $skin );
81
		$this->doTestParserAfterTidy( $instance );
82
		$this->doTestParserAfterTidyToBailOutEarly( $instance );
83
		$this->doTestSmwParserBeforeMagicWordsFinder( $instance );
84
		$this->doTestOutputPageParserOutput( $instance, $outputPage );
85
	}
86
87
	private function doTestInitProperties( $instance ) {
88
89
		$handler = 'SMW::Property::initProperties';
90
91
		$propertyRegistry = $this->getMockBuilder( '\SMW\PropertyRegistry' )
92
			->disableOriginalConstructor()
93
			->getMock();
94
95
		$this->assertTrue(
96
			$instance->isRegistered( $handler )
97
		);
98
99
		$this->assertThatHookIsExcutable(
100
			$instance->getHandlerFor( $handler ),
101
			array( $propertyRegistry )
102
		);
103
	}
104
105
	private function doTestSkinTemplateOutputPageBeforeExec( $instance, $skin ) {
106
107
		$handler = 'SkinTemplateOutputPageBeforeExec';
108
109
		$this->assertTrue(
110
			$instance->isRegistered( $handler )
111
		);
112
113
		$template = new \stdClass;
114
115
		$this->assertThatHookIsExcutable(
116
			$instance->getHandlerFor( $handler ),
117
			array( &$skin, &$template )
118
		);
119
	}
120
121
	private function doTestBeforePageDisplay( $instance, $outputPage, $skin ) {
122
123
		$handler = 'BeforePageDisplay';
124
125
		$this->assertTrue(
126
			$instance->isRegistered( $handler )
127
		);
128
129
		$this->assertThatHookIsExcutable(
130
			$instance->getHandlerFor( $handler ),
131
			array( &$outputPage, &$skin )
132
		);
133
	}
134
135
	private function doTestParserAfterTidy( $instance ) {
136
137
		$handler = 'ParserAfterTidy';
138
139
		$this->assertTrue(
140
			$instance->isRegistered( $handler )
141
		);
142
143
		$title = Title::newFromText( __METHOD__ );
144
145
		$parserOptions = $this->getMockBuilder( '\ParserOptions' )
146
			->disableOriginalConstructor()
147
			->getMock();
148
149
		$parserOutput = $this->getMockBuilder( '\ParserOutput' )
150
			->disableOriginalConstructor()
151
			->getMock();
152
153
		$parser = $this->getMockBuilder( '\Parser' )
154
			->disableOriginalConstructor()
155
			->getMock();
156
157
		$parser->expects( $this->any() )
158
			->method( 'getTitle' )
159
			->will( $this->returnValue( $title ) );
160
161
		$parser->expects( $this->any() )
162
			->method( 'getOptions' )
163
			->will( $this->returnValue( $parserOptions ) );
164
165
		$parser->expects( $this->any() )
166
			->method( 'getOutput' )
167
			->will( $this->returnValue( $parserOutput ) );
168
169
		$text = '';
170
171
		$this->assertThatHookIsExcutable(
172
			$instance->getHandlerFor( $handler ),
173
			array( &$parser, &$text )
174
		);
175
	}
176
177
	private function doTestParserAfterTidyToBailOutEarly( $instance ) {
178
179
		$handler = 'ParserAfterTidy';
180
181
		$this->assertTrue(
182
			$instance->isRegistered( $handler )
183
		);
184
185
		$title = Title::newFromText( __METHOD__ );
186
187
		$parserOptions = $this->getMockBuilder( '\ParserOptions' )
188
			->disableOriginalConstructor()
189
			->getMock();
190
191
		$parserOptions->expects( $this->any() )
192
			->method( 'getInterfaceMessage' )
193
			->will( $this->returnValue( true ) );
194
195
		$parserOutput = $this->getMockBuilder( '\ParserOutput' )
0 ignored issues
show
Unused Code introduced by
$parserOutput is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
196
			->disableOriginalConstructor()
197
			->getMock();
198
199
		$parser = $this->getMockBuilder( '\Parser' )
200
			->disableOriginalConstructor()
201
			->getMock();
202
203
		$parser->expects( $this->any() )
204
			->method( 'getTitle' )
205
			->will( $this->returnValue( $title ) );
206
207
		$parser->expects( $this->any() )
208
			->method( 'getOptions' )
209
			->will( $this->returnValue( $parserOptions ) );
210
211
		$text = '';
212
213
		$this->assertThatHookIsExcutable(
214
			$instance->getHandlerFor( $handler ),
215
			array( &$parser, &$text )
216
		);
217
	}
218
219
	private function doTestSmwParserBeforeMagicWordsFinder( $instance ) {
220
221
		$handler = 'SMW::Parser::BeforeMagicWordsFinder';
222
223
		$this->assertTrue(
224
			$instance->isRegistered( $handler )
225
		);
226
227
		$magicWords = array();
228
229
		$this->assertThatHookIsExcutable(
230
			$instance->getHandlerFor( $handler ),
231
			array( &$magicWords )
232
		);
233
234
		$this->assertContains(
235
			'SBL_NOBREADCRUMBLINKS',
236
			$magicWords
237
		);
238
	}
239
240
	private function doTestOutputPageParserOutput( $instance, $outputPage ) {
241
242
		$handler = 'OutputPageParserOutput';
243
244
		$this->assertTrue(
245
			$instance->isRegistered( $handler )
246
		);
247
248
		$parserOutput = $this->getMockBuilder( '\ParserOutput' )
249
			->disableOriginalConstructor()
250
			->getMock();
251
252
		$magicWords = array();
0 ignored issues
show
Unused Code introduced by
$magicWords is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
253
254
		$this->assertThatHookIsExcutable(
255
			$instance->getHandlerFor( $handler ),
256
			array( &$outputPage, $parserOutput )
257
		);
258
259
		$this->assertEquals(
260
			'',
261
			$outputPage->smwmagicwords
262
		);
263
	}
264
265
	private function assertThatHookIsExcutable( \Closure $handler, $arguments ) {
266
		$this->assertInternalType(
267
			'boolean',
268
			call_user_func_array( $handler, $arguments )
269
		);
270
	}
271
272
}
273