Passed
Push — master ( 63bef5...c6eec4 )
by Kyle
44s queued 11s
created

testRuleDoesNotApplyWhenSuppressed()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
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\Rule\CleanCode;
19
20
use PHPMD\AbstractTest;
21
22
/**
23
 * MissingImport Tests
24
 *
25
 * @coversDefaultClass \PHPMD\Rule\CleanCode\MissingImport
26
 */
27
class MissingImportTest extends AbstractTest
28
{
29
    /**
30
     * Get the rule under test.
31
     *
32
     * @return MissingImport
33
     */
34
    public function getRule()
35
    {
36
        $rule = new MissingImport();
37
        $rule->addProperty('ignore-global', 'false');
38
        return $rule;
39
    }
40
41
    /**
42
     * Tests the rule for cases where it should apply.
43
     *
44
     * @param string $file The test file to test against.
45
     * @return void
46
     * @dataProvider getApplyingCases
47
     */
48
    public function testRuleAppliesTo($file)
49
    {
50
        $this->expectRuleHasViolationsForFile($this->getRule(), static::ONE_VIOLATION, $file);
51
    }
52
53
    /**
54
     * Tests the rule for cases where it should not apply.
55
     *
56
     * @param string $file The test file to test against.
57
     * @return void
58
     * @dataProvider getNotApplyingCases
59
     */
60
    public function testRuleDoesNotApplyTo($file)
61
    {
62
        $this->expectRuleHasViolationsForFile($this->getRule(), static::NO_VIOLATION, $file);
63
    }
64
65
    /**
66
     * Tests that it applies to a class that has fully qualified class names
67
     *
68
     * @return void
69
     * @covers ::apply
70
     * @covers ::isSelfReference
71
     */
72
    public function testRuleAppliesTwiceToClassWithNotImportedDependencies()
73
    {
74
        $rule = $this->getRule();
75
        $rule->setReport($this->getReportMock(2));
0 ignored issues
show
Bug introduced by
It seems like $this->getReportMock(2) targeting PHPMD\AbstractTest::getReportMock() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, PHPMD\AbstractRule::setReport() does only seem to accept object<PHPMD\Report>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
76
        $rule->apply($this->getMethod());
77
    }
78
79
    /**
80
     * Tests that it does not apply to a class in root namespace when configured.
81
     *
82
     * @return void
83
     * @covers ::apply
84
     * @covers ::isGlobalNamespace
85
     */
86
    public function testRuleDoesNotApplyWhenSuppressed()
87
    {
88
        $rule = new MissingImport();
89
        $rule->addProperty('ignore-global', 'true');
90
        $files = $this->getFilesForCalledClass('testRuleAppliesTo*');
91
        foreach ($files as $file) {
92
            // Covers case when the new property is set and the rule *should* apply.
93
            if (strpos($file, 'WithNotImportedDeepDependencies')) {
94
                $this->expectRuleHasViolationsForFile($rule, static::ONE_VIOLATION, $file);
95
                continue;
96
            }
97
            // Covers case when the new property is set and the rule *should not* apply.
98
            $this->expectRuleHasViolationsForFile($rule, static::NO_VIOLATION, $file);
99
        }
100
    }
101
}
102