LintCommandTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 Symfony\Component\Yaml\Command\LintCommand;
  12. use Symfony\Component\Console\Application;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. use Symfony\Component\Console\Tester\CommandTester;
  15. /**
  16. * Tests the YamlLintCommand.
  17. *
  18. * @author Robin Chalas <robin.chalas@gmail.com>
  19. */
  20. class LintCommandTest extends \PHPUnit_Framework_TestCase
  21. {
  22. private $files;
  23. public function testLintCorrectFile()
  24. {
  25. $tester = $this->createCommandTester();
  26. $filename = $this->createFile('foo: bar');
  27. $ret = $tester->execute(array('filename' => $filename), array('verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false));
  28. $this->assertEquals(0, $ret, 'Returns 0 in case of success');
  29. $this->assertRegExp('/^\/\/ OK in /', trim($tester->getDisplay()));
  30. }
  31. public function testLintIncorrectFile()
  32. {
  33. $incorrectContent = '
  34. foo:
  35. bar';
  36. $tester = $this->createCommandTester();
  37. $filename = $this->createFile($incorrectContent);
  38. $ret = $tester->execute(array('filename' => $filename), array('decorated' => false));
  39. $this->assertEquals(1, $ret, 'Returns 1 in case of error');
  40. $this->assertContains('Unable to parse at line 3 (near "bar").', trim($tester->getDisplay()));
  41. }
  42. /**
  43. * @expectedException \RuntimeException
  44. */
  45. public function testLintFileNotReadable()
  46. {
  47. $tester = $this->createCommandTester();
  48. $filename = $this->createFile('');
  49. unlink($filename);
  50. $ret = $tester->execute(array('filename' => $filename), array('decorated' => false));
  51. }
  52. /**
  53. * @return string Path to the new file
  54. */
  55. private function createFile($content)
  56. {
  57. $filename = tempnam(sys_get_temp_dir().'/framework-yml-lint-test', 'sf-');
  58. file_put_contents($filename, $content);
  59. $this->files[] = $filename;
  60. return $filename;
  61. }
  62. /**
  63. * @return CommandTester
  64. */
  65. protected function createCommandTester()
  66. {
  67. $application = new Application();
  68. $application->add(new LintCommand());
  69. $command = $application->find('lint:yaml');
  70. return new CommandTester($command);
  71. }
  72. protected function setUp()
  73. {
  74. $this->files = array();
  75. @mkdir(sys_get_temp_dir().'/framework-yml-lint-test');
  76. }
  77. protected function tearDown()
  78. {
  79. foreach ($this->files as $file) {
  80. if (file_exists($file)) {
  81. unlink($file);
  82. }
  83. }
  84. rmdir(sys_get_temp_dir().'/framework-yml-lint-test');
  85. }
  86. }