CheckboxesTest.php 5.4 KB

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