ConsoleIoTest.php 21 KB

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