CommandRunnerTest.php 13 KB

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