CheckboxesTest.php 6.0 KB

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