CommandRunnerTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  12. * @link http://cakephp.org CakePHP(tm) Project
  13. * @since 3.5.0
  14. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Console;
  17. use Cake\Console\CommandCollection;
  18. use Cake\Console\CommandFactoryInterface;
  19. use Cake\Console\CommandRunner;
  20. use Cake\Console\ConsoleIo;
  21. use Cake\Console\Shell;
  22. use Cake\Core\Configure;
  23. use Cake\Core\ConsoleApplicationInterface;
  24. use Cake\Event\EventManager;
  25. use Cake\Http\BaseApplication;
  26. use Cake\Routing\Router;
  27. use Cake\TestSuite\Stub\ConsoleOutput;
  28. use Cake\TestSuite\TestCase;
  29. use InvalidArgumentException;
  30. use TestApp\Command\AbortCommand;
  31. use TestApp\Command\DemoCommand;
  32. use TestApp\Shell\SampleShell;
  33. /**
  34. * Test case for the CommandCollection
  35. */
  36. class CommandRunnerTest extends TestCase
  37. {
  38. /**
  39. * Tracking property for event triggering
  40. *
  41. * @var bool
  42. */
  43. protected $eventTriggered = false;
  44. /**
  45. * setup
  46. *
  47. * @return void
  48. */
  49. public function setUp(): void
  50. {
  51. parent::setUp();
  52. Configure::write('App.namespace', 'TestApp');
  53. $this->config = dirname(dirname(__DIR__));
  54. }
  55. /**
  56. * test event manager proxies to the application.
  57. *
  58. * @return void
  59. */
  60. public function testEventManagerProxies()
  61. {
  62. $app = $this->getMockForAbstractClass(
  63. BaseApplication::class,
  64. [$this->config]
  65. );
  66. $runner = new CommandRunner($app);
  67. $this->assertSame($app->getEventManager(), $runner->getEventManager());
  68. }
  69. /**
  70. * test event manager cannot be set on applications without events.
  71. *
  72. * @return void
  73. */
  74. public function testGetEventManagerNonEventedApplication()
  75. {
  76. $app = $this->createMock(ConsoleApplicationInterface::class);
  77. $runner = new CommandRunner($app);
  78. $this->assertSame(EventManager::instance(), $runner->getEventManager());
  79. }
  80. /**
  81. * test event manager cannot be set on applications without events.
  82. *
  83. * @return void
  84. */
  85. public function testSetEventManagerNonEventedApplication()
  86. {
  87. $this->expectException(InvalidArgumentException::class);
  88. $app = $this->createMock(ConsoleApplicationInterface::class);
  89. $events = new EventManager();
  90. $runner = new CommandRunner($app);
  91. $runner->setEventManager($events);
  92. }
  93. /**
  94. * Test that running with empty argv fails
  95. *
  96. * @return void
  97. */
  98. public function testRunMissingRootCommand()
  99. {
  100. $this->expectException(\RuntimeException::class);
  101. $this->expectExceptionMessage('Cannot run any commands. No arguments received.');
  102. $app = $this->getMockBuilder(BaseApplication::class)
  103. ->setMethods(['middleware', 'bootstrap', 'routes'])
  104. ->setConstructorArgs([$this->config])
  105. ->getMock();
  106. $runner = new CommandRunner($app);
  107. $runner->run([]);
  108. }
  109. /**
  110. * Test that running an unknown command raises an error.
  111. *
  112. * @return void
  113. */
  114. public function testRunInvalidCommand()
  115. {
  116. $app = $this->getMockBuilder(BaseApplication::class)
  117. ->setMethods(['middleware', 'bootstrap', 'routes'])
  118. ->setConstructorArgs([$this->config])
  119. ->getMock();
  120. $output = new ConsoleOutput();
  121. $runner = new CommandRunner($app);
  122. $runner->run(['cake', 'nope', 'nope', 'nope'], $this->getMockIo($output));
  123. $messages = implode("\n", $output->messages());
  124. $this->assertStringContainsString(
  125. 'Unknown command `cake nope`. Run `cake --help` to get the list of commands.',
  126. $messages
  127. );
  128. }
  129. /**
  130. * Test that running an unknown command gives suggestions.
  131. *
  132. * @return void
  133. */
  134. public function testRunInvalidCommandSuggestion()
  135. {
  136. $app = $this->getMockBuilder(BaseApplication::class)
  137. ->setMethods(['middleware', 'bootstrap', 'routes'])
  138. ->setConstructorArgs([$this->config])
  139. ->getMock();
  140. $output = new ConsoleOutput();
  141. $runner = new CommandRunner($app);
  142. $runner->run(['cake', 'cache'], $this->getMockIo($output));
  143. $messages = implode("\n", $output->messages());
  144. $this->assertStringContainsString(
  145. "Did you mean: `cache clear`?\n" .
  146. "\n" .
  147. "Other valid choices:\n" .
  148. "\n" .
  149. "- help",
  150. $messages
  151. );
  152. }
  153. /**
  154. * Test using `cake --help` invokes the help command
  155. *
  156. * @return void
  157. */
  158. public function testRunHelpLongOption()
  159. {
  160. $app = $this->getMockBuilder(BaseApplication::class)
  161. ->setMethods(['middleware', 'bootstrap', 'routes'])
  162. ->setConstructorArgs([$this->config])
  163. ->getMock();
  164. $output = new ConsoleOutput();
  165. $runner = new CommandRunner($app, 'cake');
  166. $result = $runner->run(['cake', '--help'], $this->getMockIo($output));
  167. $this->assertSame(0, $result);
  168. $messages = implode("\n", $output->messages());
  169. $this->assertStringContainsString('Current Paths', $messages);
  170. $this->assertStringContainsString('- i18n', $messages);
  171. $this->assertStringContainsString('Available Commands', $messages);
  172. }
  173. /**
  174. * Test using `cake -h` invokes the help command
  175. *
  176. * @return void
  177. */
  178. public function testRunHelpShortOption()
  179. {
  180. $app = $this->getMockBuilder(BaseApplication::class)
  181. ->setMethods(['middleware', 'bootstrap', 'routes'])
  182. ->setConstructorArgs([$this->config])
  183. ->getMock();
  184. $output = new ConsoleOutput();
  185. $runner = new CommandRunner($app, 'cake');
  186. $result = $runner->run(['cake', '-h'], $this->getMockIo($output));
  187. $this->assertSame(0, $result);
  188. $messages = implode("\n", $output->messages());
  189. $this->assertStringContainsString('- i18n', $messages);
  190. $this->assertStringContainsString('Available Commands', $messages);
  191. }
  192. /**
  193. * Test that no command outputs the command list
  194. *
  195. * @return void
  196. */
  197. public function testRunNoCommand()
  198. {
  199. $app = $this->getMockBuilder(BaseApplication::class)
  200. ->setMethods(['middleware', 'bootstrap', 'routes'])
  201. ->setConstructorArgs([$this->config])
  202. ->getMock();
  203. $output = new ConsoleOutput();
  204. $runner = new CommandRunner($app);
  205. $result = $runner->run(['cake'], $this->getMockIo($output));
  206. $this->assertSame(0, $result, 'help output is success.');
  207. $messages = implode("\n", $output->messages());
  208. $this->assertStringContainsString('No command provided. Choose one of the available commands', $messages);
  209. $this->assertStringContainsString('- i18n', $messages);
  210. $this->assertStringContainsString('Available Commands', $messages);
  211. }
  212. /**
  213. * Test using `cake --version` invokes the version command
  214. *
  215. * @return void
  216. */
  217. public function testRunVersionAlias()
  218. {
  219. $app = $this->getMockBuilder(BaseApplication::class)
  220. ->setMethods(['middleware', 'bootstrap', 'routes'])
  221. ->setConstructorArgs([$this->config])
  222. ->getMock();
  223. $output = new ConsoleOutput();
  224. $runner = new CommandRunner($app, 'cake');
  225. $result = $runner->run(['cake', '--version'], $this->getMockIo($output));
  226. $this->assertStringContainsString(Configure::version(), $output->messages()[0]);
  227. }
  228. /**
  229. * Test running a valid command
  230. *
  231. * @return void
  232. */
  233. public function testRunValidCommand()
  234. {
  235. $app = $this->getMockBuilder(BaseApplication::class)
  236. ->setMethods(['middleware', 'bootstrap', 'routes'])
  237. ->setConstructorArgs([$this->config])
  238. ->getMock();
  239. $output = new ConsoleOutput();
  240. $runner = new CommandRunner($app, 'cake');
  241. $result = $runner->run(['cake', 'routes'], $this->getMockIo($output));
  242. $this->assertSame(Shell::CODE_SUCCESS, $result);
  243. $contents = implode("\n", $output->messages());
  244. $this->assertStringContainsString('URI template', $contents);
  245. }
  246. /**
  247. * Test running a valid command and that backwards compatible
  248. * inflection is hooked up.
  249. *
  250. * @return void
  251. */
  252. public function testRunValidCommandInflection()
  253. {
  254. $app = $this->getMockBuilder(BaseApplication::class)
  255. ->setMethods(['middleware', 'bootstrap', 'routes'])
  256. ->setConstructorArgs([$this->config])
  257. ->getMock();
  258. $output = new ConsoleOutput();
  259. $runner = new CommandRunner($app, 'cake');
  260. $result = $runner->run(['cake', 'schema_cache', 'build'], $this->getMockIo($output));
  261. $this->assertSame(Shell::CODE_SUCCESS, $result);
  262. $contents = implode("\n", $output->messages());
  263. $this->assertStringContainsString('Cache', $contents);
  264. }
  265. /**
  266. * Test running a valid raising an error
  267. *
  268. * @return void
  269. */
  270. public function testRunValidCommandWithAbort()
  271. {
  272. $app = $this->makeAppWithCommands(['failure' => SampleShell::class]);
  273. $output = new ConsoleOutput();
  274. $runner = new CommandRunner($app, 'cake');
  275. $result = $runner->run(['cake', 'failure', 'with_abort'], $this->getMockIo($output));
  276. $this->assertSame(Shell::CODE_ERROR, $result);
  277. }
  278. /**
  279. * Test returning a non-zero value
  280. *
  281. * @return void
  282. */
  283. public function testRunValidCommandReturnInteger()
  284. {
  285. $app = $this->makeAppWithCommands(['failure' => SampleShell::class]);
  286. $output = new ConsoleOutput();
  287. $runner = new CommandRunner($app, 'cake');
  288. $result = $runner->run(['cake', 'failure', 'returnValue'], $this->getMockIo($output));
  289. $this->assertSame(99, $result);
  290. }
  291. /**
  292. * Ensure that the root command name propagates to shell help
  293. *
  294. * @return void
  295. */
  296. public function testRunRootNamePropagates()
  297. {
  298. $app = $this->makeAppWithCommands(['sample' => SampleShell::class]);
  299. $output = new ConsoleOutput();
  300. $runner = new CommandRunner($app, 'widget');
  301. $runner->run(['widget', 'sample', '-h'], $this->getMockIo($output));
  302. $result = implode("\n", $output->messages());
  303. $this->assertStringContainsString('widget sample [-h]', $result);
  304. $this->assertStringNotContainsString('cake sample [-h]', $result);
  305. }
  306. /**
  307. * Test running a valid command
  308. *
  309. * @return void
  310. */
  311. public function testRunValidCommandClass()
  312. {
  313. $app = $this->makeAppWithCommands(['ex' => DemoCommand::class]);
  314. $output = new ConsoleOutput();
  315. $runner = new CommandRunner($app, 'cake');
  316. $result = $runner->run(['cake', 'ex'], $this->getMockIo($output));
  317. $this->assertSame(Shell::CODE_SUCCESS, $result);
  318. $messages = implode("\n", $output->messages());
  319. $this->assertStringContainsString('Demo Command!', $messages);
  320. }
  321. /**
  322. * Test running a valid command with spaces in the name
  323. *
  324. * @return void
  325. */
  326. public function testRunValidCommandSubcommandName()
  327. {
  328. $app = $this->makeAppWithCommands([
  329. 'tool build' => DemoCommand::class,
  330. 'tool' => AbortCommand::class,
  331. ]);
  332. $output = new ConsoleOutput();
  333. $runner = new CommandRunner($app, 'cake');
  334. $result = $runner->run(['cake', 'tool', 'build'], $this->getMockIo($output));
  335. $this->assertSame(Shell::CODE_SUCCESS, $result);
  336. $messages = implode("\n", $output->messages());
  337. $this->assertStringContainsString('Demo Command!', $messages);
  338. }
  339. /**
  340. * Test running a valid command with spaces in the name
  341. *
  342. * @return void
  343. */
  344. public function testRunValidCommandNestedName()
  345. {
  346. $app = $this->makeAppWithCommands([
  347. 'tool build assets' => DemoCommand::class,
  348. 'tool' => AbortCommand::class,
  349. ]);
  350. $output = new ConsoleOutput();
  351. $runner = new CommandRunner($app, 'cake');
  352. $result = $runner->run(['cake', 'tool', 'build', 'assets'], $this->getMockIo($output));
  353. $this->assertSame(Shell::CODE_SUCCESS, $result);
  354. $messages = implode("\n", $output->messages());
  355. $this->assertStringContainsString('Demo Command!', $messages);
  356. }
  357. /**
  358. * Test using a custom factory
  359. *
  360. * @return void
  361. */
  362. public function testRunWithCustomFactory()
  363. {
  364. $output = new ConsoleOutput();
  365. $io = $this->getMockIo($output);
  366. $factory = $this->createMock(CommandFactoryInterface::class);
  367. $factory->expects($this->once())
  368. ->method('create')
  369. ->with(DemoCommand::class)
  370. ->willReturn(new DemoCommand());
  371. $app = $this->makeAppWithCommands(['ex' => DemoCommand::class]);
  372. $runner = new CommandRunner($app, 'cake', $factory);
  373. $result = $runner->run(['cake', 'ex'], $io);
  374. $this->assertSame(Shell::CODE_SUCCESS, $result);
  375. $messages = implode("\n", $output->messages());
  376. $this->assertStringContainsString('Demo Command!', $messages);
  377. }
  378. /**
  379. * Test running a command class' help
  380. *
  381. * @return void
  382. */
  383. public function testRunValidCommandClassHelp()
  384. {
  385. $app = $this->makeAppWithCommands(['ex' => DemoCommand::class]);
  386. $output = new ConsoleOutput();
  387. $runner = new CommandRunner($app, 'cake');
  388. $result = $runner->run(['cake', 'ex', '-h'], $this->getMockIo($output));
  389. $this->assertSame(Shell::CODE_SUCCESS, $result);
  390. $messages = implode("\n", $output->messages());
  391. $this->assertStringContainsString("\ncake ex [-h]", $messages);
  392. $this->assertStringNotContainsString('Demo Command!', $messages);
  393. }
  394. /**
  395. * Test that run() fires off the buildCommands event.
  396. *
  397. * @return void
  398. */
  399. public function testRunTriggersBuildCommandsEvent()
  400. {
  401. $app = $this->getMockBuilder(BaseApplication::class)
  402. ->setMethods(['middleware', 'bootstrap', 'routes'])
  403. ->setConstructorArgs([$this->config])
  404. ->getMock();
  405. $output = new ConsoleOutput();
  406. $runner = new CommandRunner($app, 'cake');
  407. $runner->getEventManager()->on('Console.buildCommands', function ($event, $commands) {
  408. $this->assertInstanceOf(CommandCollection::class, $commands);
  409. $this->eventTriggered = true;
  410. });
  411. $result = $runner->run(['cake', '--version'], $this->getMockIo($output));
  412. $this->assertTrue($this->eventTriggered, 'Should have triggered event.');
  413. }
  414. /**
  415. * Test that run calls plugin hook methods
  416. *
  417. * @return void
  418. */
  419. public function testRunCallsPluginHookMethods()
  420. {
  421. $app = $this->getMockBuilder(BaseApplication::class)
  422. ->setMethods([
  423. 'middleware', 'bootstrap', 'routes',
  424. 'pluginBootstrap', 'pluginConsole', 'pluginRoutes',
  425. ])
  426. ->setConstructorArgs([$this->config])
  427. ->getMock();
  428. $app->expects($this->at(0))->method('bootstrap');
  429. $app->expects($this->at(1))->method('pluginBootstrap');
  430. $commands = new CommandCollection();
  431. $app->expects($this->at(2))
  432. ->method('pluginConsole')
  433. ->with($this->isinstanceOf(CommandCollection::class))
  434. ->will($this->returnCallback(function ($commands) {
  435. return $commands;
  436. }));
  437. $app->expects($this->at(3))->method('routes');
  438. $app->expects($this->at(4))->method('pluginRoutes');
  439. $output = new ConsoleOutput();
  440. $runner = new CommandRunner($app, 'cake');
  441. $result = $runner->run(['cake', '--version'], $this->getMockIo($output));
  442. $this->assertStringContainsString(Configure::version(), $output->messages()[0]);
  443. }
  444. /**
  445. * Test that run() loads routing.
  446. *
  447. * @return void
  448. */
  449. public function testRunLoadsRoutes()
  450. {
  451. $app = $this->getMockBuilder(BaseApplication::class)
  452. ->setMethods(['middleware', 'bootstrap'])
  453. ->setConstructorArgs([TEST_APP . 'config' . DS])
  454. ->getMock();
  455. $output = new ConsoleOutput();
  456. $runner = new CommandRunner($app, 'cake');
  457. $runner->run(['cake', '--version'], $this->getMockIo($output));
  458. $this->assertGreaterThan(2, count(Router::getRouteCollection()->routes()));
  459. }
  460. protected function makeAppWithCommands($commands)
  461. {
  462. $app = $this->getMockBuilder(BaseApplication::class)
  463. ->setMethods(['middleware', 'bootstrap', 'console', 'routes'])
  464. ->setConstructorArgs([$this->config])
  465. ->getMock();
  466. $collection = new CommandCollection($commands);
  467. $app->method('console')->will($this->returnValue($collection));
  468. return $app;
  469. }
  470. protected function getMockIo($output)
  471. {
  472. $io = $this->getMockBuilder(ConsoleIo::class)
  473. ->setConstructorArgs([$output, $output, null, null])
  474. ->setMethods(['in'])
  475. ->getMock();
  476. return $io;
  477. }
  478. }