ConsoleIoTest.php 20 KB

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