Completed
Push — master ( d21053...c59b30 )
by
unknown
08:11
created

LintCommandTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 83
Duplicated Lines 27.71 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 23
loc 83
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testLintCorrectFile() 10 10 1
A testLintIncorrectFile() 13 13 1
A testLintFileNotReadable() 0 8 1
A createFile() 0 9 1
A createCommandTester() 0 8 1
A setUp() 0 5 1
A tearDown() 0 10 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Symfony\Component\Yaml\Tests\Command;
13
14
use PHPUnit\Framework\TestCase;
15
use Symfony\Component\Yaml\Command\LintCommand;
16
use Symfony\Component\Console\Application;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use Symfony\Component\Console\Tester\CommandTester;
19
20
/**
21
 * Tests the YamlLintCommand.
22
 *
23
 * @author Robin Chalas <[email protected]>
24
 */
25
class LintCommandTest extends TestCase
26
{
27
    private $files;
28
29 View Code Duplication
    public function testLintCorrectFile()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
    {
31
        $tester = $this->createCommandTester();
32
        $filename = $this->createFile('foo: bar');
33
34
        $ret = $tester->execute(array('filename' => $filename), array('verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false));
35
36
        $this->assertEquals(0, $ret, 'Returns 0 in case of success');
37
        $this->assertRegExp('/^\/\/ OK in /', trim($tester->getDisplay()));
38
    }
39
40 View Code Duplication
    public function testLintIncorrectFile()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
    {
42
        $incorrectContent = '
43
foo:
44
bar';
45
        $tester = $this->createCommandTester();
46
        $filename = $this->createFile($incorrectContent);
47
48
        $ret = $tester->execute(array('filename' => $filename), array('decorated' => false));
49
50
        $this->assertEquals(1, $ret, 'Returns 1 in case of error');
51
        $this->assertContains('Unable to parse at line 3 (near "bar").', trim($tester->getDisplay()));
52
    }
53
54
    /**
55
     * @expectedException \RuntimeException
56
     */
57
    public function testLintFileNotReadable()
58
    {
59
        $tester = $this->createCommandTester();
60
        $filename = $this->createFile('');
61
        unlink($filename);
62
63
        $ret = $tester->execute(array('filename' => $filename), array('decorated' => false));
64
    }
65
66
    /**
67
     * @return string Path to the new file
68
     */
69
    private function createFile($content)
70
    {
71
        $filename = tempnam(sys_get_temp_dir().'/framework-yml-lint-test', 'sf-');
72
        file_put_contents($filename, $content);
73
74
        $this->files[] = $filename;
75
76
        return $filename;
77
    }
78
79
    /**
80
     * @return CommandTester
81
     */
82
    protected function createCommandTester()
83
    {
84
        $application = new Application();
85
        $application->add(new LintCommand());
86
        $command = $application->find('lint:yaml');
87
88
        return new CommandTester($command);
89
    }
90
91
    protected function setUp()
92
    {
93
        $this->files = array();
94
        @mkdir(sys_get_temp_dir().'/framework-yml-lint-test');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
95
    }
96
97
    protected function tearDown()
98
    {
99
        foreach ($this->files as $file) {
100
            if (file_exists($file)) {
101
                unlink($file);
102
            }
103
        }
104
105
        rmdir(sys_get_temp_dir().'/framework-yml-lint-test');
106
    }
107
}
108