InputTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace League\CLImate\Tests\TerminalObject\Dynamic;
  3. use League\CLImate\Tests\TestBase;
  4. class InputTest extends TestBase
  5. {
  6. /** @test */
  7. public function it_can_prompt_for_basic_info()
  8. {
  9. $this->shouldReadAndReturn('Not much.');
  10. $this->shouldReceiveSameLine();
  11. $this->shouldWrite("\e[mSo what is up? \e[0m");
  12. $input = $this->cli->input('So what is up?', $this->reader);
  13. $response = $input->prompt();
  14. $this->assertSame('Not much.', $response);
  15. }
  16. /** @test */
  17. public function it_will_only_allow_loose_acceptable_responses()
  18. {
  19. $this->shouldReadAndReturn('Not much.');
  20. $this->shouldReadAndReturn('Everything.');
  21. $this->shouldReceiveSameLine();
  22. $this->shouldWrite("\e[mSo what is up? \e[0m", 2);
  23. $input = $this->cli->input('So what is up?', $this->reader);
  24. $input->accept(['everything.']);
  25. $response = $input->prompt();
  26. $this->assertSame('Everything.', $response);
  27. }
  28. /** @test */
  29. public function it_will_only_allow_strict_acceptable_responses()
  30. {
  31. $this->shouldReadAndReturn('everything.');
  32. $this->shouldReadAndReturn('Everything.');
  33. $this->shouldReceiveSameLine();
  34. $this->shouldWrite("\e[mSo what is up? \e[0m", 2);
  35. $input = $this->cli->input('So what is up?', $this->reader);
  36. $input->accept(['Everything.']);
  37. $response = $input->strict()->prompt();
  38. $this->assertSame('Everything.', $response);
  39. }
  40. /** @test */
  41. public function it_will_allow_an_array_of_acceptable_responses()
  42. {
  43. $this->shouldReadAndReturn('stuff.');
  44. $this->shouldReadAndReturn('Stuff.');
  45. $this->shouldReceiveSameLine();
  46. $this->shouldWrite("\e[mSo what is up? \e[0m", 2);
  47. $input = $this->cli->input('So what is up?', $this->reader);
  48. $input->accept(['Everything.', 'Stuff.']);
  49. $response = $input->strict()->prompt();
  50. $this->assertSame('Stuff.', $response);
  51. }
  52. /** @test */
  53. public function it_will_display_acceptable_responses()
  54. {
  55. $this->shouldReadAndReturn('Stuff.');
  56. $this->shouldReceiveSameLine();
  57. $this->shouldWrite("\e[mSo what is up? [Everything./Stuff.] \e[0m");
  58. $input = $this->cli->input('So what is up?', $this->reader);
  59. $input->accept(['Everything.', 'Stuff.'], true);
  60. $response = $input->prompt();
  61. $this->assertSame('Stuff.', $response);
  62. }
  63. /** @test */
  64. public function it_will_display_acceptable_responses_with_same_format_if_input_fails()
  65. {
  66. $this->shouldReadAndReturn('Nothing.');
  67. $this->shouldReadAndReturn('Stuff.');
  68. $this->shouldReceiveSameLine();
  69. $this->shouldWrite("\e[mSo what is up? [Everything./Stuff.] \e[0m", 2);
  70. $input = $this->cli->input('So what is up?', $this->reader);
  71. $input->accept(['Everything.', 'Stuff.'], true);
  72. $response = $input->prompt();
  73. $this->assertSame('Stuff.', $response);
  74. }
  75. /** @test */
  76. public function it_will_format_the_default_acceptable_response()
  77. {
  78. $this->shouldReadAndReturn('Stuff.');
  79. $this->shouldReceiveSameLine();
  80. $this->shouldWrite("\e[mSo what is up? [\e[1mEverything.\e[0m/Stuff.] \e[0m");
  81. $input = $this->cli->input('So what is up?', $this->reader);
  82. $input->accept(['Everything.', 'Stuff.'], true);
  83. $input->defaultTo('Everything.');
  84. $response = $input->prompt();
  85. $this->assertSame('Stuff.', $response);
  86. }
  87. /** @test */
  88. public function it_will_accept_the_default_acceptable_response()
  89. {
  90. $this->shouldReadAndReturn('');
  91. $this->shouldReceiveSameLine();
  92. $this->shouldWrite("\e[mSo what is up? [\e[1mEverything.\e[0m/Stuff.] \e[0m");
  93. $input = $this->cli->input('So what is up?', $this->reader);
  94. $input->accept(['Everything.', 'Stuff.'], true);
  95. $input->defaultTo('Everything.');
  96. $response = $input->prompt();
  97. $this->assertSame('Everything.', $response);
  98. }
  99. /** @test */
  100. public function it_will_accept_a_closure_as_an_acceptable_response()
  101. {
  102. $this->shouldReadAndReturn('everything.');
  103. $this->shouldReceiveSameLine();
  104. $this->shouldWrite("\e[mSo what is up? \e[0m");
  105. $input = $this->cli->input('So what is up?', $this->reader);
  106. $input->accept(function ($response) {
  107. return ($response == 'everything.');
  108. });
  109. $response = $input->prompt();
  110. $this->assertSame('everything.', $response);
  111. }
  112. /** @test */
  113. public function it_will_fail_via_an_accept_closure()
  114. {
  115. $this->shouldReadAndReturn('everything!');
  116. $this->shouldReadAndReturn('everything.');
  117. $this->shouldReceiveSameLine();
  118. $this->shouldWrite("\e[mSo what is up? \e[0m", 2);
  119. $input = $this->cli->input('So what is up?', $this->reader);
  120. $input->accept(function ($response) {
  121. return ($response == 'everything.');
  122. });
  123. $response = $input->strict()->prompt();
  124. $this->assertSame('everything.', $response);
  125. }
  126. /** @test */
  127. public function it_will_accept_a_default_if_no_answer_is_given()
  128. {
  129. $this->shouldReadAndReturn('');
  130. $this->shouldReceiveSameLine();
  131. $this->shouldWrite("\e[mSo what is up? \e[0m");
  132. $input = $this->cli->input('So what is up?', $this->reader);
  133. $input->defaultTo('Not much.');
  134. $response = $input->prompt();
  135. $this->assertSame('Not much.', $response);
  136. }
  137. /**
  138. * @test
  139. * @doesNotPerformAssertions
  140. */
  141. public function it_will_accept_multiple_lines()
  142. {
  143. $this->shouldReadMultipleLinesAndReturn("Multiple\nLines\x04");
  144. $this->shouldReceiveSameLine();
  145. $this->shouldWrite("\e[m>>> \e[0m");
  146. $input = $this->cli->input('>>>', $this->reader);
  147. $input->multiLine();
  148. $response = $input->prompt();
  149. }
  150. /**
  151. * @test
  152. * @doesNotPerformAssertions
  153. */
  154. public function it_will_read_after_eof()
  155. {
  156. $this->shouldReadMultipleLinesAndReturn("Multiple\nLines\x04");
  157. $this->shouldReceiveSameLine();
  158. $this->shouldWrite("\e[m>>> \e[0m");
  159. $input = $this->cli->input('>>>', $this->reader);
  160. $input->multiLine();
  161. $response = $input->prompt();
  162. $this->shouldReadMultipleLinesAndReturn("Multiple\nLines\nAgain\x04");
  163. $this->shouldReceiveSameLine();
  164. $this->shouldWrite("\e[m>>> \e[0m");
  165. $input = $this->cli->input('>>>', $this->reader);
  166. $input->multiLine();
  167. $response = $input->prompt();
  168. }
  169. }