ConsoleIoTest.php 11 KB

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