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

testCliUsageContainsIgnoreErrorsOnExitOption()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 7
loc 7
rs 10
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
use PHPMD\Rule;
22
23
/**
24
 * Test case for the {@link \PHPMD\TextUI\CommandLineOptions} class.
25
 *
26
 * @covers \PHPMD\TextUI\CommandLineOptions
27
 */
28
class CommandLineOptionsTest extends AbstractTest
29
{
30
    /**
31
     * @var resource
32
     */
33
    private $stderrStreamFilter;
34
35
    /**
36
     * @return void
37
     */
38 View Code Duplication
    protected function tearDown()
39
    {
40
        if (is_resource($this->stderrStreamFilter)) {
41
            stream_filter_remove($this->stderrStreamFilter);
42
        }
43
        $this->stderrStreamFilter = null;
44
45
        parent::tearDown();
46
    }
47
48
    /**
49
     * testAssignsInputArgumentToInputProperty
50
     *
51
     * @return void
52
     * @since 1.1.0
53
     */
54 View Code Duplication
    public function testAssignsInputArgumentToInputProperty()
55
    {
56
        $args = array('foo.php', __FILE__, 'text', 'design');
57
        $opts = new CommandLineOptions($args);
58
59
        self::assertEquals(__FILE__, $opts->getInputPath());
60
    }
61
62
    /**
63
     * testAssignsFormatArgumentToReportFormatProperty
64
     *
65
     * @return void
66
     * @since 1.1.0
67
     */
68 View Code Duplication
    public function testAssignsFormatArgumentToReportFormatProperty()
69
    {
70
        $args = array('foo.php', __FILE__, 'text', 'design');
71
        $opts = new CommandLineOptions($args);
72
73
        self::assertEquals('text', $opts->getReportFormat());
74
    }
75
76
    /**
77
     * testAssignsRuleSetsArgumentToRuleSetProperty
78
     *
79
     * @return void
80
     * @since 1.1.0
81
     */
82 View Code Duplication
    public function testAssignsRuleSetsArgumentToRuleSetProperty()
83
    {
84
        $args = array('foo.php', __FILE__, 'text', 'design');
85
        $opts = new CommandLineOptions($args);
86
87
        self::assertEquals('design', $opts->getRuleSets());
88
    }
89
90
    /**
91
     * testThrowsExpectedExceptionWhenRequiredArgumentsNotSet
92
     *
93
     * @return void
94
     * @since 1.1.0
95
     * @expectedException \InvalidArgumentException
96
     */
97
    public function testThrowsExpectedExceptionWhenRequiredArgumentsNotSet()
98
    {
99
        $args = array(__FILE__, 'text', 'design');
100
        new CommandLineOptions($args);
101
    }
102
103
    /**
104
     * testAssignsInputFileOptionToInputPathProperty
105
     *
106
     * @return void
107
     * @since 1.1.0
108
     */
109 View Code Duplication
    public function testAssignsInputFileOptionToInputPathProperty()
110
    {
111
        $uri = self::createResourceUriForTest('inputfile.txt');
112
113
        $args = array('foo.php', 'text', 'design', '--inputfile', $uri);
114
        $opts = new CommandLineOptions($args);
115
116
        self::assertEquals('Dir1/Class1.php,Dir2/Class2.php', $opts->getInputPath());
117
    }
118
119
    /**
120
     * testAssignsFormatArgumentCorrectWhenCalledWithInputFile
121
     *
122
     * @return void
123
     * @since 1.1.0
124
     */
125 View Code Duplication
    public function testAssignsFormatArgumentCorrectWhenCalledWithInputFile()
126
    {
127
        $uri = self::createResourceUriForTest('inputfile.txt');
128
129
        $args = array('foo.php', 'text', 'design', '--inputfile', $uri);
130
        $opts = new CommandLineOptions($args);
131
132
        self::assertEquals('text', $opts->getReportFormat());
133
    }
134
135
    /**
136
     * testAssignsRuleSetsArgumentCorrectWhenCalledWithInputFile
137
     *
138
     * @return void
139
     * @since 1.1.0
140
     */
141 View Code Duplication
    public function testAssignsRuleSetsArgumentCorrectWhenCalledWithInputFile()
142
    {
143
        $uri = self::createResourceUriForTest('inputfile.txt');
144
145
        $args = array('foo.php', 'text', 'design', '--inputfile', $uri);
146
        $opts = new CommandLineOptions($args);
147
148
        self::assertEquals('design', $opts->getRuleSets());
149
    }
150
151
    /**
152
     * testThrowsExpectedExceptionWhenInputFileNotExists
153
     *
154
     * @return void
155
     * @since 1.1.0
156
     * @expectedException \InvalidArgumentException
157
     */
158
    public function testThrowsExpectedExceptionWhenInputFileNotExists()
159
    {
160
        $args = array('foo.php', 'text', 'design', '--inputfile', 'inputfail.txt');
161
        new CommandLineOptions($args);
162
    }
163
164
    /**
165
     * testHasVersionReturnsFalseByDefault
166
     *
167
     * @return void
168
     */
169
    public function testHasVersionReturnsFalseByDefault()
170
    {
171
        $args = array(__FILE__, __FILE__, 'text', 'unusedcode');
172
        $opts = new CommandLineOptions($args);
173
174
        self::assertFalse($opts->hasVersion());
175
    }
176
177
    /**
178
     * testCliOptionsAcceptsVersionArgument
179
     *
180
     * @return void
181
     */
182
    public function testCliOptionsAcceptsVersionArgument()
183
    {
184
        $args = array(__FILE__, '--version');
185
        $opts = new CommandLineOptions($args);
186
187
        self::assertTrue($opts->hasVersion());
188
    }
189
190
    /**
191
     * Tests if ignoreErrorsOnExit returns false by default
192
     *
193
     * @return void
194
     */
195
    public function testIgnoreErrorsOnExitReturnsFalseByDefault()
196
    {
197
        $args = array(__FILE__, __FILE__, 'text', 'unusedcode');
198
        $opts = new CommandLineOptions($args);
199
200
        self::assertFalse($opts->ignoreErrorsOnExit());
201
    }
202
203
    /**
204
     * Tests if CLI options accepts ignoreErrorsOnExit argument
205
     *
206
     * @return void
207
     */
208 View Code Duplication
    public function testCliOptionsAcceptsIgnoreErrorsOnExitArgument()
209
    {
210
        $args = array(__FILE__, __FILE__, 'text', 'unusedcode', '--ignore-errors-on-exit');
211
        $opts = new CommandLineOptions($args);
212
213
        self::assertTrue($opts->ignoreErrorsOnExit());
214
    }
215
216
    /**
217
     * Tests if CLI usage contains ignoreErrorsOnExit option
218
     *
219
     * @return void
220
     */
221 View Code Duplication
    public function testCliUsageContainsIgnoreErrorsOnExitOption()
222
    {
223
        $args = array(__FILE__, __FILE__, 'text', 'codesize');
224
        $opts = new CommandLineOptions($args);
225
226
        $this->assertContains('--ignore-errors-on-exit:', $opts->usage());
227
    }
228
229
    /**
230
     * Tests if ignoreViolationsOnExit returns false by default
231
     *
232
     * @return void
233
     */
234
    public function testIgnoreViolationsOnExitReturnsFalseByDefault()
235
    {
236
        $args = array(__FILE__, __FILE__, 'text', 'unusedcode');
237
        $opts = new CommandLineOptions($args);
238
239
        self::assertFalse($opts->ignoreViolationsOnExit());
240
    }
241
242
    /**
243
     * Tests if CLI options accepts ignoreViolationsOnExit argument
244
     *
245
     * @return void
246
     */
247 View Code Duplication
    public function testCliOptionsAcceptsIgnoreViolationsOnExitArgument()
248
    {
249
        $args = array(__FILE__, __FILE__, 'text', 'unusedcode', '--ignore-violations-on-exit');
250
        $opts = new CommandLineOptions($args);
251
252
        self::assertTrue($opts->ignoreViolationsOnExit());
253
    }
254
255
    /**
256
     * Tests if CLI usage contains ignoreViolationsOnExit option
257
     *
258
     * @return void
259
     */
260 View Code Duplication
    public function testCliUsageContainsIgnoreViolationsOnExitOption()
261
    {
262
        $args = array(__FILE__, __FILE__, 'text', 'codesize');
263
        $opts = new CommandLineOptions($args);
264
265
        $this->assertContains('--ignore-violations-on-exit:', $opts->usage());
266
    }
267
268
    /**
269
     * Tests if CLI usage contains the auto-discovered renderers
270
     *
271
     * @return void
272
     */
273 View Code Duplication
    public function testCliUsageContainsAutoDiscoveredRenderers()
274
    {
275
        $args = array(__FILE__, __FILE__, 'text', 'codesize');
276
        $opts = new CommandLineOptions($args);
277
278
        $this->assertContains('Available formats: ansi, github, html, json, text, xml.', $opts->usage());
279
    }
280
281
    /**
282
     * testCliUsageContainsStrictOption
283
     *
284
     * @return void
285
     */
286 View Code Duplication
    public function testCliUsageContainsStrictOption()
287
    {
288
        $args = array(__FILE__, __FILE__, 'text', 'codesize');
289
        $opts = new CommandLineOptions($args);
290
291
        $this->assertContains('--strict:', $opts->usage());
292
    }
293
294
    /**
295
     * testCliOptionsIsStrictReturnsFalseByDefault
296
     *
297
     * @return void
298
     * @since 1.2.0
299
     */
300
    public function testCliOptionsIsStrictReturnsFalseByDefault()
301
    {
302
        $args = array(__FILE__, __FILE__, 'text', 'codesize');
303
        $opts = new CommandLineOptions($args);
304
305
        self::assertFalse($opts->hasStrict());
306
    }
307
308
    /**
309
     * testCliOptionsAcceptsStrictArgument
310
     *
311
     * @return void
312
     * @since 1.2.0
313
     */
314
    public function testCliOptionsAcceptsStrictArgument()
315
    {
316
        $args = array(__FILE__, '--strict', __FILE__, 'text', 'codesize');
317
        $opts = new CommandLineOptions($args);
318
319
        self::assertTrue($opts->hasStrict());
320
    }
321
322
    /**
323
     * @return void
324
     */
325 View Code Duplication
    public function testCliOptionsAcceptsMinimumpriorityArgument()
326
    {
327
        $args = array(__FILE__, '--minimumpriority', 42, __FILE__, 'text', 'codesize');
328
        $opts = new CommandLineOptions($args);
329
330
        $this->assertEquals(42, $opts->getMinimumPriority());
331
    }
332
333
    /**
334
     * @return void
335
     */
336 View Code Duplication
    public function testCliOptionsAcceptsMaximumpriorityArgument()
337
    {
338
        $args = array(__FILE__, '--maximumpriority', 42, __FILE__, 'text', 'codesize');
339
        $opts = new CommandLineOptions($args);
340
341
        $this->assertEquals(42, $opts->getMaximumPriority());
342
    }
343
344
    /**
345
     * @return void
346
     */
347 View Code Duplication
    public function testGetMinimumPriorityReturnsLowestValueByDefault()
348
    {
349
        $args = array(__FILE__, __FILE__, 'text', 'codesize');
350
        $opts = new CommandLineOptions($args);
351
352
        $this->assertEquals(Rule::LOWEST_PRIORITY, $opts->getMinimumPriority());
353
    }
354
355
    /**
356
     * @return void
357
     */
358
    public function testGetCoverageReportReturnsNullByDefault()
359
    {
360
        $args = array(__FILE__, __FILE__, 'text', 'codesize');
361
        $opts = new CommandLineOptions($args);
362
363
        $this->assertNull($opts->getCoverageReport());
364
    }
365
366
    /**
367
     * @return void
368
     */
369 View Code Duplication
    public function testGetCoverageReportWithCliOption()
370
    {
371
        $opts = new CommandLineOptions(
372
            array(
373
                __FILE__,
374
                __FILE__,
375
                'text',
376
                'codesize',
377
                '--coverage',
378
                __METHOD__,
379
            )
380
        );
381
382
        $this->assertEquals(__METHOD__, $opts->getCoverageReport());
383
    }
384
385
    /**
386
     * @param string $reportFormat
387
     * @param string $expectedClass
388
     * @return void
389
     * @dataProvider dataProviderCreateRenderer
390
     */
391
    public function testCreateRenderer($reportFormat, $expectedClass)
392
    {
393
        $args = array(__FILE__, __FILE__, $reportFormat, 'codesize');
394
        $opts = new CommandLineOptions($args);
395
396
        $this->assertInstanceOf($expectedClass, $opts->createRenderer($reportFormat));
397
    }
398
399
    /**
400
     * @return array
401
     */
402
    public function dataProviderCreateRenderer()
403
    {
404
        return array(
405
            array('html', 'PHPMD\\Renderer\\HtmlRenderer'),
406
            array('text', 'PHPMD\\Renderer\\TextRenderer'),
407
            array('xml', 'PHPMD\\Renderer\\XmlRenderer'),
408
            array('ansi', 'PHPMD\\Renderer\\AnsiRenderer'),
409
            array('PHPMD_Test_Renderer_PEARRenderer', 'PHPMD_Test_Renderer_PEARRenderer'),
410
            array('PHPMD\\Test\\Renderer\\NamespaceRenderer', 'PHPMD\\Test\\Renderer\\NamespaceRenderer'),
411
            /* Test what happens when class already exists. */
412
            array('PHPMD\\Test\\Renderer\\NamespaceRenderer', 'PHPMD\\Test\\Renderer\\NamespaceRenderer'),
413
        );
414
    }
415
416
    /**
417
     * @param string $reportFormat
418
     * @return void
419
     * @expectedException \InvalidArgumentException
420
     * @expectedExceptionMessageRegExp (^Can\'t )
421
     * @dataProvider dataProviderCreateRendererThrowsException
422
     */
423
    public function testCreateRendererThrowsException($reportFormat)
424
    {
425
        $args = array(__FILE__, __FILE__, $reportFormat, 'codesize');
426
        $opts = new CommandLineOptions($args);
427
        $opts->createRenderer();
428
    }
429
430
    /**
431
     * @return array
432
     */
433
    public function dataProviderCreateRendererThrowsException()
434
    {
435
        return array(
436
            array(''),
437
            array('PHPMD\\Test\\Renderer\\NotExistsRenderer'),
438
        );
439
    }
440
441
    /**
442
     * @param string $deprecatedName
443
     * @param string $newName
444
     * @dataProvider dataProviderDeprecatedCliOptions
445
     */
446
    public function testDeprecatedCliOptions($deprecatedName, $newName)
447
    {
448
        stream_filter_register('stderr_stream', 'PHPMD\\TextUI\\StreamFilter');
449
450
        $this->stderrStreamFilter = stream_filter_prepend(STDERR, 'stderr_stream');
451
452
        $args = array(__FILE__, __FILE__, 'text', 'codesize', sprintf('--%s', $deprecatedName), 42);
453
        new CommandLineOptions($args);
454
455
        $this->assertContains(
456
            sprintf(
457
                'The --%s option is deprecated, please use --%s instead.',
458
                $deprecatedName,
459
                $newName
460
            ),
461
            StreamFilter::$streamHandle
462
        );
463
    }
464
465
    /**
466
     * @return array
467
     */
468
    public function dataProviderDeprecatedCliOptions()
469
    {
470
        return array(
471
            array('extensions', 'suffixes'),
472
            array('ignore', 'exclude'),
473
        );
474
    }
475
476
    /**
477
     * @param array $options
478
     * @param array $expected
479
     * @return void
480
     * @dataProvider dataProviderGetReportFiles
481
     */
482 View Code Duplication
    public function testGetReportFiles(array $options, array $expected)
483
    {
484
        $args = array_merge(array(__FILE__, __FILE__, 'text', 'codesize'), $options);
485
        $opts = new CommandLineOptions($args);
486
487
        $this->assertEquals($expected, $opts->getReportFiles());
488
    }
489
490
    public function dataProviderGetReportFiles()
491
    {
492
        return array(
493
            array(
494
                array('--reportfile-xml', __FILE__),
495
                array('xml' => __FILE__),
496
            ),
497
            array(
498
                array('--reportfile-html', __FILE__),
499
                array('html' => __FILE__),
500
            ),
501
            array(
502
                array('--reportfile-text', __FILE__),
503
                array('text' => __FILE__),
504
            ),
505
            array(
506
                array(
507
                    '--reportfile-text',
508
                    __FILE__,
509
                    '--reportfile-xml',
510
                    __FILE__,
511
                    '--reportfile-html',
512
                    __FILE__,
513
                ),
514
                array('text' => __FILE__, 'xml' => __FILE__, 'html' => __FILE__),
515
            ),
516
        );
517
    }
518
}
519