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\Yaml\Command\LintCommand;
16 use Symfony\Component\Console\Application;
17 use Symfony\Component\Console\Output\OutputInterface;
18 use Symfony\Component\Console\Tester\CommandTester;
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()));
55 * @expectedException \RuntimeException
57 public function testLintFileNotReadable()
59 $tester = $this->createCommandTester();
60 $filename = $this->createFile('');
63 $ret = $tester->execute(array('filename' => $filename), array('decorated' => false));
67 * @return string Path to the new file
69 private function createFile($content)
71 $filename = tempnam(sys_get_temp_dir().'/framework-yml-lint-test', 'sf-');
72 file_put_contents($filename, $content);
74 $this->files[] = $filename;
80 * @return CommandTester
82 protected function createCommandTester()
84 $application = new Application();
85 $application->add(new LintCommand());
86 $command = $application->find('lint:yaml');
88 return new CommandTester($command);
91 protected function setUp()
93 $this->files = array();
94 @mkdir(sys_get_temp_dir().'/framework-yml-lint-test');
97 protected function tearDown()
99 foreach ($this->files as $file) {
100 if (file_exists($file)) {
105 rmdir(sys_get_temp_dir().'/framework-yml-lint-test');