ConsoleSectionOutputTest.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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\Output\ConsoleSectionOutput;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Symfony\Component\Console\Output\StreamOutput;
  16. class ConsoleSectionOutputTest extends TestCase
  17. {
  18. private $stream;
  19. protected function setUp()
  20. {
  21. $this->stream = fopen('php://memory', 'r+', false);
  22. }
  23. protected function tearDown()
  24. {
  25. $this->stream = null;
  26. }
  27. public function testClearAll()
  28. {
  29. $sections = array();
  30. $output = new ConsoleSectionOutput($this->stream, $sections, OutputInterface::VERBOSITY_NORMAL, true, new OutputFormatter());
  31. $output->writeln('Foo'.PHP_EOL.'Bar');
  32. $output->clear();
  33. rewind($output->getStream());
  34. $this->assertEquals('Foo'.PHP_EOL.'Bar'.PHP_EOL.sprintf("\x1b[%dA", 2)."\x1b[0J", stream_get_contents($output->getStream()));
  35. }
  36. public function testClearNumberOfLines()
  37. {
  38. $sections = array();
  39. $output = new ConsoleSectionOutput($this->stream, $sections, OutputInterface::VERBOSITY_NORMAL, true, new OutputFormatter());
  40. $output->writeln("Foo\nBar\nBaz\nFooBar");
  41. $output->clear(2);
  42. rewind($output->getStream());
  43. $this->assertEquals("Foo\nBar\nBaz\nFooBar".PHP_EOL.sprintf("\x1b[%dA", 2)."\x1b[0J", stream_get_contents($output->getStream()));
  44. }
  45. public function testClearNumberOfLinesWithMultipleSections()
  46. {
  47. $output = new StreamOutput($this->stream);
  48. $sections = array();
  49. $output1 = new ConsoleSectionOutput($output->getStream(), $sections, OutputInterface::VERBOSITY_NORMAL, true, new OutputFormatter());
  50. $output2 = new ConsoleSectionOutput($output->getStream(), $sections, OutputInterface::VERBOSITY_NORMAL, true, new OutputFormatter());
  51. $output2->writeln('Foo');
  52. $output2->writeln('Bar');
  53. $output2->clear(1);
  54. $output1->writeln('Baz');
  55. rewind($output->getStream());
  56. $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()));
  57. }
  58. public function testClearPreservingEmptyLines()
  59. {
  60. $output = new StreamOutput($this->stream);
  61. $sections = array();
  62. $output1 = new ConsoleSectionOutput($output->getStream(), $sections, OutputInterface::VERBOSITY_NORMAL, true, new OutputFormatter());
  63. $output2 = new ConsoleSectionOutput($output->getStream(), $sections, OutputInterface::VERBOSITY_NORMAL, true, new OutputFormatter());
  64. $output2->writeln(PHP_EOL.'foo');
  65. $output2->clear(1);
  66. $output1->writeln('bar');
  67. rewind($output->getStream());
  68. $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()));
  69. }
  70. public function testOverwrite()
  71. {
  72. $sections = array();
  73. $output = new ConsoleSectionOutput($this->stream, $sections, OutputInterface::VERBOSITY_NORMAL, true, new OutputFormatter());
  74. $output->writeln('Foo');
  75. $output->overwrite('Bar');
  76. rewind($output->getStream());
  77. $this->assertEquals('Foo'.PHP_EOL."\x1b[1A\x1b[0JBar".PHP_EOL, stream_get_contents($output->getStream()));
  78. }
  79. public function testOverwriteMultipleLines()
  80. {
  81. $sections = array();
  82. $output = new ConsoleSectionOutput($this->stream, $sections, OutputInterface::VERBOSITY_NORMAL, true, new OutputFormatter());
  83. $output->writeln('Foo'.PHP_EOL.'Bar'.PHP_EOL.'Baz');
  84. $output->overwrite('Bar');
  85. rewind($output->getStream());
  86. $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()));
  87. }
  88. public function testAddingMultipleSections()
  89. {
  90. $sections = array();
  91. $output1 = new ConsoleSectionOutput($this->stream, $sections, OutputInterface::VERBOSITY_NORMAL, true, new OutputFormatter());
  92. $output2 = new ConsoleSectionOutput($this->stream, $sections, OutputInterface::VERBOSITY_NORMAL, true, new OutputFormatter());
  93. $this->assertCount(2, $sections);
  94. }
  95. public function testMultipleSectionsOutput()
  96. {
  97. $output = new StreamOutput($this->stream);
  98. $sections = array();
  99. $output1 = new ConsoleSectionOutput($output->getStream(), $sections, OutputInterface::VERBOSITY_NORMAL, true, new OutputFormatter());
  100. $output2 = new ConsoleSectionOutput($output->getStream(), $sections, OutputInterface::VERBOSITY_NORMAL, true, new OutputFormatter());
  101. $output1->writeln('Foo');
  102. $output2->writeln('Bar');
  103. $output1->overwrite('Baz');
  104. $output2->overwrite('Foobar');
  105. rewind($output->getStream());
  106. $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()));
  107. }
  108. }