ConsoleIoTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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\Filesystem\Folder;
  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. static::setAppNamespace();
  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. * teardown method
  47. *
  48. * @return void
  49. */
  50. public function tearDown()
  51. {
  52. parent::tearDown();
  53. if (is_dir(TMP . 'shell_test')) {
  54. $folder = new Folder(TMP . 'shell_test');
  55. $folder->delete();
  56. }
  57. }
  58. /**
  59. * Provider for testing choice types.
  60. *
  61. * @return array
  62. */
  63. public function choiceProvider()
  64. {
  65. return [
  66. [['y', 'n']],
  67. ['y,n'],
  68. ['y/n'],
  69. ['y'],
  70. ];
  71. }
  72. /**
  73. * test ask choices method
  74. *
  75. * @dataProvider choiceProvider
  76. * @return void
  77. */
  78. public function testAskChoices($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 choices method
  88. *
  89. * @dataProvider choiceProvider
  90. * @return void
  91. */
  92. public function testAskChoicesInsensitive($choices)
  93. {
  94. $this->in->expects($this->at(0))
  95. ->method('read')
  96. ->will($this->returnValue('Y'));
  97. $result = $this->io->askChoice('Just a test?', $choices);
  98. $this->assertEquals('Y', $result);
  99. }
  100. /**
  101. * Test ask method
  102. *
  103. * @return void
  104. */
  105. public function testAsk()
  106. {
  107. $this->out->expects($this->at(0))
  108. ->method('write')
  109. ->with("<question>Just a test?</question>\n> ");
  110. $this->in->expects($this->at(0))
  111. ->method('read')
  112. ->will($this->returnValue('y'));
  113. $result = $this->io->ask('Just a test?');
  114. $this->assertEquals('y', $result);
  115. }
  116. /**
  117. * Test ask method
  118. *
  119. * @return void
  120. */
  121. public function testAskDefaultValue()
  122. {
  123. $this->out->expects($this->at(0))
  124. ->method('write')
  125. ->with("<question>Just a test?</question>\n[n] > ");
  126. $this->in->expects($this->at(0))
  127. ->method('read')
  128. ->will($this->returnValue(''));
  129. $result = $this->io->ask('Just a test?', 'n');
  130. $this->assertEquals('n', $result);
  131. }
  132. /**
  133. * testOut method
  134. *
  135. * @return void
  136. */
  137. public function testOut()
  138. {
  139. $this->out->expects($this->at(0))
  140. ->method('write')
  141. ->with('Just a test', 1);
  142. $this->out->expects($this->at(1))
  143. ->method('write')
  144. ->with(['Just', 'a', 'test'], 1);
  145. $this->out->expects($this->at(2))
  146. ->method('write')
  147. ->with(['Just', 'a', 'test'], 2);
  148. $this->out->expects($this->at(3))
  149. ->method('write')
  150. ->with('', 1);
  151. $this->io->out('Just a test');
  152. $this->io->out(['Just', 'a', 'test']);
  153. $this->io->out(['Just', 'a', 'test'], 2);
  154. $this->io->out();
  155. }
  156. /**
  157. * test that verbose and quiet output levels work
  158. *
  159. * @return void
  160. */
  161. public function testVerboseOut()
  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->out('Verbose', 1, ConsoleIo::VERBOSE);
  174. $this->io->out('Normal', 1, ConsoleIo::NORMAL);
  175. $this->io->out('Quiet', 1, ConsoleIo::QUIET);
  176. }
  177. /**
  178. * test that verbose and quiet output levels work
  179. *
  180. * @return void
  181. */
  182. public function testVerboseOutput()
  183. {
  184. $this->out->expects($this->at(0))
  185. ->method('write')
  186. ->with('Verbose', 1);
  187. $this->out->expects($this->at(1))
  188. ->method('write')
  189. ->with('Normal', 1);
  190. $this->out->expects($this->at(2))
  191. ->method('write')
  192. ->with('Quiet', 1);
  193. $this->io->level(ConsoleIo::VERBOSE);
  194. $this->io->verbose('Verbose');
  195. $this->io->out('Normal');
  196. $this->io->quiet('Quiet');
  197. }
  198. /**
  199. * test that verbose and quiet output levels work
  200. *
  201. * @return void
  202. */
  203. public function testQuietOutput()
  204. {
  205. $this->out->expects($this->exactly(2))
  206. ->method('write')
  207. ->with('Quiet', 1);
  208. $this->io->level(ConsoleIo::QUIET);
  209. $this->io->out('Verbose', 1, ConsoleIo::VERBOSE);
  210. $this->io->out('Normal', 1, ConsoleIo::NORMAL);
  211. $this->io->out('Quiet', 1, ConsoleIo::QUIET);
  212. $this->io->verbose('Verbose');
  213. $this->io->quiet('Quiet');
  214. }
  215. /**
  216. * testErr method
  217. *
  218. * @return void
  219. */
  220. public function testErr()
  221. {
  222. $this->err->expects($this->at(0))
  223. ->method('write')
  224. ->with('Just a test', 1);
  225. $this->err->expects($this->at(1))
  226. ->method('write')
  227. ->with(['Just', 'a', 'test'], 1);
  228. $this->err->expects($this->at(2))
  229. ->method('write')
  230. ->with(['Just', 'a', 'test'], 2);
  231. $this->err->expects($this->at(3))
  232. ->method('write')
  233. ->with('', 1);
  234. $this->io->err('Just a test');
  235. $this->io->err(['Just', 'a', 'test']);
  236. $this->io->err(['Just', 'a', 'test'], 2);
  237. $this->io->err();
  238. }
  239. /**
  240. * testNl
  241. *
  242. * @return void
  243. */
  244. public function testNl()
  245. {
  246. $newLine = "\n";
  247. if (DS === '\\') {
  248. $newLine = "\r\n";
  249. }
  250. $this->assertEquals($this->io->nl(), $newLine);
  251. $this->assertEquals($this->io->nl(true), $newLine);
  252. $this->assertEquals('', $this->io->nl(false));
  253. $this->assertEquals($this->io->nl(2), $newLine . $newLine);
  254. $this->assertEquals($this->io->nl(1), $newLine);
  255. }
  256. /**
  257. * testHr
  258. *
  259. * @return void
  260. */
  261. public function testHr()
  262. {
  263. $bar = str_repeat('-', 79);
  264. $this->out->expects($this->at(0))->method('write')->with('', 0);
  265. $this->out->expects($this->at(1))->method('write')->with($bar, 1);
  266. $this->out->expects($this->at(2))->method('write')->with('', 0);
  267. $this->out->expects($this->at(3))->method('write')->with('', true);
  268. $this->out->expects($this->at(4))->method('write')->with($bar, 1);
  269. $this->out->expects($this->at(5))->method('write')->with('', true);
  270. $this->out->expects($this->at(6))->method('write')->with('', 2);
  271. $this->out->expects($this->at(7))->method('write')->with($bar, 1);
  272. $this->out->expects($this->at(8))->method('write')->with('', 2);
  273. $this->io->hr();
  274. $this->io->hr(true);
  275. $this->io->hr(2);
  276. }
  277. /**
  278. * Test overwriting.
  279. *
  280. * @return void
  281. */
  282. public function testOverwrite()
  283. {
  284. $number = strlen('Some text I want to overwrite');
  285. $this->out->expects($this->at(0))
  286. ->method('write')
  287. ->with('Some <info>text</info> I want to overwrite', 0)
  288. ->will($this->returnValue($number));
  289. $this->out->expects($this->at(1))
  290. ->method('write')
  291. ->with(str_repeat("\x08", $number), 0);
  292. $this->out->expects($this->at(2))
  293. ->method('write')
  294. ->with('Less text', 0)
  295. ->will($this->returnValue(9));
  296. $this->out->expects($this->at(3))
  297. ->method('write')
  298. ->with(str_repeat(' ', $number - 9), 0);
  299. $this->io->out('Some <info>text</info> I want to overwrite', 0);
  300. $this->io->overwrite('Less text');
  301. }
  302. /**
  303. * Test overwriting content with shorter content
  304. *
  305. * @return void
  306. */
  307. public function testOverwriteWithShorterContent()
  308. {
  309. $length = strlen('12345');
  310. $this->out->expects($this->at(0))
  311. ->method('write')
  312. ->with('12345')
  313. ->will($this->returnValue($length));
  314. // Backspaces
  315. $this->out->expects($this->at(1))
  316. ->method('write')
  317. ->with(str_repeat("\x08", $length), 0)
  318. ->will($this->returnValue($length));
  319. $this->out->expects($this->at(2))
  320. ->method('write')
  321. ->with('123', 0)
  322. ->will($this->returnValue(3));
  323. // 2 spaces output to pad up to 5 bytes
  324. $this->out->expects($this->at(3))
  325. ->method('write')
  326. ->with(str_repeat(' ', $length - 3), 0)
  327. ->will($this->returnValue($length - 3));
  328. // Backspaces
  329. $this->out->expects($this->at(4))
  330. ->method('write')
  331. ->with(str_repeat("\x08", $length), 0)
  332. ->will($this->returnValue($length));
  333. $this->out->expects($this->at(5))
  334. ->method('write')
  335. ->with('12', 0)
  336. ->will($this->returnValue(2));
  337. $this->out->expects($this->at(6))
  338. ->method('write')
  339. ->with(str_repeat(' ', $length - 2), 0);
  340. $this->io->out('12345');
  341. $this->io->overwrite('123', 0);
  342. $this->io->overwrite('12', 0);
  343. }
  344. /**
  345. * Test overwriting content with longer content
  346. *
  347. * @return void
  348. */
  349. public function testOverwriteWithLongerContent()
  350. {
  351. $this->out->expects($this->at(0))
  352. ->method('write')
  353. ->with('1')
  354. ->will($this->returnValue(1));
  355. // Backspaces
  356. $this->out->expects($this->at(1))
  357. ->method('write')
  358. ->with(str_repeat("\x08", 1), 0)
  359. ->will($this->returnValue(1));
  360. $this->out->expects($this->at(2))
  361. ->method('write')
  362. ->with('123', 0)
  363. ->will($this->returnValue(3));
  364. // Backspaces
  365. $this->out->expects($this->at(3))
  366. ->method('write')
  367. ->with(str_repeat("\x08", 3), 0)
  368. ->will($this->returnValue(3));
  369. $this->out->expects($this->at(4))
  370. ->method('write')
  371. ->with('12345', 0)
  372. ->will($this->returnValue(5));
  373. $this->io->out('1');
  374. $this->io->overwrite('123', 0);
  375. $this->io->overwrite('12345', 0);
  376. }
  377. /**
  378. * Tests that setLoggers works properly
  379. *
  380. * @return void
  381. */
  382. public function testSetLoggers()
  383. {
  384. Log::drop('stdout');
  385. Log::drop('stderr');
  386. $this->io->setLoggers(true);
  387. $this->assertNotEmpty(Log::engine('stdout'));
  388. $this->assertNotEmpty(Log::engine('stderr'));
  389. $this->io->setLoggers(false);
  390. $this->assertFalse(Log::engine('stdout'));
  391. $this->assertFalse(Log::engine('stderr'));
  392. }
  393. /**
  394. * Tests that setLoggers works properly with quiet
  395. *
  396. * @return void
  397. */
  398. public function testSetLoggersQuiet()
  399. {
  400. Log::drop('stdout');
  401. Log::drop('stderr');
  402. $this->io->setLoggers(ConsoleIo::QUIET);
  403. $this->assertEmpty(Log::engine('stdout'));
  404. $this->assertNotEmpty(Log::engine('stderr'));
  405. }
  406. /**
  407. * Tests that setLoggers works properly with verbose
  408. *
  409. * @return void
  410. */
  411. public function testSetLoggersVerbose()
  412. {
  413. Log::drop('stdout');
  414. Log::drop('stderr');
  415. $this->io->setLoggers(ConsoleIo::VERBOSE);
  416. $this->assertNotEmpty(Log::engine('stderr'));
  417. $engine = Log::engine('stdout');
  418. $this->assertEquals(['notice', 'info', 'debug'], $engine->getConfig('levels'));
  419. }
  420. /**
  421. * Ensure that styles() just proxies to stdout.
  422. *
  423. * @return void
  424. */
  425. public function testStyles()
  426. {
  427. $this->out->expects($this->once())
  428. ->method('styles')
  429. ->with('name', 'props');
  430. $this->io->styles('name', 'props');
  431. }
  432. /**
  433. * Test the helper method.
  434. *
  435. * @return void
  436. */
  437. public function testHelper()
  438. {
  439. $this->out->expects($this->once())
  440. ->method('write')
  441. ->with('It works!well ish');
  442. $helper = $this->io->helper('simple');
  443. $this->assertInstanceOf('Cake\Console\Helper', $helper);
  444. $helper->output(['well', 'ish']);
  445. }
  446. /**
  447. * Provider for output helpers
  448. *
  449. * @return array
  450. */
  451. public function outHelperProvider()
  452. {
  453. return [['info'], ['success']];
  454. }
  455. /**
  456. * Provider for err helpers
  457. *
  458. * @return array
  459. */
  460. public function errHelperProvider()
  461. {
  462. return [['warning'], ['error']];
  463. }
  464. /**
  465. * test out helper methods
  466. *
  467. * @dataProvider outHelperProvider
  468. * @return void
  469. */
  470. public function testOutHelpers($method)
  471. {
  472. $this->out->expects($this->at(0))
  473. ->method('write')
  474. ->with("<{$method}>Just a test</{$method}>", 1);
  475. $this->out->expects($this->at(1))
  476. ->method('write')
  477. ->with(["<{$method}>Just</{$method}>", "<{$method}>a test</{$method}>"], 1);
  478. $this->io->{$method}('Just a test');
  479. $this->io->{$method}(['Just', 'a test']);
  480. }
  481. /**
  482. * test err helper methods
  483. *
  484. * @dataProvider errHelperProvider
  485. * @return void
  486. */
  487. public function testErrHelpers($method)
  488. {
  489. $this->err->expects($this->at(0))
  490. ->method('write')
  491. ->with("<{$method}>Just a test</{$method}>", 1);
  492. $this->err->expects($this->at(1))
  493. ->method('write')
  494. ->with(["<{$method}>Just</{$method}>", "<{$method}>a test</{$method}>"], 1);
  495. $this->io->{$method}('Just a test');
  496. $this->io->{$method}(['Just', 'a test']);
  497. }
  498. /**
  499. * Test that createFile
  500. *
  501. * @return void
  502. */
  503. public function testCreateFileSuccess()
  504. {
  505. $this->err->expects($this->never())
  506. ->method('write');
  507. $path = TMP . 'shell_test';
  508. mkdir($path);
  509. $file = $path . DS . 'file1.php';
  510. $contents = 'some content';
  511. $result = $this->io->createFile($file, $contents);
  512. $this->assertTrue($result);
  513. $this->assertFileExists($file);
  514. $this->assertStringEqualsFile($file, $contents);
  515. }
  516. public function testCreateFileDirectoryCreation()
  517. {
  518. $this->err->expects($this->never())
  519. ->method('write');
  520. $directory = TMP . 'shell_test';
  521. $this->assertFileNotExists($directory, 'Directory should not exist before createFile');
  522. $path = $directory . DS . 'create.txt';
  523. $contents = 'some content';
  524. $result = $this->io->createFile($path, $contents);
  525. $this->assertTrue($result, 'File should create');
  526. $this->assertFileExists($path);
  527. $this->assertStringEqualsFile($path, $contents);
  528. }
  529. /**
  530. * Test that createFile with permissions error.
  531. *
  532. * @return void
  533. */
  534. public function testCreateFilePermissionsError()
  535. {
  536. $this->skipIf(DS === '\\', 'Cant perform operations using permissions on windows.');
  537. $path = TMP . 'shell_test';
  538. $file = $path . DS . 'no_perms';
  539. if (!is_dir($path)) {
  540. mkdir($path);
  541. }
  542. chmod($path, 0444);
  543. $this->io->createFile($file, 'testing');
  544. $this->assertFileNotExists($file);
  545. chmod($path, 0744);
  546. rmdir($path);
  547. }
  548. /**
  549. * Test that `q` raises an error.
  550. *
  551. * @expectedException \Cake\Console\Exception\StopException
  552. * @return void
  553. */
  554. public function testCreateFileOverwriteQuit()
  555. {
  556. $path = TMP . 'shell_test';
  557. mkdir($path);
  558. $file = $path . DS . 'file1.php';
  559. touch($file);
  560. $this->in->expects($this->once())
  561. ->method('read')
  562. ->will($this->returnValue('q'));
  563. $this->io->createFile($file, 'some content');
  564. }
  565. /**
  566. * Test that `n` raises an error.
  567. *
  568. * @return void
  569. */
  570. public function testCreateFileOverwriteNo()
  571. {
  572. $path = TMP . 'shell_test';
  573. mkdir($path);
  574. $file = $path . DS . 'file1.php';
  575. file_put_contents($file, 'original');
  576. touch($file);
  577. $this->in->expects($this->once())
  578. ->method('read')
  579. ->will($this->returnValue('n'));
  580. $contents = 'new content';
  581. $result = $this->io->createFile($file, $contents);
  582. $this->assertFalse($result);
  583. $this->assertFileExists($file);
  584. $this->assertStringEqualsFile($file, 'original');
  585. }
  586. /**
  587. * Test the forceOverwrite parameter
  588. *
  589. * @return void
  590. */
  591. public function testCreateFileOverwriteParam()
  592. {
  593. $path = TMP . 'shell_test';
  594. mkdir($path);
  595. $file = $path . DS . 'file1.php';
  596. file_put_contents($file, 'original');
  597. touch($file);
  598. $contents = 'new content';
  599. $result = $this->io->createFile($file, $contents, true);
  600. $this->assertTrue($result);
  601. $this->assertFileExists($file);
  602. $this->assertStringEqualsFile($file, $contents);
  603. }
  604. /**
  605. * Test the `a` response
  606. *
  607. * @return void
  608. */
  609. public function testCreateFileOverwriteAll()
  610. {
  611. $path = TMP . 'shell_test';
  612. mkdir($path);
  613. $file = $path . DS . 'file1.php';
  614. file_put_contents($file, 'original');
  615. touch($file);
  616. $this->in->expects($this->once())
  617. ->method('read')
  618. ->will($this->returnValue('a'));
  619. $this->io->createFile($file, 'new content');
  620. $this->assertStringEqualsFile($file, 'new content');
  621. $this->io->createFile($file, 'newer content');
  622. $this->assertStringEqualsFile($file, 'newer content');
  623. $this->io->createFile($file, 'newest content', false);
  624. $this->assertStringEqualsFile(
  625. $file,
  626. 'newest content',
  627. 'overwrite state replaces parameter'
  628. );
  629. }
  630. }