CommandTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\console;
  9. use lithium\console\Request;
  10. use lithium\console\Response;
  11. use lithium\tests\mocks\console\MockCommand;
  12. class CommandTest extends \lithium\test\Unit {
  13. public $request;
  14. public function setUp() {
  15. $this->request = new Request(array('input' => fopen('php://temp', 'w+')));
  16. $this->classes = array('response' => 'lithium\tests\mocks\console\MockResponse');
  17. }
  18. public function testConstruct() {
  19. $command = new MockCommand(array('request' => $this->request));
  20. $expected = $this->request->env();
  21. $result = $command->request->env();
  22. $this->assertEqual($expected, $result);
  23. $this->request->params = array(
  24. 'case' => 'lithium.tests.cases.console.CommandTest'
  25. );
  26. $command = new MockCommand(array('request' => $this->request));
  27. $this->assertTrue(!empty($command->case));
  28. }
  29. public function testInvoke() {
  30. $command = new MockCommand(array('request' => $this->request));
  31. $response = $command('testRun');
  32. $result = $response;
  33. $this->assertTrue($result instanceof Response);
  34. $expected = 'testRun';
  35. $result = $response->testAction;
  36. $this->assertEqual($expected, $result);
  37. }
  38. public function testInvokeSettingResponseStatus() {
  39. $command = new MockCommand(array('request' => $this->request));
  40. $expected = 0;
  41. $result = $command('testReturnNull')->status;
  42. $this->assertEqual($expected, $result);
  43. $expected = 0;
  44. $result = $command('testReturnTrue')->status;
  45. $this->assertEqual($expected, $result);
  46. $expected = 1;
  47. $result = $command('testReturnFalse')->status;
  48. $this->assertEqual($expected, $result);
  49. $expected = -1;
  50. $result = $command('testReturnNegative1')->status;
  51. $this->assertEqual($expected, $result);
  52. $expected = 1;
  53. $result = $command('testReturn1')->status;
  54. $this->assertEqual($expected, $result);
  55. $expected = 3;
  56. $result = $command('testReturn3')->status;
  57. $this->assertEqual($expected, $result);
  58. $expected = 'this is a string';
  59. $result = $command('testReturnString')->status;
  60. $this->assertEqual($expected, $result);
  61. $expected = 1;
  62. $result = $command('testReturnEmptyArray')->status;
  63. $this->assertEqual($expected, $result);
  64. $expected = 0;
  65. $result = $command('testReturnArray')->status;
  66. $this->assertEqual($expected, $result);
  67. }
  68. public function testOut() {
  69. $command = new MockCommand(array('request' => $this->request));
  70. $expected = "ok\n";
  71. $result = $command->out('ok');
  72. $this->assertEqual($expected, $result);
  73. }
  74. public function testOutArray() {
  75. $command = new MockCommand(array('request' => $this->request));
  76. $expected = "line 1\nline 2\n";
  77. $command->out(array('line 1', 'line 2'));
  78. $result = $command->response->output;
  79. $this->assertEqual($expected, $result);
  80. }
  81. public function testError() {
  82. $command = new MockCommand(array('request' => $this->request));
  83. $expected = "ok\n";
  84. $result = $command->error('ok');
  85. $this->assertEqual($expected, $result);
  86. }
  87. public function testErrorArray() {
  88. $command = new MockCommand(array('request' => $this->request));
  89. $expected = "line 1\nline 2\n";
  90. $command->error(array('line 1', 'line 2'));
  91. $result = $command->response->error;
  92. $this->assertEqual($expected, $result);
  93. }
  94. public function testNl() {
  95. $command = new MockCommand(array('request' => $this->request));
  96. $expected = "\n\n\n";
  97. $result = $command->nl(3);
  98. $this->assertEqual($expected, $result);
  99. }
  100. public function testHr() {
  101. $command = new MockCommand(array('request' => $this->request));
  102. $expected = "----\n";
  103. $command->hr(4);
  104. $result = $command->response->output;
  105. $this->assertEqual($expected, $result);
  106. }
  107. public function testHeader() {
  108. $command = new MockCommand(array('request' => $this->request));
  109. $expected = "----\nheader\n----\n";
  110. $command->header('header', 4);
  111. $result = $command->response->output;
  112. $this->assertEqual($expected, $result);
  113. }
  114. public function testColumns() {
  115. $command = new MockCommand(array('request' => $this->request));
  116. $expected = "data1\t\ndata2\t\n";
  117. $command->columns(array('col1' => 'data1', 'col2' => 'data2'));
  118. $result = $command->response->output;
  119. $this->assertEqual($expected, $result);
  120. }
  121. public function testHelp() {
  122. $command = new MockCommand(array('request' => $this->request));
  123. $return = $command->__invoke('_help');
  124. $this->assertTrue($return instanceOf \lithium\tests\mocks\console\MockResponse);
  125. $expected = "DESCRIPTION.*This is the Mock Command";
  126. $result = $command->response->output;
  127. $this->assertPattern("/{$expected}/s", $result);
  128. $command = new MockCommand(array('request' => $this->request));
  129. $return = $command->__invoke('_help');
  130. $expected = "testRun";
  131. $result = $command->response->output;
  132. $this->assertPattern("/{$expected}/m", $result);
  133. }
  134. public function testIn() {
  135. $command = new MockCommand(array('request' => $this->request));
  136. fwrite($command->request->input, 'nada mucho');
  137. rewind($command->request->input);
  138. $expected = "nada mucho";
  139. $result = $command->in('What up dog?');
  140. $this->assertEqual($expected, $result);
  141. $expected = "What up dog? \n > ";
  142. $result = $command->response->output;
  143. $this->assertEqual($expected, $result);
  144. }
  145. public function testQuit() {
  146. $command = new MockCommand(array('request' => $this->request));
  147. fwrite($command->request->input, "q\n");
  148. rewind($command->request->input);
  149. $result = $command->in('This should return bool false');
  150. $this->assertFalse($result);
  151. }
  152. public function testInWithDefaultOption() {
  153. $command = new MockCommand(array('request' => $this->request));
  154. fwrite($command->request->input, ' ');
  155. rewind($command->request->input);
  156. $expected = "y";
  157. $result = $command->in('What up dog?', array('default' => 'y'));
  158. $this->assertEqual($expected, $result);
  159. $expected = "What up dog? \n [y] > ";
  160. $result = $command->response->output;
  161. $this->assertEqual($expected, $result);
  162. fwrite($command->request->input, "\n");
  163. fwrite($command->request->input, 'n');
  164. rewind($command->request->input);
  165. $expected = "y";
  166. $result = $command->in('R U Sure?', array('choices' => array('y', 'n'), 'default' => 'y'));
  167. $this->assertEqual($expected, $result);
  168. }
  169. public function testInWithOptions() {
  170. $command = new MockCommand(array('request' => $this->request));
  171. fwrite($command->request->input, 'y');
  172. rewind($command->request->input);
  173. $expected = "y";
  174. $result = $command->in('Everything Cool?', array('choices' => array('y', 'n')));
  175. $this->assertEqual($expected, $result);
  176. $expected = "Everything Cool? (y/n) \n > ";
  177. $result = $command->response->output;
  178. $this->assertEqual($expected, $result);
  179. }
  180. public function testInWithBadInput(){
  181. $command = new MockCommand(array('request' => $this->request));
  182. fwrite($command->request->input, "f\n");
  183. fwrite($command->request->input, 'y');
  184. rewind($command->request->input);
  185. $expected = "y";
  186. $result = $command->in('Everything Cool?', array('choices' => array('y', 'n')));
  187. $this->assertEqual($expected, $result);
  188. }
  189. public function testOutWithStyles() {
  190. $command = new MockCommand(array('request' => $this->request));
  191. $expected = "{:some-style}ok\n";
  192. $result = $command->out('ok', 'some-style');
  193. $this->assertEqual($expected, $result);
  194. }
  195. public function testOutWithoutStyles() {
  196. $command = new MockCommand(array('request' => $this->request));
  197. $command->plain = true;
  198. $expected = "ok\n";
  199. $result = $command->out('ok', 'some-style');
  200. $this->assertEqual($expected, $result);
  201. }
  202. public function testOutWithSilent() {
  203. $command = new MockCommand(array('request' => $this->request));
  204. $command->silent = true;
  205. $expected = "";
  206. $result = $command->out('ok', 'some-style');
  207. $this->assertEqual($expected, $result);
  208. }
  209. public function testColumnsOnErrorOutput() {
  210. $command = new MockCommand(array('request' => $this->request));
  211. $expected = "data1\t\ndata2\t\n";
  212. $command->columns(array('col1' => 'data1', 'col2' => 'data2'), array('error' => true));
  213. $result = $command->response->error;
  214. $this->assertEqual($expected, $result);
  215. }
  216. }
  217. ?>