Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / symfony / yaml / Tests / Command / LintCommandTest.php
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
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 <robin.chalas@gmail.com>
24  */
25 class 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(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     public function testLintIncorrectFile()
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     public function testCustomTags()
64     {
65         $yaml = <<<YAML
66 foo: !my_tag {foo: bar}
67 YAML;
68         $ret = $this->createCommandTester()->execute(array('filename' => $this->createFile($yaml), '--parse-tags' => true), array('verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false));
69         $this->assertSame(0, $ret, 'lint:yaml exits with code 0 in case of success');
70     }
71
72     public function testCustomTagsError()
73     {
74         $yaml = <<<YAML
75 foo: !my_tag {foo: bar}
76 YAML;
77         $ret = $this->createCommandTester()->execute(array('filename' => $this->createFile($yaml)), array('verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false));
78         $this->assertSame(1, $ret, 'lint:yaml exits with code 1 in case of error');
79     }
80
81     /**
82      * @expectedException \RuntimeException
83      */
84     public function testLintFileNotReadable()
85     {
86         $tester = $this->createCommandTester();
87         $filename = $this->createFile('');
88         unlink($filename);
89
90         $ret = $tester->execute(array('filename' => $filename), array('decorated' => false));
91     }
92
93     /**
94      * @return string Path to the new file
95      */
96     private function createFile($content)
97     {
98         $filename = tempnam(sys_get_temp_dir().'/framework-yml-lint-test', 'sf-');
99         file_put_contents($filename, $content);
100
101         $this->files[] = $filename;
102
103         return $filename;
104     }
105
106     /**
107      * @return CommandTester
108      */
109     protected function createCommandTester()
110     {
111         $application = new Application();
112         $application->add(new LintCommand());
113         $command = $application->find('lint:yaml');
114
115         return new CommandTester($command);
116     }
117
118     protected function setUp()
119     {
120         $this->files = array();
121         @mkdir(sys_get_temp_dir().'/framework-yml-lint-test');
122     }
123
124     protected function tearDown()
125     {
126         foreach ($this->files as $file) {
127             if (file_exists($file)) {
128                 unlink($file);
129             }
130         }
131
132         rmdir(sys_get_temp_dir().'/framework-yml-lint-test');
133     }
134 }
135
136 class Foo
137 {
138     const TEST = 'foo';
139 }