LintCommandTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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\Console\Application;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. use Symfony\Component\Console\Tester\CommandTester;
  15. use Symfony\Component\Yaml\Command\LintCommand;
  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(['filename' => $filename], ['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(['filename' => $filename], ['decorated' => false]);
  40. $this->assertEquals(1, $ret, 'Returns 1 in case of error');
  41. $this->assertStringContainsString('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(['filename' => $this->createFile($yaml)], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]);
  49. $this->assertSame(0, $ret, 'lint:yaml exits with code 0 in case of success');
  50. }
  51. public function testCustomTags()
  52. {
  53. $yaml = <<<YAML
  54. foo: !my_tag {foo: bar}
  55. YAML;
  56. $ret = $this->createCommandTester()->execute(['filename' => $this->createFile($yaml), '--parse-tags' => true], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]);
  57. $this->assertSame(0, $ret, 'lint:yaml exits with code 0 in case of success');
  58. }
  59. public function testCustomTagsError()
  60. {
  61. $yaml = <<<YAML
  62. foo: !my_tag {foo: bar}
  63. YAML;
  64. $ret = $this->createCommandTester()->execute(['filename' => $this->createFile($yaml)], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]);
  65. $this->assertSame(1, $ret, 'lint:yaml exits with code 1 in case of error');
  66. }
  67. public function testLintFileNotReadable()
  68. {
  69. $this->expectException('RuntimeException');
  70. $tester = $this->createCommandTester();
  71. $filename = $this->createFile('');
  72. unlink($filename);
  73. $tester->execute(['filename' => $filename], ['decorated' => false]);
  74. }
  75. /**
  76. * @return string Path to the new file
  77. */
  78. private function createFile($content)
  79. {
  80. $filename = tempnam(sys_get_temp_dir().'/framework-yml-lint-test', 'sf-');
  81. file_put_contents($filename, $content);
  82. $this->files[] = $filename;
  83. return $filename;
  84. }
  85. /**
  86. * @return CommandTester
  87. */
  88. protected function createCommandTester()
  89. {
  90. $application = new Application();
  91. $application->add(new LintCommand());
  92. $command = $application->find('lint:yaml');
  93. return new CommandTester($command);
  94. }
  95. protected function setUp()
  96. {
  97. $this->files = [];
  98. @mkdir(sys_get_temp_dir().'/framework-yml-lint-test');
  99. }
  100. protected function tearDown()
  101. {
  102. foreach ($this->files as $file) {
  103. if (file_exists($file)) {
  104. unlink($file);
  105. }
  106. }
  107. rmdir(sys_get_temp_dir().'/framework-yml-lint-test');
  108. }
  109. }
  110. class Foo
  111. {
  112. const TEST = 'foo';
  113. }