Passed
Push — master ( 830897...939e6f )
by Kyle
01:43 queued 35s
created

testHasErrorsReturnsTrueForSourceWithError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 18
loc 18
rs 9.6666
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 PHPMD\Renderer\XMLRenderer;
21
use PHPMD\Stubs\WriterStub;
22
23
/**
24
 * Test case for the main PHPMD class.
25
 *
26
 * @covers \PHPMD\PHPMD
27
 */
28
class PHPMDTest extends AbstractTest
29
{
30
    /**
31
     * Tests the main PHPMD interface with default settings an a xml-renderer.
32
     *
33
     * @return void
34
     */
35 View Code Duplication
    public function testRunWithDefaultSettingsAndXmlRenderer()
36
    {
37
        self::changeWorkingDirectory();
38
39
        $writer = new WriterStub();
40
41
        $renderer = new XMLRenderer();
42
        $renderer->setWriter($writer);
43
44
        $phpmd = new PHPMD();
45
46
        $phpmd->processFiles(
47
            self::createFileUri('source/ccn_function.php'),
48
            'pmd-refset1',
49
            array($renderer),
50
            new RuleSetFactory()
51
        );
52
53
        $this->assertXmlEquals($writer->getData(), 'pmd/default-xml.xml');
54
    }
55
56
    /**
57
     * testRunWithDefaultSettingsAndXmlRendererAgainstSingleFile
58
     *
59
     * @return void
60
     */
61 View Code Duplication
    public function testRunWithDefaultSettingsAndXmlRendererAgainstDirectory()
62
    {
63
        self::changeWorkingDirectory();
64
65
        $writer = new WriterStub();
66
67
        $renderer = new XMLRenderer();
68
        $renderer->setWriter($writer);
69
70
        $phpmd = new PHPMD();
71
        $phpmd->processFiles(
72
            self::createFileUri('source'),
73
            'pmd-refset1',
74
            array($renderer),
75
            new RuleSetFactory()
76
        );
77
78
        $this->assertXmlEquals($writer->getData(), 'pmd/single-directory.xml');
79
    }
80
81
    /**
82
     * testRunWithDefaultSettingsAndXmlRendererAgainstSingleFile
83
     *
84
     * @return void
85
     */
86 View Code Duplication
    public function testRunWithDefaultSettingsAndXmlRendererAgainstSingleFile()
87
    {
88
        self::changeWorkingDirectory();
89
90
        $writer = new WriterStub();
91
92
        $renderer = new XMLRenderer();
93
        $renderer->setWriter($writer);
94
95
        $phpmd = new PHPMD();
96
        $phpmd->processFiles(
97
            self::createFileUri('source/ccn_function.php'),
98
            'pmd-refset1',
99
            array($renderer),
100
            new RuleSetFactory()
101
        );
102
103
        $this->assertXmlEquals($writer->getData(), 'pmd/single-file.xml');
104
    }
105
106
    /**
107
     * testHasErrorsReturnsFalseByDefault
108
     *
109
     * @return void
110
     */
111
    public function testHasErrorsReturnsFalseByDefault()
112
    {
113
        $phpmd = new PHPMD();
114
        $this->assertFalse($phpmd->hasErrors());
115
    }
116
117
    /**
118
     * testHasViolationsReturnsFalseByDefault
119
     *
120
     * @return void
121
     */
122
    public function testHasViolationsReturnsFalseByDefault()
123
    {
124
        $phpmd = new PHPMD();
125
        $this->assertFalse($phpmd->hasViolations());
126
    }
127
128
    /**
129
     * testHasViolationsReturnsFalseForSourceWithoutViolations
130
     *
131
     * @return void
132
     */
133 View Code Duplication
    public function testHasViolationsReturnsFalseForSourceWithoutViolations()
134
    {
135
        self::changeWorkingDirectory();
136
137
        $renderer = new XMLRenderer();
138
        $renderer->setWriter(new WriterStub());
139
140
        $phpmd = new PHPMD();
141
        $phpmd->processFiles(
142
            self::createFileUri('source/source_without_violations.php'),
143
            'pmd-refset1',
144
            array($renderer),
145
            new RuleSetFactory()
146
        );
147
148
        $this->assertFalse($phpmd->hasErrors());
149
        $this->assertFalse($phpmd->hasViolations());
150
    }
151
152
    /**
153
     * testHasViolationsReturnsTrueForSourceWithViolation
154
     *
155
     * @return void
156
     */
157 View Code Duplication
    public function testHasViolationsReturnsTrueForSourceWithViolation()
158
    {
159
        self::changeWorkingDirectory();
160
161
        $renderer = new XMLRenderer();
162
        $renderer->setWriter(new WriterStub());
163
164
        $phpmd = new PHPMD();
165
        $phpmd->processFiles(
166
            self::createFileUri('source/source_with_npath_violation.php'),
167
            'pmd-refset1',
168
            array($renderer),
169
            new RuleSetFactory()
170
        );
171
172
        $this->assertFalse($phpmd->hasErrors());
173
        $this->assertTrue($phpmd->hasViolations());
174
    }
175
176
    /**
177
     * testHasErrorsReturnsTrueForSourceWithError
178
     *
179
     * @return void
180
     */
181 View Code Duplication
    public function testHasErrorsReturnsTrueForSourceWithError()
182
    {
183
        self::changeWorkingDirectory();
184
185
        $renderer = new XMLRenderer();
186
        $renderer->setWriter(new WriterStub());
187
188
        $phpmd = new PHPMD();
189
        $phpmd->processFiles(
190
            self::createFileUri('source/source_with_parse_error.php'),
191
            'pmd-refset1',
192
            array($renderer),
193
            new RuleSetFactory()
194
        );
195
196
        $this->assertTrue($phpmd->hasErrors());
197
        $this->assertFalse($phpmd->hasViolations());
198
    }
199
200
    /**
201
     * testIgnorePattern
202
     *
203
     * @return void
204
     */
205
    public function testIgnorePattern()
206
    {
207
        self::changeWorkingDirectory();
208
209
        $phpmd = new PHPMD();
210
211
        // Process without exclusions, should result in violations.
212
        $phpmd->processFiles(
213
            self::createFileUri('sourceExcluded/'),
214
            'pmd-refset1',
215
            array(),
216
            new RuleSetFactory()
217
        );
218
219
        $this->assertFalse($phpmd->hasErrors());
220
        $this->assertTrue($phpmd->hasViolations());
221
222
        // Process with exclusions, should result in no violations.
223
        $phpmd->processFiles(
224
            self::createFileUri('sourceExcluded/'),
225
            'exclude-pattern',
226
            array(),
227
            new RuleSetFactory()
228
        );
229
230
        $this->assertFalse($phpmd->hasErrors());
231
        $this->assertFalse($phpmd->hasViolations());
232
    }
233
}
234