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

testCliOptionUpdateBaselineShouldBeSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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