CommandTest.php 7.7 KB

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