CommandTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. /**
  3. * CakePHP : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP Project
  12. * @since 3.6.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Console;
  16. use Cake\Console\Command;
  17. use Cake\Console\ConsoleIo;
  18. use Cake\Console\ConsoleOptionParser;
  19. use Cake\ORM\Locator\TableLocator;
  20. use Cake\ORM\Table;
  21. use Cake\TestSuite\Stub\ConsoleOutput;
  22. use Cake\TestSuite\TestCase;
  23. use TestApp\Command\DemoCommand;
  24. /**
  25. * Test case for Console\Command
  26. */
  27. class CommandTest extends TestCase
  28. {
  29. /**
  30. * test orm locator is setup
  31. *
  32. * @return void
  33. */
  34. public function testConstructorSetsLocator()
  35. {
  36. $command = new Command();
  37. $result = $command->getTableLocator();
  38. $this->assertInstanceOf(TableLocator::class, $result);
  39. }
  40. /**
  41. * test loadModel is configured properly
  42. *
  43. * @return void
  44. */
  45. public function testConstructorLoadModel()
  46. {
  47. $command = new Command();
  48. $command->loadModel('Comments');
  49. $this->assertInstanceOf(Table::class, $command->Comments);
  50. }
  51. /**
  52. * Test name
  53. *
  54. * @return void
  55. */
  56. public function testSetName()
  57. {
  58. $command = new Command();
  59. $this->assertSame($command, $command->setName('routes show'));
  60. $this->assertSame('routes show', $command->getName());
  61. }
  62. /**
  63. * Test invalid name
  64. *
  65. * @expectedException InvalidArgumentException
  66. * @expectedExceptionMessage The name 'routes_show' is missing a space. Names should look like `cake routes`
  67. * @return void
  68. */
  69. public function testSetNameInvalid()
  70. {
  71. $command = new Command();
  72. $command->setName('routes_show');
  73. }
  74. /**
  75. * Test invalid name
  76. *
  77. * @expectedException InvalidArgumentException
  78. * @return void
  79. */
  80. public function testSetNameInvalidLeadingSpace()
  81. {
  82. $command = new Command();
  83. $command->setName(' routes_show');
  84. }
  85. /**
  86. * Test option parser fetching
  87. *
  88. * @return void
  89. */
  90. public function testGetOptionParser()
  91. {
  92. $command = new Command();
  93. $command->setName('cake routes show');
  94. $parser = $command->getOptionParser();
  95. $this->assertInstanceOf(ConsoleOptionParser::class, $parser);
  96. $this->assertSame('routes show', $parser->getCommand());
  97. }
  98. /**
  99. * Test option parser fetching
  100. *
  101. * @expectedException RuntimeException
  102. * @return void
  103. */
  104. public function testGetOptionParserInvalid()
  105. {
  106. $command = $this->getMockBuilder(Command::class)
  107. ->setMethods(['buildOptionParser'])
  108. ->getMock();
  109. $command->expects($this->once())
  110. ->method('buildOptionParser')
  111. ->will($this->returnValue(null));
  112. $command->getOptionParser();
  113. }
  114. /**
  115. * Test that initialize is called.
  116. *
  117. * @return void
  118. */
  119. public function testRunCallsInitialize()
  120. {
  121. $command = $this->getMockBuilder(Command::class)
  122. ->setMethods(['initialize'])
  123. ->getMock();
  124. $command->setName('cake example');
  125. $command->expects($this->once())->method('initialize');
  126. $command->run([], $this->getMockIo(new ConsoleOutput()));
  127. }
  128. /**
  129. * Test run() outputs help
  130. *
  131. * @return void
  132. */
  133. public function testRunOutputHelp()
  134. {
  135. $command = new Command();
  136. $command->setName('cake demo');
  137. $output = new ConsoleOutput();
  138. $this->assertSame(
  139. Command::CODE_SUCCESS,
  140. $command->run(['-h'], $this->getMockIo($output))
  141. );
  142. $messages = implode("\n", $output->messages());
  143. $this->assertNotContains('Demo', $messages);
  144. $this->assertContains('cake demo [-h]', $messages);
  145. }
  146. /**
  147. * Test run() outputs help
  148. *
  149. * @return void
  150. */
  151. public function testRunOutputHelpLongOption()
  152. {
  153. $command = new Command();
  154. $command->setName('cake demo');
  155. $output = new ConsoleOutput();
  156. $this->assertSame(
  157. Command::CODE_SUCCESS,
  158. $command->run(['--help'], $this->getMockIo($output))
  159. );
  160. $messages = implode("\n", $output->messages());
  161. $this->assertNotContains('Demo', $messages);
  162. $this->assertContains('cake demo [-h]', $messages);
  163. }
  164. /**
  165. * Test run() sets output level
  166. *
  167. * @return void
  168. */
  169. public function testRunVerboseOption()
  170. {
  171. $command = new DemoCommand();
  172. $command->setName('cake demo');
  173. $output = new ConsoleOutput();
  174. $this->assertNull($command->run(['--verbose'], $this->getMockIo($output)));
  175. $messages = implode("\n", $output->messages());
  176. $this->assertContains('Verbose!', $messages);
  177. $this->assertContains('Demo Command!', $messages);
  178. $this->assertContains('Quiet!', $messages);
  179. $this->assertNotContains('cake demo [-h]', $messages);
  180. }
  181. /**
  182. * Test run() sets output level
  183. *
  184. * @return void
  185. */
  186. public function testRunQuietOption()
  187. {
  188. $command = new DemoCommand();
  189. $command->setName('cake demo');
  190. $output = new ConsoleOutput();
  191. $this->assertNull($command->run(['--quiet'], $this->getMockIo($output)));
  192. $messages = implode("\n", $output->messages());
  193. $this->assertContains('Quiet!', $messages);
  194. $this->assertNotContains('Verbose!', $messages);
  195. $this->assertNotContains('Demo Command!', $messages);
  196. }
  197. /**
  198. * Test run() sets option parser failure
  199. *
  200. * @return void
  201. */
  202. public function testRunOptionParserFailure()
  203. {
  204. $command = $this->getMockBuilder(Command::class)
  205. ->setMethods(['getOptionParser'])
  206. ->getMock();
  207. $parser = new ConsoleOptionParser('cake example');
  208. $parser->addArgument('name', ['required' => true]);
  209. $command->method('getOptionParser')->will($this->returnValue($parser));
  210. $output = new ConsoleOutput();
  211. $result = $command->run([], $this->getMockIo($output));
  212. $this->assertSame(Command::CODE_ERROR, $result);
  213. $messages = implode("\n", $output->messages());
  214. $this->assertContains('Error: Missing required arguments. name is required', $messages);
  215. }
  216. /**
  217. * Test abort()
  218. *
  219. * @expectedException \Cake\Console\Exception\StopException
  220. * @expectedExceptionCode 1
  221. * @return void
  222. */
  223. public function testAbort()
  224. {
  225. $command = new Command();
  226. $command->abort();
  227. }
  228. /**
  229. * Test abort()
  230. *
  231. * @expectedException \Cake\Console\Exception\StopException
  232. * @expectedExceptionCode 99
  233. * @return void
  234. */
  235. public function testAbortCustomCode()
  236. {
  237. $command = new Command();
  238. $command->abort(99);
  239. }
  240. protected function getMockIo($output)
  241. {
  242. $io = $this->getMockBuilder(ConsoleIo::class)
  243. ->setConstructorArgs([$output, $output, null, null])
  244. ->setMethods(['in'])
  245. ->getMock();
  246. return $io;
  247. }
  248. }