TerminalTest.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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\Terminal;
  13. class TerminalTest extends TestCase
  14. {
  15. private $colSize;
  16. private $lineSize;
  17. private $ansiCon;
  18. protected function setUp(): void
  19. {
  20. $this->colSize = getenv('COLUMNS');
  21. $this->lineSize = getenv('LINES');
  22. $this->ansiCon = getenv('ANSICON');
  23. $this->resetStatics();
  24. }
  25. protected function tearDown(): void
  26. {
  27. putenv($this->colSize ? 'COLUMNS='.$this->colSize : 'COLUMNS');
  28. putenv($this->lineSize ? 'LINES' : 'LINES='.$this->lineSize);
  29. putenv($this->ansiCon ? 'ANSICON='.$this->ansiCon : 'ANSICON');
  30. $this->resetStatics();
  31. }
  32. private function resetStatics()
  33. {
  34. foreach (['height', 'width', 'stty'] as $name) {
  35. $property = new \ReflectionProperty(Terminal::class, $name);
  36. $property->setAccessible(true);
  37. $property->setValue(null, null);
  38. }
  39. }
  40. public function test()
  41. {
  42. putenv('COLUMNS=100');
  43. putenv('LINES=50');
  44. $terminal = new Terminal();
  45. $this->assertSame(100, $terminal->getWidth());
  46. $this->assertSame(50, $terminal->getHeight());
  47. putenv('COLUMNS=120');
  48. putenv('LINES=60');
  49. $terminal = new Terminal();
  50. $this->assertSame(120, $terminal->getWidth());
  51. $this->assertSame(60, $terminal->getHeight());
  52. }
  53. public function testZeroValues()
  54. {
  55. putenv('COLUMNS=0');
  56. putenv('LINES=0');
  57. $terminal = new Terminal();
  58. $this->assertSame(0, $terminal->getWidth());
  59. $this->assertSame(0, $terminal->getHeight());
  60. }
  61. public function testSttyOnWindows()
  62. {
  63. if ('\\' !== \DIRECTORY_SEPARATOR) {
  64. $this->markTestSkipped('Must be on windows');
  65. }
  66. $sttyString = shell_exec('(stty -a | grep columns) 2> NUL');
  67. if (!$sttyString) {
  68. $this->markTestSkipped('Must have stty support');
  69. }
  70. $matches = [];
  71. if (0 === preg_match('/columns.(\d+)/i', $sttyString, $matches)) {
  72. $this->fail('Could not determine existing stty columns');
  73. }
  74. putenv('COLUMNS');
  75. putenv('LINES');
  76. putenv('ANSICON');
  77. $terminal = new Terminal();
  78. $this->assertSame((int) $matches[1], $terminal->getWidth());
  79. }
  80. }