testCreateRuleSetsForLocalFileNameReturnsArray()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 7
rs 10
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
use PHPMD\RuleClassNotFoundException;
22
use PHPMD\RuleClassFileNotFoundException;
23
use PHPMD\RuleSetNotFoundException;
24
use PHPMD\RuleSet;
25
use PHPMD\AbstractRule;
26
27
/**
28
 * Test case for the rule set factory class.
29
 *
30
 * @covers \PHPMD\RuleSetFactory
31
 */
32
class RuleSetFactoryTest extends AbstractTest
33
{
34
    /**
35
     * Used to test files/directories access for ignore code rule
36
     *
37
     * @var string
38
     */
39
    const DIR_UNDER_TESTS = 'designăôü0汉字';
40
41
    /**
42
     * testCreateRuleSetFileNameFindsXmlFileInBundledRuleSets
43
     *
44
     * @return void
45
     */
46
    public function testCreateRuleSetFileNameFindsXmlFileInBundledRuleSets()
47
    {
48
        $factory = new RuleSetFactory();
49
        $ruleSet = $factory->createSingleRuleSet('codesize');
50
51
        $this->assertContains('The Code Size Ruleset', $ruleSet->getDescription());
52
    }
53
54
    /**
55
     * testCreateRuleSetFileNameFindsXmlFileInCurrentWorkingDirectory
56
     *
57
     * @return void
58
     */
59
    public function testCreateRuleSetFileNameFindsXmlFileInCurrentWorkingDirectory()
60
    {
61
        self::changeWorkingDirectory('rulesets');
62
63
        $factory = new RuleSetFactory();
64
        $ruleSet = $factory->createSingleRuleSet('set1.xml');
65
66
        $this->assertEquals('First description...', $ruleSet->getDescription());
67
    }
68
69
    /**
70
     * testCreateRuleSetsReturnsArray
71
     *
72
     * @return void
73
     */
74
    public function testCreateRuleSetsReturnsArray()
75
    {
76
        $ruleSets = $this->createRuleSetsFromAbsoluteFiles('rulesets/set1.xml');
77
        $this->assertInternalType('array', $ruleSets);
78
    }
79
80
    /**
81
     * testCreateRuleSetsForSingleFileReturnsArrayWithOneElement
82
     *
83
     * @return void
84
     */
85
    public function testCreateRuleSetsForSingleFileReturnsArrayWithOneElement()
86
    {
87
        $ruleSets = $this->createRuleSetsFromAbsoluteFiles('rulesets/set1.xml');
88
        $this->assertCount(1, $ruleSets);
89
    }
90
91
    /**
92
     * testCreateRuleSetsForSingleFileReturnsOneRuleSetInstance
93
     *
94
     * @return void
95
     */
96
    public function testCreateRuleSetsForSingleFileReturnsOneRuleSetInstance()
97
    {
98
        $ruleSets = $this->createRuleSetsFromAbsoluteFiles('rulesets/set1.xml');
99
        $this->assertInstanceOf(RuleSet::class, $ruleSets[0]);
100
    }
101
102
    /**
103
     * testCreateRuleSetsConfiguresExpectedRuleSetName
104
     *
105
     * @return void
106
     */
107
    public function testCreateRuleSetsConfiguresExpectedRuleSetName()
108
    {
109
        $ruleSets = $this->createRuleSetsFromAbsoluteFiles('rulesets/set1.xml');
110
        $this->assertEquals('First Test RuleSet', $ruleSets[0]->getName());
111
    }
112
113
    /**
114
     * testCreateRuleSetsConfiguresExpectedRuleSetName
115
     *
116
     * @return void
117
     */
118
    public function testCreateRuleSetsConfiguresExpectedRuleSetDescription()
119
    {
120
        $ruleSets = $this->createRuleSetsFromAbsoluteFiles('rulesets/set1.xml');
121
        $this->assertEquals('First description...', $ruleSets[0]->getDescription());
122
    }
123
124
    /**
125
     * testCreateRuleSetsForTwoFilesReturnsArrayWithTwoElements
126
     *
127
     * @return void
128
     */
129
    public function testCreateRuleSetsForTwoFilesReturnsArrayWithTwoElements()
130
    {
131
        $ruleSets = $this->createRuleSetsFromAbsoluteFiles(
132
            'rulesets/set1.xml',
133
            'rulesets/set2.xml'
134
        );
135
        $this->assertCount(2, $ruleSets);
136
    }
137
138
    /**
139
     * testCreateRuleSetsForTwoFilesReturnsExpectedRuleSetInstances
140
     *
141
     * @return void
142
     */
143
    public function testCreateRuleSetsForTwoFilesReturnsExpectedRuleSetInstances()
144
    {
145
        $ruleSets = $this->createRuleSetsFromAbsoluteFiles(
146
            'rulesets/set1.xml',
147
            'rulesets/set2.xml'
148
        );
149
        $this->assertInstanceOf(RuleSet::class, $ruleSets[0]);
150
        $this->assertInstanceOf(RuleSet::class, $ruleSets[1]);
151
    }
152
153
    /**
154
     * testCreateRuleSetsForTwoConfiguresExpectedRuleSetNames
155
     *
156
     * @return void
157
     */
158
    public function testCreateRuleSetsForTwoConfiguresExpectedRuleSetNames()
159
    {
160
        $ruleSets = $this->createRuleSetsFromAbsoluteFiles(
161
            'rulesets/set1.xml',
162
            'rulesets/set2.xml'
163
        );
164
        $this->assertEquals('First Test RuleSet', $ruleSets[0]->getName());
165
        $this->assertEquals('Second Test RuleSet', $ruleSets[1]->getName());
166
    }
167
168
    /**
169
     * testCreateRuleSetsForTwoConfiguresExpectedRuleSetDescriptions
170
     *
171
     * @return void
172
     */
173
    public function testCreateRuleSetsForTwoConfiguresExpectedRuleSetDescriptions()
174
    {
175
        $ruleSets = $this->createRuleSetsFromAbsoluteFiles(
176
            'rulesets/set1.xml',
177
            'rulesets/set2.xml'
178
        );
179
        $this->assertSame('First description...', $ruleSets[0]->getDescription());
180
        $this->assertSame('Second description...', $ruleSets[1]->getDescription());
181
    }
182
183
    /**
184
     * testCreateRuleSetsForSingleLocalFileNameReturnsArray
185
     *
186
     * @return void
187
     */
188
    public function testCreateRuleSetsForLocalFileNameReturnsArray()
189
    {
190
        self::changeWorkingDirectory();
191
192
        $ruleSets = $this->createRuleSetsFromFiles('rulesets/set1.xml');
193
        $this->assertInternalType('array', $ruleSets);
194
    }
195
196
    /**
197
     * testCreateRuleSetsForSingleLocalFileNameReturnsArrayWithOneElement
198
     *
199
     * @return void
200
     */
201
    public function testCreateRuleSetsForLocalFileNameReturnsArrayWithOneElement()
202
    {
203
        self::changeWorkingDirectory();
204
205
        $ruleSets = $this->createRuleSetsFromFiles('rulesets/set1.xml');
206
        $this->assertCount(1, $ruleSets);
207
    }
208
209
    /**
210
     * testCreateRuleSetsForSingleLocalFileNameConfiguresExpectedRuleSetName
211
     *
212
     * @return void
213
     */
214
    public function testCreateRuleSetsForLocalFileNameConfiguresExpectedRuleSetName()
215
    {
216
        self::changeWorkingDirectory();
217
218
        $ruleSets = $this->createRuleSetsFromFiles('rulesets/set1.xml');
219
        $this->assertEquals('First Test RuleSet', $ruleSets[0]->getName());
220
    }
221
222
    /**
223
     * testCreateRuleSetsWithReferenceContainsExpectedRuleSet
224
     *
225
     * @return void
226
     */
227
    public function testCreateRuleSetsWithReferenceContainsExpectedRuleSet()
228
    {
229
        self::changeWorkingDirectory();
230
231
        $ruleSets = $this->createRuleSetsFromAbsoluteFiles('rulesets/refset1.xml');
232
        $this->assertEquals('First Test RuleSet', $ruleSets[0]->getName());
233
    }
234
235
    /**
236
     * testCreateRuleSetsWithReferenceContainsExpectedNumberOfRules
237
     *
238
     * @return void
239
     */
240
    public function testCreateRuleSetsWithReferenceContainsExpectedNumberOfRules()
241
    {
242
        self::changeWorkingDirectory();
243
244
        $ruleSets = $this->createRuleSetsFromAbsoluteFiles('rulesets/refset1.xml');
245
        $this->assertEquals(4, iterator_count($ruleSets[0]));
246
    }
247
248
    /**
249
     * testCreateRuleSetsForLocalFileWithRuleSetReferenceNodes
250
     *
251
     * @return void
252
     */
253
    public function testCreateRuleSetsWithReferenceContainsRuleInstances()
254
    {
255
        self::changeWorkingDirectory();
256
257
        $ruleSets = $this->createRuleSetsFromAbsoluteFiles('rulesets/refset1.xml');
258
        $this->assertInstanceOf(AbstractRule::class, $ruleSets[0]->getRules()->current());
259
    }
260
261
    /**
262
     * testCreateRuleSetsWithReferenceContainsExpectedRules
263
     *
264
     * @return void
265
     */
266
    public function testCreateRuleSetsWithReferenceContainsExpectedRules()
267
    {
268
        self::changeWorkingDirectory();
269
270
        $ruleSets = $this->createRuleSetsFromAbsoluteFiles('rulesets/refset2.xml');
271
272
        $actual   = array();
273
        $expected = array('RuleTwoInFirstRuleSet', 'RuleOneInSecondRuleSet');
274
275
        foreach ($ruleSets[0]->getRules() as $rule) {
276
            $actual[] = $rule->getName();
277
        }
278
279
        $this->assertEquals($expected, $actual);
280
    }
281
282
    /**
283
     * testCreateSingleRuleSetReturnsRuleSetInstance
284
     *
285
     * @return void
286
     */
287
    public function testCreateSingleRuleSetReturnsRuleSetInstance()
288
    {
289
        self::changeWorkingDirectory();
290
291
        $factory = new RuleSetFactory();
292
        $ruleSet = $factory->createSingleRuleSet('set1');
293
294
        $this->assertInstanceOf(RuleSet::class, $ruleSet);
295
    }
296
297
    /**
298
     * Tests that the rule-set factory applies a set minimum priority filter correct.
299
     *
300
     * @return void
301
     */
302
    public function testCreateRuleSetWithSpecifiedMinimumPriorityOnlyContainsMatchingRules()
303
    {
304
        self::changeWorkingDirectory();
305
306
        $factory = new RuleSetFactory();
307
        $factory->setMinimumPriority(2);
308
309
        $ruleSet = $factory->createSingleRuleSet('set1');
310
        $this->assertSame(1, iterator_count($ruleSet->getRules()));
311
    }
312
313
    /**
314
     * Tests that the rule-set factory applies a set maximum priority filter correct.
315
     *
316
     * @return void
317
     */
318
    public function testCreateRuleSetWithSpecifiedMaximumPriorityOnlyContainsMatchingRules()
319
    {
320
        self::changeWorkingDirectory();
321
322
        $factory = new RuleSetFactory();
323
        $factory->setMaximumPriority(2);
324
325
        $ruleSet = $factory->createSingleRuleSet('set1');
326
        $this->assertSame(1, iterator_count($ruleSet->getRules()));
327
    }
328
329
    /**
330
     * Tests that the rule-set factory applies a set maximum priority filter correct.
331
     *
332
     * @return void
333
     */
334
    public function testCreateRuleSetWithSpecifiedPrioritiesOnlyContainsMatchingRules()
335
    {
336
        self::changeWorkingDirectory();
337
338
        $factory = new RuleSetFactory();
339
        $factory->setMinimumPriority(2);
340
        $factory->setMaximumPriority(2);
341
342
        $ruleSet = $factory->createSingleRuleSet('set1');
343
        $this->assertCount(0, $ruleSet->getRules());
344
    }
345
346
    /**
347
     * testCreateRuleWithExcludePattern
348
     *
349
     * @return void
350
     */
351
    public function testCreateRuleWithExcludePattern()
352
    {
353
        self::changeWorkingDirectory();
354
355
        $factory  = new RuleSetFactory();
356
        $excludes = $factory->getIgnorePattern('exclude-pattern');
357
358
        $expected = array(
359
            'some/excluded/files'
360
        );
361
362
        $this->assertEquals($expected, $excludes);
363
    }
364
365
    /**
366
     * testCreateRuleSetsWithRuleReferenceThatOverwritesPrioritySetting
367
     *
368
     * @return void
369
     */
370 View Code Duplication
    public function testCreateRuleSetsWithRuleReferenceThatOverwritesPrioritySetting()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
371
    {
372
        self::changeWorkingDirectory();
373
374
        $factory  = new RuleSetFactory();
375
        $ruleSets = $factory->createRuleSets('refset3');
376
377
        $rule = $ruleSets[0]->getRules()->current();
378
        $this->assertSame(4, $rule->getPriority());
379
    }
380
381
    /**
382
     * testCreateRuleWithExpectedExample
383
     *
384
     * @return void
385
     */
386 View Code Duplication
    public function testCreateRuleWithExpectedExample()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
387
    {
388
        self::changeWorkingDirectory();
389
390
        $factory  = new RuleSetFactory();
391
        $ruleSets = $factory->createRuleSets('set1');
392
393
        $rule = $ruleSets[0]->getRules()->current();
394
        $this->assertEquals(array(__FUNCTION__), $rule->getExamples());
395
    }
396
397
    /**
398
     * testCreateRuleWithExpectedMultipleExamples
399
     *
400
     * @return void
401
     */
402
    public function testCreateRuleWithExpectedMultipleExamples()
403
    {
404
        self::changeWorkingDirectory();
405
406
        $factory  = new RuleSetFactory();
407
        $ruleSets = $factory->createRuleSets('set2');
408
409
        $rule = $ruleSets[0]->getRules()->current();
410
        $this->assertEquals(array(__FUNCTION__ . 'One', __FUNCTION__ . 'Two'), $rule->getExamples());
411
    }
412
413
    /**
414
     * testCreateRuleSetsWithRuleReferenceThatOverwritesDescriptionSetting
415
     *
416
     * @return void
417
     */
418 View Code Duplication
    public function testCreateRuleSetsWithRuleReferenceThatOverwritesDescriptionSetting()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
419
    {
420
        self::changeWorkingDirectory();
421
422
        $factory  = new RuleSetFactory();
423
        $ruleSets = $factory->createRuleSets('refset3');
424
425
        $rule = $ruleSets[0]->getRules()->current();
426
        $this->assertSame('description 42', $rule->getDescription());
427
    }
428
429
    /**
430
     * testCreateRuleSetsWithRuleReferenceThatOverwritesPropertySetting
431
     *
432
     * @return void
433
     */
434 View Code Duplication
    public function testCreateRuleSetsWithRuleReferenceThatOverwritesPropertySetting()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
435
    {
436
        self::changeWorkingDirectory();
437
438
        $factory  = new RuleSetFactory();
439
        $ruleSets = $factory->createRuleSets('refset3');
440
441
        $rule = $ruleSets[0]->getRules()->current();
442
        $this->assertSame(42, $rule->getIntProperty('foo'));
443
    }
444
445
    /**
446
     * testFactorySupportsAlternativeSyntaxForPropertyValue
447
     *
448
     * @return void
449
     */
450 View Code Duplication
    public function testFactorySupportsAlternativeSyntaxForPropertyValue()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
451
    {
452
        self::changeWorkingDirectory();
453
454
        $factory  = new RuleSetFactory();
455
        $ruleSets = $factory->createRuleSets('alternative-property-value-syntax');
456
457
        $rule = $ruleSets[0]->getRules()->current();
458
        $this->assertSame(42, $rule->getIntProperty('foo'));
459
    }
460
461
    /**
462
     * testCreateRuleSetsWithRuleReferenceThatOverwritesExamplesSetting
463
     *
464
     * @return void
465
     */
466 View Code Duplication
    public function testCreateRuleSetsWithRuleReferenceThatOverwritesExamplesSetting()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
467
    {
468
        self::changeWorkingDirectory();
469
470
        $factory  = new RuleSetFactory();
471
        $ruleSets = $factory->createRuleSets('refset3');
472
473
        $rule = $ruleSets[0]->getRules()->current();
474
475
        $examples = $rule->getExamples();
476
        $this->assertEquals('foreach ($foo as $bar) { echo $bar; }', $examples[0]);
477
    }
478
479
    /**
480
     * testCreateRuleSetsWithRuleReferenceThatOverwritesExamplesSetting
481
     *
482
     * @return void
483
     */
484 View Code Duplication
    public function testCreateRuleSetsWithRuleReferenceThatOverwritesNameSetting()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
485
    {
486
        self::changeWorkingDirectory();
487
488
        $factory  = new RuleSetFactory();
489
        $ruleSets = $factory->createRuleSets('refset4');
490
491
        $rule = $ruleSets[0]->getRules()->current();
492
        $this->assertEquals('Name overwritten', $rule->getName());
493
    }
494
495
    /**
496
     * testCreateRuleSetsWithRuleReferenceThatOverwritesMessageSetting
497
     *
498
     * @return void
499
     */
500 View Code Duplication
    public function testCreateRuleSetsWithRuleReferenceThatOverwritesMessageSetting()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
501
    {
502
        self::changeWorkingDirectory();
503
504
        $factory  = new RuleSetFactory();
505
        $ruleSets = $factory->createRuleSets('refset4');
506
507
        $rule = $ruleSets[0]->getRules()->current();
508
        $this->assertEquals('Message overwritten', $rule->getMessage());
509
    }
510
511
    /**
512
     * testCreateRuleSetsWithRuleReferenceThatOverwritesExtInfoUrlSetting
513
     *
514
     * @return void
515
     */
516 View Code Duplication
    public function testCreateRuleSetsWithRuleReferenceThatOverwritesExtInfoUrlSetting()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
517
    {
518
        self::changeWorkingDirectory();
519
520
        $factory  = new RuleSetFactory();
521
        $ruleSets = $factory->createRuleSets('refset4');
522
523
        $rule = $ruleSets[0]->getRules()->current();
524
        $this->assertEquals('http://example.com/overwritten', $rule->getExternalInfoUrl());
525
    }
526
527
    /**
528
     * testCreateRuleSetsWithRuleReferenceNotContainsExcludedRule
529
     *
530
     * @return void
531
     */
532 View Code Duplication
    public function testCreateRuleSetsWithRuleReferenceNotContainsExcludedRule()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
533
    {
534
        self::changeWorkingDirectory();
535
536
        $factory  = new RuleSetFactory();
537
        $ruleSets = $factory->createRuleSets('refset-exclude-one');
538
539
        $rules = $ruleSets[0]->getRules();
540
        $this->assertEquals(1, iterator_count($rules));
541
    }
542
543
    /**
544
     * testCreateRuleSetsWithRuleReferenceNotContainsExcludedRules
545
     *
546
     * @return void
547
     */
548 View Code Duplication
    public function testCreateRuleSetsWithRuleReferenceNotContainsExcludedRules()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
549
    {
550
        self::changeWorkingDirectory();
551
552
        $factory  = new RuleSetFactory();
553
        $ruleSets = $factory->createRuleSets('refset-exclude-all');
554
555
        $rules = $ruleSets[0]->getRules();
556
        $this->assertEquals(0, iterator_count($rules));
557
    }
558
559
    /**
560
     * Tests that the factory throws the expected exception for an invalid ruleset
561
     * identifier.
562
     *
563
     * @return void
564
     * @covers \PHPMD\RuleSetNotFoundException
565
     */
566
    public function testCreateRuleSetsThrowsExceptionForInvalidIdentifier()
567
    {
568
        $factory = new RuleSetFactory();
569
570
        $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

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...
571
            RuleSetNotFoundException::class,
572
            'Cannot find specified rule-set "foo-bar-ruleset-23".'
573
        );
574
575
        $factory->createRuleSets('foo-bar-ruleset-23');
576
    }
577
578
    /**
579
     * Tests that the factory throws an exception when the source code filename
580
     * for the configured rule does not exist.
581
     *
582
     * @return void
583
     * @covers \PHPMD\RuleClassFileNotFoundException
584
     */
585 View Code Duplication
    public function testCreateRuleSetsThrowsExceptionWhenClassFileNotInIncludePath()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
586
    {
587
        $fileName = self::createFileUri('rulesets/set-class-file-not-found.xml');
588
        $factory  = new RuleSetFactory();
589
590
        $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

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...
591
            RuleClassFileNotFoundException::class,
592
            'Cannot load source file for class: PHPMD\\Stubs\\ClassFileNotFoundRule'
593
        );
594
595
        $factory->createRuleSets($fileName);
596
    }
597
598
    /**
599
     * Tests that the factory throws the expected exception when a rule class
600
     * cannot be found.
601
     *
602
     * @return void
603
     * @covers \PHPMD\RuleClassNotFoundException
604
     */
605 View Code Duplication
    public function testCreateRuleSetThrowsExceptionWhenFileNotContainsClass()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
606
    {
607
        $fileName = self::createFileUri('rulesets/set-class-not-found.xml');
608
        $factory  = new RuleSetFactory();
609
610
        $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

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...
611
            RuleClassNotFoundException::class,
612
            'Cannot find rule class: PHPMD\\Stubs\\ClassNotFoundRule'
613
        );
614
615
        $factory->createRuleSets($fileName);
616
    }
617
618
    /**
619
     * Tests that the factory throws the expected exception when a rule class
620
     * cannot be found.
621
     *
622
     * @return void
623
     * @covers \PHPMD\RuleClassNotFoundException
624
     * @expectedException \RuntimeException
625
     */
626
    public function testCreateRuleSetsThrowsExpectedExceptionForInvalidXmlFile()
627
    {
628
        $fileName = self::createFileUri('rulesets/set-invalid-xml.xml');
629
630
        $factory = new RuleSetFactory();
631
        $factory->createRuleSets($fileName);
632
    }
633
634
    /**
635
     * testCreateRuleSetsActivatesStrictModeOnRuleSet
636
     *
637
     * @return void
638
     */
639
    public function testCreateRuleSetsActivatesStrictModeOnRuleSet()
640
    {
641
        $fileName = self::createFileUri('rulesets/set1.xml');
642
643
        $factory = new RuleSetFactory();
644
        $factory->setStrict();
645
646
        $ruleSets = $factory->createRuleSets($fileName);
647
648
        $this->assertAttributeEquals(true, 'strict', $ruleSets[0]);
649
    }
650
651
    /**
652
     * Tests that adding an include-path via ruleset works.
653
     * Also implicitly tests (by parsing the ruleset) that
654
     * reference-by-includepath and explicit-classfile-declaration works.
655
     *
656
     * @return void
657
     * @throws \Exception
658
     */
659
    public function testAddPHPIncludePath()
660
    {
661
        $includePathBefore = get_include_path();
662
663
        $rulesetFilepath = 'rulesets/ruleset-refs.xml';
664
        $fileName = self::createFileUri($rulesetFilepath);
665
666
        try {
667
            $factory = new RuleSetFactory();
668
            $factory->createRuleSets($fileName);
669
670
            $expectedIncludePath  = "/foo/bar/baz";
671
            $actualIncludePaths   = explode(PATH_SEPARATOR, get_include_path());
672
            $isIncludePathPresent = in_array($expectedIncludePath, $actualIncludePaths);
673
        } catch (\Exception $exception) {
674
            set_include_path($includePathBefore);
675
            throw $exception;
676
        }
677
678
        set_include_path($includePathBefore);
679
680
        $this->assertTrue(
681
            $isIncludePathPresent,
682
            "The include-path from '{$rulesetFilepath}' was not set!"
683
        );
684
    }
685
686
    /**
687
     * Checks if PHPMD doesn't treat directories named as code rule as files
688
     *
689
     * @return void
690
     * @link https://github.com/phpmd/phpmd/issues/47
691
     */
692
    public function testIfGettingRuleFilePathExcludeUnreadablePaths()
693
    {
694
        self::changeWorkingDirectory(__DIR__);
695
        $factory = new RuleSetFactory();
696
        $runtimeExceptionCount = 0;
697
        $ruleSetNotFoundExceptionCount = 0;
698
699
        foreach ($this->getPathsForFileAccessTest() as $path) {
700
            try {
701
                $this->assertEquals(
702
                    array('some/excluded/files'),
703
                    $factory->getIgnorePattern($path . self::DIR_UNDER_TESTS)
704
                );
705
            } catch (RuleSetNotFoundException $e) {
706
                $ruleSetNotFoundExceptionCount++;
707
            } catch (\RuntimeException $e) {
708
                $runtimeExceptionCount++;
709
            }
710
        }
711
        $this->assertEquals(0, $runtimeExceptionCount);
712
        $this->assertEquals(5, $ruleSetNotFoundExceptionCount);
713
    }
714
715
    /**
716
     * Invokes the <b>createRuleSets()</b> of the {@link RuleSetFactory}
717
     * class.
718
     *
719
     * @param string $file At least one rule configuration file name. You can
720
     *        also pass multiple parameters with ruleset configuration files.
721
     * @return \PHPMD\RuleSet[]
722
     */
723
    private function createRuleSetsFromAbsoluteFiles($file)
724
    {
725
        $files = (1 === func_num_args() ? array($file) : func_get_args());
726
        $files = array_map(array(__CLASS__, 'createFileUri'), $files);
727
728
        return call_user_func_array(array($this, 'createRuleSetsFromFiles'), $files);
729
    }
730
731
    /**
732
     * Invokes the <b>createRuleSets()</b> of the {@link RuleSetFactory}
733
     * class.
734
     *
735
     * @param string $file At least one rule configuration file name. You can
736
     *        also pass multiple parameters with ruleset configuration files.
737
     * @return \PHPMD\RuleSet[]
738
     */
739
    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...
740
    {
741
        $args = func_get_args();
742
743
        $factory = new RuleSetFactory();
744
        return $factory->createRuleSets(implode(',', $args));
745
    }
746
747
    /**
748
     * Sets up files and directories for XML rule file access test
749
     *
750
     * @return array Paths to test against
751
     */
752
    public function getPathsForFileAccessTest()
753
    {
754
        $fileContent = file_get_contents(__DIR__ . '/../../resources/files/rulesets/exclude-pattern.xml');
755
        $structure = array(
756
            'dir1' => array(
757
                self::DIR_UNDER_TESTS => array(), // directory - skipped
758
                'foo' => array(), // directory, criteria do not apply
759
            ),
760
            'dir2' => array(
761
                self::DIR_UNDER_TESTS => array(), // directory, wrong permissions
762
            ),
763
            'dir3' => array(
764
                self::DIR_UNDER_TESTS => array(), // directory, wrong owner and group
765
            ),
766
            'dirÅ' => array( // check UTF-8 characters handling
767
                'foo' => array(
768
                    self::DIR_UNDER_TESTS => $fileContent, // wrong permissions
769
                ),
770
                'bar' => array(
771
                    self::DIR_UNDER_TESTS => $fileContent, // OK
772
                ),
773
            ),
774
        );
775
        $root = vfsStream::setup('root', null, $structure);
776
        $root->getChild('dir2/' . self::DIR_UNDER_TESTS)->chmod(000);
777
        $root->getChild('dir3/' . self::DIR_UNDER_TESTS)->chown(vfsStream::OWNER_ROOT)->chgrp(vfsStream::GROUP_ROOT);
778
        $root->getChild('dirÅ/foo/' . self::DIR_UNDER_TESTS)->chmod(000);
779
780
        return array(
781
            $root->url(),
782
            $root->url() . '/dir1/',
783
            $root->url() . '/dir2/',
784
            $root->url() . '/dir3/',
785
            $root->url() . '/dirÅ/foo/',
786
            $root->url() . '/dirÅ/bar/',
787
        );
788
    }
789
}
790