CommandRunnerTest.php 11 KB

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