CheckboxesTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace League\CLImate\Tests;
  3. use League\CLImate\Util\System\Linux;
  4. use League\CLImate\Util\UtilFactory;
  5. use Mockery;
  6. use function str_repeat;
  7. class CheckboxesTest extends TestBase
  8. {
  9. protected function shouldHideCursor()
  10. {
  11. $this->shouldReceiveSameLine();
  12. $this->shouldWrite("\e[?25l");
  13. }
  14. protected function shouldShowCursor()
  15. {
  16. $this->shouldReceiveSameLine();
  17. $this->shouldWrite("\e[?25h");
  18. }
  19. /** @test */
  20. public function it_can_select_a_checkbox()
  21. {
  22. // Select the first one
  23. $this->shouldReadCharAndReturn(' ');
  24. // Confirm entry
  25. $this->shouldReadCharAndReturn("\n");
  26. $this->shouldWrite("\e[mCurrent mood: (use <space> to select)\e[0m");
  27. $this->shouldWrite("\e[m❯ ○ Happy" . str_repeat(' ', 71) . "\e[0m");
  28. $this->shouldWrite("\e[m ○ Sad" . str_repeat(' ', 73) . "\e[0m");
  29. $this->shouldReceiveSameLine();
  30. $this->shouldWrite("\e[m ○ Thrilled" . str_repeat(' ', 68) . "\e[10D\e[8m\e[0m");
  31. $this->util->system->shouldReceive('exec')->with('stty -icanon');
  32. $this->util->system->shouldReceive('exec')->with('stty sane');
  33. $this->shouldHideCursor();
  34. $this->shouldReceiveSameLine();
  35. $this->shouldWrite("\e[2A\r");
  36. $this->shouldWrite("\e[m❯ ● Happy" . str_repeat(' ', 71) . "\e[0m");
  37. $this->shouldWrite("\e[m ○ Sad" . str_repeat(' ', 73) . "\e[0m");
  38. $this->shouldReceiveSameLine();
  39. $this->shouldWrite("\e[m ○ Thrilled" . str_repeat(' ', 68) . "\e[10D\e[8m\e[0m");
  40. $this->shouldShowCursor();
  41. $this->shouldReceiveSameLine();
  42. $this->shouldWrite("\e[0m");
  43. $options = ['Happy', 'Sad', 'Thrilled'];
  44. $input = $this->cli->checkboxes('Current mood:', $options, $this->reader);
  45. $response = $input->prompt();
  46. $this->assertSame(['Happy'], $response);
  47. }
  48. /** @test */
  49. public function it_can_select_multiple_checkboxes()
  50. {
  51. // Select the first one
  52. $this->shouldReadCharAndReturn(' ');
  53. // Go down one
  54. $this->shouldReadCharAndReturn("\e");
  55. $this->shouldReadCharAndReturn("[B", 2);
  56. // Go down one
  57. $this->shouldReadCharAndReturn("\e");
  58. $this->shouldReadCharAndReturn("[B", 2);
  59. // Select the third one
  60. $this->shouldReadCharAndReturn(' ');
  61. // Confirm entry
  62. $this->shouldReadCharAndReturn("\n");
  63. $this->shouldWrite("\e[mCurrent mood: (use <space> to select)\e[0m");
  64. $this->shouldWrite("\e[m❯ ○ Happy" . str_repeat(' ', 71) . "\e[0m");
  65. $this->shouldWrite("\e[m ○ Sad" . str_repeat(' ', 73) . "\e[0m");
  66. $this->shouldReceiveSameLine();
  67. $this->shouldWrite("\e[m ○ Thrilled" . str_repeat(' ', 68) . "\e[10D\e[8m\e[0m");
  68. $this->util->system->shouldReceive('exec')->with('stty -icanon');
  69. $this->util->system->shouldReceive('exec')->with('stty sane');
  70. $this->shouldHideCursor();
  71. $this->shouldReceiveSameLine();
  72. $this->shouldWrite("\e[2A\r", 4);
  73. $this->shouldWrite("\e[m❯ ● Happy" . str_repeat(' ', 71) . "\e[0m");
  74. $this->shouldWrite("\e[m ○ Sad" . str_repeat(' ', 73) . "\e[0m", 3);
  75. $this->shouldReceiveSameLine();
  76. $this->shouldWrite("\e[m ○ Thrilled" . str_repeat(' ', 68) . "\e[10D\e[8m\e[0m", 2);
  77. $this->shouldWrite("\e[m ● Happy" . str_repeat(' ', 71) . "\e[0m", 3);
  78. $this->shouldWrite("\e[m❯ ○ Sad" . str_repeat(' ', 73) . "\e[0m");
  79. $this->shouldReceiveSameLine();
  80. $this->shouldReceiveSameLine();
  81. $this->shouldWrite("\e[m❯ ○ Thrilled" . str_repeat(' ', 68) . "\e[10D\e[8m\e[0m");
  82. $this->shouldReceiveSameLine();
  83. $this->shouldWrite("\e[m❯ ● Thrilled" . str_repeat(' ', 68) . "\e[10D\e[8m\e[0m");
  84. $this->shouldShowCursor();
  85. $this->shouldReceiveSameLine();
  86. $this->shouldWrite("\e[0m");
  87. $options = ['Happy', 'Sad', 'Thrilled'];
  88. $input = $this->cli->checkboxes('Current mood:', $options, $this->reader);
  89. $response = $input->prompt();
  90. $this->assertSame(['Happy', 'Thrilled'], $response);
  91. }
  92. /** @test */
  93. public function it_can_toggle_a_checkbox()
  94. {
  95. // Select the first one
  96. $this->shouldReadCharAndReturn(' ');
  97. // Un-select the first one
  98. $this->shouldReadCharAndReturn(' ');
  99. // Confirm entry
  100. $this->shouldReadCharAndReturn("\n");
  101. $this->shouldWrite("\e[mCurrent mood: (use <space> to select)\e[0m");
  102. $this->shouldWrite("\e[m❯ ○ Happy" . str_repeat(' ', 71) . "\e[0m", 2);
  103. $this->shouldWrite("\e[m ○ Sad" . str_repeat(' ', 73) . "\e[0m", 3);
  104. $this->shouldReceiveSameLine();
  105. $this->shouldWrite("\e[m ○ Thrilled" . str_repeat(' ', 68) . "\e[10D\e[8m\e[0m", 3);
  106. $this->util->system->shouldReceive('exec')->with('stty -icanon');
  107. $this->util->system->shouldReceive('exec')->with('stty sane');
  108. $this->shouldHideCursor();
  109. $this->shouldReceiveSameLine();
  110. $this->shouldWrite("\e[2A\r", 2);
  111. $this->shouldWrite("\e[m❯ ● Happy" . str_repeat(' ', 71) . "\e[0m");
  112. $this->shouldShowCursor();
  113. $this->shouldReceiveSameLine();
  114. $this->shouldWrite("\e[0m");
  115. $options = ['Happy', 'Sad', 'Thrilled'];
  116. $input = $this->cli->checkboxes('Current mood:', $options, $this->reader);
  117. $response = $input->prompt();
  118. $this->assertSame([], $response);
  119. }
  120. /**
  121. * Ensure we can output checkboxes when the terminal width is unknown
  122. */
  123. public function test1(): void
  124. {
  125. $system = Mockery::mock(Linux::class);
  126. $system->shouldReceive("hasAnsiSupport")->andReturn(true);
  127. # When the width is unknown then a default of 80 should be used
  128. $system->shouldReceive("width")->andReturn(null);
  129. $this->util = new UtilFactory($system);
  130. $this->cli->setUtil($this->util);
  131. $this->it_can_select_a_checkbox();
  132. }
  133. }