LintCommandTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 testLintCorrectFiles()
  33. {
  34. $tester = $this->createCommandTester();
  35. $filename1 = $this->createFile('foo: bar');
  36. $filename2 = $this->createFile('bar: baz');
  37. $ret = $tester->execute(['filename' => [$filename1, $filename2]], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]);
  38. $this->assertEquals(0, $ret, 'Returns 0 in case of success');
  39. $this->assertRegExp('/^\/\/ OK in /', trim($tester->getDisplay()));
  40. }
  41. public function testLintIncorrectFile()
  42. {
  43. $incorrectContent = '
  44. foo:
  45. bar';
  46. $tester = $this->createCommandTester();
  47. $filename = $this->createFile($incorrectContent);
  48. $ret = $tester->execute(['filename' => $filename], ['decorated' => false]);
  49. $this->assertEquals(1, $ret, 'Returns 1 in case of error');
  50. $this->assertContains('Unable to parse at line 3 (near "bar").', trim($tester->getDisplay()));
  51. }
  52. public function testConstantAsKey()
  53. {
  54. $yaml = <<<YAML
  55. !php/const 'Symfony\Component\Yaml\Tests\Command\Foo::TEST': bar
  56. YAML;
  57. $ret = $this->createCommandTester()->execute(['filename' => $this->createFile($yaml)], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]);
  58. $this->assertSame(0, $ret, 'lint:yaml exits with code 0 in case of success');
  59. }
  60. public function testCustomTags()
  61. {
  62. $yaml = <<<YAML
  63. foo: !my_tag {foo: bar}
  64. YAML;
  65. $ret = $this->createCommandTester()->execute(['filename' => $this->createFile($yaml), '--parse-tags' => true], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]);
  66. $this->assertSame(0, $ret, 'lint:yaml exits with code 0 in case of success');
  67. }
  68. public function testCustomTagsError()
  69. {
  70. $yaml = <<<YAML
  71. foo: !my_tag {foo: bar}
  72. YAML;
  73. $ret = $this->createCommandTester()->execute(['filename' => $this->createFile($yaml)], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]);
  74. $this->assertSame(1, $ret, 'lint:yaml exits with code 1 in case of error');
  75. }
  76. /**
  77. * @expectedException \RuntimeException
  78. */
  79. public function testLintFileNotReadable()
  80. {
  81. $tester = $this->createCommandTester();
  82. $filename = $this->createFile('');
  83. unlink($filename);
  84. $ret = $tester->execute(['filename' => $filename], ['decorated' => false]);
  85. }
  86. /**
  87. * @return string Path to the new file
  88. */
  89. private function createFile($content)
  90. {
  91. $filename = tempnam(sys_get_temp_dir().'/framework-yml-lint-test', 'sf-');
  92. file_put_contents($filename, $content);
  93. $this->files[] = $filename;
  94. return $filename;
  95. }
  96. /**
  97. * @return CommandTester
  98. */
  99. protected function createCommandTester()
  100. {
  101. $application = new Application();
  102. $application->add(new LintCommand());
  103. $command = $application->find('lint:yaml');
  104. return new CommandTester($command);
  105. }
  106. protected function setUp()
  107. {
  108. $this->files = [];
  109. @mkdir(sys_get_temp_dir().'/framework-yml-lint-test');
  110. }
  111. protected function tearDown()
  112. {
  113. foreach ($this->files as $file) {
  114. if (file_exists($file)) {
  115. unlink($file);
  116. }
  117. }
  118. rmdir(sys_get_temp_dir().'/framework-yml-lint-test');
  119. }
  120. }
  121. class Foo
  122. {
  123. const TEST = 'foo';
  124. }