菜谱项目

LintCommandTest.php 4.0KB

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