CommandRunnerTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. * setup
  32. *
  33. * @return void
  34. */
  35. public function setUp()
  36. {
  37. parent::setUp();
  38. Configure::write('App.namespace', 'TestApp');
  39. $this->config = dirname(dirname(__DIR__));
  40. }
  41. /**
  42. * Test that the console hook not returning a command collection
  43. * raises an error.
  44. *
  45. * @expectedException \RuntimeException
  46. * @expectedExceptionMessage The application's `console` method did not return a CommandCollection.
  47. * @return void
  48. */
  49. public function testRunConsoleHookFailure()
  50. {
  51. $app = $this->getMockBuilder(BaseApplication::class)
  52. ->setMethods(['console', 'middleware', 'bootstrap'])
  53. ->setConstructorArgs([$this->config])
  54. ->getMock();
  55. $runner = new CommandRunner($app);
  56. $runner->run(['cake', '-h']);
  57. }
  58. /**
  59. * Test that running with empty argv fails
  60. *
  61. * @expectedException \RuntimeException
  62. * @expectedExceptionMessage Unknown root command. Was expecting `cake`
  63. * @return void
  64. */
  65. public function testRunMissingRootCommand()
  66. {
  67. $app = $this->getMockBuilder(BaseApplication::class)
  68. ->setMethods(['middleware', 'bootstrap'])
  69. ->setConstructorArgs([$this->config])
  70. ->getMock();
  71. $runner = new CommandRunner($app);
  72. $runner->run([]);
  73. }
  74. /**
  75. * Test that running an unknown command raises an error.
  76. *
  77. * @expectedException \RuntimeException
  78. * @expectedExceptionMessage Unknown root command `bad`. Was expecting `cake`
  79. * @return void
  80. */
  81. public function testRunInvalidRootCommand()
  82. {
  83. $app = $this->getMockBuilder(BaseApplication::class)
  84. ->setMethods(['middleware', 'bootstrap'])
  85. ->setConstructorArgs([$this->config])
  86. ->getMock();
  87. $runner = new CommandRunner($app);
  88. $runner->run(['bad', 'i18n']);
  89. }
  90. /**
  91. * Test that running an unknown command raises an error.
  92. *
  93. * @expectedException \RuntimeException
  94. * @expectedExceptionMessage Unknown command `cake nope`. Run `cake --help` to get the list of valid commands.
  95. * @return void
  96. */
  97. public function testRunInvalidCommand()
  98. {
  99. $app = $this->getMockBuilder(BaseApplication::class)
  100. ->setMethods(['middleware', 'bootstrap'])
  101. ->setConstructorArgs([$this->config])
  102. ->getMock();
  103. $runner = new CommandRunner($app);
  104. $runner->run(['cake', 'nope', 'nope', 'nope']);
  105. }
  106. /**
  107. * Test using `cake --help` invokes the help command
  108. *
  109. * @return void
  110. */
  111. public function testRunHelpLongOption()
  112. {
  113. $app = $this->getMockBuilder(BaseApplication::class)
  114. ->setMethods(['middleware', 'bootstrap'])
  115. ->setConstructorArgs([$this->config])
  116. ->getMock();
  117. $output = new ConsoleOutput();
  118. $runner = new CommandRunner($app, 'cake');
  119. $result = $runner->run(['cake', '--help'], $this->getMockIo($output));
  120. $this->assertSame(0, $result);
  121. $messages = implode("\n", $output->messages());
  122. $this->assertContains('Current Paths', $messages);
  123. $this->assertContains('- i18n', $messages);
  124. $this->assertContains('Available Commands', $messages);
  125. }
  126. /**
  127. * Test using `cake -h` invokes the help command
  128. *
  129. * @return void
  130. */
  131. public function testRunHelpShortOption()
  132. {
  133. $app = $this->getMockBuilder(BaseApplication::class)
  134. ->setMethods(['middleware', 'bootstrap'])
  135. ->setConstructorArgs([$this->config])
  136. ->getMock();
  137. $output = new ConsoleOutput();
  138. $runner = new CommandRunner($app, 'cake');
  139. $result = $runner->run(['cake', '-h'], $this->getMockIo($output));
  140. $this->assertSame(0, $result);
  141. $messages = implode("\n", $output->messages());
  142. $this->assertContains('- i18n', $messages);
  143. $this->assertContains('Available Commands', $messages);
  144. }
  145. /**
  146. * Test using `cake --verson` invokes the version command
  147. *
  148. * @return void
  149. */
  150. public function testRunVersionAlias()
  151. {
  152. $app = $this->getMockBuilder(BaseApplication::class)
  153. ->setMethods(['middleware', 'bootstrap'])
  154. ->setConstructorArgs([$this->config])
  155. ->getMock();
  156. $output = new ConsoleOutput();
  157. $runner = new CommandRunner($app, 'cake');
  158. $result = $runner->run(['cake', '--version'], $this->getMockIo($output));
  159. $this->assertContains(Configure::version(), $output->messages()[0]);
  160. }
  161. /**
  162. * Test running a valid command
  163. *
  164. * @return void
  165. */
  166. public function testRunValidCommand()
  167. {
  168. $app = $this->getMockBuilder(BaseApplication::class)
  169. ->setMethods(['middleware', 'bootstrap'])
  170. ->setConstructorArgs([$this->config])
  171. ->getMock();
  172. $output = new ConsoleOutput();
  173. $runner = new CommandRunner($app, 'cake');
  174. $result = $runner->run(['cake', 'routes'], $this->getMockIo($output));
  175. $this->assertSame(Shell::CODE_SUCCESS, $result);
  176. $contents = implode("\n", $output->messages());
  177. $this->assertContains('URI template', $contents);
  178. $this->assertContains('Welcome to CakePHP', $contents);
  179. }
  180. /**
  181. * Test running a valid raising an error
  182. *
  183. * @return void
  184. */
  185. public function testRunValidCommandWithAbort()
  186. {
  187. $app = $this->getMockBuilder(BaseApplication::class)
  188. ->setMethods(['middleware', 'bootstrap', 'console'])
  189. ->setConstructorArgs([$this->config])
  190. ->getMock();
  191. $commands = new CommandCollection(['failure' => SampleShell::class]);
  192. $app->method('console')->will($this->returnValue($commands));
  193. $output = new ConsoleOutput();
  194. $runner = new CommandRunner($app, 'cake');
  195. $result = $runner->run(['cake', 'failure', 'with_abort'], $this->getMockIo($output));
  196. $this->assertSame(Shell::CODE_ERROR, $result);
  197. }
  198. /**
  199. * Test returning a non-zero value
  200. *
  201. * @return void
  202. */
  203. public function testRunValidCommandReturnInteger()
  204. {
  205. $app = $this->getMockBuilder(BaseApplication::class)
  206. ->setMethods(['middleware', 'bootstrap', 'console'])
  207. ->setConstructorArgs([$this->config])
  208. ->getMock();
  209. $commands = new CommandCollection(['failure' => SampleShell::class]);
  210. $app->method('console')->will($this->returnValue($commands));
  211. $output = new ConsoleOutput();
  212. $runner = new CommandRunner($app, 'cake');
  213. $result = $runner->run(['cake', 'failure', 'returnValue'], $this->getMockIo($output));
  214. $this->assertSame(99, $result);
  215. }
  216. protected function getMockIo($output)
  217. {
  218. $io = $this->getMockBuilder(ConsoleIo::class)
  219. ->setConstructorArgs([$output, $output, null, null])
  220. ->setMethods(['in'])
  221. ->getMock();
  222. return $io;
  223. }
  224. }