CommandRunnerTest.php 15 KB

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