CommandTest.php 7.3 KB

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