ConsoleIoTest.php 11 KB

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