Completed
Push — master ( 043426...79cf54 )
by mw
138:09 queued 103:23
created

PageUpdaterTest::testPurge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
nc 1
nop 2
dl 0
loc 18
rs 9.4285
c 1
b 0
f 0
1
<?php
2
3
namespace SMW\Tests\MediaWiki;
4
5
use SMW\MediaWiki\PageUpdater;
6
7
/**
8
 * @covers \SMW\MediaWiki\PageUpdater
9
 * @group semantic-mediawiki
10
 *
11
 * @license GNU GPL v2+
12
 * @since   2.1
13
 *
14
 * @author mwjames
15
 */
16
class PageUpdaterTest extends \PHPUnit_Framework_TestCase {
17
18
	private $connection;
19
20
	protected function setUp() {
21
		parent::setup();
22
23
		$this->connection = $this->getMockBuilder( '\SMW\MediaWiki\Database' )
24
			->disableOriginalConstructor()
25
			->getMock();
26
	}
27
28
	public function testCanConstruct() {
29
30
		$this->assertInstanceOf(
31
			'\SMW\MediaWiki\PageUpdater',
32
			 new PageUpdater()
33
		);
34
	}
35
36
	public function testCanUpdate() {
37
38
		$instance = new PageUpdater();
39
40
		$this->assertInternalType(
41
			'boolean',
42
			 $instance->canUpdate()
43
		);
44
	}
45
46
	/**
47
	 * @dataProvider purgeMethodProvider
48
	 */
49
	public function testPurge( $purgeMethod, $titleMethod ) {
50
51
		$title = $this->getMockBuilder( '\Title' )
52
			->disableOriginalConstructor()
53
			->getMock();
54
55
		$title->expects( $this->once() )
56
			->method( 'getDBKey' )
57
			->will( $this->returnValue( 'Foo' ) );
58
59
		$title->expects( $this->once() )
60
			->method( $titleMethod );
61
62
		$instance = new PageUpdater();
63
		$instance->addPage( $title );
64
65
		call_user_func( [ $instance, $purgeMethod ] );
66
	}
67
68
	public function testDisablePurgeHtmlCache() {
69
70
		$title = $this->getMockBuilder( '\Title' )
71
			->disableOriginalConstructor()
72
			->getMock();
73
74
		$title->expects( $this->once() )
75
			->method( 'getDBKey' )
76
			->will( $this->returnValue( 'Foo' ) );
77
78
		$title->expects( $this->never() )
79
			->method( 'touchLinks' );
80
81
		$instance = new PageUpdater();
82
		$instance->addPage( $title );
83
84
		$instance->isHtmlCacheUpdate( false );
85
		$instance->doPurgeHtmlCache();
86
	}
87
88
	public function testFilterDuplicatePages() {
89
90
		$title = $this->getMockBuilder( '\Title' )
91
			->disableOriginalConstructor()
92
			->getMock();
93
94
		$title->expects( $this->exactly( 2 ) )
95
			->method( 'getDBKey' )
96
			->will( $this->returnValue( 'Foo' ) );
97
98
		$title->expects( $this->once() )
99
			->method( 'invalidateCache' );
100
101
		$instance = new PageUpdater();
102
		$instance->addPage( $title );
103
		$instance->addPage( $title );
104
105
		$instance->doPurgeParserCache();
106
	}
107
108
	/**
109
	 * @dataProvider purgeMethodProvider
110
	 */
111
	public function testPurgeOnTransactionIdle( $purgeMethod, $titleMethod ) {
112
113
		$this->connection->expects( $this->once() )
114
			->method( 'onTransactionIdle' )
115
			->will( $this->returnCallback( function( $callback ) {
116
				return call_user_func( $callback ); }
117
			) );
118
119
		$title = $this->getMockBuilder( '\Title' )
120
			->disableOriginalConstructor()
121
			->getMock();
122
123
		$title->expects( $this->once() )
124
			->method( 'getDBKey' )
125
			->will( $this->returnValue( 'Foo' ) );
126
127
		$title->expects( $this->once() )
128
			->method( $titleMethod );
129
130
		$instance = new PageUpdater( $this->connection );
131
		$instance->addPage( $title );
132
133
		$instance->waitOnTransactionIdle();
134
135
		call_user_func( [ $instance, $purgeMethod ] );
136
	}
137
138
	/**
139
	 * @dataProvider purgeMethodProvider
140
	 */
141
	public function testPurgeWillNotWaitOnTransactionIdleForMissingConnection(  $purgeMethod, $titleMethod ) {
142
143
		$title = $this->getMockBuilder( '\Title' )
144
			->disableOriginalConstructor()
145
			->getMock();
146
147
		$title->expects( $this->once() )
148
			->method( 'getDBKey' )
149
			->will( $this->returnValue( 'Foo' ) );
150
151
		$title->expects( $this->once() )
152
			->method(  $titleMethod );
153
154
		$instance = new PageUpdater();
155
		$instance->addPage( $title );
156
157
		$instance->waitOnTransactionIdle();
158
159
		call_user_func( [ $instance, $purgeMethod ] );
160
	}
161
162
	/**
163
	 * @dataProvider purgeMethodProvider
164
	 */
165
	public function testPurgeWillNotWaitOnTransactionIdleWhenCommandLineIsTrue( $purgeMethod, $titleMethod ) {
166
167
		$this->connection->expects( $this->never() )
168
			->method( 'onTransactionIdle' );
169
170
		$title = $this->getMockBuilder( '\Title' )
171
			->disableOriginalConstructor()
172
			->getMock();
173
174
		$title->expects( $this->once() )
175
			->method( 'getDBKey' )
176
			->will( $this->returnValue( 'Foo' ) );
177
178
		$title->expects( $this->once() )
179
			->method( $titleMethod );
180
181
		$instance = new PageUpdater( $this->connection );
182
		$instance->addPage( $title );
183
		$instance->isCommandLineMode( true );
184
185
		$instance->waitOnTransactionIdle();
186
187
		call_user_func( [ $instance, $purgeMethod ] );
188
	}
189
190
	public function testAddNullPage() {
191
192
		$title = $this->getMockBuilder( '\Title' )
193
			->disableOriginalConstructor()
194
			->getMock();
195
196
		$title->expects( $this->never() )
197
			->method( 'getDBKey' )
198
			->will( $this->returnValue( 'Foo' ) );
199
200
		$instance = new PageUpdater();
201
		$instance->addPage( null );
202
	}
203
204
	/**
205
	 * @dataProvider purgeMethodProvider
206
	 */
207
	public function testPushPendingWaitableUpdate( $purgeMethod, $titleMethod ) {
208
209
		$this->connection->expects( $this->once() )
210
			->method( 'onTransactionIdle' )
211
			->will( $this->returnCallback( function( $callback ) {
212
				return call_user_func( $callback ); }
213
			) );
214
215
		$title = $this->getMockBuilder( '\Title' )
216
			->disableOriginalConstructor()
217
			->getMock();
218
219
		$title->expects( $this->once() )
220
			->method( 'getDBKey' )
221
			->will( $this->returnValue( 'Foo' ) );
222
223
		$title->expects( $this->once() )
224
			->method( $titleMethod );
225
226
		$instance = new PageUpdater( $this->connection );
227
		$instance->addPage( $title );
228
229
		$instance->markAsPending();
230
		$instance->waitOnTransactionIdle();
231
232
		// To avoid the DeferredUpdates queue
233
		$instance->isCommandLineMode( true );
234
235
		call_user_func( [ $instance, $purgeMethod ] );
236
237
		$instance->pushUpdate();
238
	}
239
240
	public function testPurgeCacheAsPoolPurge() {
241
242
		$row = new \stdClass;
243
		$row->page_id = 42;
244
245
		$this->connection->expects( $this->once() )
246
			->method( 'select' )
247
			->will( $this->returnValue( array( $row ) ) );
248
249
		$this->connection->expects( $this->once() )
250
			->method( 'update' );
251
252
		$this->connection->expects( $this->once() )
253
			->method( 'onTransactionIdle' )
254
			->will( $this->returnCallback( function( $callback ) {
255
				return call_user_func( $callback ); }
256
			) );
257
258
		$title = $this->getMockBuilder( '\Title' )
259
			->disableOriginalConstructor()
260
			->getMock();
261
262
		$title->expects( $this->once() )
263
			->method( 'getDBKey' )
264
			->will( $this->returnValue( 'Foo' ) );
265
266
		$title->expects( $this->never() )
267
			->method( 'invalidateCache' );
268
269
		$instance = new PageUpdater( $this->connection );
270
		$instance->addPage( $title );
271
272
		$instance->asPoolPurge();
273
		$instance->waitOnTransactionIdle();
274
275
		$instance->doPurgeParserCache();
276
	}
277
278
	public function purgeMethodProvider() {
279
280
		$provider[] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$provider was never initialized. Although not strictly required by PHP, it is generally a good practice to add $provider = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
281
			'doPurgeParserCache',
282
			'invalidateCache'
283
		);
284
285
286
		$provider[] = array(
287
			'doPurgeHtmlCache',
288
			'touchLinks'
289
		);
290
291
		$provider[] = array(
292
			'doPurgeWebCache',
293
			'purgeSquid'
294
		);
295
296
		return $provider;
297
	}
298
299
}
300