Passed
Push — master ( 98491e...1964e0 )
by
unknown
02:21 queued 11s
created

testDefaultRuleSetsProvideExternalInfoUrls()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of PHP Mess Detector.
4
 *
5
 * Copyright (c) Manuel Pichler <[email protected]>.
6
 * All rights reserved.
7
 *
8
 * Licensed under BSD License
9
 * For full copyright and license information, please see the LICENSE file.
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @author Manuel Pichler <[email protected]>
13
 * @copyright Manuel Pichler. All rights reserved.
14
 * @license https://opensource.org/licenses/bsd-license.php BSD License
15
 * @link http://phpmd.org/
16
 */
17
18
namespace PHPMD;
19
20
use org\bovigo\vfs\vfsStream;
21
22
/**
23
 * Test case for the rule set factory class.
24
 *
25
 * @covers \PHPMD\RuleSetFactory
26
 */
27
class RuleSetFactoryTest extends AbstractTest
28
{
29
    /**
30
     * Used to test files/directories access for ignore code rule
31
     *
32
     * @var string
33
     */
34
    const DIR_UNDER_TESTS = 'designăôü0汉字';
35
36
    /**
37
     * testCreateRuleSetFileNameFindsXmlFileInBundledRuleSets
38
     *
39
     * @return void
40
     */
41
    public function testCreateRuleSetFileNameFindsXmlFileInBundledRuleSets()
42
    {
43
        $factory = new RuleSetFactory();
44
        $ruleSet = $factory->createSingleRuleSet('codesize');
45
46
        $this->assertContains('The Code Size Ruleset', $ruleSet->getDescription());
47
    }
48
49
    /**
50
     * testCreateRuleSetFileNameFindsXmlFileInCurrentWorkingDirectory
51
     *
52
     * @return void
53
     */
54
    public function testCreateRuleSetFileNameFindsXmlFileInCurrentWorkingDirectory()
55
    {
56
        self::changeWorkingDirectory('rulesets');
57
58
        $factory = new RuleSetFactory();
59
        $ruleSet = $factory->createSingleRuleSet('set1.xml');
60
61
        $this->assertEquals('First description...', $ruleSet->getDescription());
62
    }
63
64
    /**
65
     * testCreateRuleSetsReturnsArray
66
     *
67
     * @return void
68
     */
69
    public function testCreateRuleSetsReturnsArray()
70
    {
71
        $ruleSets = $this->createRuleSetsFromAbsoluteFiles('rulesets/set1.xml');
72
        $this->assertInternalType('array', $ruleSets);
73
    }
74
75
    /**
76
     * testCreateRuleSetsForSingleFileReturnsArrayWithOneElement
77
     *
78
     * @return void
79
     */
80
    public function testCreateRuleSetsForSingleFileReturnsArrayWithOneElement()
81
    {
82
        $ruleSets = $this->createRuleSetsFromAbsoluteFiles('rulesets/set1.xml');
83
        $this->assertEquals(1, count($ruleSets));
84
    }
85
86
    /**
87
     * testCreateRuleSetsForSingleFileReturnsOneRuleSetInstance
88
     *
89
     * @return void
90
     */
91
    public function testCreateRuleSetsForSingleFileReturnsOneRuleSetInstance()
92
    {
93
        $ruleSets = $this->createRuleSetsFromAbsoluteFiles('rulesets/set1.xml');
94
        $this->assertInstanceOf('PHPMD\\RuleSet', $ruleSets[0]);
95
    }
96
97
    /**
98
     * testCreateRuleSetsConfiguresExpectedRuleSetName
99
     *
100
     * @return void
101
     */
102
    public function testCreateRuleSetsConfiguresExpectedRuleSetName()
103
    {
104
        $ruleSets = $this->createRuleSetsFromAbsoluteFiles('rulesets/set1.xml');
105
        $this->assertEquals('First Test RuleSet', $ruleSets[0]->getName());
106
    }
107
108
    /**
109
     * testCreateRuleSetsConfiguresExpectedRuleSetName
110
     *
111
     * @return void
112
     */
113
    public function testCreateRuleSetsConfiguresExpectedRuleSetDescription()
114
    {
115
        $ruleSets = $this->createRuleSetsFromAbsoluteFiles('rulesets/set1.xml');
116
        $this->assertEquals('First description...', $ruleSets[0]->getDescription());
117
    }
118
119
    /**
120
     * testCreateRuleSetsForTwoFilesReturnsArrayWithTwoElements
121
     *
122
     * @return void
123
     */
124
    public function testCreateRuleSetsForTwoFilesReturnsArrayWithTwoElements()
125
    {
126
        $ruleSets = $this->createRuleSetsFromAbsoluteFiles(
127
            'rulesets/set1.xml',
128
            'rulesets/set2.xml'
129
        );
130
        $this->assertEquals(2, count($ruleSets));
131
    }
132
133
    /**
134
     * testCreateRuleSetsForTwoFilesReturnsExpectedRuleSetInstances
135
     *
136
     * @return void
137
     */
138
    public function testCreateRuleSetsForTwoFilesReturnsExpectedRuleSetInstances()
139
    {
140
        $ruleSets = $this->createRuleSetsFromAbsoluteFiles(
141
            'rulesets/set1.xml',
142
            'rulesets/set2.xml'
143
        );
144
        $this->assertInstanceOf('PHPMD\\RuleSet', $ruleSets[0]);
145
        $this->assertInstanceOf('PHPMD\\RuleSet', $ruleSets[1]);
146
    }
147
148
    /**
149
     * testCreateRuleSetsForTwoConfiguresExpectedRuleSetNames
150
     *
151
     * @return void
152
     */
153
    public function testCreateRuleSetsForTwoConfiguresExpectedRuleSetNames()
154
    {
155
        $ruleSets = $this->createRuleSetsFromAbsoluteFiles(
156
            'rulesets/set1.xml',
157
            'rulesets/set2.xml'
158
        );
159
        $this->assertEquals('First Test RuleSet', $ruleSets[0]->getName());
160
        $this->assertEquals('Second Test RuleSet', $ruleSets[1]->getName());
161
    }
162
163
    /**
164
     * testCreateRuleSetsForTwoConfiguresExpectedRuleSetDescriptions
165
     *
166
     * @return void
167
     */
168
    public function testCreateRuleSetsForTwoConfiguresExpectedRuleSetDescriptions()
169
    {
170
        $ruleSets = $this->createRuleSetsFromAbsoluteFiles(
171
            'rulesets/set1.xml',
172
            'rulesets/set2.xml'
173
        );
174
        $this->assertSame('First description...', $ruleSets[0]->getDescription());
175
        $this->assertSame('Second description...', $ruleSets[1]->getDescription());
176
    }
177
178
    /**
179
     * testCreateRuleSetsForSingleLocalFileNameReturnsArray
180
     *
181
     * @return void
182
     */
183
    public function testCreateRuleSetsForLocalFileNameReturnsArray()
184
    {
185
        self::changeWorkingDirectory();
186
187
        $ruleSets = $this->createRuleSetsFromFiles('rulesets/set1.xml');
188
        $this->assertInternalType('array', $ruleSets);
189
    }
190
191
    /**
192
     * testCreateRuleSetsForSingleLocalFileNameReturnsArrayWithOneElement
193
     *
194
     * @return void
195
     */
196
    public function testCreateRuleSetsForLocalFileNameReturnsArrayWithOneElement()
197
    {
198
        self::changeWorkingDirectory();
199
200
        $ruleSets = $this->createRuleSetsFromFiles('rulesets/set1.xml');
201
        $this->assertEquals(1, count($ruleSets));
202
    }
203
204
    /**
205
     * testCreateRuleSetsForSingleLocalFileNameConfiguresExpectedRuleSetName
206
     *
207
     * @return void
208
     */
209
    public function testCreateRuleSetsForLocalFileNameConfiguresExpectedRuleSetName()
210
    {
211
        self::changeWorkingDirectory();
212
213
        $ruleSets = $this->createRuleSetsFromFiles('rulesets/set1.xml');
214
        $this->assertEquals('First Test RuleSet', $ruleSets[0]->getName());
215
    }
216
217
    /**
218
     * testCreateRuleSetsWithReferenceContainsExpectedRuleSet
219
     *
220
     * @return void
221
     */
222
    public function testCreateRuleSetsWithReferenceContainsExpectedRuleSet()
223
    {
224
        self::changeWorkingDirectory();
225
226
        $ruleSets = $this->createRuleSetsFromAbsoluteFiles('rulesets/refset1.xml');
227
        $this->assertEquals('First Test RuleSet', $ruleSets[0]->getName());
228
    }
229
230
    /**
231
     * testCreateRuleSetsWithReferenceContainsExpectedNumberOfRules
232
     *
233
     * @return void
234
     */
235
    public function testCreateRuleSetsWithReferenceContainsExpectedNumberOfRules()
236
    {
237
        self::changeWorkingDirectory();
238
239
        $ruleSets = $this->createRuleSetsFromAbsoluteFiles('rulesets/refset1.xml');
240
        $this->assertEquals(4, iterator_count($ruleSets[0]));
241
    }
242
243
    /**
244
     * testCreateRuleSetsForLocalFileWithRuleSetReferenceNodes
245
     *
246
     * @return void
247
     */
248
    public function testCreateRuleSetsWithReferenceContainsRuleInstances()
249
    {
250
        self::changeWorkingDirectory();
251
252
        $ruleSets = $this->createRuleSetsFromAbsoluteFiles('rulesets/refset1.xml');
253
        $this->assertInstanceOf('PHPMD\\AbstractRule', $ruleSets[0]->getRules()->current());
254
    }
255
256
    /**
257
     * testCreateRuleSetsWithReferenceContainsExpectedRules
258
     *
259
     * @return void
260
     */
261
    public function testCreateRuleSetsWithReferenceContainsExpectedRules()
262
    {
263
        self::changeWorkingDirectory();
264
265
        $ruleSets = $this->createRuleSetsFromAbsoluteFiles('rulesets/refset2.xml');
266
267
        $actual = array();
268
        $expected = array('RuleTwoInFirstRuleSet', 'RuleOneInSecondRuleSet');
269
270
        foreach ($ruleSets[0]->getRules() as $rule) {
271
            $actual[] = $rule->getName();
272
        }
273
274
        $this->assertEquals($expected, $actual);
275
    }
276
277
    /**
278
     * testCreateSingleRuleSetReturnsRuleSetInstance
279
     *
280
     * @return void
281
     */
282
    public function testCreateSingleRuleSetReturnsRuleSetInstance()
283
    {
284
        self::changeWorkingDirectory();
285
286
        $factory = new RuleSetFactory();
287
        $ruleSet = $factory->createSingleRuleSet('set1');
288
289
        $this->assertInstanceOf('PHPMD\\RuleSet', $ruleSet);
290
    }
291
292
    /**
293
     * Tests that the rule-set factory applies a set minimum priority filter correct.
294
     *
295
     * @return void
296
     */
297
    public function testCreateRuleSetWithSpecifiedMinimumPriorityOnlyContainsMatchingRules()
298
    {
299
        self::changeWorkingDirectory();
300
301
        $factory = new RuleSetFactory();
302
        $factory->setMinimumPriority(2);
303
304
        $ruleSet = $factory->createSingleRuleSet('set1');
305
        $this->assertSame(1, iterator_count($ruleSet->getRules()));
306
    }
307
308
    /**
309
     * Tests that the rule-set factory applies a set maximum priority filter correct.
310
     *
311
     * @return void
312
     */
313
    public function testCreateRuleSetWithSpecifiedMaximumPriorityOnlyContainsMatchingRules()
314
    {
315
        self::changeWorkingDirectory();
316
317
        $factory = new RuleSetFactory();
318
        $factory->setMaximumPriority(2);
319
320
        $ruleSet = $factory->createSingleRuleSet('set1');
321
        $this->assertSame(1, iterator_count($ruleSet->getRules()));
322
    }
323
324
    /**
325
     * Tests that the rule-set factory applies a set maximum priority filter correct.
326
     *
327
     * @return void
328
     */
329
    public function testCreateRuleSetWithSpecifiedPrioritiesOnlyContainsMatchingRules()
330
    {
331
        self::changeWorkingDirectory();
332
333
        $factory = new RuleSetFactory();
334
        $factory->setMinimumPriority(2);
335
        $factory->setMaximumPriority(2);
336
337
        $ruleSet = $factory->createSingleRuleSet('set1');
338
        $this->assertCount(0, $ruleSet->getRules());
339
    }
340
341
    /**
342
     * testCreateRuleWithExcludePattern
343
     *
344
     * @return void
345
     */
346
    public function testCreateRuleWithExcludePattern()
347
    {
348
        self::changeWorkingDirectory();
349
350
        $factory = new RuleSetFactory();
351
        $excludes = $factory->getIgnorePattern('exclude-pattern');
352
353
        $expected = array(
354
            '*sourceExcluded/*.php',
355
            '*sourceExcluded\*.php',
356
        );
357
358
        $this->assertEquals($expected, $excludes);
359
    }
360
361
    /**
362
     * testCreateRuleSetsWithRuleReferenceThatOverwritesPrioritySetting
363
     *
364
     * @return void
365
     */
366 View Code Duplication
    public function testCreateRuleSetsWithRuleReferenceThatOverwritesPrioritySetting()
367
    {
368
        self::changeWorkingDirectory();
369
370
        $factory = new RuleSetFactory();
371
        $ruleSets = $factory->createRuleSets('refset3');
372
373
        $rule = $ruleSets[0]->getRules()->current();
374
        $this->assertSame(4, $rule->getPriority());
375
    }
376
377
    /**
378
     * testCreateRuleWithExpectedExample
379
     *
380
     * @return void
381
     */
382 View Code Duplication
    public function testCreateRuleWithExpectedExample()
383
    {
384
        self::changeWorkingDirectory();
385
386
        $factory = new RuleSetFactory();
387
        $ruleSets = $factory->createRuleSets('set1');
388
389
        $rule = $ruleSets[0]->getRules()->current();
390
        $this->assertEquals(array(__FUNCTION__), $rule->getExamples());
391
    }
392
393
    /**
394
     * testCreateRuleWithExpectedMultipleExamples
395
     *
396
     * @return void
397
     */
398
    public function testCreateRuleWithExpectedMultipleExamples()
399
    {
400
        self::changeWorkingDirectory();
401
402
        $factory = new RuleSetFactory();
403
        $ruleSets = $factory->createRuleSets('set2');
404
405
        $rule = $ruleSets[0]->getRules()->current();
406
        $this->assertEquals(array(__FUNCTION__ . 'One', __FUNCTION__ . 'Two'), $rule->getExamples());
407
    }
408
409
    /**
410
     * testCreateRuleSetsWithRuleReferenceThatOverwritesDescriptionSetting
411
     *
412
     * @return void
413
     */
414 View Code Duplication
    public function testCreateRuleSetsWithRuleReferenceThatOverwritesDescriptionSetting()
415
    {
416
        self::changeWorkingDirectory();
417
418
        $factory = new RuleSetFactory();
419
        $ruleSets = $factory->createRuleSets('refset3');
420
421
        $rule = $ruleSets[0]->getRules()->current();
422
        $this->assertSame('description 42', $rule->getDescription());
423
    }
424
425
    /**
426
     * testCreateRuleSetsWithRuleReferenceThatOverwritesPropertySetting
427
     *
428
     * @return void
429
     */
430 View Code Duplication
    public function testCreateRuleSetsWithRuleReferenceThatOverwritesPropertySetting()
431
    {
432
        self::changeWorkingDirectory();
433
434
        $factory = new RuleSetFactory();
435
        $ruleSets = $factory->createRuleSets('refset3');
436
437
        $rule = $ruleSets[0]->getRules()->current();
438
        $this->assertSame(42, $rule->getIntProperty('foo'));
439
    }
440
441
    /**
442
     * testFactorySupportsAlternativeSyntaxForPropertyValue
443
     *
444
     * @return void
445
     */
446 View Code Duplication
    public function testFactorySupportsAlternativeSyntaxForPropertyValue()
447
    {
448
        self::changeWorkingDirectory();
449
450
        $factory = new RuleSetFactory();
451
        $ruleSets = $factory->createRuleSets('alternative-property-value-syntax');
452
453
        $rule = $ruleSets[0]->getRules()->current();
454
        $this->assertSame(42, $rule->getIntProperty('foo'));
455
    }
456
457
    /**
458
     * testCreateRuleSetsWithRuleReferenceThatOverwritesExamplesSetting
459
     *
460
     * @return void
461
     */
462 View Code Duplication
    public function testCreateRuleSetsWithRuleReferenceThatOverwritesExamplesSetting()
463
    {
464
        self::changeWorkingDirectory();
465
466
        $factory = new RuleSetFactory();
467
        $ruleSets = $factory->createRuleSets('refset3');
468
469
        $rule = $ruleSets[0]->getRules()->current();
470
471
        $examples = $rule->getExamples();
472
        $this->assertEquals('foreach ($foo as $bar) { echo $bar; }', $examples[0]);
473
    }
474
475
    /**
476
     * testCreateRuleSetsWithRuleReferenceThatOverwritesExamplesSetting
477
     *
478
     * @return void
479
     */
480 View Code Duplication
    public function testCreateRuleSetsWithRuleReferenceThatOverwritesNameSetting()
481
    {
482
        self::changeWorkingDirectory();
483
484
        $factory = new RuleSetFactory();
485
        $ruleSets = $factory->createRuleSets('refset4');
486
487
        $rule = $ruleSets[0]->getRules()->current();
488
        $this->assertEquals('Name overwritten', $rule->getName());
489
    }
490
491
    /**
492
     * testCreateRuleSetsWithRuleReferenceThatOverwritesMessageSetting
493
     *
494
     * @return void
495
     */
496 View Code Duplication
    public function testCreateRuleSetsWithRuleReferenceThatOverwritesMessageSetting()
497
    {
498
        self::changeWorkingDirectory();
499
500
        $factory = new RuleSetFactory();
501
        $ruleSets = $factory->createRuleSets('refset4');
502
503
        $rule = $ruleSets[0]->getRules()->current();
504
        $this->assertEquals('Message overwritten', $rule->getMessage());
505
    }
506
507
    /**
508
     * testCreateRuleSetsWithRuleReferenceThatOverwritesExtInfoUrlSetting
509
     *
510
     * @return void
511
     */
512 View Code Duplication
    public function testCreateRuleSetsWithRuleReferenceThatOverwritesExtInfoUrlSetting()
513
    {
514
        self::changeWorkingDirectory();
515
516
        $factory = new RuleSetFactory();
517
        $ruleSets = $factory->createRuleSets('refset4');
518
519
        $rule = $ruleSets[0]->getRules()->current();
520
        $this->assertEquals('http://example.com/overwritten', $rule->getExternalInfoUrl());
521
    }
522
523
    /**
524
     * testCreateRuleSetsWithRuleReferenceNotContainsExcludedRule
525
     *
526
     * @return void
527
     */
528 View Code Duplication
    public function testCreateRuleSetsWithRuleReferenceNotContainsExcludedRule()
529
    {
530
        self::changeWorkingDirectory();
531
532
        $factory = new RuleSetFactory();
533
        $ruleSets = $factory->createRuleSets('refset-exclude-one');
534
535
        $rules = $ruleSets[0]->getRules();
536
        $this->assertEquals(1, iterator_count($rules));
537
    }
538
539
    /**
540
     * testCreateRuleSetsWithRuleReferenceNotContainsExcludedRules
541
     *
542
     * @return void
543
     */
544 View Code Duplication
    public function testCreateRuleSetsWithRuleReferenceNotContainsExcludedRules()
545
    {
546
        self::changeWorkingDirectory();
547
548
        $factory = new RuleSetFactory();
549
        $ruleSets = $factory->createRuleSets('refset-exclude-all');
550
551
        $rules = $ruleSets[0]->getRules();
552
        $this->assertEquals(0, iterator_count($rules));
553
    }
554
555
    /**
556
     * Tests that the factory throws the expected exception for an invalid ruleset
557
     * identifier.
558
     *
559
     * @return void
560
     * @covers \PHPMD\RuleSetNotFoundException
561
     */
562
    public function testCreateRuleSetsThrowsExceptionForInvalidIdentifier()
563
    {
564
        $factory = new RuleSetFactory();
565
566
        $this->setExpectedException(
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
567
            'PHPMD\\RuleSetNotFoundException',
568
            'Cannot find specified rule-set "foo-bar-ruleset-23".'
569
        );
570
571
        $factory->createRuleSets('foo-bar-ruleset-23');
572
    }
573
574
    /**
575
     * Tests that the factory throws an exception when the source code filename
576
     * for the configured rule does not exist.
577
     *
578
     * @return void
579
     * @covers \PHPMD\RuleClassFileNotFoundException
580
     */
581 View Code Duplication
    public function testCreateRuleSetsThrowsExceptionWhenClassFileNotInIncludePath()
582
    {
583
        $fileName = self::createFileUri('rulesets/set-class-file-not-found.xml');
584
        $factory = new RuleSetFactory();
585
586
        $this->setExpectedException(
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
587
            'PHPMD\\RuleClassFileNotFoundException',
588
            'Cannot load source file for class: PHPMD\\Stubs\\ClassFileNotFoundRule'
589
        );
590
591
        $factory->createRuleSets($fileName);
592
    }
593
594
    /**
595
     * Tests that the factory throws the expected exception when a rule class
596
     * cannot be found.
597
     *
598
     * @return void
599
     * @covers \PHPMD\RuleClassNotFoundException
600
     */
601 View Code Duplication
    public function testCreateRuleSetThrowsExceptionWhenFileNotContainsClass()
602
    {
603
        $fileName = self::createFileUri('rulesets/set-class-not-found.xml');
604
        $factory = new RuleSetFactory();
605
606
        $this->setExpectedException(
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
607
            'PHPMD\\RuleClassNotFoundException',
608
            'Cannot find rule class: PHPMD\\Stubs\\ClassNotFoundRule'
609
        );
610
611
        $factory->createRuleSets($fileName);
612
    }
613
614
    /**
615
     * Tests that the factory throws the expected exception when a rule class
616
     * cannot be found.
617
     *
618
     * @return void
619
     * @covers \PHPMD\RuleClassNotFoundException
620
     * @expectedException \RuntimeException
621
     */
622
    public function testCreateRuleSetsThrowsExpectedExceptionForInvalidXmlFile()
623
    {
624
        $fileName = self::createFileUri('rulesets/set-invalid-xml.xml');
625
626
        $factory = new RuleSetFactory();
627
        $factory->createRuleSets($fileName);
628
    }
629
630
    /**
631
     * testCreateRuleSetsActivatesStrictModeOnRuleSet
632
     *
633
     * @return void
634
     */
635
    public function testCreateRuleSetsActivatesStrictModeOnRuleSet()
636
    {
637
        $fileName = self::createFileUri('rulesets/set1.xml');
638
639
        $factory = new RuleSetFactory();
640
        $factory->setStrict();
641
642
        $ruleSets = $factory->createRuleSets($fileName);
643
644
        $this->assertAttributeEquals(true, 'strict', $ruleSets[0]);
645
    }
646
647
    /**
648
     * Tests that adding an include-path via ruleset works.
649
     * Also implicitly tests (by parsing the ruleset) that
650
     * reference-by-includepath and explicit-classfile-declaration works.
651
     *
652
     * @return void
653
     * @throws \Exception
654
     */
655
    public function testAddPHPIncludePath()
656
    {
657
        $includePathBefore = get_include_path();
658
659
        $rulesetFilepath = 'rulesets/ruleset-refs.xml';
660
        $fileName = self::createFileUri($rulesetFilepath);
661
662
        try {
663
            $factory = new RuleSetFactory();
664
            $factory->createRuleSets($fileName);
665
666
            $expectedIncludePath = "/foo/bar/baz";
667
            $actualIncludePaths = explode(PATH_SEPARATOR, get_include_path());
668
            $isIncludePathPresent = in_array($expectedIncludePath, $actualIncludePaths);
669
        } catch (\Exception $exception) {
670
            set_include_path($includePathBefore);
671
            throw $exception;
672
        }
673
674
        set_include_path($includePathBefore);
675
676
        $this->assertTrue(
677
            $isIncludePathPresent,
678
            "The include-path from '{$rulesetFilepath}' was not set!"
679
        );
680
    }
681
682
    /**
683
     * Checks if PHPMD doesn't treat directories named as code rule as files
684
     *
685
     * @return void
686
     * @link https://github.com/phpmd/phpmd/issues/47
687
     */
688
    public function testIfGettingRuleFilePathExcludeUnreadablePaths()
689
    {
690
        self::changeWorkingDirectory(__DIR__);
691
        $factory = new RuleSetFactory();
692
        $runtimeExceptionCount = 0;
693
        $ruleSetNotFoundExceptionCount = 0;
694
695
        foreach ($this->getPathsForFileAccessTest() as $path) {
696
            try {
697
                $this->assertEquals(
698
                    array(
699
                        '*sourceExcluded/*.php',
700
                        '*sourceExcluded\*.php',
701
                    ),
702
                    $factory->getIgnorePattern($path . self::DIR_UNDER_TESTS)
703
                );
704
            } catch (RuleSetNotFoundException $e) {
0 ignored issues
show
Bug introduced by
The class PHPMD\RuleSetNotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
705
                $ruleSetNotFoundExceptionCount++;
706
            } catch (\RuntimeException $e) {
707
                $runtimeExceptionCount++;
708
            }
709
        }
710
        $this->assertEquals(0, $runtimeExceptionCount);
711
        $this->assertEquals(5, $ruleSetNotFoundExceptionCount);
712
    }
713
714
    /**
715
     * Checks the ruleset XML files provided with PHPMD all provide externalInfoUrls
716
     *
717
     * @param string $file The path to the ruleset xml to test
718
     * @return void
719
     * @dataProvider getDefaultRuleSets
720
     */
721
    public function testDefaultRuleSetsProvideExternalInfoUrls($file)
722
    {
723
        $ruleSets = $this->createRuleSetsFromFiles($file);
724
        $ruleSet = $ruleSets[0];
725
        /** @var Rule $rule */
726
        foreach ($ruleSet->getRules() as $rule) {
727
            $message = sprintf(
728
                '%s in rule set %s should provide an externalInfoUrl',
729
                $rule->getName(),
730
                $ruleSet->getName()
731
            );
732
733
            $this->assertNotEmpty($rule->getExternalInfoUrl(), $message);
734
        }
735
    }
736
737
    /**
738
     * Provides an array of the file paths to rule sets provided with PHPMD
739
     *
740
     * @return array
741
     */
742
    public function getDefaultRuleSets()
743
    {
744
        return static::getValuesAsArrays(glob(__DIR__ . '/../../../main/resources/rulesets/*.xml'));
745
    }
746
747
    /**
748
     * Invokes the <b>createRuleSets()</b> of the {@link RuleSetFactory}
749
     * class.
750
     *
751
     * @param string $file At least one rule configuration file name. You can
752
     *        also pass multiple parameters with ruleset configuration files.
753
     * @return \PHPMD\RuleSet[]
754
     */
755
    private function createRuleSetsFromAbsoluteFiles($file)
756
    {
757
        $files = (1 === func_num_args() ? array($file) : func_get_args());
758
        $files = array_map(array(__CLASS__, 'createFileUri'), $files);
759
760
        return call_user_func_array(array($this, 'createRuleSetsFromFiles'), $files);
761
    }
762
763
    /**
764
     * Invokes the <b>createRuleSets()</b> of the {@link RuleSetFactory}
765
     * class.
766
     *
767
     * @param string $file At least one rule configuration file name. You can
768
     *        also pass multiple parameters with ruleset configuration files.
769
     * @return \PHPMD\RuleSet[]
770
     * @phpcsSuppress SlevomatCodingStandard.Functions.UnusedParameter
771
     */
772
    private function createRuleSetsFromFiles($file)
0 ignored issues
show
Unused Code introduced by
The parameter $file is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
773
    {
774
        $args = func_get_args();
775
776
        $factory = new RuleSetFactory();
777
778
        return $factory->createRuleSets(implode(',', $args));
779
    }
780
781
    /**
782
     * Sets up files and directories for XML rule file access test
783
     *
784
     * @return array Paths to test against
785
     */
786
    public function getPathsForFileAccessTest()
787
    {
788
        $fileContent = file_get_contents(__DIR__ . '/../../resources/files/rulesets/exclude-pattern.xml');
789
        $structure = array(
790
            'dir1' => array(
791
                self::DIR_UNDER_TESTS => array(), // directory - skipped
792
                'foo' => array(), // directory, criteria do not apply
793
            ),
794
            'dir2' => array(
795
                self::DIR_UNDER_TESTS => array(), // directory, wrong permissions
796
            ),
797
            'dir3' => array(
798
                self::DIR_UNDER_TESTS => array(), // directory, wrong owner and group
799
            ),
800
            'dirÅ' => array( // check UTF-8 characters handling
801
                'foo' => array(
802
                    self::DIR_UNDER_TESTS => $fileContent, // wrong permissions
803
                ),
804
                'bar' => array(
805
                    self::DIR_UNDER_TESTS => $fileContent, // OK
806
                ),
807
            ),
808
        );
809
        $root = vfsStream::setup('root', null, $structure);
810
        $root->getChild('dir2/' . self::DIR_UNDER_TESTS)->chmod(000);
811
        $root->getChild('dir3/' . self::DIR_UNDER_TESTS)->chown(vfsStream::OWNER_ROOT)->chgrp(vfsStream::GROUP_ROOT);
812
        $root->getChild('dirÅ/foo/' . self::DIR_UNDER_TESTS)->chmod(000);
813
814
        return array(
815
            $root->url(),
816
            $root->url() . '/dir1/',
817
            $root->url() . '/dir2/',
818
            $root->url() . '/dir3/',
819
            $root->url() . '/dirÅ/foo/',
820
            $root->url() . '/dirÅ/bar/',
821
        );
822
    }
823
}
824