123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- <?php
- namespace League\CLImate\Tests;
- class CheckboxesTest extends TestBase
- {
- protected function shouldHideCursor()
- {
- $this->shouldReceiveSameLine();
- $this->shouldWrite("\e[?25l");
- }
- protected function shouldShowCursor()
- {
- $this->shouldReceiveSameLine();
- $this->shouldWrite("\e[?25h");
- }
- /** @test */
- public function it_can_select_a_checkbox()
- {
- // Select the first one
- $this->shouldReadCharAndReturn(' ');
- // Confirm entry
- $this->shouldReadCharAndReturn("\n");
- $this->shouldWrite("\e[mCurrent mood: (use <space> to select)\e[0m");
- $this->shouldWrite("\e[m❯ ○ Happy" . str_repeat(' ', 71) . "\e[0m");
- $this->shouldWrite("\e[m ○ Sad" . str_repeat(' ', 73) . "\e[0m");
- $this->shouldReceiveSameLine();
- $this->shouldWrite("\e[m ○ Thrilled" . str_repeat(' ', 68) . "\e[10D\e[8m\e[0m");
- $this->util->system->shouldReceive('exec')->with('stty -icanon');
- $this->util->system->shouldReceive('exec')->with('stty sane');
- $this->shouldHideCursor();
- $this->shouldReceiveSameLine();
- $this->shouldWrite("\e[2A\r");
- $this->shouldWrite("\e[m❯ ● Happy" . str_repeat(' ', 71) . "\e[0m");
- $this->shouldWrite("\e[m ○ Sad" . str_repeat(' ', 73) . "\e[0m");
- $this->shouldReceiveSameLine();
- $this->shouldWrite("\e[m ○ Thrilled" . str_repeat(' ', 68) . "\e[10D\e[8m\e[0m");
- $this->shouldShowCursor();
- $this->shouldReceiveSameLine();
- $this->shouldWrite("\e[0m");
- $options = ['Happy', 'Sad', 'Thrilled'];
- $input = $this->cli->checkboxes('Current mood:', $options, $this->reader);
- $response = $input->prompt();
- $this->assertSame(['Happy'], $response);
- }
- /** @test */
- public function it_can_select_multiple_checkboxes()
- {
- // Select the first one
- $this->shouldReadCharAndReturn(' ');
- // Go down one
- $this->shouldReadCharAndReturn("\e");
- $this->shouldReadCharAndReturn("[B", 2);
- // Go down one
- $this->shouldReadCharAndReturn("\e");
- $this->shouldReadCharAndReturn("[B", 2);
- // Select the third one
- $this->shouldReadCharAndReturn(' ');
- // Confirm entry
- $this->shouldReadCharAndReturn("\n");
- $this->shouldWrite("\e[mCurrent mood: (use <space> to select)\e[0m");
- $this->shouldWrite("\e[m❯ ○ Happy" . str_repeat(' ', 71) . "\e[0m");
- $this->shouldWrite("\e[m ○ Sad" . str_repeat(' ', 73) . "\e[0m");
- $this->shouldReceiveSameLine();
- $this->shouldWrite("\e[m ○ Thrilled" . str_repeat(' ', 68) . "\e[10D\e[8m\e[0m");
- $this->util->system->shouldReceive('exec')->with('stty -icanon');
- $this->util->system->shouldReceive('exec')->with('stty sane');
- $this->shouldHideCursor();
- $this->shouldReceiveSameLine();
- $this->shouldWrite("\e[2A\r", 4);
- $this->shouldWrite("\e[m❯ ● Happy" . str_repeat(' ', 71) . "\e[0m");
- $this->shouldWrite("\e[m ○ Sad" . str_repeat(' ', 73) . "\e[0m", 3);
- $this->shouldReceiveSameLine();
- $this->shouldWrite("\e[m ○ Thrilled" . str_repeat(' ', 68) . "\e[10D\e[8m\e[0m", 2);
- $this->shouldWrite("\e[m ● Happy" . str_repeat(' ', 71) . "\e[0m", 3);
- $this->shouldWrite("\e[m❯ ○ Sad" . str_repeat(' ', 73) . "\e[0m");
- $this->shouldReceiveSameLine();
- $this->shouldReceiveSameLine();
- $this->shouldWrite("\e[m❯ ○ Thrilled" . str_repeat(' ', 68) . "\e[10D\e[8m\e[0m");
- $this->shouldReceiveSameLine();
- $this->shouldWrite("\e[m❯ ● Thrilled" . str_repeat(' ', 68) . "\e[10D\e[8m\e[0m");
- $this->shouldShowCursor();
- $this->shouldReceiveSameLine();
- $this->shouldWrite("\e[0m");
- $options = ['Happy', 'Sad', 'Thrilled'];
- $input = $this->cli->checkboxes('Current mood:', $options, $this->reader);
- $response = $input->prompt();
- $this->assertSame(['Happy', 'Thrilled'], $response);
- }
- /** @test */
- public function it_can_toggle_a_checkbox()
- {
- // Select the first one
- $this->shouldReadCharAndReturn(' ');
- // Un-select the first one
- $this->shouldReadCharAndReturn(' ');
- // Confirm entry
- $this->shouldReadCharAndReturn("\n");
- $this->shouldWrite("\e[mCurrent mood: (use <space> to select)\e[0m");
- $this->shouldWrite("\e[m❯ ○ Happy" . str_repeat(' ', 71) . "\e[0m", 2);
- $this->shouldWrite("\e[m ○ Sad" . str_repeat(' ', 73) . "\e[0m", 3);
- $this->shouldReceiveSameLine();
- $this->shouldWrite("\e[m ○ Thrilled" . str_repeat(' ', 68) . "\e[10D\e[8m\e[0m", 3);
- $this->util->system->shouldReceive('exec')->with('stty -icanon');
- $this->util->system->shouldReceive('exec')->with('stty sane');
- $this->shouldHideCursor();
- $this->shouldReceiveSameLine();
- $this->shouldWrite("\e[2A\r", 2);
- $this->shouldWrite("\e[m❯ ● Happy" . str_repeat(' ', 71) . "\e[0m");
- $this->shouldShowCursor();
- $this->shouldReceiveSameLine();
- $this->shouldWrite("\e[0m");
- $options = ['Happy', 'Sad', 'Thrilled'];
- $input = $this->cli->checkboxes('Current mood:', $options, $this->reader);
- $response = $input->prompt();
- $this->assertSame([], $response);
- }
- }
|