CommandRunnerTest.php 9.7 KB

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