LintCommandTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Yaml\Tests\Command;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Yaml\Command\LintCommand;
  13. use Symfony\Component\Console\Application;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Symfony\Component\Console\Tester\CommandTester;
  16. /**
  17. * Tests the YamlLintCommand.
  18. *
  19. * @author Robin Chalas <robin.chalas@gmail.com>
  20. */
  21. class LintCommandTest extends TestCase
  22. {
  23. private $files;
  24. public function testLintCorrectFile()
  25. {
  26. $tester = $this->createCommandTester();
  27. $filename = $this->createFile('foo: bar');
  28. $ret = $tester->execute(array('filename' => $filename), array('verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false));
  29. $this->assertEquals(0, $ret, 'Returns 0 in case of success');
  30. $this->assertRegExp('/^\/\/ OK in /', trim($tester->getDisplay()));
  31. }
  32. public function testLintIncorrectFile()
  33. {
  34. $incorrectContent = '
  35. foo:
  36. bar';
  37. $tester = $this->createCommandTester();
  38. $filename = $this->createFile($incorrectContent);
  39. $ret = $tester->execute(array('filename' => $filename), array('decorated' => false));
  40. $this->assertEquals(1, $ret, 'Returns 1 in case of error');
  41. $this->assertContains('Unable to parse at line 3 (near "bar").', trim($tester->getDisplay()));
  42. }
  43. public function testConstantAsKey()
  44. {
  45. $yaml = <<<YAML
  46. !php/const:Symfony\Component\Yaml\Tests\Command\Foo::TEST: bar
  47. YAML;
  48. $ret = $this->createCommandTester()->execute(array('filename' => $this->createFile($yaml)), array('verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false));
  49. $this->assertSame(0, $ret, 'lint:yaml exits with code 0 in case of success');
  50. }
  51. /**
  52. * @expectedException \RuntimeException
  53. */
  54. public function testLintFileNotReadable()
  55. {
  56. $tester = $this->createCommandTester();
  57. $filename = $this->createFile('');
  58. unlink($filename);
  59. $ret = $tester->execute(array('filename' => $filename), array('decorated' => false));
  60. }
  61. /**
  62. * @return string Path to the new file
  63. */
  64. private function createFile($content)
  65. {
  66. $filename = tempnam(sys_get_temp_dir().'/framework-yml-lint-test', 'sf-');
  67. file_put_contents($filename, $content);
  68. $this->files[] = $filename;
  69. return $filename;
  70. }
  71. /**
  72. * @return CommandTester
  73. */
  74. protected function createCommandTester()
  75. {
  76. $application = new Application();
  77. $application->add(new LintCommand());
  78. $command = $application->find('lint:yaml');
  79. return new CommandTester($command);
  80. }
  81. protected function setUp()
  82. {
  83. $this->files = array();
  84. @mkdir(sys_get_temp_dir().'/framework-yml-lint-test');
  85. }
  86. protected function tearDown()
  87. {
  88. foreach ($this->files as $file) {
  89. if (file_exists($file)) {
  90. unlink($file);
  91. }
  92. }
  93. rmdir(sys_get_temp_dir().'/framework-yml-lint-test');
  94. }
  95. }
  96. class Foo
  97. {
  98. const TEST = 'foo';
  99. }