ConsoleSectionOutputTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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\Output;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Console\Formatter\OutputFormatter;
  13. use Symfony\Component\Console\Helper\QuestionHelper;
  14. use Symfony\Component\Console\Input\StreamableInputInterface;
  15. use Symfony\Component\Console\Output\ConsoleSectionOutput;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. use Symfony\Component\Console\Output\StreamOutput;
  18. use Symfony\Component\Console\Question\Question;
  19. class ConsoleSectionOutputTest extends TestCase
  20. {
  21. private $stream;
  22. protected function setUp()
  23. {
  24. $this->stream = fopen('php://memory', 'r+b', false);
  25. }
  26. protected function tearDown()
  27. {
  28. $this->stream = null;
  29. }
  30. public function testClearAll()
  31. {
  32. $sections = [];
  33. $output = new ConsoleSectionOutput($this->stream, $sections, OutputInterface::VERBOSITY_NORMAL, true, new OutputFormatter());
  34. $output->writeln('Foo'.PHP_EOL.'Bar');
  35. $output->clear();
  36. rewind($output->getStream());
  37. $this->assertEquals('Foo'.PHP_EOL.'Bar'.PHP_EOL.sprintf("\x1b[%dA", 2)."\x1b[0J", stream_get_contents($output->getStream()));
  38. }
  39. public function testClearNumberOfLines()
  40. {
  41. $sections = [];
  42. $output = new ConsoleSectionOutput($this->stream, $sections, OutputInterface::VERBOSITY_NORMAL, true, new OutputFormatter());
  43. $output->writeln("Foo\nBar\nBaz\nFooBar");
  44. $output->clear(2);
  45. rewind($output->getStream());
  46. $this->assertEquals("Foo\nBar\nBaz\nFooBar".PHP_EOL.sprintf("\x1b[%dA", 2)."\x1b[0J", stream_get_contents($output->getStream()));
  47. }
  48. public function testClearNumberOfLinesWithMultipleSections()
  49. {
  50. $output = new StreamOutput($this->stream);
  51. $sections = [];
  52. $output1 = new ConsoleSectionOutput($output->getStream(), $sections, OutputInterface::VERBOSITY_NORMAL, true, new OutputFormatter());
  53. $output2 = new ConsoleSectionOutput($output->getStream(), $sections, OutputInterface::VERBOSITY_NORMAL, true, new OutputFormatter());
  54. $output2->writeln('Foo');
  55. $output2->writeln('Bar');
  56. $output2->clear(1);
  57. $output1->writeln('Baz');
  58. rewind($output->getStream());
  59. $this->assertEquals('Foo'.PHP_EOL.'Bar'.PHP_EOL."\x1b[1A\x1b[0J\e[1A\e[0J".'Baz'.PHP_EOL.'Foo'.PHP_EOL, stream_get_contents($output->getStream()));
  60. }
  61. public function testClearPreservingEmptyLines()
  62. {
  63. $output = new StreamOutput($this->stream);
  64. $sections = [];
  65. $output1 = new ConsoleSectionOutput($output->getStream(), $sections, OutputInterface::VERBOSITY_NORMAL, true, new OutputFormatter());
  66. $output2 = new ConsoleSectionOutput($output->getStream(), $sections, OutputInterface::VERBOSITY_NORMAL, true, new OutputFormatter());
  67. $output2->writeln(PHP_EOL.'foo');
  68. $output2->clear(1);
  69. $output1->writeln('bar');
  70. rewind($output->getStream());
  71. $this->assertEquals(PHP_EOL.'foo'.PHP_EOL."\x1b[1A\x1b[0J\x1b[1A\x1b[0J".'bar'.PHP_EOL.PHP_EOL, stream_get_contents($output->getStream()));
  72. }
  73. public function testOverwrite()
  74. {
  75. $sections = [];
  76. $output = new ConsoleSectionOutput($this->stream, $sections, OutputInterface::VERBOSITY_NORMAL, true, new OutputFormatter());
  77. $output->writeln('Foo');
  78. $output->overwrite('Bar');
  79. rewind($output->getStream());
  80. $this->assertEquals('Foo'.PHP_EOL."\x1b[1A\x1b[0JBar".PHP_EOL, stream_get_contents($output->getStream()));
  81. }
  82. public function testOverwriteMultipleLines()
  83. {
  84. $sections = [];
  85. $output = new ConsoleSectionOutput($this->stream, $sections, OutputInterface::VERBOSITY_NORMAL, true, new OutputFormatter());
  86. $output->writeln('Foo'.PHP_EOL.'Bar'.PHP_EOL.'Baz');
  87. $output->overwrite('Bar');
  88. rewind($output->getStream());
  89. $this->assertEquals('Foo'.PHP_EOL.'Bar'.PHP_EOL.'Baz'.PHP_EOL.sprintf("\x1b[%dA", 3)."\x1b[0J".'Bar'.PHP_EOL, stream_get_contents($output->getStream()));
  90. }
  91. public function testAddingMultipleSections()
  92. {
  93. $sections = [];
  94. $output1 = new ConsoleSectionOutput($this->stream, $sections, OutputInterface::VERBOSITY_NORMAL, true, new OutputFormatter());
  95. $output2 = new ConsoleSectionOutput($this->stream, $sections, OutputInterface::VERBOSITY_NORMAL, true, new OutputFormatter());
  96. $this->assertCount(2, $sections);
  97. }
  98. public function testMultipleSectionsOutput()
  99. {
  100. $output = new StreamOutput($this->stream);
  101. $sections = [];
  102. $output1 = new ConsoleSectionOutput($output->getStream(), $sections, OutputInterface::VERBOSITY_NORMAL, true, new OutputFormatter());
  103. $output2 = new ConsoleSectionOutput($output->getStream(), $sections, OutputInterface::VERBOSITY_NORMAL, true, new OutputFormatter());
  104. $output1->writeln('Foo');
  105. $output2->writeln('Bar');
  106. $output1->overwrite('Baz');
  107. $output2->overwrite('Foobar');
  108. rewind($output->getStream());
  109. $this->assertEquals('Foo'.PHP_EOL.'Bar'.PHP_EOL."\x1b[2A\x1b[0JBar".PHP_EOL."\x1b[1A\x1b[0JBaz".PHP_EOL.'Bar'.PHP_EOL."\x1b[1A\x1b[0JFoobar".PHP_EOL, stream_get_contents($output->getStream()));
  110. }
  111. public function testClearSectionContainingQuestion()
  112. {
  113. $inputStream = fopen('php://memory', 'r+b', false);
  114. fwrite($inputStream, "Batman & Robin\n");
  115. rewind($inputStream);
  116. $input = $this->getMockBuilder(StreamableInputInterface::class)->getMock();
  117. $input->expects($this->once())->method('isInteractive')->willReturn(true);
  118. $input->expects($this->once())->method('getStream')->willReturn($inputStream);
  119. $sections = [];
  120. $output = new ConsoleSectionOutput($this->stream, $sections, OutputInterface::VERBOSITY_NORMAL, true, new OutputFormatter());
  121. (new QuestionHelper())->ask($input, $output, new Question('What\'s your favorite super hero?'));
  122. $output->clear();
  123. rewind($output->getStream());
  124. $this->assertSame('What\'s your favorite super hero?'.PHP_EOL."\x1b[2A\x1b[0J", stream_get_contents($output->getStream()));
  125. }
  126. }