CommandRunnerTest.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 raising an error
  191. *
  192. * @return void
  193. */
  194. public function testRunValidCommandWithAbort()
  195. {
  196. $app = $this->getMockBuilder(BaseApplication::class)
  197. ->setMethods(['middleware', 'bootstrap', 'console'])
  198. ->setConstructorArgs([$this->config])
  199. ->getMock();
  200. $commands = new CommandCollection(['failure' => SampleShell::class]);
  201. $app->method('console')->will($this->returnValue($commands));
  202. $output = new ConsoleOutput();
  203. $runner = new CommandRunner($app, 'cake');
  204. $result = $runner->run(['cake', 'failure', 'with_abort'], $this->getMockIo($output));
  205. $this->assertSame(Shell::CODE_ERROR, $result);
  206. }
  207. /**
  208. * Test returning a non-zero value
  209. *
  210. * @return void
  211. */
  212. public function testRunValidCommandReturnInteger()
  213. {
  214. $app = $this->getMockBuilder(BaseApplication::class)
  215. ->setMethods(['middleware', 'bootstrap', 'console'])
  216. ->setConstructorArgs([$this->config])
  217. ->getMock();
  218. $commands = new CommandCollection(['failure' => SampleShell::class]);
  219. $app->method('console')->will($this->returnValue($commands));
  220. $output = new ConsoleOutput();
  221. $runner = new CommandRunner($app, 'cake');
  222. $result = $runner->run(['cake', 'failure', 'returnValue'], $this->getMockIo($output));
  223. $this->assertSame(99, $result);
  224. }
  225. /**
  226. * Ensure that the root command name propagates to shell help
  227. *
  228. * @return void
  229. */
  230. public function testRunRootNamePropagates()
  231. {
  232. $app = $this->getMockBuilder(BaseApplication::class)
  233. ->setMethods(['middleware', 'bootstrap', 'console'])
  234. ->setConstructorArgs([$this->config])
  235. ->getMock();
  236. $commands = new CommandCollection(['sample' => SampleShell::class]);
  237. $app->method('console')->will($this->returnValue($commands));
  238. $output = new ConsoleOutput();
  239. $runner = new CommandRunner($app, 'widget');
  240. $runner->run(['widget', 'sample', '-h'], $this->getMockIo($output));
  241. $result = implode("\n", $output->messages());
  242. $this->assertContains('widget sample [-h]', $result);
  243. $this->assertNotContains('cake sample [-h]', $result);
  244. }
  245. /**
  246. * Test that run() fires off the buildCommands event.
  247. *
  248. * @return void
  249. */
  250. public function testRunTriggersBuildCommandsEvent()
  251. {
  252. $app = $this->getMockBuilder(BaseApplication::class)
  253. ->setMethods(['middleware', 'bootstrap'])
  254. ->setConstructorArgs([$this->config])
  255. ->getMock();
  256. $output = new ConsoleOutput();
  257. $runner = new CommandRunner($app, 'cake');
  258. $runner->getEventManager()->on('Console.buildCommands', function ($event, $commands) {
  259. $this->assertInstanceOf(CommandCollection::class, $commands);
  260. $this->eventTriggered = true;
  261. });
  262. $result = $runner->run(['cake', '--version'], $this->getMockIo($output));
  263. $this->assertTrue($this->eventTriggered, 'Should have triggered event.');
  264. }
  265. protected function getMockIo($output)
  266. {
  267. $io = $this->getMockBuilder(ConsoleIo::class)
  268. ->setConstructorArgs([$output, $output, null, null])
  269. ->setMethods(['in'])
  270. ->getMock();
  271. return $io;
  272. }
  273. }