LintCommandTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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\Bridge\Twig\Tests\Command;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Bridge\Twig\Command\LintCommand;
  13. use Symfony\Component\Console\Application;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Symfony\Component\Console\Tester\CommandTester;
  16. use Twig\Loader\FilesystemLoader;
  17. use Twig\Environment;
  18. class LintCommandTest extends TestCase
  19. {
  20. private $files;
  21. public function testLintCorrectFile()
  22. {
  23. $tester = $this->createCommandTester();
  24. $filename = $this->createFile('{{ foo }}');
  25. $ret = $tester->execute(array('filename' => array($filename)), array('verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false));
  26. $this->assertEquals(0, $ret, 'Returns 0 in case of success');
  27. $this->assertContains('OK in', trim($tester->getDisplay()));
  28. }
  29. public function testLintIncorrectFile()
  30. {
  31. $tester = $this->createCommandTester();
  32. $filename = $this->createFile('{{ foo');
  33. $ret = $tester->execute(array('filename' => array($filename)), array('decorated' => false));
  34. $this->assertEquals(1, $ret, 'Returns 1 in case of error');
  35. $this->assertRegExp('/ERROR in \S+ \(line /', trim($tester->getDisplay()));
  36. }
  37. /**
  38. * @expectedException \RuntimeException
  39. */
  40. public function testLintFileNotReadable()
  41. {
  42. $tester = $this->createCommandTester();
  43. $filename = $this->createFile('');
  44. unlink($filename);
  45. $ret = $tester->execute(array('filename' => array($filename)), array('decorated' => false));
  46. }
  47. public function testLintFileCompileTimeException()
  48. {
  49. $tester = $this->createCommandTester();
  50. $filename = $this->createFile("{{ 2|number_format(2, decimal_point='.', ',') }}");
  51. $ret = $tester->execute(array('filename' => array($filename)), array('decorated' => false));
  52. $this->assertEquals(1, $ret, 'Returns 1 in case of error');
  53. $this->assertRegExp('/ERROR in \S+ \(line /', trim($tester->getDisplay()));
  54. }
  55. /**
  56. * @return CommandTester
  57. */
  58. private function createCommandTester()
  59. {
  60. $twig = new Environment(new FilesystemLoader());
  61. $command = new LintCommand();
  62. $command->setTwigEnvironment($twig);
  63. $application = new Application();
  64. $application->add($command);
  65. $command = $application->find('lint:twig');
  66. return new CommandTester($command);
  67. }
  68. /**
  69. * @return string Path to the new file
  70. */
  71. private function createFile($content)
  72. {
  73. $filename = tempnam(sys_get_temp_dir(), 'sf-');
  74. file_put_contents($filename, $content);
  75. $this->files[] = $filename;
  76. return $filename;
  77. }
  78. protected function setUp()
  79. {
  80. $this->files = array();
  81. }
  82. protected function tearDown()
  83. {
  84. foreach ($this->files as $file) {
  85. if (file_exists($file)) {
  86. unlink($file);
  87. }
  88. }
  89. }
  90. }