1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\Yaml\Tests\Command;
13
14use PHPUnit\Framework\TestCase;
15use Symfony\Component\Console\Application;
16use Symfony\Component\Console\Output\OutputInterface;
17use Symfony\Component\Console\Tester\CommandTester;
18use Symfony\Component\Yaml\Command\LintCommand;
19
20/**
21 * Tests the YamlLintCommand.
22 *
23 * @author Robin Chalas <robin.chalas@gmail.com>
24 */
25class LintCommandTest extends TestCase
26{
27    private $files;
28
29    public function testLintCorrectFile()
30    {
31        $tester = $this->createCommandTester();
32        $filename = $this->createFile('foo: bar');
33
34        $ret = $tester->execute(['filename' => $filename], ['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    public function testLintCorrectFiles()
41    {
42        $tester = $this->createCommandTester();
43        $filename1 = $this->createFile('foo: bar');
44        $filename2 = $this->createFile('bar: baz');
45
46        $ret = $tester->execute(['filename' => [$filename1, $filename2]], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]);
47
48        $this->assertEquals(0, $ret, 'Returns 0 in case of success');
49        $this->assertRegExp('/^\/\/ OK in /', trim($tester->getDisplay()));
50    }
51
52    public function testLintIncorrectFile()
53    {
54        $incorrectContent = '
55foo:
56bar';
57        $tester = $this->createCommandTester();
58        $filename = $this->createFile($incorrectContent);
59
60        $ret = $tester->execute(['filename' => $filename], ['decorated' => false]);
61
62        $this->assertEquals(1, $ret, 'Returns 1 in case of error');
63        $this->assertContains('Unable to parse at line 3 (near "bar").', trim($tester->getDisplay()));
64    }
65
66    public function testConstantAsKey()
67    {
68        $yaml = <<<YAML
69!php/const 'Symfony\Component\Yaml\Tests\Command\Foo::TEST': bar
70YAML;
71        $ret = $this->createCommandTester()->execute(['filename' => $this->createFile($yaml)], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]);
72        $this->assertSame(0, $ret, 'lint:yaml exits with code 0 in case of success');
73    }
74
75    public function testCustomTags()
76    {
77        $yaml = <<<YAML
78foo: !my_tag {foo: bar}
79YAML;
80        $ret = $this->createCommandTester()->execute(['filename' => $this->createFile($yaml), '--parse-tags' => true], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]);
81        $this->assertSame(0, $ret, 'lint:yaml exits with code 0 in case of success');
82    }
83
84    public function testCustomTagsError()
85    {
86        $yaml = <<<YAML
87foo: !my_tag {foo: bar}
88YAML;
89        $ret = $this->createCommandTester()->execute(['filename' => $this->createFile($yaml)], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]);
90        $this->assertSame(1, $ret, 'lint:yaml exits with code 1 in case of error');
91    }
92
93    /**
94     * @expectedException \RuntimeException
95     */
96    public function testLintFileNotReadable()
97    {
98        $tester = $this->createCommandTester();
99        $filename = $this->createFile('');
100        unlink($filename);
101
102        $ret = $tester->execute(['filename' => $filename], ['decorated' => false]);
103    }
104
105    /**
106     * @return string Path to the new file
107     */
108    private function createFile($content)
109    {
110        $filename = tempnam(sys_get_temp_dir().'/framework-yml-lint-test', 'sf-');
111        file_put_contents($filename, $content);
112
113        $this->files[] = $filename;
114
115        return $filename;
116    }
117
118    /**
119     * @return CommandTester
120     */
121    protected function createCommandTester()
122    {
123        $application = new Application();
124        $application->add(new LintCommand());
125        $command = $application->find('lint:yaml');
126
127        return new CommandTester($command);
128    }
129
130    protected function setUp()
131    {
132        $this->files = [];
133        @mkdir(sys_get_temp_dir().'/framework-yml-lint-test');
134    }
135
136    protected function tearDown()
137    {
138        foreach ($this->files as $file) {
139            if (file_exists($file)) {
140                unlink($file);
141            }
142        }
143
144        rmdir(sys_get_temp_dir().'/framework-yml-lint-test');
145    }
146}
147
148class Foo
149{
150    const TEST = 'foo';
151}
152