SymfonyStyleTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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\Console\Tests\Style;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Exception\RuntimeException;
  14. use Symfony\Component\Console\Formatter\OutputFormatter;
  15. use Symfony\Component\Console\Input\ArrayInput;
  16. use Symfony\Component\Console\Input\InputInterface;
  17. use Symfony\Component\Console\Output\ConsoleOutputInterface;
  18. use Symfony\Component\Console\Output\ConsoleSectionOutput;
  19. use Symfony\Component\Console\Output\NullOutput;
  20. use Symfony\Component\Console\Output\OutputInterface;
  21. use Symfony\Component\Console\Style\SymfonyStyle;
  22. use Symfony\Component\Console\Tester\CommandTester;
  23. class SymfonyStyleTest extends TestCase
  24. {
  25. /** @var Command */
  26. protected $command;
  27. /** @var CommandTester */
  28. protected $tester;
  29. private $colSize;
  30. protected function setUp(): void
  31. {
  32. $this->colSize = getenv('COLUMNS');
  33. putenv('COLUMNS=121');
  34. $this->command = new Command('sfstyle');
  35. $this->tester = new CommandTester($this->command);
  36. }
  37. protected function tearDown(): void
  38. {
  39. putenv($this->colSize ? 'COLUMNS='.$this->colSize : 'COLUMNS');
  40. $this->command = null;
  41. $this->tester = null;
  42. }
  43. /**
  44. * @dataProvider inputCommandToOutputFilesProvider
  45. */
  46. public function testOutputs($inputCommandFilepath, $outputFilepath)
  47. {
  48. $code = require $inputCommandFilepath;
  49. $this->command->setCode($code);
  50. $this->tester->execute([], ['interactive' => false, 'decorated' => false]);
  51. $this->assertStringEqualsFile($outputFilepath, $this->tester->getDisplay(true));
  52. }
  53. /**
  54. * @dataProvider inputInteractiveCommandToOutputFilesProvider
  55. */
  56. public function testInteractiveOutputs($inputCommandFilepath, $outputFilepath)
  57. {
  58. $code = require $inputCommandFilepath;
  59. $this->command->setCode($code);
  60. $this->tester->execute([], ['interactive' => true, 'decorated' => false]);
  61. $this->assertStringEqualsFile($outputFilepath, $this->tester->getDisplay(true));
  62. }
  63. public static function inputInteractiveCommandToOutputFilesProvider()
  64. {
  65. $baseDir = __DIR__.'/../Fixtures/Style/SymfonyStyle';
  66. return array_map(null, glob($baseDir.'/command/interactive_command_*.php'), glob($baseDir.'/output/interactive_output_*.txt'));
  67. }
  68. public static function inputCommandToOutputFilesProvider()
  69. {
  70. $baseDir = __DIR__.'/../Fixtures/Style/SymfonyStyle';
  71. return array_map(null, glob($baseDir.'/command/command_*.php'), glob($baseDir.'/output/output_*.txt'));
  72. }
  73. public function testOutputProgressIterate()
  74. {
  75. $code = require __DIR__.'/../Fixtures/Style/SymfonyStyle/progress/command_progress_iterate.php';
  76. if ('\\' === \DIRECTORY_SEPARATOR || 'Hyper' === getenv('TERM_PROGRAM')) {
  77. $outputFilepath = __DIR__.'/../Fixtures/Style/SymfonyStyle/progress/output_progress_iterate_no_shade.txt';
  78. } else {
  79. $outputFilepath = __DIR__.'/../Fixtures/Style/SymfonyStyle/progress/output_progress_iterate_shade.txt';
  80. }
  81. $this->command->setCode($code);
  82. $this->tester->execute([], ['interactive' => false, 'decorated' => false]);
  83. $this->assertStringEqualsFile($outputFilepath, $this->tester->getDisplay(true));
  84. }
  85. public function testGetErrorStyle()
  86. {
  87. $input = $this->createMock(InputInterface::class);
  88. $errorOutput = $this->createMock(OutputInterface::class);
  89. $errorOutput
  90. ->method('getFormatter')
  91. ->willReturn(new OutputFormatter());
  92. $errorOutput
  93. ->expects($this->once())
  94. ->method('write');
  95. $output = $this->createMock(ConsoleOutputInterface::class);
  96. $output
  97. ->method('getFormatter')
  98. ->willReturn(new OutputFormatter());
  99. $output
  100. ->expects($this->once())
  101. ->method('getErrorOutput')
  102. ->willReturn($errorOutput);
  103. $io = new SymfonyStyle($input, $output);
  104. $io->getErrorStyle()->write('');
  105. }
  106. public function testCreateTableWithConsoleOutput()
  107. {
  108. $input = $this->createMock(InputInterface::class);
  109. $output = $this->createMock(ConsoleOutputInterface::class);
  110. $output
  111. ->method('getFormatter')
  112. ->willReturn(new OutputFormatter());
  113. $output
  114. ->expects($this->once())
  115. ->method('section')
  116. ->willReturn($this->createMock(ConsoleSectionOutput::class));
  117. $style = new SymfonyStyle($input, $output);
  118. $style->createTable();
  119. }
  120. public function testCreateTableWithoutConsoleOutput()
  121. {
  122. $input = $this->createMock(InputInterface::class);
  123. $output = $this->createMock(OutputInterface::class);
  124. $output
  125. ->method('getFormatter')
  126. ->willReturn(new OutputFormatter());
  127. $style = new SymfonyStyle($input, $output);
  128. $this->expectException(RuntimeException::class);
  129. $this->expectExceptionMessage('Output should be an instance of "Symfony\Component\Console\Output\ConsoleSectionOutput"');
  130. $style->createTable()->appendRow(['row']);
  131. }
  132. public function testGetErrorStyleUsesTheCurrentOutputIfNoErrorOutputIsAvailable()
  133. {
  134. $output = $this->createMock(OutputInterface::class);
  135. $output
  136. ->method('getFormatter')
  137. ->willReturn(new OutputFormatter());
  138. $style = new SymfonyStyle($this->createMock(InputInterface::class), $output);
  139. $this->assertInstanceOf(SymfonyStyle::class, $style->getErrorStyle());
  140. }
  141. public function testMemoryConsumption()
  142. {
  143. $io = new SymfonyStyle(new ArrayInput([]), new NullOutput());
  144. $str = 'teststr';
  145. $io->writeln($str, SymfonyStyle::VERBOSITY_QUIET);
  146. $io->writeln($str, SymfonyStyle::VERBOSITY_QUIET);
  147. $start = memory_get_usage();
  148. for ($i = 0; $i < 100; ++$i) {
  149. $io->writeln($str, SymfonyStyle::VERBOSITY_QUIET);
  150. }
  151. $this->assertSame(0, memory_get_usage() - $start);
  152. }
  153. }