InputTest.php 6.5 KB

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