Completed
Push — master ( 28f5b7...87cc9d )
by mw
10:20
created

HooksTest::callOnSMWRevisionGuardChangeFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
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->callOnSMWRevisionGuardIsApprovedRevision( $instance );
38
		$this->callOnSMWRevisionGuardChangeRevision( $instance );
39
		$this->callOnSMWRevisionGuardChangeRevisionID( $instance );
40
		$this->callOnSMWInitProperties( $instance );
41
		$this->callOnSMWStoreUpdateDataBefore( $instance );
42
		$this->callOnSMWConfigBeforeCompletion( $instance );
43
		$this->callOnSMWRevisionGuardChangeFile( $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 callOnSMWRevisionGuardIsApprovedRevision( $instance ) {
111
112
		$handler = 'SMW::RevisionGuard::IsApprovedRevision';
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 callOnSMWRevisionGuardChangeRevision( $instance ) {
131
132
		$handler = 'SMW::RevisionGuard::ChangeRevision';
133
134
		$title = $this->getMockBuilder( '\Title' )
135
			->disableOriginalConstructor()
136
			->getMock();
137
138
		$this->assertTrue(
139
			$instance->isRegistered( $handler )
140
		);
141
142
		$revision = null;
143
144
		$this->assertThatHookIsExcutable(
145
			$instance->getHandlers( $handler ),
146
			[ $title, &$revision ]
147
		);
148
	}
149
150
	public function callOnSMWRevisionGuardChangeRevisionID( $instance ) {
151
152
		$handler = 'SMW::RevisionGuard::ChangeRevisionID';
153
154
		$title = $this->getMockBuilder( '\Title' )
155
			->disableOriginalConstructor()
156
			->getMock();
157
158
		$this->assertTrue(
159
			$instance->isRegistered( $handler )
160
		);
161
162
		$latestRevID = 0;
163
164
		$this->assertThatHookIsExcutable(
165
			$instance->getHandlers( $handler ),
166
			[ $title, &$latestRevID ]
167
		);
168
	}
169
170
	public function callOnSMWInitProperties( $instance ) {
171
172
		$handler = 'SMW::Property::initProperties';
173
174
		$propertyRegistry = $this->getMockBuilder( '\SMW\PropertyRegistry' )
175
			->disableOriginalConstructor()
176
			->getMock();
177
178
		$this->assertTrue(
179
			$instance->isRegistered( $handler )
180
		);
181
182
		$this->assertThatHookIsExcutable(
183
			$instance->getHandlers( $handler ),
184
			[ $propertyRegistry ]
185
		);
186
	}
187
188
	public function callOnSMWStoreUpdateDataBefore( $instance ) {
189
190
		$handler = 'SMWStore::updateDataBefore';
191
192
		$store = $this->getMockBuilder( '\SMW\Store' )
193
			->disableOriginalConstructor()
194
			->getMockForAbstractClass();
195
196
		$semanticData = $this->getMockBuilder( '\SMW\SemanticData' )
197
			->disableOriginalConstructor()
198
			->getMock();
199
200
		$this->assertTrue(
201
			$instance->isRegistered( $handler )
202
		);
203
204
		$this->assertThatHookIsExcutable(
205
			$instance->getHandlers( $handler ),
206
			[ $store, $semanticData ]
207
		);
208
	}
209
210
	public function callOnSMWConfigBeforeCompletion( $instance ) {
211
212
		$handler = 'SMW::Config::BeforeCompletion';
213
214
		$this->assertTrue(
215
			$instance->isRegistered( $handler )
216
		);
217
218
		$config = [
219
			'smwgImportFileDirs' => []
220
		];
221
222
		$this->assertThatHookIsExcutable(
223
			$instance->getHandlers( $handler ),
224
			[ &$config ]
225
		);
226
227
		$this->assertArrayHasKey(
228
			'sar',
229
			$config['smwgImportFileDirs']
230
		);
231
	}
232
233
	public function callOnSMWRevisionGuardChangeFile( $instance ) {
234
235
		$handler = 'SMW::RevisionGuard::ChangeFile';
236
237
		$this->assertTrue(
238
			$instance->isRegistered( $handler )
239
		);
240
241
		$title = $this->getMockBuilder( '\Title' )
242
			->disableOriginalConstructor()
243
			->getMock();
244
245
		$file = null;
246
247
		$this->assertThatHookIsExcutable(
248
			$instance->getHandlers( $handler ),
249
			[ $title, &$file ]
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