Completed
Push — master ( ad2fa3...3f5086 )
by mw
02:16
created

HooksTest::callOnSMWConfigBeforeCompletion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace SMW\ApprovedRevs\Tests;
4
5
use SMW\ApprovedRevs\Hooks;
6
7
/**
8
 * @covers \SMW\ApprovedRevs\Hooks
9
 * @group semantic-approved-revs
10
 *
11
 * @license GNU GPL v2+
12
 * @since 1.0
13
 *
14
 * @author mwjames
15
 */
16
class HooksTest extends \PHPUnit_Framework_TestCase {
17
18
	public function testCanConstruct() {
19
20
		$config =  [];
0 ignored issues
show
Unused Code introduced by
$config 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...
21
22
		$this->assertInstanceOf(
23
			Hooks::class,
24
			new Hooks()
25
		);
26
	}
27
28
	public function testRegister() {
29
30
		$instance = new Hooks();
31
		$instance->deregister();
32
		$instance->register();
33
34
		$this->callOnApprovedRevsRevisionApproved( $instance );
35
		$this->callOnApprovedRevsFileRevisionApproved( $instance );
36
37
		$this->callOnSMWLinksUpdateApprovedUpdate( $instance );
38
		$this->callOnSMWDataUpdaterSkipUpdate( $instance );
39
		$this->callOnSMWParserChangeRevision( $instance );
40
		$this->callOnSMWFactboxOverrideRevisionID( $instance );
41
		$this->callOnSMWInitProperties( $instance );
42
		$this->callOnSMWStoreUpdateDataBefore( $instance );
43
		$this->callOnSMWConfigBeforeCompletion( $instance );
44
	}
45
46
	public function callOnApprovedRevsRevisionApproved( $instance ) {
47
48
		$handler = 'ApprovedRevsRevisionApproved';
49
50
		$title = $this->getMockBuilder( '\Title' )
51
			->disableOriginalConstructor()
52
			->getMock();
53
54
		$cache = $this->getMockBuilder( '\Onoi\Cache\Cache' )
55
			->disableOriginalConstructor()
56
			->getMock();
57
58
		$cache->expects( $this->once() )
59
			->method( 'save' )
60
			->with( $this->stringContains( 'smw:parseraftertidy' ) );
61
62
		$instance->setCache( $cache );
63
64
		$this->assertTrue(
65
			$instance->isRegistered( $handler )
66
		);
67
68
		$output = '';
69
		$rev_id = 42;
70
		$content = '';
71
72
		$this->assertThatHookIsExcutable(
73
			$instance->getHandlers( $handler ),
74
			[ $output, $title, $rev_id, $content ]
75
		);
76
	}
77
78
	public function callOnApprovedRevsFileRevisionApproved( $instance ) {
79
80
		$handler = 'ApprovedRevsFileRevisionApproved';
81
82
		$title = $this->getMockBuilder( '\Title' )
83
			->disableOriginalConstructor()
84
			->getMock();
85
86
		$cache = $this->getMockBuilder( '\Onoi\Cache\Cache' )
87
			->disableOriginalConstructor()
88
			->getMock();
89
90
		$cache->expects( $this->once() )
91
			->method( 'save' )
92
			->with( $this->stringContains( 'smw:parseraftertidy' ) );
93
94
		$instance->setCache( $cache );
95
96
		$this->assertTrue(
97
			$instance->isRegistered( $handler )
98
		);
99
100
		$parser = '';
101
		$timestamp = 42;
102
		$sha1 = '1001';
103
104
		$this->assertThatHookIsExcutable(
105
			$instance->getHandlers( $handler ),
106
			[ $parser, $title, $timestamp, $sha1 ]
107
		);
108
	}
109
110
	public function callOnSMWLinksUpdateApprovedUpdate( $instance ) {
111
112
		$handler = 'SMW::LinksUpdate::ApprovedUpdate';
113
114
		$title = $this->getMockBuilder( '\Title' )
115
			->disableOriginalConstructor()
116
			->getMock();
117
118
		$this->assertTrue(
119
			$instance->isRegistered( $handler )
120
		);
121
122
		$rev = 0;
123
124
		$this->assertThatHookIsExcutable(
125
			$instance->getHandlers( $handler ),
126
			[ $title, $rev ]
127
		);
128
	}
129
130
	public function callOnSMWDataUpdaterSkipUpdate( $instance ) {
131
132
		$handler = 'SMW::DataUpdater::SkipUpdate';
133
134
		$title = $this->getMockBuilder( '\Title' )
135
			->disableOriginalConstructor()
136
			->getMock();
137
138
		$this->assertTrue(
139
			$instance->isRegistered( $handler )
140
		);
141
142
		$rev = 0;
143
144
		$this->assertThatHookIsExcutable(
145
			$instance->getHandlers( $handler ),
146
			[ $title, $rev ]
147
		);
148
	}
149
150
	public function callOnSMWParserChangeRevision( $instance ) {
151
152
		$handler = 'SMW::Parser::ChangeRevision';
153
154
		$title = $this->getMockBuilder( '\Title' )
155
			->disableOriginalConstructor()
156
			->getMock();
157
158
		$this->assertTrue(
159
			$instance->isRegistered( $handler )
160
		);
161
162
		$revision = null;
163
164
		$this->assertThatHookIsExcutable(
165
			$instance->getHandlers( $handler ),
166
			[ $title, &$revision ]
167
		);
168
	}
169
170
	public function callOnSMWFactboxOverrideRevisionID( $instance ) {
171
172
		$handler = 'SMW::Factbox::OverrideRevisionID';
173
174
		$title = $this->getMockBuilder( '\Title' )
175
			->disableOriginalConstructor()
176
			->getMock();
177
178
		$this->assertTrue(
179
			$instance->isRegistered( $handler )
180
		);
181
182
		$latestRevID = 0;
183
184
		$this->assertThatHookIsExcutable(
185
			$instance->getHandlers( $handler ),
186
			[ $title, &$latestRevID ]
187
		);
188
	}
189
190
	public function callOnSMWInitProperties( $instance ) {
191
192
		$handler = 'SMW::Property::initProperties';
193
194
		$propertyRegistry = $this->getMockBuilder( '\SMW\PropertyRegistry' )
195
			->disableOriginalConstructor()
196
			->getMock();
197
198
		$this->assertTrue(
199
			$instance->isRegistered( $handler )
200
		);
201
202
		$this->assertThatHookIsExcutable(
203
			$instance->getHandlers( $handler ),
204
			[ $propertyRegistry ]
205
		);
206
	}
207
208
	public function callOnSMWStoreUpdateDataBefore( $instance ) {
209
210
		$handler = 'SMWStore::updateDataBefore';
211
212
		$store = $this->getMockBuilder( '\SMW\Store' )
213
			->disableOriginalConstructor()
214
			->getMockForAbstractClass();
215
216
		$semanticData = $this->getMockBuilder( '\SMW\SemanticData' )
217
			->disableOriginalConstructor()
218
			->getMock();
219
220
		$this->assertTrue(
221
			$instance->isRegistered( $handler )
222
		);
223
224
		$this->assertThatHookIsExcutable(
225
			$instance->getHandlers( $handler ),
226
			[ $store, $semanticData ]
227
		);
228
	}
229
230
	public function callOnSMWConfigBeforeCompletion( $instance ) {
231
232
		$handler = 'SMW::Config::BeforeCompletion';
233
234
		$this->assertTrue(
235
			$instance->isRegistered( $handler )
236
		);
237
238
		$config = [
239
			'smwgImportFileDirs' => []
240
		];
241
242
		$this->assertThatHookIsExcutable(
243
			$instance->getHandlers( $handler ),
244
			[ &$config ]
245
		);
246
247
		$this->assertArrayHasKey(
248
			'sar',
249
			$config['smwgImportFileDirs']
250
		);
251
	}
252
253
	private function assertThatHookIsExcutable( $hooks, $arguments ) {
254
255
		if ( is_callable( $hooks ) ) {
256
			$hooks = [ $hooks ];
257
		}
258
259
		foreach ( $hooks as $hook ) {
260
261
			$this->assertInternalType(
262
				'boolean',
263
				call_user_func_array( $hook, $arguments )
264
			);
265
		}
266
	}
267
268
}
269