CursorTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Console\Cursor;
  13. use Symfony\Component\Console\Output\StreamOutput;
  14. class CursorTest extends TestCase
  15. {
  16. protected $stream;
  17. protected function setUp(): void
  18. {
  19. $this->stream = fopen('php://memory', 'r+');
  20. }
  21. protected function tearDown(): void
  22. {
  23. fclose($this->stream);
  24. $this->stream = null;
  25. }
  26. public function testMoveUpOneLine()
  27. {
  28. $cursor = new Cursor($output = $this->getOutputStream());
  29. $cursor->moveUp();
  30. $this->assertEquals("\x1b[1A", $this->getOutputContent($output));
  31. }
  32. public function testMoveUpMultipleLines()
  33. {
  34. $cursor = new Cursor($output = $this->getOutputStream());
  35. $cursor->moveUp(12);
  36. $this->assertEquals("\x1b[12A", $this->getOutputContent($output));
  37. }
  38. public function testMoveDownOneLine()
  39. {
  40. $cursor = new Cursor($output = $this->getOutputStream());
  41. $cursor->moveDown();
  42. $this->assertEquals("\x1b[1B", $this->getOutputContent($output));
  43. }
  44. public function testMoveDownMultipleLines()
  45. {
  46. $cursor = new Cursor($output = $this->getOutputStream());
  47. $cursor->moveDown(12);
  48. $this->assertEquals("\x1b[12B", $this->getOutputContent($output));
  49. }
  50. public function testMoveLeftOneLine()
  51. {
  52. $cursor = new Cursor($output = $this->getOutputStream());
  53. $cursor->moveLeft();
  54. $this->assertEquals("\x1b[1D", $this->getOutputContent($output));
  55. }
  56. public function testMoveLeftMultipleLines()
  57. {
  58. $cursor = new Cursor($output = $this->getOutputStream());
  59. $cursor->moveLeft(12);
  60. $this->assertEquals("\x1b[12D", $this->getOutputContent($output));
  61. }
  62. public function testMoveRightOneLine()
  63. {
  64. $cursor = new Cursor($output = $this->getOutputStream());
  65. $cursor->moveRight();
  66. $this->assertEquals("\x1b[1C", $this->getOutputContent($output));
  67. }
  68. public function testMoveRightMultipleLines()
  69. {
  70. $cursor = new Cursor($output = $this->getOutputStream());
  71. $cursor->moveRight(12);
  72. $this->assertEquals("\x1b[12C", $this->getOutputContent($output));
  73. }
  74. public function testMoveToColumn()
  75. {
  76. $cursor = new Cursor($output = $this->getOutputStream());
  77. $cursor->moveToColumn(6);
  78. $this->assertEquals("\x1b[6G", $this->getOutputContent($output));
  79. }
  80. public function testMoveToPosition()
  81. {
  82. $cursor = new Cursor($output = $this->getOutputStream());
  83. $cursor->moveToPosition(18, 16);
  84. $this->assertEquals("\x1b[17;18H", $this->getOutputContent($output));
  85. }
  86. public function testClearLine()
  87. {
  88. $cursor = new Cursor($output = $this->getOutputStream());
  89. $cursor->clearLine();
  90. $this->assertEquals("\x1b[2K", $this->getOutputContent($output));
  91. }
  92. public function testSavePosition()
  93. {
  94. $cursor = new Cursor($output = $this->getOutputStream());
  95. $cursor->savePosition();
  96. $this->assertEquals("\x1b7", $this->getOutputContent($output));
  97. }
  98. public function testHide()
  99. {
  100. $cursor = new Cursor($output = $this->getOutputStream());
  101. $cursor->hide();
  102. $this->assertEquals("\x1b[?25l", $this->getOutputContent($output));
  103. }
  104. public function testShow()
  105. {
  106. $cursor = new Cursor($output = $this->getOutputStream());
  107. $cursor->show();
  108. $this->assertEquals("\x1b[?25h\x1b[?0c", $this->getOutputContent($output));
  109. }
  110. public function testRestorePosition()
  111. {
  112. $cursor = new Cursor($output = $this->getOutputStream());
  113. $cursor->restorePosition();
  114. $this->assertEquals("\x1b8", $this->getOutputContent($output));
  115. }
  116. public function testClearOutput()
  117. {
  118. $cursor = new Cursor($output = $this->getOutputStream());
  119. $cursor->clearOutput();
  120. $this->assertEquals("\x1b[0J", $this->getOutputContent($output));
  121. }
  122. public function testGetCurrentPosition()
  123. {
  124. $cursor = new Cursor($output = $this->getOutputStream());
  125. $cursor->moveToPosition(10, 10);
  126. $position = $cursor->getCurrentPosition();
  127. $this->assertEquals("\x1b[11;10H", $this->getOutputContent($output));
  128. $isTtySupported = (bool) @proc_open('echo 1 >/dev/null', [['file', '/dev/tty', 'r'], ['file', '/dev/tty', 'w'], ['file', '/dev/tty', 'w']], $pipes);
  129. if ($isTtySupported) {
  130. // When tty is supported, we can't validate the exact cursor position since it depends where the cursor is when the test runs.
  131. // Instead we just make sure that it doesn't return 1,1
  132. $this->assertNotEquals([1, 1], $position);
  133. } else {
  134. $this->assertEquals([1, 1], $position);
  135. }
  136. }
  137. protected function getOutputContent(StreamOutput $output)
  138. {
  139. rewind($output->getStream());
  140. return str_replace(\PHP_EOL, "\n", stream_get_contents($output->getStream()));
  141. }
  142. protected function getOutputStream(): StreamOutput
  143. {
  144. return new StreamOutput($this->stream, StreamOutput::VERBOSITY_NORMAL);
  145. }
  146. }