HookRegistryTest::doTestParserAfterTidy()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 9.264
c 0
b 0
f 0
cc 1
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 = [
61
			'useSubpageFinderFallback' => false,
62
			'tryToFindClosestDescendant' => false,
63
			'propertySearchPatternByNamespace' => [],
64
			'breadcrumbTrailStyleClass' => 'foo',
65
			'breadcrumbDividerStyleClass' => 'bar',
66
			'hideSubpageParent' => true,
67
			'enabledSubpageParentAnnotation' => true,
68
			'disableTranslationSubpageAnnotation' => false,
69
			'wgNamespacesWithSubpages' => []
70
		];
71
72
		$instance = new HookRegistry(
73
			$store,
74
			new Options( $configuration )
75
		);
76
77
		$instance->register();
78
79
		$this->doTestInitProperties( $instance );
80
		$this->doTestSkinTemplateOutputPageBeforeExec( $instance, $skin );
81
		$this->doTestBeforePageDisplay( $instance, $outputPage, $skin );
82
		$this->doTestParserAfterTidy( $instance );
83
		$this->doTestParserAfterTidyToBailOutEarly( $instance );
84
		$this->doTestSmwParserBeforeMagicWordsFinder( $instance );
85
		$this->doTestOutputPageParserOutput( $instance, $outputPage );
86
	}
87
88
	private function doTestInitProperties( $instance ) {
89
90
		$handler = 'SMW::Property::initProperties';
91
92
		$propertyRegistry = $this->getMockBuilder( '\SMW\PropertyRegistry' )
93
			->disableOriginalConstructor()
94
			->getMock();
95
96
		$this->assertTrue(
97
			$instance->isRegistered( $handler )
98
		);
99
100
		$this->assertThatHookIsExcutable(
101
			$instance->getHandlerFor( $handler ),
102
			[ $propertyRegistry ]
103
		);
104
	}
105
106
	private function doTestSkinTemplateOutputPageBeforeExec( $instance, $skin ) {
107
108
		$handler = 'SkinTemplateOutputPageBeforeExec';
109
110
		$this->assertTrue(
111
			$instance->isRegistered( $handler )
112
		);
113
114
		$template = new \stdClass;
115
116
		$this->assertThatHookIsExcutable(
117
			$instance->getHandlerFor( $handler ),
118
			[ &$skin, &$template ]
119
		);
120
	}
121
122
	private function doTestBeforePageDisplay( $instance, $outputPage, $skin ) {
123
124
		$handler = 'BeforePageDisplay';
125
126
		$this->assertTrue(
127
			$instance->isRegistered( $handler )
128
		);
129
130
		$this->assertThatHookIsExcutable(
131
			$instance->getHandlerFor( $handler ),
132
			[ &$outputPage, &$skin ]
133
		);
134
	}
135
136
	private function doTestParserAfterTidy( $instance ) {
137
138
		$handler = 'ParserAfterTidy';
139
140
		$this->assertTrue(
141
			$instance->isRegistered( $handler )
142
		);
143
144
		$title = Title::newFromText( __METHOD__ );
145
146
		$parserOptions = $this->getMockBuilder( '\ParserOptions' )
147
			->disableOriginalConstructor()
148
			->getMock();
149
150
		$parserOutput = $this->getMockBuilder( '\ParserOutput' )
151
			->disableOriginalConstructor()
152
			->getMock();
153
154
		$parser = $this->getMockBuilder( '\Parser' )
155
			->disableOriginalConstructor()
156
			->getMock();
157
158
		$parser->expects( $this->any() )
159
			->method( 'getTitle' )
160
			->will( $this->returnValue( $title ) );
161
162
		$parser->expects( $this->any() )
163
			->method( 'getOptions' )
164
			->will( $this->returnValue( $parserOptions ) );
165
166
		$parser->expects( $this->any() )
167
			->method( 'getOutput' )
168
			->will( $this->returnValue( $parserOutput ) );
169
170
		$text = '';
171
172
		$this->assertThatHookIsExcutable(
173
			$instance->getHandlerFor( $handler ),
174
			[ &$parser, &$text ]
175
		);
176
	}
177
178
	private function doTestParserAfterTidyToBailOutEarly( $instance ) {
179
180
		$handler = 'ParserAfterTidy';
181
182
		$this->assertTrue(
183
			$instance->isRegistered( $handler )
184
		);
185
186
		$title = Title::newFromText( __METHOD__ );
187
188
		$parserOptions = $this->getMockBuilder( '\ParserOptions' )
189
			->disableOriginalConstructor()
190
			->getMock();
191
192
		$parserOptions->expects( $this->any() )
193
			->method( 'getInterfaceMessage' )
194
			->will( $this->returnValue( true ) );
195
196
		$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...
197
			->disableOriginalConstructor()
198
			->getMock();
199
200
		$parser = $this->getMockBuilder( '\Parser' )
201
			->disableOriginalConstructor()
202
			->getMock();
203
204
		$parser->expects( $this->any() )
205
			->method( 'getTitle' )
206
			->will( $this->returnValue( $title ) );
207
208
		$parser->expects( $this->any() )
209
			->method( 'getOptions' )
210
			->will( $this->returnValue( $parserOptions ) );
211
212
		$text = '';
213
214
		$this->assertThatHookIsExcutable(
215
			$instance->getHandlerFor( $handler ),
216
			[ &$parser, &$text ]
217
		);
218
	}
219
220
	private function doTestSmwParserBeforeMagicWordsFinder( $instance ) {
221
222
		$handler = 'SMW::Parser::BeforeMagicWordsFinder';
223
224
		$this->assertTrue(
225
			$instance->isRegistered( $handler )
226
		);
227
228
		$magicWords = [];
229
230
		$this->assertThatHookIsExcutable(
231
			$instance->getHandlerFor( $handler ),
232
			[ &$magicWords ]
233
		);
234
235
		$this->assertContains(
236
			'SBL_NOBREADCRUMBLINKS',
237
			$magicWords
238
		);
239
	}
240
241
	private function doTestOutputPageParserOutput( $instance, $outputPage ) {
242
243
		$handler = 'OutputPageParserOutput';
244
245
		$this->assertTrue(
246
			$instance->isRegistered( $handler )
247
		);
248
249
		$parserOutput = $this->getMockBuilder( '\ParserOutput' )
250
			->disableOriginalConstructor()
251
			->getMock();
252
253
		$magicWords = [];
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...
254
255
		$this->assertThatHookIsExcutable(
256
			$instance->getHandlerFor( $handler ),
257
			[ &$outputPage, $parserOutput ]
258
		);
259
260
		$this->assertEquals(
261
			'',
262
			$outputPage->smwmagicwords
263
		);
264
	}
265
266
	private function assertThatHookIsExcutable( \Closure $handler, $arguments ) {
267
		$this->assertInternalType(
268
			'boolean',
269
			call_user_func_array( $handler, $arguments )
270
		);
271
	}
272
273
}
274