CommandRunnerTest.php 11 KB

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