4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Yaml\Tests\Command;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Console\Application;
16 use Symfony\Component\Console\Output\OutputInterface;
17 use Symfony\Component\Console\Tester\CommandTester;
18 use Symfony\Component\Yaml\Command\LintCommand;
21 * Tests the YamlLintCommand.
23 * @author Robin Chalas <robin.chalas@gmail.com>
25 class LintCommandTest extends TestCase
29 public function testLintCorrectFile()
31 $tester = $this->createCommandTester();
32 $filename = $this->createFile('foo: bar');
34 $ret = $tester->execute(array('filename' => $filename), array('verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false));
36 $this->assertEquals(0, $ret, 'Returns 0 in case of success');
37 $this->assertRegExp('/^\/\/ OK in /', trim($tester->getDisplay()));
40 public function testLintIncorrectFile()
45 $tester = $this->createCommandTester();
46 $filename = $this->createFile($incorrectContent);
48 $ret = $tester->execute(array('filename' => $filename), array('decorated' => false));
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()));
54 public function testConstantAsKey()
57 !php/const 'Symfony\Component\Yaml\Tests\Command\Foo::TEST': bar
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');
63 public function testCustomTags()
66 foo: !my_tag {foo: bar}
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');
72 public function testCustomTagsError()
75 foo: !my_tag {foo: bar}
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');
82 * @expectedException \RuntimeException
84 public function testLintFileNotReadable()
86 $tester = $this->createCommandTester();
87 $filename = $this->createFile('');
90 $ret = $tester->execute(array('filename' => $filename), array('decorated' => false));
94 * @return string Path to the new file
96 private function createFile($content)
98 $filename = tempnam(sys_get_temp_dir().'/framework-yml-lint-test', 'sf-');
99 file_put_contents($filename, $content);
101 $this->files[] = $filename;
107 * @return CommandTester
109 protected function createCommandTester()
111 $application = new Application();
112 $application->add(new LintCommand());
113 $command = $application->find('lint:yaml');
115 return new CommandTester($command);
118 protected function setUp()
120 $this->files = array();
121 @mkdir(sys_get_temp_dir().'/framework-yml-lint-test');
124 protected function tearDown()
126 foreach ($this->files as $file) {
127 if (file_exists($file)) {
132 rmdir(sys_get_temp_dir().'/framework-yml-lint-test');