CommandRunnerTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 Unknown root command. Was expecting `cake`
  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 root command `bad`. Was expecting `cake`
  85. * @return void
  86. */
  87. public function testRunInvalidRootCommand()
  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(['bad', 'i18n']);
  95. }
  96. /**
  97. * Test that running an unknown command raises an error.
  98. *
  99. * @expectedException \RuntimeException
  100. * @expectedExceptionMessage Unknown command `cake nope`. Run `cake --help` to get the list of valid commands.
  101. * @return void
  102. */
  103. public function testRunInvalidCommand()
  104. {
  105. $app = $this->getMockBuilder(BaseApplication::class)
  106. ->setMethods(['middleware', 'bootstrap'])
  107. ->setConstructorArgs([$this->config])
  108. ->getMock();
  109. $runner = new CommandRunner($app);
  110. $runner->run(['cake', 'nope', 'nope', 'nope']);
  111. }
  112. /**
  113. * Test using `cake --help` invokes the help command
  114. *
  115. * @return void
  116. */
  117. public function testRunHelpLongOption()
  118. {
  119. $app = $this->getMockBuilder(BaseApplication::class)
  120. ->setMethods(['middleware', 'bootstrap'])
  121. ->setConstructorArgs([$this->config])
  122. ->getMock();
  123. $output = new ConsoleOutput();
  124. $runner = new CommandRunner($app, 'cake');
  125. $result = $runner->run(['cake', '--help'], $this->getMockIo($output));
  126. $this->assertSame(0, $result);
  127. $messages = implode("\n", $output->messages());
  128. $this->assertContains('Current Paths', $messages);
  129. $this->assertContains('- i18n', $messages);
  130. $this->assertContains('Available Commands', $messages);
  131. }
  132. /**
  133. * Test using `cake -h` invokes the help command
  134. *
  135. * @return void
  136. */
  137. public function testRunHelpShortOption()
  138. {
  139. $app = $this->getMockBuilder(BaseApplication::class)
  140. ->setMethods(['middleware', 'bootstrap'])
  141. ->setConstructorArgs([$this->config])
  142. ->getMock();
  143. $output = new ConsoleOutput();
  144. $runner = new CommandRunner($app, 'cake');
  145. $result = $runner->run(['cake', '-h'], $this->getMockIo($output));
  146. $this->assertSame(0, $result);
  147. $messages = implode("\n", $output->messages());
  148. $this->assertContains('- i18n', $messages);
  149. $this->assertContains('Available Commands', $messages);
  150. }
  151. /**
  152. * Test using `cake --verson` invokes the version command
  153. *
  154. * @return void
  155. */
  156. public function testRunVersionAlias()
  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', '--version'], $this->getMockIo($output));
  165. $this->assertContains(Configure::version(), $output->messages()[0]);
  166. }
  167. /**
  168. * Test running a valid command
  169. *
  170. * @return void
  171. */
  172. public function testRunValidCommand()
  173. {
  174. $app = $this->getMockBuilder(BaseApplication::class)
  175. ->setMethods(['middleware', 'bootstrap'])
  176. ->setConstructorArgs([$this->config])
  177. ->getMock();
  178. $output = new ConsoleOutput();
  179. $runner = new CommandRunner($app, 'cake');
  180. $result = $runner->run(['cake', 'routes'], $this->getMockIo($output));
  181. $this->assertSame(Shell::CODE_SUCCESS, $result);
  182. $contents = implode("\n", $output->messages());
  183. $this->assertContains('URI template', $contents);
  184. }
  185. /**
  186. * Test running a valid raising an error
  187. *
  188. * @return void
  189. */
  190. public function testRunValidCommandWithAbort()
  191. {
  192. $app = $this->getMockBuilder(BaseApplication::class)
  193. ->setMethods(['middleware', 'bootstrap', 'console'])
  194. ->setConstructorArgs([$this->config])
  195. ->getMock();
  196. $commands = new CommandCollection(['failure' => SampleShell::class]);
  197. $app->method('console')->will($this->returnValue($commands));
  198. $output = new ConsoleOutput();
  199. $runner = new CommandRunner($app, 'cake');
  200. $result = $runner->run(['cake', 'failure', 'with_abort'], $this->getMockIo($output));
  201. $this->assertSame(Shell::CODE_ERROR, $result);
  202. }
  203. /**
  204. * Test returning a non-zero value
  205. *
  206. * @return void
  207. */
  208. public function testRunValidCommandReturnInteger()
  209. {
  210. $app = $this->getMockBuilder(BaseApplication::class)
  211. ->setMethods(['middleware', 'bootstrap', 'console'])
  212. ->setConstructorArgs([$this->config])
  213. ->getMock();
  214. $commands = new CommandCollection(['failure' => SampleShell::class]);
  215. $app->method('console')->will($this->returnValue($commands));
  216. $output = new ConsoleOutput();
  217. $runner = new CommandRunner($app, 'cake');
  218. $result = $runner->run(['cake', 'failure', 'returnValue'], $this->getMockIo($output));
  219. $this->assertSame(99, $result);
  220. }
  221. /**
  222. * Ensure that the root command name propagates to shell help
  223. *
  224. * @return void
  225. */
  226. public function testRunRootNamePropagates()
  227. {
  228. $app = $this->getMockBuilder(BaseApplication::class)
  229. ->setMethods(['middleware', 'bootstrap', 'console'])
  230. ->setConstructorArgs([$this->config])
  231. ->getMock();
  232. $commands = new CommandCollection(['sample' => SampleShell::class]);
  233. $app->method('console')->will($this->returnValue($commands));
  234. $output = new ConsoleOutput();
  235. $runner = new CommandRunner($app, 'widget');
  236. $runner->run(['widget', 'sample', '-h'], $this->getMockIo($output));
  237. $result = implode("\n", $output->messages());
  238. $this->assertContains('widget sample [-h]', $result);
  239. $this->assertNotContains('cake sample [-h]', $result);
  240. }
  241. /**
  242. * Test that run() fires off the buildCommands event.
  243. *
  244. * @return void
  245. */
  246. public function testRunTriggersBuildCommandsEvent()
  247. {
  248. $app = $this->getMockBuilder(BaseApplication::class)
  249. ->setMethods(['middleware', 'bootstrap'])
  250. ->setConstructorArgs([$this->config])
  251. ->getMock();
  252. $output = new ConsoleOutput();
  253. $runner = new CommandRunner($app, 'cake');
  254. $runner->getEventManager()->on('Console.buildCommands', function ($event, $commands) {
  255. $this->assertInstanceOf(CommandCollection::class, $commands);
  256. $this->eventTriggered = true;
  257. });
  258. $result = $runner->run(['cake', '--version'], $this->getMockIo($output));
  259. $this->assertTrue($this->eventTriggered, 'Should have triggered event.');
  260. }
  261. protected function getMockIo($output)
  262. {
  263. $io = $this->getMockBuilder(ConsoleIo::class)
  264. ->setConstructorArgs([$output, $output, null, null])
  265. ->setMethods(['in'])
  266. ->getMock();
  267. return $io;
  268. }
  269. }