CommandRunnerTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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\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. $this->expectException(\RuntimeException::class);
  117. $this->expectExceptionMessage('Unknown command `cake nope`. Run `cake --help` to get the list of valid commands.');
  118. $app = $this->getMockBuilder(BaseApplication::class)
  119. ->setMethods(['middleware', 'bootstrap', 'routes'])
  120. ->setConstructorArgs([$this->config])
  121. ->getMock();
  122. $runner = new CommandRunner($app);
  123. $runner->run(['cake', 'nope', 'nope', 'nope']);
  124. }
  125. /**
  126. * Test using `cake --help` invokes the help command
  127. *
  128. * @return void
  129. */
  130. public function testRunHelpLongOption()
  131. {
  132. $app = $this->getMockBuilder(BaseApplication::class)
  133. ->setMethods(['middleware', 'bootstrap', 'routes'])
  134. ->setConstructorArgs([$this->config])
  135. ->getMock();
  136. $output = new ConsoleOutput();
  137. $runner = new CommandRunner($app, 'cake');
  138. $result = $runner->run(['cake', '--help'], $this->getMockIo($output));
  139. $this->assertSame(0, $result);
  140. $messages = implode("\n", $output->messages());
  141. $this->assertStringContainsString('Current Paths', $messages);
  142. $this->assertStringContainsString('- i18n', $messages);
  143. $this->assertStringContainsString('Available Commands', $messages);
  144. }
  145. /**
  146. * Test using `cake -h` invokes the help command
  147. *
  148. * @return void
  149. */
  150. public function testRunHelpShortOption()
  151. {
  152. $app = $this->getMockBuilder(BaseApplication::class)
  153. ->setMethods(['middleware', 'bootstrap', 'routes'])
  154. ->setConstructorArgs([$this->config])
  155. ->getMock();
  156. $output = new ConsoleOutput();
  157. $runner = new CommandRunner($app, 'cake');
  158. $result = $runner->run(['cake', '-h'], $this->getMockIo($output));
  159. $this->assertSame(0, $result);
  160. $messages = implode("\n", $output->messages());
  161. $this->assertStringContainsString('- i18n', $messages);
  162. $this->assertStringContainsString('Available Commands', $messages);
  163. }
  164. /**
  165. * Test that no command outputs the command list
  166. *
  167. * @return void
  168. */
  169. public function testRunNoCommand()
  170. {
  171. $app = $this->getMockBuilder(BaseApplication::class)
  172. ->setMethods(['middleware', 'bootstrap', 'routes'])
  173. ->setConstructorArgs([$this->config])
  174. ->getMock();
  175. $output = new ConsoleOutput();
  176. $runner = new CommandRunner($app);
  177. $result = $runner->run(['cake'], $this->getMockIo($output));
  178. $this->assertSame(0, $result, 'help output is success.');
  179. $messages = implode("\n", $output->messages());
  180. $this->assertStringContainsString('No command provided. Choose one of the available commands', $messages);
  181. $this->assertStringContainsString('- i18n', $messages);
  182. $this->assertStringContainsString('Available Commands', $messages);
  183. }
  184. /**
  185. * Test using `cake --verson` invokes the version command
  186. *
  187. * @return void
  188. */
  189. public function testRunVersionAlias()
  190. {
  191. $app = $this->getMockBuilder(BaseApplication::class)
  192. ->setMethods(['middleware', 'bootstrap', 'routes'])
  193. ->setConstructorArgs([$this->config])
  194. ->getMock();
  195. $output = new ConsoleOutput();
  196. $runner = new CommandRunner($app, 'cake');
  197. $result = $runner->run(['cake', '--version'], $this->getMockIo($output));
  198. $this->assertStringContainsString(Configure::version(), $output->messages()[0]);
  199. }
  200. /**
  201. * Test running a valid command
  202. *
  203. * @return void
  204. */
  205. public function testRunValidCommand()
  206. {
  207. $app = $this->getMockBuilder(BaseApplication::class)
  208. ->setMethods(['middleware', 'bootstrap', 'routes'])
  209. ->setConstructorArgs([$this->config])
  210. ->getMock();
  211. $output = new ConsoleOutput();
  212. $runner = new CommandRunner($app, 'cake');
  213. $result = $runner->run(['cake', 'routes'], $this->getMockIo($output));
  214. $this->assertSame(Shell::CODE_SUCCESS, $result);
  215. $contents = implode("\n", $output->messages());
  216. $this->assertStringContainsString('URI template', $contents);
  217. }
  218. /**
  219. * Test running a valid command and that backwards compatible
  220. * inflection is hooked up.
  221. *
  222. * @return void
  223. */
  224. public function testRunValidCommandInflection()
  225. {
  226. $app = $this->getMockBuilder(BaseApplication::class)
  227. ->setMethods(['middleware', 'bootstrap', 'routes'])
  228. ->setConstructorArgs([$this->config])
  229. ->getMock();
  230. $output = new ConsoleOutput();
  231. $runner = new CommandRunner($app, 'cake');
  232. $result = $runner->run(['cake', 'schema_cache', 'build'], $this->getMockIo($output));
  233. $this->assertSame(Shell::CODE_SUCCESS, $result);
  234. $contents = implode("\n", $output->messages());
  235. $this->assertStringContainsString('Cache', $contents);
  236. }
  237. /**
  238. * Test running a valid raising an error
  239. *
  240. * @return void
  241. */
  242. public function testRunValidCommandWithAbort()
  243. {
  244. $app = $this->makeAppWithCommands(['failure' => SampleShell::class]);
  245. $output = new ConsoleOutput();
  246. $runner = new CommandRunner($app, 'cake');
  247. $result = $runner->run(['cake', 'failure', 'with_abort'], $this->getMockIo($output));
  248. $this->assertSame(Shell::CODE_ERROR, $result);
  249. }
  250. /**
  251. * Test returning a non-zero value
  252. *
  253. * @return void
  254. */
  255. public function testRunValidCommandReturnInteger()
  256. {
  257. $app = $this->makeAppWithCommands(['failure' => SampleShell::class]);
  258. $output = new ConsoleOutput();
  259. $runner = new CommandRunner($app, 'cake');
  260. $result = $runner->run(['cake', 'failure', 'returnValue'], $this->getMockIo($output));
  261. $this->assertSame(99, $result);
  262. }
  263. /**
  264. * Ensure that the root command name propagates to shell help
  265. *
  266. * @return void
  267. */
  268. public function testRunRootNamePropagates()
  269. {
  270. $app = $this->makeAppWithCommands(['sample' => SampleShell::class]);
  271. $output = new ConsoleOutput();
  272. $runner = new CommandRunner($app, 'widget');
  273. $runner->run(['widget', 'sample', '-h'], $this->getMockIo($output));
  274. $result = implode("\n", $output->messages());
  275. $this->assertStringContainsString('widget sample [-h]', $result);
  276. $this->assertStringNotContainsString('cake sample [-h]', $result);
  277. }
  278. /**
  279. * Test running a valid command
  280. *
  281. * @return void
  282. */
  283. public function testRunValidCommandClass()
  284. {
  285. $app = $this->makeAppWithCommands(['ex' => DemoCommand::class]);
  286. $output = new ConsoleOutput();
  287. $runner = new CommandRunner($app, 'cake');
  288. $result = $runner->run(['cake', 'ex'], $this->getMockIo($output));
  289. $this->assertSame(Shell::CODE_SUCCESS, $result);
  290. $messages = implode("\n", $output->messages());
  291. $this->assertStringContainsString('Demo Command!', $messages);
  292. }
  293. /**
  294. * Test running a valid command with spaces in the name
  295. *
  296. * @return void
  297. */
  298. public function testRunValidCommandSubcommandName()
  299. {
  300. $app = $this->makeAppWithCommands([
  301. 'tool build' => DemoCommand::class,
  302. 'tool' => AbortCommand::class,
  303. ]);
  304. $output = new ConsoleOutput();
  305. $runner = new CommandRunner($app, 'cake');
  306. $result = $runner->run(['cake', 'tool', 'build'], $this->getMockIo($output));
  307. $this->assertSame(Shell::CODE_SUCCESS, $result);
  308. $messages = implode("\n", $output->messages());
  309. $this->assertStringContainsString('Demo Command!', $messages);
  310. }
  311. /**
  312. * Test running a valid command with spaces in the name
  313. *
  314. * @return void
  315. */
  316. public function testRunValidCommandNestedName()
  317. {
  318. $app = $this->makeAppWithCommands([
  319. 'tool build assets' => DemoCommand::class,
  320. 'tool' => AbortCommand::class,
  321. ]);
  322. $output = new ConsoleOutput();
  323. $runner = new CommandRunner($app, 'cake');
  324. $result = $runner->run(['cake', 'tool', 'build', 'assets'], $this->getMockIo($output));
  325. $this->assertSame(Shell::CODE_SUCCESS, $result);
  326. $messages = implode("\n", $output->messages());
  327. $this->assertStringContainsString('Demo Command!', $messages);
  328. }
  329. /**
  330. * Test using a custom factory
  331. *
  332. * @return void
  333. */
  334. public function testRunWithCustomFactory()
  335. {
  336. $output = new ConsoleOutput();
  337. $io = $this->getMockIo($output);
  338. $factory = $this->createMock(CommandFactoryInterface::class);
  339. $factory->expects($this->once())
  340. ->method('create')
  341. ->with(DemoCommand::class)
  342. ->willReturn(new DemoCommand());
  343. $app = $this->makeAppWithCommands(['ex' => DemoCommand::class]);
  344. $runner = new CommandRunner($app, 'cake', $factory);
  345. $result = $runner->run(['cake', 'ex'], $io);
  346. $this->assertSame(Shell::CODE_SUCCESS, $result);
  347. $messages = implode("\n", $output->messages());
  348. $this->assertStringContainsString('Demo Command!', $messages);
  349. }
  350. /**
  351. * Test running a command class' help
  352. *
  353. * @return void
  354. */
  355. public function testRunValidCommandClassHelp()
  356. {
  357. $app = $this->makeAppWithCommands(['ex' => DemoCommand::class]);
  358. $output = new ConsoleOutput();
  359. $runner = new CommandRunner($app, 'cake');
  360. $result = $runner->run(['cake', 'ex', '-h'], $this->getMockIo($output));
  361. $this->assertSame(Shell::CODE_SUCCESS, $result);
  362. $messages = implode("\n", $output->messages());
  363. $this->assertStringContainsString("\ncake ex [-h]", $messages);
  364. $this->assertStringNotContainsString('Demo Command!', $messages);
  365. }
  366. /**
  367. * Test that run() fires off the buildCommands event.
  368. *
  369. * @return void
  370. */
  371. public function testRunTriggersBuildCommandsEvent()
  372. {
  373. $app = $this->getMockBuilder(BaseApplication::class)
  374. ->setMethods(['middleware', 'bootstrap', 'routes'])
  375. ->setConstructorArgs([$this->config])
  376. ->getMock();
  377. $output = new ConsoleOutput();
  378. $runner = new CommandRunner($app, 'cake');
  379. $runner->getEventManager()->on('Console.buildCommands', function ($event, $commands) {
  380. $this->assertInstanceOf(CommandCollection::class, $commands);
  381. $this->eventTriggered = true;
  382. });
  383. $result = $runner->run(['cake', '--version'], $this->getMockIo($output));
  384. $this->assertTrue($this->eventTriggered, 'Should have triggered event.');
  385. }
  386. /**
  387. * Test that run calls plugin hook methods
  388. *
  389. * @return void
  390. */
  391. public function testRunCallsPluginHookMethods()
  392. {
  393. $app = $this->getMockBuilder(BaseApplication::class)
  394. ->setMethods([
  395. 'middleware', 'bootstrap', 'routes',
  396. 'pluginBootstrap', 'pluginConsole', 'pluginRoutes',
  397. ])
  398. ->setConstructorArgs([$this->config])
  399. ->getMock();
  400. $app->expects($this->at(0))->method('bootstrap');
  401. $app->expects($this->at(1))->method('pluginBootstrap');
  402. $commands = new CommandCollection();
  403. $app->expects($this->at(2))
  404. ->method('pluginConsole')
  405. ->with($this->isinstanceOf(CommandCollection::class))
  406. ->will($this->returnCallback(function ($commands) {
  407. return $commands;
  408. }));
  409. $app->expects($this->at(3))->method('routes');
  410. $app->expects($this->at(4))->method('pluginRoutes');
  411. $output = new ConsoleOutput();
  412. $runner = new CommandRunner($app, 'cake');
  413. $result = $runner->run(['cake', '--version'], $this->getMockIo($output));
  414. $this->assertStringContainsString(Configure::version(), $output->messages()[0]);
  415. }
  416. /**
  417. * Test that run() loads routing.
  418. *
  419. * @return void
  420. */
  421. public function testRunLoadsRoutes()
  422. {
  423. $app = $this->getMockBuilder(BaseApplication::class)
  424. ->setMethods(['middleware', 'bootstrap'])
  425. ->setConstructorArgs([TEST_APP . 'config' . DS])
  426. ->getMock();
  427. $output = new ConsoleOutput();
  428. $runner = new CommandRunner($app, 'cake');
  429. $runner->run(['cake', '--version'], $this->getMockIo($output));
  430. $this->assertGreaterThan(2, count(Router::getRouteCollection()->routes()));
  431. }
  432. protected function makeAppWithCommands($commands)
  433. {
  434. $app = $this->getMockBuilder(BaseApplication::class)
  435. ->setMethods(['middleware', 'bootstrap', 'console', 'routes'])
  436. ->setConstructorArgs([$this->config])
  437. ->getMock();
  438. $collection = new CommandCollection($commands);
  439. $app->method('console')->will($this->returnValue($collection));
  440. return $app;
  441. }
  442. protected function getMockIo($output)
  443. {
  444. $io = $this->getMockBuilder(ConsoleIo::class)
  445. ->setConstructorArgs([$output, $output, null, null])
  446. ->setMethods(['in'])
  447. ->getMock();
  448. return $io;
  449. }
  450. }