ConsoleIoTest.php 9.3 KB

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