CommandRunnerTest.php 17 KB

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