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

CommandTest::dataProviderTestMainWithOption()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 65

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 65
rs 8.7636
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 testMainWritesExceptionMessageToStderr()
228
    {
229
        stream_filter_register('stderr_stream', 'PHPMD\\TextUI\\StreamFilter');
230
231
        $this->stderrStreamFilter = stream_filter_prepend(STDERR, 'stderr_stream');
232
233
        Command::main(
234
            array(
235
                __FILE__,
236
                self::createFileUri('source/source_with_npath_violation.php'),
237
                "''",
238
                'naming',
239
            )
240
        );
241
242
        $this->assertContains(
243
            'Can\'t find the custom report class: ',
244
            StreamFilter::$streamHandle
245
        );
246
    }
247
248
    public function testMainPrintsVersionToStdout()
249
    {
250
        stream_filter_register('stderr_stream', 'PHPMD\\TextUI\\StreamFilter');
251
252
        $this->stderrStreamFilter = stream_filter_prepend(STDOUT, 'stderr_stream');
253
254
        Command::main(
255
            array(
256
                __FILE__,
257
                '--version',
258
            )
259
        );
260
261
        $data = @parse_ini_file(__DIR__ . '/../../../../../build.properties');
262
        $version = $data['project.version'];
263
264
        $this->assertEquals('PHPMD ' . $version, trim(StreamFilter::$streamHandle));
265
    }
266
}
267