PageRenderTime 45ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Symfony/Bundle/FrameworkBundle/Tests/Command/YamlLintCommandTest.php

http://github.com/fabpot/symfony
PHP | 176 lines | 129 code | 34 blank | 13 comment | 3 complexity | 5129abdb7a8262f6c43b14f1eff4350a MD5 | raw file
  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\Bundle\FrameworkBundle\Tests\Command;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Bundle\FrameworkBundle\Command\YamlLintCommand;
  13. use Symfony\Bundle\FrameworkBundle\Console\Application;
  14. use Symfony\Component\Console\Application as BaseApplication;
  15. use Symfony\Component\Console\Helper\HelperSet;
  16. use Symfony\Component\Console\Input\InputDefinition;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. use Symfony\Component\Console\Tester\CommandTester;
  19. use Symfony\Component\HttpKernel\KernelInterface;
  20. /**
  21. * Tests the YamlLintCommand.
  22. *
  23. * @author Robin Chalas <robin.chalas@gmail.com>
  24. */
  25. class YamlLintCommandTest extends TestCase
  26. {
  27. private $files;
  28. public function testLintCorrectFile()
  29. {
  30. $tester = $this->createCommandTester();
  31. $filename = $this->createFile('foo: bar');
  32. $tester->execute(
  33. ['filename' => $filename],
  34. ['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]
  35. );
  36. $this->assertEquals(0, $tester->getStatusCode(), 'Returns 0 in case of success');
  37. $this->assertStringContainsString('OK', trim($tester->getDisplay()));
  38. }
  39. public function testLintIncorrectFile()
  40. {
  41. $incorrectContent = '
  42. foo:
  43. bar';
  44. $tester = $this->createCommandTester();
  45. $filename = $this->createFile($incorrectContent);
  46. $tester->execute(['filename' => $filename], ['decorated' => false]);
  47. $this->assertEquals(1, $tester->getStatusCode(), 'Returns 1 in case of error');
  48. $this->assertStringContainsString('Unable to parse at line 3 (near "bar").', trim($tester->getDisplay()));
  49. }
  50. public function testLintFileNotReadable()
  51. {
  52. $this->expectException('RuntimeException');
  53. $tester = $this->createCommandTester();
  54. $filename = $this->createFile('');
  55. unlink($filename);
  56. $tester->execute(['filename' => $filename], ['decorated' => false]);
  57. }
  58. public function testGetHelp()
  59. {
  60. $command = new YamlLintCommand();
  61. $expected = <<<EOF
  62. Or find all files in a bundle:
  63. <info>php %command.full_name% @AcmeDemoBundle</info>
  64. EOF;
  65. $this->assertStringContainsString($expected, $command->getHelp());
  66. }
  67. public function testLintFilesFromBundleDirectory()
  68. {
  69. $tester = $this->createCommandTester($this->getKernelAwareApplicationMock());
  70. $tester->execute(
  71. ['filename' => '@AppBundle/Resources'],
  72. ['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]
  73. );
  74. $this->assertEquals(0, $tester->getStatusCode(), 'Returns 0 in case of success');
  75. $this->assertStringContainsString('[OK] All 0 YAML files contain valid syntax', trim($tester->getDisplay()));
  76. }
  77. private function createFile($content): string
  78. {
  79. $filename = tempnam(sys_get_temp_dir().'/yml-lint-test', 'sf-');
  80. file_put_contents($filename, $content);
  81. $this->files[] = $filename;
  82. return $filename;
  83. }
  84. private function createCommandTester($application = null): CommandTester
  85. {
  86. if (!$application) {
  87. $application = new BaseApplication();
  88. $application->add(new YamlLintCommand());
  89. }
  90. $command = $application->find('lint:yaml');
  91. if ($application) {
  92. $command->setApplication($application);
  93. }
  94. return new CommandTester($command);
  95. }
  96. private function getKernelAwareApplicationMock()
  97. {
  98. $kernel = $this->getMockBuilder(KernelInterface::class)
  99. ->disableOriginalConstructor()
  100. ->getMock();
  101. $kernel
  102. ->expects($this->once())
  103. ->method('locateResource')
  104. ->with('@AppBundle/Resources')
  105. ->willReturn(sys_get_temp_dir().'/yml-lint-test');
  106. $application = $this->getMockBuilder(Application::class)
  107. ->disableOriginalConstructor()
  108. ->getMock();
  109. $application
  110. ->expects($this->once())
  111. ->method('getKernel')
  112. ->willReturn($kernel);
  113. $application
  114. ->expects($this->once())
  115. ->method('getHelperSet')
  116. ->willReturn(new HelperSet());
  117. $application
  118. ->expects($this->any())
  119. ->method('getDefinition')
  120. ->willReturn(new InputDefinition());
  121. $application
  122. ->expects($this->once())
  123. ->method('find')
  124. ->with('lint:yaml')
  125. ->willReturn(new YamlLintCommand());
  126. return $application;
  127. }
  128. protected function setUp(): void
  129. {
  130. @mkdir(sys_get_temp_dir().'/yml-lint-test');
  131. $this->files = [];
  132. }
  133. protected function tearDown(): void
  134. {
  135. foreach ($this->files as $file) {
  136. if (file_exists($file)) {
  137. unlink($file);
  138. }
  139. }
  140. rmdir(sys_get_temp_dir().'/yml-lint-test');
  141. }
  142. }