Passed
Branch master (631cc3)
by Stephan
33:03
created

BasicBackendTest::provideForTestNext()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 104
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 51
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 104
rs 8.2857

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * This file is part of the MediaWiki extension Lingo.
4
 *
5
 * @copyright 2011 - 2017, Stephan Gambke
6
 * @license   GNU General Public License, version 2 (or any later version)
7
 *
8
 * The Lingo extension is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by the Free
10
 * Software Foundation; either version 2 of the License, or (at your option) any
11
 * later version.
12
 *
13
 * The Lingo extension is distributed in the hope that it will be useful, but
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16
 * details.
17
 *
18
 * You should have received a copy of the GNU General Public License along
19
 * with this program. If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 * @author Stephan Gambke
22
 * @since 2.0
23
 * @file
24
 * @ingroup Lingo
25
 */
26
27
namespace Lingo\Tests\Unit;
28
29
use Lingo\BasicBackend;
30
31
/**
32
 * @group extensions-lingo
33
 * @group extensions-lingo-unit
34
 * @group mediawiki-databaseless
35
 *
36
 * @coversDefaultClass \Lingo\BasicBackend
37
 * @covers ::<private>
38
 * @covers ::<protected>
39
 *
40
 * @ingroup Lingo
41
 * @ingroup Test
42
 */
43
class BasicBackendTest extends BackendTest {
44
45
	/**
46
	 * @covers ::__construct
47
	 */
48
	public function testCanConstruct() {
49
50
		$this->assertInstanceOf(
51
			'\Lingo\BasicBackend',
52
			new \Lingo\BasicBackend()
53
		);
54
	}
55
56
	/**
57
	 * @covers ::purgeCache
58
	 */
59
	public function testPurgeCache() {
60
61
		$GLOBALS[ 'wgexLingoPage' ] = 'SomePage';
62
63
		$title = $this->getMock( 'Title' );
64
65
		$wikiPage = $this->getMockBuilder( 'WikiPage' )
66
			->disableOriginalConstructor()
67
			->getMock();
68
69
		$lingoParser = $this->getMock( 'Lingo\LingoParser' );
70
71
		$testObject = $this->getMockBuilder( 'Lingo\BasicBackend' )
72
			->setMethods( array( 'getLingoParser' ) )
73
			->getMock();
74
75
76
		// Assert that the wikipage is tested against the wgexLingoPage, i.e.
77
		// that $wikipage->getTitle()->getText() === $page is tested
78
79
		$wikiPage->expects( $this->once() )
80
			->method( 'getTitle' )
81
			->willReturn( $title );
82
83
		$title->expects( $this->once() )
84
			->method( 'getText' )
85
			->willReturn( 'SomePage' );
86
87
		// Assert that purgeGlossaryFromCache is called
88
		$lingoParser->expects( $this->once() )
89
			->method( 'purgeGlossaryFromCache' );
90
91
92
		$testObject->expects( $this->once() )
93
			->method( 'getLingoParser' )
94
			->willReturn( $lingoParser );
95
96
		$this->assertTrue( $testObject->purgeCache( $wikiPage ) );
97
	}
98
99
	/**
100
	 * @covers ::useCache
101
	 */
102
	public function testUseCache() {
103
		$backend = new BasicBackend();
104
		$this->assertTrue( $backend->useCache() );
105
	}
106
107
	/**
108
	 * @covers ::next
109
	 * @dataProvider provideForTestNext
110
	 */
111
	public function testNext( $lingoPageText, $expectedResults ) {
112
113
		$backend = $this->getTestObject( $lingoPageText );
114
		foreach ( $expectedResults as $expected ) {
115
			$this->assertEquals( $expected, $backend->next() );
116
		}
117
	}
118
119
	public function testNext_LingoPageIsInterwiki() {
120
121
		$backend = $this->getTestObject( ';SOT:Some old text', 'view', 'someInterwiki' );
122
		$backend->getMessageLog()->expects( $this->once() )
123
			->method( 'addError' )
124
			->willReturn( null );
125
126
		$this->assertNull( $backend->next() );
127
	}
128
129
	public function testNext_LingoPageWasJustEdited() {
130
131
		$backend = $this->getTestObject( ';SOT:Some old text', 'submit' );
132
		$this->assertEquals( array( 'JST', 'Just saved text', null, null ), $backend->next() );
133
	}
134
135
	public function testNext_LingoPageDoesNotExist() {
136
137
		$backend = $this->getTestObject( ';SOT:Some old text', 'view', '', null, false );
138
		$backend->getMessageLog()->expects( $this->once() )
139
			->method( 'addWarning' )
140
			->willReturn( null );
141
142
		$this->assertEquals( null, $backend->next() );
143
	}
144
145
	public function testNext_LingoPageNotAccessible() {
146
147
		$backend = $this->getTestObject( ';SOT:Some old text', 'view', '', false, null );
148
		$this->assertEquals( null, $backend->next() );
149
	}
150
151
	public function testNext_LingoPageIsNotATextPage() {
152
153
		$backend = $this->getTestObject( ';SOT:Some old text', 'view', '', false, 'This is not a TextContent object' );
154
		$backend->getMessageLog()->expects( $this->once() )
155
			->method( 'addError' )
156
			->willReturn( null );
157
158
		$this->assertEquals( null, $backend->next() );
159
	}
160
161
	public function testNext_ApprovedRevsEnabledButNotInstalled() {
162
163
		$backend = $this->getTestObject( ';SOT:Some old text', 'view', '', false, false, ';SAT:Some approved text' );
164
		$backend->getMessageLog()->expects( $this->once() )
165
			->method( 'addWarning' )
166
			->willReturn( null );
167
168
		$GLOBALS[ 'wgexLingoEnableApprovedRevs' ] = true;
169
170
		$this->assertEquals( array( 'SOT', 'Some old text', null, null ), $backend->next() );
171
	}
172
173
	public function testNext_ApprovedRevsEnabledAndInstalled() {
174
175
		$backend = $this->getTestObject( ';SOT:Some old text', 'view', '', false, false, ';SAT:Some approved text' );
176
177
		$GLOBALS[ 'wgexLingoEnableApprovedRevs' ] = true;
178
		define( 'APPROVED_REVS_VERSION', '42' );
179
180
		$this->assertEquals( array( 'SAT', 'Some approved text', null, null ), $backend->next() );
181
	}
182
183
184
	/**
185
	 * @return array
186
	 */
187
	public function provideForTestNext() {
188
		return array(
189
190
			// Empty page
191
			array(
192
				'',
193
				array( null )
194
			),
195
196
			// Simple entries
197
			array(
198
<<<'TESTTEXT'
199
;CIP:Common image point
200
;CMP:Common midpoint
201
TESTTEXT
202
			,
203
				array(
204
					array( 'CMP', 'Common midpoint', null, null ),
205
					array( 'CIP', 'Common image point', null, null ),
206
				),
207
			),
208
209
			// Simple entries with line break
210
			array(
211
<<<'TESTTEXT'
212
;CIP
213
:Common image point
214
;CMP
215
:Common midpoint
216
TESTTEXT
217
			,
218
				array(
219
					array( 'CMP', 'Common midpoint', null, null ),
220
					array( 'CIP', 'Common image point', null, null ),
221
				),
222
			),
223
224
			// Two terms having the same definition
225
			array(
226
<<<'TESTTEXT'
227
;CIP
228
;CMP
229
:Common midpoint
230
TESTTEXT
231
			,
232
				array(
233
					array( 'CMP', 'Common midpoint', null, null ),
234
					array( 'CIP', 'Common midpoint', null, null ),
235
				),
236
			),
237
238
			// One term having two definitions
239
			array(
240
<<<'TESTTEXT'
241
;CIP
242
:Common image point
243
:Common midpoint
244
TESTTEXT
245
			,
246
				array(
247
					array( 'CIP', 'Common image point', null, null ),
248
					array( 'CIP', 'Common midpoint', null, null ),
249
				),
250
			),
251
252
			// Two terms sharing two definitions
253
			array(
254
<<<'TESTTEXT'
255
;CIP
256
;CMP
257
:Common image point
258
:Common midpoint
259
TESTTEXT
260
			,
261
				array(
262
					array( 'CMP', 'Common image point', null, null ),
263
					array( 'CMP', 'Common midpoint', null, null ),
264
					array( 'CIP', 'Common image point', null, null ),
265
					array( 'CIP', 'Common midpoint', null, null ),
266
				),
267
			),
268
269
			// Mixed entries and noise
270
			array(
271
<<<'TESTTEXT'
272
;CIP:Common image point
273
; CMP : Common midpoint
274
275
;DIMO
276
;DMO
277
:Dip move-out
278
279
== headline ==
280
Sed ut perspiciatis unde; omnis iste natus error: sit voluptatem accusantium...
281
282
;NMO:Normal move-out
283
TESTTEXT
284
			,
285
				array(
286
					array( 'NMO', 'Normal move-out', null, null ),
287
					array( 'DMO', 'Dip move-out', null, null ),
288
					array( 'DIMO', 'Dip move-out', null, null ),
289
					array( 'CMP', 'Common midpoint', null, null ),
290
					array( 'CIP', 'Common image point', null, null ),
291
				),
292
			),
293
294
		);
295
	}
296
297
	/**
298
	 * @return \PHPUnit_Framework_MockObject_MockObject
0 ignored issues
show
Bug introduced by
The type PHPUnit_Framework_MockObject_MockObject was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
299
	 */
300
	protected function getTestObject( $lingoPageText = '', $action = 'view', $interwiki = '', $lingoPageRevision = false, $lingoPageContent = false, $lingoApprovedText = '' ) {
301
		$messageLog = $this->getMock( 'Lingo\MessageLog' );
302
303
		$backend = $this->getMockBuilder( 'Lingo\BasicBackend' )
304
			->disableOriginalConstructor()
305
			->setMethods( array(
306
				'getLatestRevisionFromTitle',
307
				'getApprovedRevisionFromTitle',
308
				'getTitleFromText',
309
			) )
310
			->getMock();
311
312
		$reflected = new \ReflectionClass( '\Lingo\BasicBackend' );
313
		$constructor = $reflected->getConstructor();
314
		$constructor->invokeArgs( $backend, array( &$messageLog ) );
315
316
		$GLOBALS[ 'wgLingoPageName' ] = 'SomePage';
317
318
		$lingoPageTitle = $this->getMock( 'Title' );
319
		$lingoPageTitle->expects( $this->once() )
320
			->method( 'getInterwiki' )
321
			->willReturn( $interwiki );
322
		$lingoPageTitle->expects( $this->any() )
323
			->method( 'getArticleID' )
324
			->willReturn( 'Foom' );
325
326
		$backend->expects( $this->any() )
327
			->method( 'getTitleFromText' )
328
			->willReturn( $lingoPageTitle );
329
330
		$request = $this->getMock( 'FauxRequest' );
331
		$request->expects( $this->any() )
332
			->method( 'getVal' )
333
			->willReturnMap( array(
334
				array( 'action', 'view', $action ), // action = submit
335
				array( 'title', null, $lingoPageTitle ), // title = $lingoPageTitle
336
				array( 'wpTextbox1', null, ';JST:Just saved text' )
337
			) );
338
339
		$GLOBALS[ 'wgRequest' ] = $request;
340
341
		unset( $GLOBALS[ 'wgexLingoEnableApprovedRevs' ] );
342
343
		$backend->expects( $this->any() )
344
			->method( 'getLatestRevisionFromTitle' )
345
			->willReturn( $this->getRevisionMock( $lingoPageText, $lingoPageRevision, $lingoPageContent ) );
346
347
		$backend->expects( $this->any() )
348
			->method( 'getApprovedRevisionFromTitle' )
349
			->willReturn( $this->getRevisionMock( $lingoApprovedText ) );
350
351
		return $backend;
352
	}
353
354
	/**
355
	 * @param $lingoPageText
356
	 * @param $lingoPageRevision
357
	 * @param $lingoPageContent
358
	 * @return \PHPUnit_Framework_MockObject_MockObject
359
	 */
360
	protected function getRevisionMock( $lingoPageText, $lingoPageRevision = false, $lingoPageContent = false ) {
361
		if ( $lingoPageRevision === false ) {
362
363
			if ( $lingoPageContent === false ) {
364
				$lingoPageContent = $this->getMockBuilder( 'TextContent' )
365
					->disableOriginalConstructor()
366
					->getMock();
367
				$lingoPageContent->expects( $this->any() )
368
					->method( 'getNativeData' )
369
					->willReturn( $lingoPageText );
370
			}
371
372
			$lingoPageRevision = $this->getMockBuilder( 'Revision' )
373
				->disableOriginalConstructor()
374
				->getMock();
375
			$lingoPageRevision->expects( $this->any() )
376
				->method( 'getContent' )
377
				->willReturn( $lingoPageContent );
378
			return $lingoPageRevision;
379
		}
380
		return $lingoPageRevision;
381
	}
382
383
}
384