Completed
Push — master ( 6a3f6b...ad2fa3 )
by mw
02:09
created

HooksTest::callOnApprovedRevsRevisionApproved()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 9.424
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
	}
44
45
	public function callOnApprovedRevsRevisionApproved( $instance ) {
46
47
		$handler = 'ApprovedRevsRevisionApproved';
48
49
		$title = $this->getMockBuilder( '\Title' )
50
			->disableOriginalConstructor()
51
			->getMock();
52
53
		$cache = $this->getMockBuilder( '\Onoi\Cache\Cache' )
54
			->disableOriginalConstructor()
55
			->getMock();
56
57
		$cache->expects( $this->once() )
58
			->method( 'save' )
59
			->with( $this->stringContains( 'smw:parseraftertidy' ) );
60
61
		$instance->setCache( $cache );
62
63
		$this->assertTrue(
64
			$instance->isRegistered( $handler )
65
		);
66
67
		$output = '';
68
		$rev_id = 42;
69
		$content = '';
70
71
		$this->assertThatHookIsExcutable(
72
			$instance->getHandlers( $handler ),
73
			[ $output, $title, $rev_id, $content ]
74
		);
75
	}
76
77
	public function callOnApprovedRevsFileRevisionApproved( $instance ) {
78
79
		$handler = 'ApprovedRevsFileRevisionApproved';
80
81
		$title = $this->getMockBuilder( '\Title' )
82
			->disableOriginalConstructor()
83
			->getMock();
84
85
		$cache = $this->getMockBuilder( '\Onoi\Cache\Cache' )
86
			->disableOriginalConstructor()
87
			->getMock();
88
89
		$cache->expects( $this->once() )
90
			->method( 'save' )
91
			->with( $this->stringContains( 'smw:parseraftertidy' ) );
92
93
		$instance->setCache( $cache );
94
95
		$this->assertTrue(
96
			$instance->isRegistered( $handler )
97
		);
98
99
		$parser = '';
100
		$timestamp = 42;
101
		$sha1 = '1001';
102
103
		$this->assertThatHookIsExcutable(
104
			$instance->getHandlers( $handler ),
105
			[ $parser, $title, $timestamp, $sha1 ]
106
		);
107
	}
108
109
	public function callOnSMWLinksUpdateApprovedUpdate( $instance ) {
110
111
		$handler = 'SMW::LinksUpdate::ApprovedUpdate';
112
113
		$title = $this->getMockBuilder( '\Title' )
114
			->disableOriginalConstructor()
115
			->getMock();
116
117
		$this->assertTrue(
118
			$instance->isRegistered( $handler )
119
		);
120
121
		$rev = 0;
122
123
		$this->assertThatHookIsExcutable(
124
			$instance->getHandlers( $handler ),
125
			[ $title, $rev ]
126
		);
127
	}
128
129
	public function callOnSMWDataUpdaterSkipUpdate( $instance ) {
130
131
		$handler = 'SMW::DataUpdater::SkipUpdate';
132
133
		$title = $this->getMockBuilder( '\Title' )
134
			->disableOriginalConstructor()
135
			->getMock();
136
137
		$this->assertTrue(
138
			$instance->isRegistered( $handler )
139
		);
140
141
		$rev = 0;
142
143
		$this->assertThatHookIsExcutable(
144
			$instance->getHandlers( $handler ),
145
			[ $title, $rev ]
146
		);
147
	}
148
149
	public function callOnSMWParserChangeRevision( $instance ) {
150
151
		$handler = 'SMW::Parser::ChangeRevision';
152
153
		$title = $this->getMockBuilder( '\Title' )
154
			->disableOriginalConstructor()
155
			->getMock();
156
157
		$this->assertTrue(
158
			$instance->isRegistered( $handler )
159
		);
160
161
		$revision = null;
162
163
		$this->assertThatHookIsExcutable(
164
			$instance->getHandlers( $handler ),
165
			[ $title, &$revision ]
166
		);
167
	}
168
169
	public function callOnSMWFactboxOverrideRevisionID( $instance ) {
170
171
		$handler = 'SMW::Factbox::OverrideRevisionID';
172
173
		$title = $this->getMockBuilder( '\Title' )
174
			->disableOriginalConstructor()
175
			->getMock();
176
177
		$this->assertTrue(
178
			$instance->isRegistered( $handler )
179
		);
180
181
		$latestRevID = 0;
182
183
		$this->assertThatHookIsExcutable(
184
			$instance->getHandlers( $handler ),
185
			[ $title, &$latestRevID ]
186
		);
187
	}
188
189
	public function callOnSMWInitProperties( $instance ) {
190
191
		$handler = 'SMW::Property::initProperties';
192
193
		$propertyRegistry = $this->getMockBuilder( '\SMW\PropertyRegistry' )
194
			->disableOriginalConstructor()
195
			->getMock();
196
197
		$this->assertTrue(
198
			$instance->isRegistered( $handler )
199
		);
200
201
		$this->assertThatHookIsExcutable(
202
			$instance->getHandlers( $handler ),
203
			[ $propertyRegistry ]
204
		);
205
	}
206
207
	public function callOnSMWStoreUpdateDataBefore( $instance ) {
208
209
		$handler = 'SMWStore::updateDataBefore';
210
211
		$store = $this->getMockBuilder( '\SMW\Store' )
212
			->disableOriginalConstructor()
213
			->getMockForAbstractClass();
214
215
		$semanticData = $this->getMockBuilder( '\SMW\SemanticData' )
216
			->disableOriginalConstructor()
217
			->getMock();
218
219
		$this->assertTrue(
220
			$instance->isRegistered( $handler )
221
		);
222
223
		$this->assertThatHookIsExcutable(
224
			$instance->getHandlers( $handler ),
225
			[ $store, $semanticData ]
226
		);
227
	}
228
229
	private function assertThatHookIsExcutable( $hooks, $arguments ) {
230
231
		if ( is_callable( $hooks ) ) {
232
			$hooks = [ $hooks ];
233
		}
234
235
		foreach ( $hooks as $hook ) {
236
237
			$this->assertInternalType(
238
				'boolean',
239
				call_user_func_array( $hook, $arguments )
240
			);
241
		}
242
	}
243
244
}
245