ConsoleIoTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. /**
  3. * CakePHP : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Console;
  16. use Cake\Console\ConsoleIo;
  17. use Cake\TestSuite\TestCase;
  18. /**
  19. * ConsoleIo test.
  20. */
  21. class ConsoleIoTest extends TestCase {
  22. /**
  23. * setUp method
  24. *
  25. * @return void
  26. */
  27. public function setUp() {
  28. parent::setUp();
  29. $this->out = $this->getMock('Cake\Console\ConsoleOutput', [], [], '', false);
  30. $this->err = $this->getMock('Cake\Console\ConsoleOutput', [], [], '', false);
  31. $this->in = $this->getMock('Cake\Console\ConsoleInput', [], [], '', false);
  32. $this->io = new ConsoleIo($this->out, $this->err, $this->in);
  33. }
  34. /**
  35. * Provider for testing choice types.
  36. *
  37. * @return array
  38. */
  39. public function choiceProvider() {
  40. return [
  41. [['y', 'n']],
  42. ['y,n'],
  43. ['y/n'],
  44. ['y'],
  45. ];
  46. }
  47. /**
  48. * test ask choices method
  49. *
  50. * @dataProvider choiceProvider
  51. * @return void
  52. */
  53. public function testAskChoices($choices) {
  54. $this->in->expects($this->at(0))
  55. ->method('read')
  56. ->will($this->returnValue('y'));
  57. $result = $this->io->askChoice('Just a test?', $choices);
  58. $this->assertEquals('y', $result);
  59. }
  60. /**
  61. * test ask choices method
  62. *
  63. * @dataProvider choiceProvider
  64. * @return void
  65. */
  66. public function testAskChoicesInsensitive($choices) {
  67. $this->in->expects($this->at(0))
  68. ->method('read')
  69. ->will($this->returnValue('Y'));
  70. $result = $this->io->askChoice('Just a test?', $choices);
  71. $this->assertEquals('Y', $result);
  72. }
  73. /**
  74. * Test ask method
  75. *
  76. * @return void
  77. */
  78. public function testAsk() {
  79. $this->out->expects($this->at(0))
  80. ->method('write')
  81. ->with("<question>Just a test?</question>\n> ");
  82. $this->in->expects($this->at(0))
  83. ->method('read')
  84. ->will($this->returnValue('y'));
  85. $result = $this->io->ask('Just a test?');
  86. $this->assertEquals('y', $result);
  87. }
  88. /**
  89. * Test ask method
  90. *
  91. * @return void
  92. */
  93. public function testAskDefaultValue() {
  94. $this->out->expects($this->at(0))
  95. ->method('write')
  96. ->with("<question>Just a test?</question>\n[n] > ");
  97. $this->in->expects($this->at(0))
  98. ->method('read')
  99. ->will($this->returnValue(''));
  100. $result = $this->io->ask('Just a test?', 'n');
  101. $this->assertEquals('n', $result);
  102. }
  103. /**
  104. * testOut method
  105. *
  106. * @return void
  107. */
  108. public function testOut() {
  109. $this->out->expects($this->at(0))
  110. ->method('write')
  111. ->with("Just a test", 1);
  112. $this->out->expects($this->at(1))
  113. ->method('write')
  114. ->with(array('Just', 'a', 'test'), 1);
  115. $this->out->expects($this->at(2))
  116. ->method('write')
  117. ->with(array('Just', 'a', 'test'), 2);
  118. $this->out->expects($this->at(3))
  119. ->method('write')
  120. ->with('', 1);
  121. $this->io->out('Just a test');
  122. $this->io->out(array('Just', 'a', 'test'));
  123. $this->io->out(array('Just', 'a', 'test'), 2);
  124. $this->io->out();
  125. }
  126. /**
  127. * test that verbose and quiet output levels work
  128. *
  129. * @return void
  130. */
  131. public function testVerboseOut() {
  132. $this->out->expects($this->at(0))
  133. ->method('write')
  134. ->with('Verbose', 1);
  135. $this->out->expects($this->at(1))
  136. ->method('write')
  137. ->with('Normal', 1);
  138. $this->out->expects($this->at(2))
  139. ->method('write')
  140. ->with('Quiet', 1);
  141. $this->io->level(ConsoleIo::VERBOSE);
  142. $this->io->out('Verbose', 1, ConsoleIo::VERBOSE);
  143. $this->io->out('Normal', 1, ConsoleIo::NORMAL);
  144. $this->io->out('Quiet', 1, ConsoleIo::QUIET);
  145. }
  146. /**
  147. * test that verbose and quiet output levels work
  148. *
  149. * @return void
  150. */
  151. public function testVerboseOutput() {
  152. $this->out->expects($this->at(0))
  153. ->method('write')
  154. ->with('Verbose', 1);
  155. $this->out->expects($this->at(1))
  156. ->method('write')
  157. ->with('Normal', 1);
  158. $this->out->expects($this->at(2))
  159. ->method('write')
  160. ->with('Quiet', 1);
  161. $this->io->level(ConsoleIo::VERBOSE);
  162. $this->io->verbose('Verbose');
  163. $this->io->out('Normal');
  164. $this->io->quiet('Quiet');
  165. }
  166. /**
  167. * test that verbose and quiet output levels work
  168. *
  169. * @return void
  170. */
  171. public function testQuietOutput() {
  172. $this->out->expects($this->exactly(2))
  173. ->method('write')
  174. ->with('Quiet', 1);
  175. $this->io->level(ConsoleIo::QUIET);
  176. $this->io->out('Verbose', 1, ConsoleIo::VERBOSE);
  177. $this->io->out('Normal', 1, ConsoleIo::NORMAL);
  178. $this->io->out('Quiet', 1, ConsoleIo::QUIET);
  179. $this->io->verbose('Verbose');
  180. $this->io->quiet('Quiet');
  181. }
  182. /**
  183. * testErr method
  184. *
  185. * @return void
  186. */
  187. public function testErr() {
  188. $this->err->expects($this->at(0))
  189. ->method('write')
  190. ->with("Just a test", 1);
  191. $this->err->expects($this->at(1))
  192. ->method('write')
  193. ->with(array('Just', 'a', 'test'), 1);
  194. $this->err->expects($this->at(2))
  195. ->method('write')
  196. ->with(array('Just', 'a', 'test'), 2);
  197. $this->err->expects($this->at(3))
  198. ->method('write')
  199. ->with('', 1);
  200. $this->io->err('Just a test');
  201. $this->io->err(array('Just', 'a', 'test'));
  202. $this->io->err(array('Just', 'a', 'test'), 2);
  203. $this->io->err();
  204. }
  205. /**
  206. * testNl
  207. *
  208. * @return void
  209. */
  210. public function testNl() {
  211. $newLine = "\n";
  212. if (DS === '\\') {
  213. $newLine = "\r\n";
  214. }
  215. $this->assertEquals($this->io->nl(), $newLine);
  216. $this->assertEquals($this->io->nl(true), $newLine);
  217. $this->assertEquals("", $this->io->nl(false));
  218. $this->assertEquals($this->io->nl(2), $newLine . $newLine);
  219. $this->assertEquals($this->io->nl(1), $newLine);
  220. }
  221. /**
  222. * testHr
  223. *
  224. * @return void
  225. */
  226. public function testHr() {
  227. $bar = '---------------------------------------------------------------';
  228. $this->out->expects($this->at(0))->method('write')->with('', 0);
  229. $this->out->expects($this->at(1))->method('write')->with($bar, 1);
  230. $this->out->expects($this->at(2))->method('write')->with('', 0);
  231. $this->out->expects($this->at(3))->method('write')->with("", true);
  232. $this->out->expects($this->at(4))->method('write')->with($bar, 1);
  233. $this->out->expects($this->at(5))->method('write')->with("", true);
  234. $this->out->expects($this->at(6))->method('write')->with("", 2);
  235. $this->out->expects($this->at(7))->method('write')->with($bar, 1);
  236. $this->out->expects($this->at(8))->method('write')->with("", 2);
  237. $this->io->hr();
  238. $this->io->hr(true);
  239. $this->io->hr(2);
  240. }
  241. /**
  242. * Test overwriting.
  243. *
  244. * @return void
  245. */
  246. public function testOverwrite() {
  247. $number = strlen('Some text I want to overwrite');
  248. $this->out->expects($this->at(0))
  249. ->method('write')
  250. ->with('Some <info>text</info> I want to overwrite', 0)
  251. ->will($this->returnValue($number));
  252. $this->out->expects($this->at(1))
  253. ->method('write')
  254. ->with(str_repeat("\x08", $number), 0);
  255. $this->out->expects($this->at(2))
  256. ->method('write')
  257. ->with('Less text', 0)
  258. ->will($this->returnValue(9));
  259. $this->out->expects($this->at(3))
  260. ->method('write')
  261. ->with(str_repeat(' ', $number - 9), 0);
  262. $this->io->out('Some <info>text</info> I want to overwrite', 0);
  263. $this->io->overwrite('Less text');
  264. }
  265. }