CommandRunnerTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 using `cake --verson` invokes the version command
  137. *
  138. * @return void
  139. */
  140. public function testRunVersionAlias()
  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, 'cake');
  148. $result = $runner->run(['cake', '--version'], $this->getMockIo($output));
  149. $this->assertContains(Configure::version(), $output->messages()[0]);
  150. }
  151. /**
  152. * Test running a valid command
  153. *
  154. * @return void
  155. */
  156. public function testRunValidCommand()
  157. {
  158. $app = $this->getMockBuilder(BaseApplication::class)
  159. ->setMethods(['middleware', 'bootstrap'])
  160. ->setConstructorArgs([$this->config])
  161. ->getMock();
  162. $output = new ConsoleOutput();
  163. $runner = new CommandRunner($app, 'cake');
  164. $result = $runner->run(['cake', 'routes'], $this->getMockIo($output));
  165. $this->assertSame(Shell::CODE_SUCCESS, $result);
  166. $contents = implode("\n", $output->messages());
  167. $this->assertContains('URI template', $contents);
  168. }
  169. /**
  170. * Test running a valid raising an error
  171. *
  172. * @return void
  173. */
  174. public function testRunValidCommandWithAbort()
  175. {
  176. $app = $this->getMockBuilder(BaseApplication::class)
  177. ->setMethods(['middleware', 'bootstrap', 'console'])
  178. ->setConstructorArgs([$this->config])
  179. ->getMock();
  180. $commands = new CommandCollection(['failure' => SampleShell::class]);
  181. $app->method('console')->will($this->returnValue($commands));
  182. $output = new ConsoleOutput();
  183. $runner = new CommandRunner($app, 'cake');
  184. $result = $runner->run(['cake', 'failure', 'with_abort'], $this->getMockIo($output));
  185. $this->assertSame(Shell::CODE_ERROR, $result);
  186. }
  187. /**
  188. * Test returning a non-zero value
  189. *
  190. * @return void
  191. */
  192. public function testRunValidCommandReturnInteger()
  193. {
  194. $app = $this->getMockBuilder(BaseApplication::class)
  195. ->setMethods(['middleware', 'bootstrap', 'console'])
  196. ->setConstructorArgs([$this->config])
  197. ->getMock();
  198. $commands = new CommandCollection(['failure' => SampleShell::class]);
  199. $app->method('console')->will($this->returnValue($commands));
  200. $output = new ConsoleOutput();
  201. $runner = new CommandRunner($app, 'cake');
  202. $result = $runner->run(['cake', 'failure', 'returnValue'], $this->getMockIo($output));
  203. $this->assertSame(99, $result);
  204. }
  205. /**
  206. * Ensure that the root command name propagates to shell help
  207. *
  208. * @return void
  209. */
  210. public function testRunRootNamePropagates()
  211. {
  212. $app = $this->getMockBuilder(BaseApplication::class)
  213. ->setMethods(['middleware', 'bootstrap', 'console'])
  214. ->setConstructorArgs([$this->config])
  215. ->getMock();
  216. $commands = new CommandCollection(['sample' => SampleShell::class]);
  217. $app->method('console')->will($this->returnValue($commands));
  218. $output = new ConsoleOutput();
  219. $runner = new CommandRunner($app, 'widget');
  220. $runner->run(['widget', 'sample', '-h'], $this->getMockIo($output));
  221. $result = implode("\n", $output->messages());
  222. $this->assertContains('widget sample [-h]', $result);
  223. $this->assertNotContains('cake sample [-h]', $result);
  224. }
  225. /**
  226. * Test that run() fires off the buildCommands event.
  227. *
  228. * @return void
  229. */
  230. public function testRunTriggersBuildCommandsEvent()
  231. {
  232. $app = $this->getMockBuilder(BaseApplication::class)
  233. ->setMethods(['middleware', 'bootstrap'])
  234. ->setConstructorArgs([$this->config])
  235. ->getMock();
  236. $output = new ConsoleOutput();
  237. $runner = new CommandRunner($app, 'cake');
  238. $runner->getEventManager()->on('Console.buildCommands', function ($event, $commands) {
  239. $this->assertInstanceOf(CommandCollection::class, $commands);
  240. $this->eventTriggered = true;
  241. });
  242. $result = $runner->run(['cake', '--version'], $this->getMockIo($output));
  243. $this->assertTrue($this->eventTriggered, 'Should have triggered event.');
  244. }
  245. protected function getMockIo($output)
  246. {
  247. $io = $this->getMockBuilder(ConsoleIo::class)
  248. ->setConstructorArgs([$output, $output, null, null])
  249. ->setMethods(['in'])
  250. ->getMock();
  251. return $io;
  252. }
  253. }