Completed
Push — composer-installed ( c4bac5 )
by Ilia
07:57
created

LintCommandTest::testConstantAsKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
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
    public function testConstantAsKey()
55
    {
56
        $yaml = <<<YAML
57
!php/const:Symfony\Component\Yaml\Tests\Command\Foo::TEST: bar
58
YAML;
59
        $ret = $this->createCommandTester()->execute(array('filename' => $this->createFile($yaml)), array('verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false));
60
        $this->assertSame(0, $ret, 'lint:yaml exits with code 0 in case of success');
61
    }
62
63
    /**
64
     * @expectedException \RuntimeException
65
     */
66
    public function testLintFileNotReadable()
67
    {
68
        $tester = $this->createCommandTester();
69
        $filename = $this->createFile('');
70
        unlink($filename);
71
72
        $ret = $tester->execute(array('filename' => $filename), array('decorated' => false));
73
    }
74
75
    /**
76
     * @return string Path to the new file
77
     */
78
    private function createFile($content)
79
    {
80
        $filename = tempnam(sys_get_temp_dir().'/framework-yml-lint-test', 'sf-');
81
        file_put_contents($filename, $content);
82
83
        $this->files[] = $filename;
84
85
        return $filename;
86
    }
87
88
    /**
89
     * @return CommandTester
90
     */
91
    protected function createCommandTester()
92
    {
93
        $application = new Application();
94
        $application->add(new LintCommand());
95
        $command = $application->find('lint:yaml');
96
97
        return new CommandTester($command);
98
    }
99
100
    protected function setUp()
101
    {
102
        $this->files = array();
103
        @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...
104
    }
105
106
    protected function tearDown()
107
    {
108
        foreach ($this->files as $file) {
109
            if (file_exists($file)) {
110
                unlink($file);
111
            }
112
        }
113
114
        rmdir(sys_get_temp_dir().'/framework-yml-lint-test');
115
    }
116
}
117
118
class Foo
119
{
120
    const TEST = 'foo';
121
}
122