HookRegistryTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 150
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testCanConstruct() 0 11 1
A testRegister() 0 23 1
A doTestRegisteredBeforePageDisplay() 0 21 1
A doTestRegisteredNewRevisionFromEditComplete() 0 35 1
A doTestRegisteredGetPreferences() 0 24 1
A doTestRegisteredResourceLoaderGetConfigVars() 0 20 1
A assertThatHookIsExcutable() 0 6 1
1
<?php
2
3
namespace SUC\Tests;
4
5
use SUC\HookRegistry;
6
use SUC\Options;
7
8
/**
9
 * @covers \SUC\HookRegistry
10
 * @group summary-cards
11
 *
12
 * @license GNU GPL v2+
13
 * @since 1.0
14
 *
15
 * @author mwjames
16
 */
17
class HookRegistryTest extends \PHPUnit_Framework_TestCase {
18
19
	public function testCanConstruct() {
20
21
		$options = $this->getMockBuilder( Options::class )
22
			->disableOriginalConstructor()
23
			->getMock();
24
25
		$this->assertInstanceOf(
26
			HookRegistry::class,
27
			new HookRegistry( $options )
28
		);
29
	}
30
31
	public function testRegister() {
32
33
		$configuration = array(
34
			'tooltipRequestCacheTTL'       => 1,
35
			'cachePrefix'                  => false,
36
			'enabledNamespaceWithTemplate' => array(),
37
			'enabledForAnonUsers'          => false,
38
			'backendParserCacheLifetime'   => 1,
39
			'backendParserCacheType'       => 'hash'
40
		);
41
42
		$instance = new HookRegistry(
43
			new Options( $configuration )
44
		);
45
46
		$instance->clear();
47
		$instance->register();
48
49
		$this->doTestRegisteredBeforePageDisplay( $instance );
50
		$this->doTestRegisteredNewRevisionFromEditComplete( $instance );
51
		$this->doTestRegisteredGetPreferences( $instance );
52
		$this->doTestRegisteredResourceLoaderGetConfigVars( $instance );
53
	}
54
55
	public function doTestRegisteredBeforePageDisplay( $instance ) {
56
57
		$hook = 'BeforePageDisplay';
58
59
		$this->assertTrue(
60
			$instance->isRegistered( $hook )
61
		);
62
63
		$outputPage = $this->getMockBuilder( '\OutputPage' )
64
			->disableOriginalConstructor()
65
			->getMock();
66
67
		$skin = $this->getMockBuilder( '\Skin' )
68
			->disableOriginalConstructor()
69
			->getMock();
70
71
		$this->assertThatHookIsExcutable(
72
			$instance->getHandlerFor( $hook ),
73
			array( &$outputPage, &$skin )
74
		);
75
	}
76
77
	public function doTestRegisteredNewRevisionFromEditComplete( $instance ) {
78
79
		$hook = 'NewRevisionFromEditComplete';
80
81
		$this->assertTrue(
82
			$instance->isRegistered( $hook )
83
		);
84
85
		$title = $this->getMockBuilder( '\Title' )
86
			->disableOriginalConstructor()
87
			->getMock();
88
89
		$wikiPage = $this->getMockBuilder( '\WikiPage' )
90
			->disableOriginalConstructor()
91
			->getMock();
92
93
		$wikiPage->expects( $this->once() )
94
			->method( 'getTitle' )
95
			->will( $this->returnValue( $title ) );
96
97
		$revision = $this->getMockBuilder( '\Revision' )
98
			->disableOriginalConstructor()
99
			->getMock();
100
101
		$user = $this->getMockBuilder( '\User' )
102
			->disableOriginalConstructor()
103
			->getMock();
104
105
		$baseId = 0;
106
107
		$this->assertThatHookIsExcutable(
108
			$instance->getHandlerFor( $hook ),
109
			array( $wikiPage, $revision, $baseId, $user )
110
		);
111
	}
112
113
	public function doTestRegisteredGetPreferences( $instance ) {
114
115
		$hook = 'GetPreferences';
116
117
		$this->assertTrue(
118
			$instance->isRegistered( $hook )
119
		);
120
121
		$user = $this->getMockBuilder( '\User' )
122
			->disableOriginalConstructor()
123
			->getMock();
124
125
		$preferences = array();
126
127
		$this->assertThatHookIsExcutable(
128
			$instance->getHandlerFor( $hook ),
129
			array( $user, &$preferences )
130
		);
131
132
		$this->assertArrayHasKey(
133
			'suc-tooltip-disabled',
134
			$preferences
135
		);
136
	}
137
138
	public function doTestRegisteredResourceLoaderGetConfigVars( $instance ) {
139
140
		$hook = 'ResourceLoaderGetConfigVars';
141
142
		$this->assertTrue(
143
			$instance->isRegistered( $hook )
144
		);
145
146
		$vars = array();
147
148
		$this->assertThatHookIsExcutable(
149
			$instance->getHandlerFor( $hook ),
150
			array( &$vars )
151
		);
152
153
		$this->assertArrayHasKey(
154
			'ext.summaryCards.config',
155
			$vars
156
		);
157
	}
158
159
	private function assertThatHookIsExcutable( \Closure $handler, $arguments = array() ) {
160
		$this->assertInternalType(
161
			'boolean',
162
			call_user_func_array( $handler, $arguments )
163
		);
164
	}
165
166
}
167