Completed
Pull Request — master (#11)
by mw
02:01
created

HookRegistryTest::doTestInitProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 7
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
	}
83
84
	private function doTestInitProperties( $instance ) {
85
86
		$handler = 'SMW::Property::initProperties';
87
88
		$this->assertTrue(
89
			$instance->isRegistered( $handler )
90
		);
91
92
		$this->assertThatHookIsExcutable(
93
			$instance->getHandlerFor( $handler ),
94
			array()
95
		);
96
	}
97
98
	private function doTestSkinTemplateOutputPageBeforeExec( $instance, $skin ) {
99
100
		$handler = 'SkinTemplateOutputPageBeforeExec';
101
102
		$this->assertTrue(
103
			$instance->isRegistered( $handler )
104
		);
105
106
		$template = new \stdClass;
107
108
		$this->assertThatHookIsExcutable(
109
			$instance->getHandlerFor( $handler ),
110
			array( &$skin, &$template )
111
		);
112
	}
113
114
	private function doTestBeforePageDisplay( $instance, $outputPage, $skin ) {
115
116
		$handler = 'BeforePageDisplay';
117
118
		$this->assertTrue(
119
			$instance->isRegistered( $handler )
120
		);
121
122
		$this->assertThatHookIsExcutable(
123
			$instance->getHandlerFor( $handler ),
124
			array( &$outputPage, &$skin )
125
		);
126
	}
127
128
	private function doTestParserAfterTidy( $instance ) {
129
130
		$handler = 'ParserAfterTidy';
131
132
		$this->assertTrue(
133
			$instance->isRegistered( $handler )
134
		);
135
136
		$title = Title::newFromText( __METHOD__ );
137
138
		$parserOptions = $this->getMockBuilder( '\ParserOptions' )
139
			->disableOriginalConstructor()
140
			->getMock();
141
142
		$parserOutput = $this->getMockBuilder( '\ParserOutput' )
143
			->disableOriginalConstructor()
144
			->getMock();
145
146
		$parser = $this->getMockBuilder( '\Parser' )
147
			->disableOriginalConstructor()
148
			->getMock();
149
150
		$parser->expects( $this->any() )
151
			->method( 'getTitle' )
152
			->will( $this->returnValue( $title ) );
153
154
		$parser->expects( $this->any() )
155
			->method( 'getOptions' )
156
			->will( $this->returnValue( $parserOptions ) );
157
158
		$parser->expects( $this->any() )
159
			->method( 'getOutput' )
160
			->will( $this->returnValue( $parserOutput ) );
161
162
		$text = '';
163
164
		$this->assertThatHookIsExcutable(
165
			$instance->getHandlerFor( $handler ),
166
			array( &$parser, &$text )
167
		);
168
	}
169
170
	private function assertThatHookIsExcutable( \Closure $handler, $arguments ) {
171
		$this->assertInternalType(
172
			'boolean',
173
			call_user_func_array( $handler, $arguments )
174
		);
175
	}
176
177
}
178