CommandRunnerTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 deprecated method defined in interface
  93. *
  94. * @return void
  95. */
  96. public function testEventManagerCompat()
  97. {
  98. $this->deprecated(function () {
  99. $app = $this->createMock(ConsoleApplicationInterface::class);
  100. $runner = new CommandRunner($app);
  101. $this->assertSame(EventManager::instance(), $runner->eventManager());
  102. });
  103. }
  104. /**
  105. * Test that the console hook not returning a command collection
  106. * raises an error.
  107. *
  108. * @return void
  109. */
  110. public function testRunConsoleHookFailure()
  111. {
  112. $this->expectException(\RuntimeException::class);
  113. $this->expectExceptionMessage('The application\'s `console` method did not return a CommandCollection.');
  114. $app = $this->getMockBuilder(BaseApplication::class)
  115. ->setMethods(['console', 'middleware', 'bootstrap'])
  116. ->setConstructorArgs([$this->config])
  117. ->getMock();
  118. $runner = new CommandRunner($app);
  119. $runner->run(['cake', '-h']);
  120. }
  121. /**
  122. * Test that running with empty argv fails
  123. *
  124. * @return void
  125. */
  126. public function testRunMissingRootCommand()
  127. {
  128. $this->expectException(\RuntimeException::class);
  129. $this->expectExceptionMessage('Cannot run any commands. No arguments received.');
  130. $app = $this->getMockBuilder(BaseApplication::class)
  131. ->setMethods(['middleware', 'bootstrap'])
  132. ->setConstructorArgs([$this->config])
  133. ->getMock();
  134. $runner = new CommandRunner($app);
  135. $runner->run([]);
  136. }
  137. /**
  138. * Test that running an unknown command raises an error.
  139. *
  140. * @return void
  141. */
  142. public function testRunInvalidCommand()
  143. {
  144. $this->expectException(\RuntimeException::class);
  145. $this->expectExceptionMessage('Unknown command `cake nope`. Run `cake --help` to get the list of valid commands.');
  146. $app = $this->getMockBuilder(BaseApplication::class)
  147. ->setMethods(['middleware', 'bootstrap'])
  148. ->setConstructorArgs([$this->config])
  149. ->getMock();
  150. $runner = new CommandRunner($app);
  151. $runner->run(['cake', 'nope', 'nope', 'nope']);
  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'])
  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->assertContains('Current Paths', $messages);
  170. $this->assertContains('- i18n', $messages);
  171. $this->assertContains('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'])
  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->assertContains('- i18n', $messages);
  190. $this->assertContains('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'])
  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->assertContains('No command provided. Choose one of the available commands', $messages);
  209. $this->assertContains('- i18n', $messages);
  210. $this->assertContains('Available Commands', $messages);
  211. }
  212. /**
  213. * Test using `cake --verson` invokes the version command
  214. *
  215. * @return void
  216. */
  217. public function testRunVersionAlias()
  218. {
  219. $app = $this->getMockBuilder(BaseApplication::class)
  220. ->setMethods(['middleware', 'bootstrap'])
  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->assertContains(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'])
  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->assertContains('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'])
  256. ->setConstructorArgs([$this->config])
  257. ->getMock();
  258. $output = new ConsoleOutput();
  259. $runner = new CommandRunner($app, 'cake');
  260. $result = $runner->run(['cake', 'OrmCache', 'build'], $this->getMockIo($output));
  261. $this->assertSame(Shell::CODE_SUCCESS, $result);
  262. $contents = implode("\n", $output->messages());
  263. $this->assertContains('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->assertContains('widget sample [-h]', $result);
  304. $this->assertNotContains('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->assertContains('Demo Command!', $messages);
  320. }
  321. /**
  322. * Test running a command class' help
  323. *
  324. * @return void
  325. */
  326. public function testRunValidCommandClassHelp()
  327. {
  328. $app = $this->makeAppWithCommands(['ex' => DemoCommand::class]);
  329. $output = new ConsoleOutput();
  330. $runner = new CommandRunner($app, 'cake');
  331. $result = $runner->run(['cake', 'ex', '-h'], $this->getMockIo($output));
  332. $this->assertSame(Shell::CODE_SUCCESS, $result);
  333. $messages = implode("\n", $output->messages());
  334. $this->assertContains("\ncake ex [-h]", $messages);
  335. $this->assertNotContains('Demo Command!', $messages);
  336. }
  337. /**
  338. * Test that run() fires off the buildCommands event.
  339. *
  340. * @return void
  341. */
  342. public function testRunTriggersBuildCommandsEvent()
  343. {
  344. $app = $this->getMockBuilder(BaseApplication::class)
  345. ->setMethods(['middleware', 'bootstrap'])
  346. ->setConstructorArgs([$this->config])
  347. ->getMock();
  348. $output = new ConsoleOutput();
  349. $runner = new CommandRunner($app, 'cake');
  350. $runner->getEventManager()->on('Console.buildCommands', function ($event, $commands) {
  351. $this->assertInstanceOf(CommandCollection::class, $commands);
  352. $this->eventTriggered = true;
  353. });
  354. $result = $runner->run(['cake', '--version'], $this->getMockIo($output));
  355. $this->assertTrue($this->eventTriggered, 'Should have triggered event.');
  356. }
  357. /**
  358. * Test that run calls plugin hook methods
  359. *
  360. * @return void
  361. */
  362. public function testRunCallsPluginHookMethods()
  363. {
  364. $app = $this->getMockBuilder(BaseApplication::class)
  365. ->setMethods(['middleware', 'bootstrap', 'pluginBootstrap', 'pluginEvents', 'pluginConsole'])
  366. ->setConstructorArgs([$this->config])
  367. ->getMock();
  368. $app->expects($this->at(0))->method('bootstrap');
  369. $app->expects($this->at(1))->method('pluginBootstrap');
  370. $commands = new CommandCollection();
  371. $app->expects($this->at(2))
  372. ->method('pluginConsole')
  373. ->with($this->isinstanceOf(CommandCollection::class))
  374. ->will($this->returnCallback(function ($commands) {
  375. return $commands;
  376. }));
  377. $output = new ConsoleOutput();
  378. $runner = new CommandRunner($app, 'cake');
  379. $result = $runner->run(['cake', '--version'], $this->getMockIo($output));
  380. $this->assertContains(Configure::version(), $output->messages()[0]);
  381. }
  382. protected function makeAppWithCommands($commands)
  383. {
  384. $app = $this->getMockBuilder(BaseApplication::class)
  385. ->setMethods(['middleware', 'bootstrap', 'console'])
  386. ->setConstructorArgs([$this->config])
  387. ->getMock();
  388. $collection = new CommandCollection($commands);
  389. $app->method('console')->will($this->returnValue($collection));
  390. return $app;
  391. }
  392. protected function getMockIo($output)
  393. {
  394. $io = $this->getMockBuilder(ConsoleIo::class)
  395. ->setConstructorArgs([$output, $output, null, null])
  396. ->setMethods(['in'])
  397. ->getMock();
  398. return $io;
  399. }
  400. }