Passed
Push — master ( 95efe0...902a34 )
by Tobias van
41s queued 11s
created

CommandTest::testMainUpdateBaseline()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 23
rs 9.552
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\TextUI;
19
20
use PHPMD\AbstractTest;
21
22
/**
23
 * Test case for the {@link \PHPMD\TextUI\Command} class.
24
 *
25
 * @covers \PHPMD\TextUI\Command
26
 */
27
class CommandTest extends AbstractTest
28
{
29
    /**
30
     * @var resource
31
     */
32
    private $stderrStreamFilter;
33
34
    /**
35
     * @return void
36
     */
37 View Code Duplication
    protected function tearDown()
38
    {
39
        if (is_resource($this->stderrStreamFilter)) {
40
            stream_filter_remove($this->stderrStreamFilter);
41
        }
42
        $this->stderrStreamFilter = null;
43
44
        parent::tearDown();
45
    }
46
47
    /**
48
     * @param            $sourceFile
49
     * @param            $expectedExitCode
50
     * @param array|null $options
51
     * @return void
52
     * @dataProvider dataProviderTestMainWithOption
53
     */
54
    public function testMainStrictOptionIsOfByDefault($sourceFile, $expectedExitCode, array $options = null)
55
    {
56
        $args = array_filter(
57
            array_merge(
58
                array(
59
                    __FILE__,
60
                    self::createFileUri($sourceFile),
61
                    'html',
62
                    'codesize',
63
                    '--reportfile',
64
                    self::createTempFileUri(),
65
                ),
66
                (array)$options
67
            )
68
        );
69
70
        $exitCode = Command::main($args);
71
        $this->assertEquals($expectedExitCode, $exitCode);
72
    }
73
74
    /**
75
     * @return array
76
     */
77
    public function dataProviderTestMainWithOption()
78
    {
79
        return array(
80
            array(
81
                'source/source_without_violations.php',
82
                Command::EXIT_SUCCESS,
83
            ),
84
            array(
85
                'source/source_with_npath_violation.php',
86
                Command::EXIT_VIOLATION,
87
            ),
88
            array(
89
                'source/source_with_npath_violation.php',
90
                Command::EXIT_SUCCESS,
91
                array('--ignore-violations-on-exit'),
92
            ),
93
            array(
94
                'source/source_with_npath_violation.php',
95
                Command::EXIT_VIOLATION,
96
                array('--ignore-errors-on-exit'),
97
            ),
98
            array(
99
                'source/source_with_parse_error.php',
100
                Command::EXIT_ERROR,
101
            ),
102
            array(
103
                'source/source_with_parse_error.php',
104
                Command::EXIT_ERROR,
105
                array('--ignore-violations-on-exit'),
106
            ),
107
            array(
108
                'source/source_with_parse_error.php',
109
                Command::EXIT_SUCCESS,
110
                array('--ignore-errors-on-exit'),
111
            ),
112
            array(
113
                'source',
114
                Command::EXIT_ERROR,
115
            ),
116
            array(
117
                'source',
118
                Command::EXIT_ERROR,
119
                array('--ignore-violations-on-exit'),
120
            ),
121
            array(
122
                'source',
123
                Command::EXIT_VIOLATION,
124
                array('--ignore-errors-on-exit'),
125
            ),
126
            array(
127
                'source',
128
                Command::EXIT_SUCCESS,
129
                array('--ignore-errors-on-exit', '--ignore-violations-on-exit'),
130
            ),
131
            array(
132
                'source/ccn_suppress_function.php',
133
                Command::EXIT_VIOLATION,
134
                array('--strict'),
135
            ),
136
            array(
137
                'source/ccn_suppress_function.php',
138
                Command::EXIT_SUCCESS,
139
            ),
140
        );
141
    }
142
143
    /**
144
     * @return void
145
     */
146
    public function testWithMultipleReportFiles()
147
    {
148
        $args = array(
149
            __FILE__,
150
            self::createFileUri('source/source_with_npath_violation.php'),
151
            'xml',
152
            'design',
153
            '--reportfile',
154
            self::createTempFileUri(),
155
            '--reportfile-xml',
156
            $xml = self::createTempFileUri(),
157
            '--reportfile-html',
158
            $html = self::createTempFileUri(),
159
            '--reportfile-text',
160
            $text = self::createTempFileUri(),
161
            '--reportfile-json',
162
            $json = self::createTempFileUri(),
163
        );
164
165
        Command::main($args);
166
167
        $this->assertFileExists($xml);
168
        $this->assertFileExists($html);
169
        $this->assertFileExists($text);
170
        $this->assertFileExists($json);
171
    }
172
173
    public function testOutput()
174
    {
175
        $uri      = realpath(self::createFileUri('source/source_with_anonymous_class.php'));
176
        $temp     = self::createTempFileUri();
177
        $exitCode = Command::main(array(
178
            __FILE__,
179
            $uri,
180
            'text',
181
            'naming',
182
            '--reportfile',
183
            $temp,
184
        ));
185
186
        $this->assertSame(Command::EXIT_VIOLATION, $exitCode);
187
        $this->assertSame(
188
            "$uri:8	Avoid variables with short names like \$a. Configured minimum length is 3." . PHP_EOL,
189
            file_get_contents($temp)
190
        );
191
    }
192
193
    /**
194
     * @param string $option
195
     * @param string $value
196
     * @return void
197
     * @dataProvider dataProviderWithFilter
198
     */
199
    public function testWithFilter($option, $value)
200
    {
201
        $args = array(
202
            __FILE__,
203
            self::createFileUri('source/'),
204
            'text',
205
            'codesize',
206
            '--reportfile',
207
            self::createTempFileUri(),
208
            $option,
209
            $value,
210
        );
211
212
        $exitCode = Command::main($args);
213
        $this->assertEquals(Command::EXIT_SUCCESS, $exitCode);
214
    }
215
216
    /**
217
     * @return array
218
     */
219
    public function dataProviderWithFilter()
220
    {
221
        return array(
222
            array('--suffixes', '.class.php'),
223
            array('--exclude', 'ccn_,npath_,parse_error'),
224
        );
225
    }
226
227
    public function testMainGenerateBaseline()
228
    {
229
        $uri      = str_replace("\\", "/", realpath(self::createFileUri('source/source_with_anonymous_class.php')));
230
        $temp     = self::createTempFileUri();
231
        $exitCode = Command::main(array(
232
            __FILE__,
233
            $uri,
234
            'text',
235
            'naming',
236
            '--generate-baseline',
237
            '--baseline-file',
238
            $temp,
239
        ));
240
241
        static::assertSame(Command::EXIT_SUCCESS, $exitCode);
242
        static::assertFileExists($temp);
243
        static::assertContains($uri, file_get_contents($temp));
244
    }
245
246
    /**
247
     * Testcase:
248
     * - Class has existing ShortVariable and new BooleanGetMethodName violations
249
     * - Baseline has ShortVariable and LongClassName baseline violations
250
     * Expect in baseline:
251
     * - LongClassName violation should be removed
252
     * - ShortVariable violation should still exist
253
     * - BooleanGetMethodName shouldn't be added
254
     */
255
    public function testMainUpdateBaseline()
256
    {
257
        $sourceTemp   = self::createTempFileUri('ClassWithMultipleViolations.php');
258
        $baselineTemp = self::createTempFileUri();
259
        copy(static::createResourceUriForTest('UpdateBaseline/ClassWithMultipleViolations.php'), $sourceTemp);
260
        copy(static::createResourceUriForTest('UpdateBaseline/phpmd.baseline.xml'), $baselineTemp);
261
262
        $exitCode = Command::main(array(
263
            __FILE__,
264
            $sourceTemp,
265
            'text',
266
            'naming',
267
            '--update-baseline',
268
            '--baseline-file',
269
            $baselineTemp,
270
        ));
271
272
        static::assertSame(Command::EXIT_SUCCESS, $exitCode);
273
        static::assertXmlStringEqualsXmlString(
274
            file_get_contents(static::createResourceUriForTest('UpdateBaseline/expected.baseline.xml')),
275
            file_get_contents($baselineTemp)
276
        );
277
    }
278
279
    public function testMainBaselineViolationShouldBeIgnored()
280
    {
281
        $sourceFile   = realpath(static::createResourceUriForTest('Baseline/ClassWithShortVariable.php'));
282
        $baselineFile = realpath(static::createResourceUriForTest('Baseline/phpmd.baseline.xml'));
283
        $exitCode     = Command::main(array(
284
            __FILE__,
285
            $sourceFile,
286
            'text',
287
            'naming',
288
            '--baseline-file',
289
            $baselineFile,
290
        ));
291
292
        static::assertSame(Command::EXIT_SUCCESS, $exitCode);
293
    }
294
295
    public function testMainWritesExceptionMessageToStderr()
296
    {
297
        stream_filter_register('stderr_stream', 'PHPMD\\TextUI\\StreamFilter');
298
299
        $this->stderrStreamFilter = stream_filter_prepend(STDERR, 'stderr_stream');
300
301
        Command::main(
302
            array(
303
                __FILE__,
304
                self::createFileUri('source/source_with_npath_violation.php'),
305
                "''",
306
                'naming',
307
            )
308
        );
309
310
        $this->assertContains(
311
            'Can\'t find the custom report class: ',
312
            StreamFilter::$streamHandle
313
        );
314
    }
315
316
    public function testMainPrintsVersionToStdout()
317
    {
318
        stream_filter_register('stderr_stream', 'PHPMD\\TextUI\\StreamFilter');
319
320
        $this->stderrStreamFilter = stream_filter_prepend(STDOUT, 'stderr_stream');
321
322
        Command::main(
323
            array(
324
                __FILE__,
325
                '--version',
326
            )
327
        );
328
329
        $data    = @parse_ini_file(__DIR__ . '/../../../../../build.properties');
330
        $version = $data['project.version'];
331
332
        $this->assertEquals('PHPMD ' . $version, trim(StreamFilter::$streamHandle));
333
    }
334
}
335