LintCommandTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. /**
  44. * @expectedException \RuntimeException
  45. */
  46. public function testLintFileNotReadable()
  47. {
  48. $tester = $this->createCommandTester();
  49. $filename = $this->createFile('');
  50. unlink($filename);
  51. $ret = $tester->execute(array('filename' => $filename), array('decorated' => false));
  52. }
  53. /**
  54. * @return string Path to the new file
  55. */
  56. private function createFile($content)
  57. {
  58. $filename = tempnam(sys_get_temp_dir().'/framework-yml-lint-test', 'sf-');
  59. file_put_contents($filename, $content);
  60. $this->files[] = $filename;
  61. return $filename;
  62. }
  63. /**
  64. * @return CommandTester
  65. */
  66. protected function createCommandTester()
  67. {
  68. $application = new Application();
  69. $application->add(new LintCommand());
  70. $command = $application->find('lint:yaml');
  71. return new CommandTester($command);
  72. }
  73. protected function setUp()
  74. {
  75. $this->files = array();
  76. @mkdir(sys_get_temp_dir().'/framework-yml-lint-test');
  77. }
  78. protected function tearDown()
  79. {
  80. foreach ($this->files as $file) {
  81. if (file_exists($file)) {
  82. unlink($file);
  83. }
  84. }
  85. rmdir(sys_get_temp_dir().'/framework-yml-lint-test');
  86. }
  87. }