CommandRunnerTest.php 17 KB

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