Completed
Push — master ( 17dff3...f2d821 )
by mw
42:32 queued 07:38
created

testQueryIdStabilityForFixedSetOfParametersWithFingerprintMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 52
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 30
nc 1
nop 0
dl 0
loc 52
rs 9.4929
c 0
b 0
f 0

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
namespace SMW\Tests\ParserFunctions;
4
5
use ParserOutput;
6
use ReflectionClass;
7
use SMW\ApplicationFactory;
8
use SMW\ParserFunctions\AskParserFunction;
9
use SMW\Localizer;
10
use SMW\Tests\TestEnvironment;
11
use Title;
12
13
/**
14
 * @covers \SMW\ParserFunctions\AskParserFunction
15
 * @group semantic-mediawiki
16
 *
17
 * @license GNU GPL v2+
18
 * @since 1.9
19
 *
20
 * @author mwjames
21
 */
22
class AskParserFunctionTest extends \PHPUnit_Framework_TestCase {
23
24
	private $testEnvironment;
25
	private $semanticDataValidator;
26
27
	protected function setUp() {
28
		parent::setUp();
29
30
		$this->testEnvironment = new TestEnvironment();
31
		$this->semanticDataValidator = $this->testEnvironment->getUtilityFactory()->newValidatorFactory()->newSemanticDataValidator();
32
33
		$this->testEnvironment->addConfiguration( 'smwgQueryDurationEnabled', false );
34
		$this->testEnvironment->addConfiguration( 'smwgQMaxLimit', 1000 );
35
	}
36
37
	protected function tearDown() {
38
		$this->testEnvironment->tearDown();
39
		parent::tearDown();
40
	}
41
42
	public function testCanConstruct() {
43
44
		$parserData = $this->getMockBuilder( '\SMW\ParserData' )
45
			->disableOriginalConstructor()
46
			->getMock();
47
48
		$messageFormatter = $this->getMockBuilder( '\SMW\MessageFormatter' )
49
			->disableOriginalConstructor()
50
			->getMock();
51
52
		$circularReferenceGuard = $this->getMockBuilder( '\SMW\Utils\CircularReferenceGuard' )
53
			->disableOriginalConstructor()
54
			->getMock();
55
56
		$this->assertInstanceOf(
57
			'\SMW\ParserFunctions\AskParserFunction',
58
			new AskParserFunction( $parserData, $messageFormatter, $circularReferenceGuard )
59
		);
60
	}
61
62
	/**
63
	 * @dataProvider queryDataProvider
64
	 */
65
	public function testParse( array $params ) {
66
67
		$parserData = ApplicationFactory::getInstance()->newParserData(
68
			Title::newFromText( __METHOD__ ),
69
			new ParserOutput()
70
		);
71
72
		$messageFormatter = $this->getMockBuilder( '\SMW\MessageFormatter' )
73
			->disableOriginalConstructor()
74
			->getMock();
75
76
		$circularReferenceGuard = $this->getMockBuilder( '\SMW\Utils\CircularReferenceGuard' )
77
			->disableOriginalConstructor()
78
			->getMock();
79
80
		$instance = new AskParserFunction(
81
			$parserData,
82
			$messageFormatter,
83
			$circularReferenceGuard
84
		);
85
86
		$this->assertInternalType(
87
			'string',
88
			$instance->parse( $params )
89
		);
90
	}
91
92
	public function testIsQueryDisabled() {
93
94
		$parserData = $this->getMockBuilder( '\SMW\ParserData' )
95
			->disableOriginalConstructor()
96
			->getMock();
97
98
		$messageFormatter = $this->getMockBuilder( '\SMW\MessageFormatter' )
99
			->disableOriginalConstructor()
100
			->getMock();
101
102
		$messageFormatter->expects( $this->any() )
103
			->method( 'addFromKey' )
104
			->will( $this->returnSelf() );
105
106
		$messageFormatter->expects( $this->once() )
107
			->method( 'getHtml' );
108
109
		$circularReferenceGuard = $this->getMockBuilder( '\SMW\Utils\CircularReferenceGuard' )
110
			->disableOriginalConstructor()
111
			->getMock();
112
113
		$instance = new AskParserFunction(
114
			$parserData,
115
			$messageFormatter,
116
			$circularReferenceGuard
117
		);
118
119
		$instance->isQueryDisabled();
120
	}
121
122
	public function testSetShowMode() {
123
124
		$parserData = $this->getMockBuilder( '\SMW\ParserData' )
125
			->disableOriginalConstructor()
126
			->getMock();
127
128
		$messageFormatter = $this->getMockBuilder( '\SMW\MessageFormatter' )
129
			->disableOriginalConstructor()
130
			->getMock();
131
132
		$circularReferenceGuard = $this->getMockBuilder( '\SMW\Utils\CircularReferenceGuard' )
133
			->disableOriginalConstructor()
134
			->getMock();
135
136
		$instance = new AskParserFunction(
137
			$parserData,
138
			$messageFormatter,
139
			$circularReferenceGuard
140
		);
141
142
		$reflector = new ReflectionClass( '\SMW\ParserFunctions\AskParserFunction' );
143
		$showMode = $reflector->getProperty( 'showMode' );
144
		$showMode->setAccessible( true );
145
146
		$this->assertFalse( $showMode->getValue( $instance ) );
147
		$instance->setShowMode( true );
148
149
		$this->assertTrue( $showMode->getValue( $instance ) );
150
	}
151
152
	public function testCircularGuard() {
153
154
		$parserData = ApplicationFactory::getInstance()->newParserData(
155
			Title::newFromText( __METHOD__ ),
156
			new ParserOutput()
157
		);
158
159
		$messageFormatter = $this->getMockBuilder( '\SMW\MessageFormatter' )
160
			->disableOriginalConstructor()
161
			->getMock();
162
163
		$circularReferenceGuard = $this->getMockBuilder( '\SMW\Utils\CircularReferenceGuard' )
164
			->disableOriginalConstructor()
165
			->getMock();
166
167
		$circularReferenceGuard->expects( $this->once() )
168
			->method( 'mark' );
169
170
		$circularReferenceGuard->expects( $this->never() )
171
			->method( 'unmark' );
172
173
		$circularReferenceGuard->expects( $this->once() )
174
			->method( 'isCircularByRecursionFor' )
175
			->will( $this->returnValue( true ) );
176
177
		$instance = new AskParserFunction(
178
			$parserData,
179
			$messageFormatter,
180
			$circularReferenceGuard
181
		);
182
183
		$params = array();
184
185
		$this->assertEmpty(
186
			$instance->parse( $params )
187
		);
188
	}
189
190
	public function testQueryIdStabilityForFixedSetOfParameters() {
191
192
		$this->testEnvironment->addConfiguration( 'smwgQueryResultCacheType', false );
193
		$this->testEnvironment->addConfiguration( 'smwgQFilterDuplicates', false );
194
195
		$parserData = ApplicationFactory::getInstance()->newParserData(
196
			Title::newFromText( __METHOD__ ),
197
			new ParserOutput()
198
		);
199
200
		$messageFormatter = $this->getMockBuilder( '\SMW\MessageFormatter' )
201
			->disableOriginalConstructor()
202
			->getMock();
203
204
		$circularReferenceGuard = $this->getMockBuilder( '\SMW\Utils\CircularReferenceGuard' )
205
			->disableOriginalConstructor()
206
			->getMock();
207
208
		$instance = new AskParserFunction(
209
			$parserData,
210
			$messageFormatter,
211
			$circularReferenceGuard
212
		);
213
214
		$params = array(
215
			'[[Modification date::+]]',
216
			'?Modification date',
217
			'format=list'
218
		);
219
220
		$instance->parse( $params );
221
222
		$this->assertTrue(
223
			$parserData->getSemanticData()->hasSubSemanticData( '_QUERY702bb82fc5ac212df176709f98b4f5b9' )
224
		);
225
226
		// Limit is a factor that influences the query id, count uses the
227
		// max limit available in $GLOBALS['smwgQMaxLimit'] therefore we set
228
		// the limit to make the test independent from possible other settings
229
230
		$params = array(
231
			'[[Modification date::+]]',
232
			'?Modification date',
233
			'format=count'
234
		);
235
236
		$instance->parse( $params );
237
238
		$this->assertTrue(
239
			$parserData->getSemanticData()->hasSubSemanticData( '_QUERYf161b0f405d169d1f038812484619c1f' )
240
		);
241
	}
242
243
	public function testQueryIdStabilityForFixedSetOfParametersWithFingerprintMethod() {
244
245
		$this->testEnvironment->addConfiguration( 'smwgQueryResultCacheType', false );
246
		$this->testEnvironment->addConfiguration( 'smwgQFilterDuplicates', true );
247
248
		$parserData = ApplicationFactory::getInstance()->newParserData(
249
			Title::newFromText( __METHOD__ ),
250
			new ParserOutput()
251
		);
252
253
		$messageFormatter = $this->getMockBuilder( '\SMW\MessageFormatter' )
254
			->disableOriginalConstructor()
255
			->getMock();
256
257
		$circularReferenceGuard = $this->getMockBuilder( '\SMW\Utils\CircularReferenceGuard' )
258
			->disableOriginalConstructor()
259
			->getMock();
260
261
		$instance = new AskParserFunction(
262
			$parserData,
263
			$messageFormatter,
264
			$circularReferenceGuard
265
		);
266
267
		$params = array(
268
			'[[Modification date::+]]',
269
			'?Modification date',
270
			'format=list'
271
		);
272
273
		$instance->parse( $params );
274
275
		$this->assertTrue(
276
			$parserData->getSemanticData()->hasSubSemanticData( '_QUERYaa38249db4bc6d3e8133588fb08d0f0d' )
277
		);
278
279
		// Limit is a factor that influences the query id, count uses the
280
		// max limit available in $GLOBALS['smwgQMaxLimit'] therefore we set
281
		// the limit to make the test independent from possible other settings
282
283
		$params = array(
284
			'[[Modification date::+]]',
285
			'?Modification date',
286
			'format=count'
287
		);
288
289
		$instance->parse( $params );
290
291
		$this->assertTrue(
292
			$parserData->getSemanticData()->hasSubSemanticData( '_QUERYaa38249db4bc6d3e8133588fb08d0f0d' )
293
		);
294
	}
295
296
	/**
297
	 * @dataProvider queryDataProvider
298
	 */
299
	public function testInstantiatedQueryData( array $params, array $expected, array $settings ) {
300
301
		foreach ( $settings as $key => $value ) {
302
			$this->testEnvironment->addConfiguration( $key, $value );
303
		}
304
305
		$parserData = ApplicationFactory::getInstance()->newParserData(
306
			Title::newFromText( __METHOD__ ),
307
			new ParserOutput()
308
		);
309
310
		$messageFormatter = $this->getMockBuilder( '\SMW\MessageFormatter' )
311
			->disableOriginalConstructor()
312
			->getMock();
313
314
		$circularReferenceGuard = $this->getMockBuilder( '\SMW\Utils\CircularReferenceGuard' )
315
			->disableOriginalConstructor()
316
			->getMock();
317
318
		$instance = new AskParserFunction(
319
			$parserData,
320
			$messageFormatter,
321
			$circularReferenceGuard
322
		);
323
324
		$instance->parse( $params );
325
326
		foreach ( $parserData->getSemanticData()->getSubSemanticData() as $containerSemanticData ){
327
			$this->assertInstanceOf( 'SMWContainerSemanticData', $containerSemanticData );
328
329
			$this->semanticDataValidator->assertThatPropertiesAreSet(
330
				$expected,
331
				$containerSemanticData
332
			);
333
		}
334
	}
335
336
	public function testEmbeddedQueryWithError() {
337
338
		$params = array(
339
			'[[--ABC·|DEF::123]]',
340
			'format=table'
341
		);
342
343
		$expected = array(
344
			'propertyCount'  => 2,
345
			'propertyKeys'   => array( '_ASK', '_ERRC' ),
346
		);
347
348
		$parserData = ApplicationFactory::getInstance()->newParserData(
349
			Title::newFromText( __METHOD__ ),
350
			new ParserOutput()
351
		);
352
353
		$messageFormatter = $this->getMockBuilder( '\SMW\MessageFormatter' )
354
			->disableOriginalConstructor()
355
			->getMock();
356
357
		$circularReferenceGuard = $this->getMockBuilder( '\SMW\Utils\CircularReferenceGuard' )
358
			->disableOriginalConstructor()
359
			->getMock();
360
361
		$instance = new AskParserFunction(
362
			$parserData,
363
			$messageFormatter,
364
			$circularReferenceGuard
365
		);
366
367
		$instance->parse( $params );
368
369
		$this->semanticDataValidator->assertThatPropertiesAreSet(
370
			$expected,
371
			$parserData->getSemanticData()
372
		);
373
	}
374
375
	public function testWithDisabledQueryProfiler() {
376
377
		$params = array(
378
			'[[Modification date::+]]',
379
			'format=table'
380
		);
381
382
		$expected = array(
383
			'propertyCount'  => 0
384
		);
385
386
		$this->testEnvironment->addConfiguration( 'smwgQueryProfiler', false );
387
388
		$parserData = ApplicationFactory::getInstance()->newParserData(
389
			Title::newFromText( __METHOD__ ),
390
			new ParserOutput()
391
		);
392
393
		$messageFormatter = $this->getMockBuilder( '\SMW\MessageFormatter' )
394
			->disableOriginalConstructor()
395
			->getMock();
396
397
		$circularReferenceGuard = $this->getMockBuilder( '\SMW\Utils\CircularReferenceGuard' )
398
			->disableOriginalConstructor()
399
			->getMock();
400
401
		$instance = new AskParserFunction(
402
			$parserData,
403
			$messageFormatter,
404
			$circularReferenceGuard
405
		);
406
407
		$instance->parse( $params );
408
409
		$this->semanticDataValidator->assertThatPropertiesAreSet(
410
			$expected,
411
			$parserData->getSemanticData()
412
		);
413
	}
414
415
	public function queryDataProvider() {
416
417
		$categoryNS = Localizer::getInstance()->getNamespaceTextById( NS_CATEGORY );
418
		$fileNS = Localizer::getInstance()->getNamespaceTextById( NS_FILE );
419
420
		$provider = array();
421
422
		// #0
423
		// {{#ask: [[Modification date::+]]
424
		// |?Modification date
425
		// |format=list
426
		// }}
427
		$provider[] = array(
428
			array(
429
				'[[Modification date::+]]',
430
				'?Modification date',
431
				'format=list'
432
			),
433
			array(
434
				'propertyCount'  => 4,
435
				'propertyKeys'   => array( '_ASKST', '_ASKSI', '_ASKDE', '_ASKFO' ),
436
				'propertyValues' => array( 'list', 1, 1, '[[Modification date::+]]' )
437
			),
438
			array(
439
				'smwgQueryDurationEnabled' => false
440
			)
441
		);
442
443
		// #1 Query string with spaces
444
		// {{#ask: [[Modification date::+]] [[Category:Foo bar]] [[Has title::!Foo bar]]
445
		// |?Modification date
446
		// |?Has title
447
		// |format=list
448
		// }}
449
		$provider[] = array(
450
			array(
451
				'[[Modification date::+]] [[Category:Foo bar]] [[Has title::!Foo bar]]',
452
				'?Modification date',
453
				'?Has title',
454
				'format=list'
455
			),
456
			array(
457
				'propertyCount'  => 4,
458
				'propertyKeys'   => array( '_ASKST', '_ASKSI', '_ASKDE', '_ASKFO' ),
459
				'propertyValues' => array( 'list', 4, 1, "[[Modification date::+]] [[$categoryNS:Foo bar]] [[Has title::!Foo bar]]" )
460
			),
461
			array(
462
				'smwgQueryDurationEnabled' => false
463
			)
464
		);
465
466
		// #2
467
		// {{#ask: [[Modification date::+]][[Category:Foo]]
468
		// |?Modification date
469
		// |?Has title
470
		// |format=list
471
		// }}
472
		$provider[] = array(
473
			array(
474
				'[[Modification date::+]][[Category:Foo]]',
475
				'?Modification date',
476
				'?Has title',
477
				'format=list'
478
			),
479
			array(
480
				'propertyCount'  => 4,
481
				'propertyKeys'   => array( '_ASKST', '_ASKSI', '_ASKDE', '_ASKFO' ),
482
				'propertyValues' => array( 'list', 2, 1, "[[Modification date::+]] [[$categoryNS:Foo]]" )
483
			),
484
			array(
485
				'smwgQueryDurationEnabled' => false
486
			)
487
		);
488
489
		// #3 Known format
490
		// {{#ask: [[File:Fooo]]
491
		// |?Modification date
492
		// |default=no results
493
		// |format=feed
494
		// }}
495
		$provider[] = array(
496
			array(
497
				'[[File:Fooo]]',
498
				'?Modification date',
499
				'default=no results',
500
				'format=feed'
501
			),
502
			array(
503
				'propertyCount'  => 4,
504
				'propertyKeys'   => array( '_ASKST', '_ASKSI', '_ASKDE', '_ASKFO' ),
505
				'propertyValues' => array( 'feed', 1, 1, "[[:$fileNS:Fooo]]" )
506
			),
507
			array(
508
				'smwgQueryDurationEnabled' => false
509
			)
510
		);
511
512
		// #4 Unknown format, default table
513
		// {{#ask: [[Modification date::+]][[Category:Foo]]
514
		// |?Modification date
515
		// |?Has title
516
		// |format=bar
517
		// }}
518
		$provider[] = array(
519
			array(
520
				'[[Modification date::+]][[Category:Foo]]',
521
				'?Modification date',
522
				'?Has title',
523
				'format=lula'
524
			),
525
			array(
526
				'propertyCount'  => 4,
527
				'propertyKeys'   => array( '_ASKST', '_ASKSI', '_ASKDE', '_ASKFO' ),
528
				'propertyValues' => array( 'table', 2, 1, "[[Modification date::+]] [[$categoryNS:Foo]]" )
529
			),
530
			array(
531
				'smwgQueryDurationEnabled' => false
532
			)
533
		);
534
535
		// #5 QueryTime enabled
536
		$provider[] = array(
537
			array(
538
				'[[Modification date::+]][[Category:Foo]]',
539
				'?Modification date',
540
				'?Has title',
541
				'format=lula'
542
			),
543
			array(
544
				'propertyCount'  => 5,
545
				'propertyKeys'   => array( '_ASKST', '_ASKSI', '_ASKDE', '_ASKFO', '_ASKDU' ),
546
			),
547
			array(
548
				'smwgQueryDurationEnabled' => true
549
			)
550
		);
551
552
		// #6 Invalid parameters
553
		// {{#ask: [[Modification date::+]]
554
		// |?Modification date
555
		// |format=list
556
		// |someParameterWithoutValue
557
		// |{{{template}}}
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
558
		// |@internal
559
		// }}
560
		$provider[] = array(
561
			array(
562
				'[[Modification date::+]]',
563
				'someParameterWithoutValue',
564
				'{{{template}}}',
565
				'format=list',
566
				'@internal',
567
				'?Modification date'
568
			),
569
			array(
570
				'propertyCount'  => 4,
571
				'propertyKeys'   => array( '_ASKST', '_ASKSI', '_ASKDE', '_ASKFO' ),
572
				'propertyValues' => array( 'list', 1, 1, '[[Modification date::+]]' )
573
			),
574
			array(
575
				'smwgQueryDurationEnabled' => false
576
			)
577
		);
578
579
		return $provider;
580
	}
581
582
}
583