ConsoleIoTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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\Log\Log;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * ConsoleIo test.
  21. */
  22. class ConsoleIoTest extends TestCase {
  23. /**
  24. * setUp method
  25. *
  26. * @return void
  27. */
  28. public function setUp() {
  29. parent::setUp();
  30. $this->out = $this->getMock('Cake\Console\ConsoleOutput', [], [], '', false);
  31. $this->err = $this->getMock('Cake\Console\ConsoleOutput', [], [], '', false);
  32. $this->in = $this->getMock('Cake\Console\ConsoleInput', [], [], '', false);
  33. $this->io = new ConsoleIo($this->out, $this->err, $this->in);
  34. }
  35. /**
  36. * Provider for testing choice types.
  37. *
  38. * @return array
  39. */
  40. public function choiceProvider() {
  41. return [
  42. [['y', 'n']],
  43. ['y,n'],
  44. ['y/n'],
  45. ['y'],
  46. ];
  47. }
  48. /**
  49. * test ask choices method
  50. *
  51. * @dataProvider choiceProvider
  52. * @return void
  53. */
  54. public function testAskChoices($choices) {
  55. $this->in->expects($this->at(0))
  56. ->method('read')
  57. ->will($this->returnValue('y'));
  58. $result = $this->io->askChoice('Just a test?', $choices);
  59. $this->assertEquals('y', $result);
  60. }
  61. /**
  62. * test ask choices method
  63. *
  64. * @dataProvider choiceProvider
  65. * @return void
  66. */
  67. public function testAskChoicesInsensitive($choices) {
  68. $this->in->expects($this->at(0))
  69. ->method('read')
  70. ->will($this->returnValue('Y'));
  71. $result = $this->io->askChoice('Just a test?', $choices);
  72. $this->assertEquals('Y', $result);
  73. }
  74. /**
  75. * Test ask method
  76. *
  77. * @return void
  78. */
  79. public function testAsk() {
  80. $this->out->expects($this->at(0))
  81. ->method('write')
  82. ->with("<question>Just a test?</question>\n> ");
  83. $this->in->expects($this->at(0))
  84. ->method('read')
  85. ->will($this->returnValue('y'));
  86. $result = $this->io->ask('Just a test?');
  87. $this->assertEquals('y', $result);
  88. }
  89. /**
  90. * Test ask method
  91. *
  92. * @return void
  93. */
  94. public function testAskDefaultValue() {
  95. $this->out->expects($this->at(0))
  96. ->method('write')
  97. ->with("<question>Just a test?</question>\n[n] > ");
  98. $this->in->expects($this->at(0))
  99. ->method('read')
  100. ->will($this->returnValue(''));
  101. $result = $this->io->ask('Just a test?', 'n');
  102. $this->assertEquals('n', $result);
  103. }
  104. /**
  105. * testOut method
  106. *
  107. * @return void
  108. */
  109. public function testOut() {
  110. $this->out->expects($this->at(0))
  111. ->method('write')
  112. ->with("Just a test", 1);
  113. $this->out->expects($this->at(1))
  114. ->method('write')
  115. ->with(array('Just', 'a', 'test'), 1);
  116. $this->out->expects($this->at(2))
  117. ->method('write')
  118. ->with(array('Just', 'a', 'test'), 2);
  119. $this->out->expects($this->at(3))
  120. ->method('write')
  121. ->with('', 1);
  122. $this->io->out('Just a test');
  123. $this->io->out(array('Just', 'a', 'test'));
  124. $this->io->out(array('Just', 'a', 'test'), 2);
  125. $this->io->out();
  126. }
  127. /**
  128. * test that verbose and quiet output levels work
  129. *
  130. * @return void
  131. */
  132. public function testVerboseOut() {
  133. $this->out->expects($this->at(0))
  134. ->method('write')
  135. ->with('Verbose', 1);
  136. $this->out->expects($this->at(1))
  137. ->method('write')
  138. ->with('Normal', 1);
  139. $this->out->expects($this->at(2))
  140. ->method('write')
  141. ->with('Quiet', 1);
  142. $this->io->level(ConsoleIo::VERBOSE);
  143. $this->io->out('Verbose', 1, ConsoleIo::VERBOSE);
  144. $this->io->out('Normal', 1, ConsoleIo::NORMAL);
  145. $this->io->out('Quiet', 1, ConsoleIo::QUIET);
  146. }
  147. /**
  148. * test that verbose and quiet output levels work
  149. *
  150. * @return void
  151. */
  152. public function testVerboseOutput() {
  153. $this->out->expects($this->at(0))
  154. ->method('write')
  155. ->with('Verbose', 1);
  156. $this->out->expects($this->at(1))
  157. ->method('write')
  158. ->with('Normal', 1);
  159. $this->out->expects($this->at(2))
  160. ->method('write')
  161. ->with('Quiet', 1);
  162. $this->io->level(ConsoleIo::VERBOSE);
  163. $this->io->verbose('Verbose');
  164. $this->io->out('Normal');
  165. $this->io->quiet('Quiet');
  166. }
  167. /**
  168. * test that verbose and quiet output levels work
  169. *
  170. * @return void
  171. */
  172. public function testQuietOutput() {
  173. $this->out->expects($this->exactly(2))
  174. ->method('write')
  175. ->with('Quiet', 1);
  176. $this->io->level(ConsoleIo::QUIET);
  177. $this->io->out('Verbose', 1, ConsoleIo::VERBOSE);
  178. $this->io->out('Normal', 1, ConsoleIo::NORMAL);
  179. $this->io->out('Quiet', 1, ConsoleIo::QUIET);
  180. $this->io->verbose('Verbose');
  181. $this->io->quiet('Quiet');
  182. }
  183. /**
  184. * testErr method
  185. *
  186. * @return void
  187. */
  188. public function testErr() {
  189. $this->err->expects($this->at(0))
  190. ->method('write')
  191. ->with("Just a test", 1);
  192. $this->err->expects($this->at(1))
  193. ->method('write')
  194. ->with(array('Just', 'a', 'test'), 1);
  195. $this->err->expects($this->at(2))
  196. ->method('write')
  197. ->with(array('Just', 'a', 'test'), 2);
  198. $this->err->expects($this->at(3))
  199. ->method('write')
  200. ->with('', 1);
  201. $this->io->err('Just a test');
  202. $this->io->err(array('Just', 'a', 'test'));
  203. $this->io->err(array('Just', 'a', 'test'), 2);
  204. $this->io->err();
  205. }
  206. /**
  207. * testNl
  208. *
  209. * @return void
  210. */
  211. public function testNl() {
  212. $newLine = "\n";
  213. if (DS === '\\') {
  214. $newLine = "\r\n";
  215. }
  216. $this->assertEquals($this->io->nl(), $newLine);
  217. $this->assertEquals($this->io->nl(true), $newLine);
  218. $this->assertEquals("", $this->io->nl(false));
  219. $this->assertEquals($this->io->nl(2), $newLine . $newLine);
  220. $this->assertEquals($this->io->nl(1), $newLine);
  221. }
  222. /**
  223. * testHr
  224. *
  225. * @return void
  226. */
  227. public function testHr() {
  228. $bar = str_repeat('-', 79);
  229. $this->out->expects($this->at(0))->method('write')->with('', 0);
  230. $this->out->expects($this->at(1))->method('write')->with($bar, 1);
  231. $this->out->expects($this->at(2))->method('write')->with('', 0);
  232. $this->out->expects($this->at(3))->method('write')->with("", true);
  233. $this->out->expects($this->at(4))->method('write')->with($bar, 1);
  234. $this->out->expects($this->at(5))->method('write')->with("", true);
  235. $this->out->expects($this->at(6))->method('write')->with("", 2);
  236. $this->out->expects($this->at(7))->method('write')->with($bar, 1);
  237. $this->out->expects($this->at(8))->method('write')->with("", 2);
  238. $this->io->hr();
  239. $this->io->hr(true);
  240. $this->io->hr(2);
  241. }
  242. /**
  243. * Test overwriting.
  244. *
  245. * @return void
  246. */
  247. public function testOverwrite() {
  248. $number = strlen('Some text I want to overwrite');
  249. $this->out->expects($this->at(0))
  250. ->method('write')
  251. ->with('Some <info>text</info> I want to overwrite', 0)
  252. ->will($this->returnValue($number));
  253. $this->out->expects($this->at(1))
  254. ->method('write')
  255. ->with(str_repeat("\x08", $number), 0);
  256. $this->out->expects($this->at(2))
  257. ->method('write')
  258. ->with('Less text', 0)
  259. ->will($this->returnValue(9));
  260. $this->out->expects($this->at(3))
  261. ->method('write')
  262. ->with(str_repeat(' ', $number - 9), 0);
  263. $this->io->out('Some <info>text</info> I want to overwrite', 0);
  264. $this->io->overwrite('Less text');
  265. }
  266. /**
  267. * Tests that setLoggers works properly
  268. *
  269. * @return void
  270. */
  271. public function testSetLoggers() {
  272. Log::drop('stdout');
  273. Log::drop('stderr');
  274. $this->io->setLoggers(true);
  275. $this->assertNotEmpty(Log::engine('stdout'));
  276. $this->assertNotEmpty(Log::engine('stderr'));
  277. $this->io->setLoggers(false);
  278. $this->assertFalse(Log::engine('stdout'));
  279. $this->assertFalse(Log::engine('stderr'));
  280. }
  281. /**
  282. * Ensure that styles() just proxies to stdout.
  283. *
  284. * @return void
  285. */
  286. public function testStyles() {
  287. $this->out->expects($this->once())
  288. ->method('styles')
  289. ->with('name', 'props');
  290. $this->io->styles('name', 'props');
  291. }
  292. }