ConsoleIoTest.php 10 KB

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