Passed
Push — master ( 1964e0...1e1514 )
by Marc
49s queued 11s
created

CommandTest::testOutput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 19
rs 9.6333
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/ccn_suppress_function.php',
95
                Command::EXIT_VIOLATION,
96
                array('--strict'),
97
            ),
98
            array(
99
                'source/ccn_suppress_function.php',
100
                Command::EXIT_SUCCESS,
101
            ),
102
        );
103
    }
104
105
    /**
106
     * @return void
107
     */
108
    public function testWithMultipleReportFiles()
109
    {
110
        $args = array(
111
            __FILE__,
112
            self::createFileUri('source/source_with_npath_violation.php'),
113
            'xml',
114
            'design',
115
            '--reportfile',
116
            self::createTempFileUri(),
117
            '--reportfile-xml',
118
            $xml = self::createTempFileUri(),
119
            '--reportfile-html',
120
            $html = self::createTempFileUri(),
121
            '--reportfile-text',
122
            $text = self::createTempFileUri(),
123
            '--reportfile-json',
124
            $json = self::createTempFileUri(),
125
        );
126
127
        Command::main($args);
128
129
        $this->assertFileExists($xml);
130
        $this->assertFileExists($html);
131
        $this->assertFileExists($text);
132
        $this->assertFileExists($json);
133
    }
134
135
    public function testOutput()
136
    {
137
        $uri = realpath(self::createFileUri('source/source_with_anonymous_class.php'));
138
        $temp = self::createTempFileUri();
139
        $exitCode = Command::main(array(
140
            __FILE__,
141
            $uri,
142
            'text',
143
            'naming',
144
            '--reportfile',
145
            $temp,
146
        ));
147
148
        $this->assertSame(Command::EXIT_VIOLATION, $exitCode);
149
        $this->assertSame(
150
            "$uri:8	Avoid variables with short names like \$a. Configured minimum length is 3." . PHP_EOL,
151
            file_get_contents($temp)
152
        );
153
    }
154
155
    /**
156
     * @param string $option
157
     * @param string $value
158
     * @return void
159
     * @dataProvider dataProviderWithFilter
160
     */
161
    public function testWithFilter($option, $value)
162
    {
163
        $args = array(
164
            __FILE__,
165
            self::createFileUri('source/'),
166
            'text',
167
            'codesize',
168
            '--reportfile',
169
            self::createTempFileUri(),
170
            $option,
171
            $value,
172
        );
173
174
        $exitCode = Command::main($args);
175
        $this->assertEquals(Command::EXIT_SUCCESS, $exitCode);
176
    }
177
178
    /**
179
     * @return array
180
     */
181
    public function dataProviderWithFilter()
182
    {
183
        return array(
184
            array('--suffixes', '.class.php'),
185
            array('--exclude', 'ccn_,npath_'),
186
        );
187
    }
188
189
    public function testMainWritesExceptionMessageToStderr()
190
    {
191
        stream_filter_register('stderr_stream', 'PHPMD\\TextUI\\StreamFilter');
192
193
        $this->stderrStreamFilter = stream_filter_prepend(STDERR, 'stderr_stream');
194
195
        Command::main(
196
            array(
197
                __FILE__,
198
                self::createFileUri('source/source_with_npath_violation.php'),
199
                "''",
200
                'naming',
201
            )
202
        );
203
204
        $this->assertContains(
205
            'Can\'t find the custom report class: ',
206
            StreamFilter::$streamHandle
207
        );
208
    }
209
210
    public function testMainPrintsVersionToStdout()
211
    {
212
        stream_filter_register('stderr_stream', 'PHPMD\\TextUI\\StreamFilter');
213
214
        $this->stderrStreamFilter = stream_filter_prepend(STDOUT, 'stderr_stream');
215
216
        Command::main(
217
            array(
218
                __FILE__,
219
                '--version',
220
            )
221
        );
222
223
        $data = @parse_ini_file(__DIR__ . '/../../../../../build.properties');
224
        $version = $data['project.version'];
225
226
        $this->assertEquals('PHPMD ' . $version, trim(StreamFilter::$streamHandle));
227
    }
228
}
229