CommandTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 InvalidArgumentException;
  24. use TestApp\Command\AbortCommand;
  25. use TestApp\Command\AutoLoadModelCommand;
  26. use TestApp\Command\DemoCommand;
  27. /**
  28. * Test case for Console\Command
  29. */
  30. class CommandTest extends TestCase
  31. {
  32. /**
  33. * test orm locator is setup
  34. *
  35. * @return void
  36. */
  37. public function testConstructorSetsLocator()
  38. {
  39. $command = new Command();
  40. $result = $command->getTableLocator();
  41. $this->assertInstanceOf(TableLocator::class, $result);
  42. }
  43. /**
  44. * test loadModel is configured properly
  45. *
  46. * @return void
  47. */
  48. public function testConstructorLoadModel()
  49. {
  50. $command = new Command();
  51. $command->loadModel('Comments');
  52. $this->assertInstanceOf(Table::class, $command->Comments);
  53. }
  54. /**
  55. * test loadModel is configured properly
  56. *
  57. * @return void
  58. */
  59. public function testConstructorAutoLoadModel()
  60. {
  61. $command = new AutoLoadModelCommand();
  62. $this->assertInstanceOf(Table::class, $command->Posts);
  63. }
  64. /**
  65. * Test name
  66. *
  67. * @return void
  68. */
  69. public function testSetName()
  70. {
  71. $command = new Command();
  72. $this->assertSame($command, $command->setName('routes show'));
  73. $this->assertSame('routes show', $command->getName());
  74. }
  75. /**
  76. * Test invalid name
  77. *
  78. * @expectedException InvalidArgumentException
  79. * @expectedExceptionMessage The name 'routes_show' is missing a space. Names should look like `cake routes`
  80. * @return void
  81. */
  82. public function testSetNameInvalid()
  83. {
  84. $command = new Command();
  85. $command->setName('routes_show');
  86. }
  87. /**
  88. * Test invalid name
  89. *
  90. * @expectedException InvalidArgumentException
  91. * @return void
  92. */
  93. public function testSetNameInvalidLeadingSpace()
  94. {
  95. $command = new Command();
  96. $command->setName(' routes_show');
  97. }
  98. /**
  99. * Test option parser fetching
  100. *
  101. * @return void
  102. */
  103. public function testGetOptionParser()
  104. {
  105. $command = new Command();
  106. $command->setName('cake routes show');
  107. $parser = $command->getOptionParser();
  108. $this->assertInstanceOf(ConsoleOptionParser::class, $parser);
  109. $this->assertSame('routes show', $parser->getCommand());
  110. }
  111. /**
  112. * Test option parser fetching
  113. *
  114. * @expectedException RuntimeException
  115. * @return void
  116. */
  117. public function testGetOptionParserInvalid()
  118. {
  119. $command = $this->getMockBuilder(Command::class)
  120. ->setMethods(['buildOptionParser'])
  121. ->getMock();
  122. $command->expects($this->once())
  123. ->method('buildOptionParser')
  124. ->will($this->returnValue(null));
  125. $command->getOptionParser();
  126. }
  127. /**
  128. * Test that initialize is called.
  129. *
  130. * @return void
  131. */
  132. public function testRunCallsInitialize()
  133. {
  134. $command = $this->getMockBuilder(Command::class)
  135. ->setMethods(['initialize'])
  136. ->getMock();
  137. $command->setName('cake example');
  138. $command->expects($this->once())->method('initialize');
  139. $command->run([], $this->getMockIo(new ConsoleOutput()));
  140. }
  141. /**
  142. * Test run() outputs help
  143. *
  144. * @return void
  145. */
  146. public function testRunOutputHelp()
  147. {
  148. $command = new Command();
  149. $command->setName('cake demo');
  150. $output = new ConsoleOutput();
  151. $this->assertSame(
  152. Command::CODE_SUCCESS,
  153. $command->run(['-h'], $this->getMockIo($output))
  154. );
  155. $messages = implode("\n", $output->messages());
  156. $this->assertNotContains('Demo', $messages);
  157. $this->assertContains('cake demo [-h]', $messages);
  158. }
  159. /**
  160. * Test run() outputs help
  161. *
  162. * @return void
  163. */
  164. public function testRunOutputHelpLongOption()
  165. {
  166. $command = new Command();
  167. $command->setName('cake demo');
  168. $output = new ConsoleOutput();
  169. $this->assertSame(
  170. Command::CODE_SUCCESS,
  171. $command->run(['--help'], $this->getMockIo($output))
  172. );
  173. $messages = implode("\n", $output->messages());
  174. $this->assertNotContains('Demo', $messages);
  175. $this->assertContains('cake demo [-h]', $messages);
  176. }
  177. /**
  178. * Test run() sets output level
  179. *
  180. * @return void
  181. */
  182. public function testRunVerboseOption()
  183. {
  184. $command = new DemoCommand();
  185. $command->setName('cake demo');
  186. $output = new ConsoleOutput();
  187. $this->assertNull($command->run(['--verbose'], $this->getMockIo($output)));
  188. $messages = implode("\n", $output->messages());
  189. $this->assertContains('Verbose!', $messages);
  190. $this->assertContains('Demo Command!', $messages);
  191. $this->assertContains('Quiet!', $messages);
  192. $this->assertNotContains('cake demo [-h]', $messages);
  193. }
  194. /**
  195. * Test run() sets output level
  196. *
  197. * @return void
  198. */
  199. public function testRunQuietOption()
  200. {
  201. $command = new DemoCommand();
  202. $command->setName('cake demo');
  203. $output = new ConsoleOutput();
  204. $this->assertNull($command->run(['--quiet'], $this->getMockIo($output)));
  205. $messages = implode("\n", $output->messages());
  206. $this->assertContains('Quiet!', $messages);
  207. $this->assertNotContains('Verbose!', $messages);
  208. $this->assertNotContains('Demo Command!', $messages);
  209. }
  210. /**
  211. * Test run() sets option parser failure
  212. *
  213. * @return void
  214. */
  215. public function testRunOptionParserFailure()
  216. {
  217. $command = $this->getMockBuilder(Command::class)
  218. ->setMethods(['getOptionParser'])
  219. ->getMock();
  220. $parser = new ConsoleOptionParser('cake example');
  221. $parser->addArgument('name', ['required' => true]);
  222. $command->method('getOptionParser')->will($this->returnValue($parser));
  223. $output = new ConsoleOutput();
  224. $result = $command->run([], $this->getMockIo($output));
  225. $this->assertSame(Command::CODE_ERROR, $result);
  226. $messages = implode("\n", $output->messages());
  227. $this->assertContains('Error: Missing required arguments. name is required', $messages);
  228. }
  229. /**
  230. * Test abort()
  231. *
  232. * @expectedException \Cake\Console\Exception\StopException
  233. * @expectedExceptionCode 1
  234. * @return void
  235. */
  236. public function testAbort()
  237. {
  238. $command = new Command();
  239. $command->abort();
  240. }
  241. /**
  242. * Test abort()
  243. *
  244. * @expectedException \Cake\Console\Exception\StopException
  245. * @expectedExceptionCode 99
  246. * @return void
  247. */
  248. public function testAbortCustomCode()
  249. {
  250. $command = new Command();
  251. $command->abort(99);
  252. }
  253. /**
  254. * test executeCommand with a string class
  255. *
  256. * @return void
  257. */
  258. public function testExecuteCommandString()
  259. {
  260. $output = new ConsoleOutput();
  261. $command = new Command();
  262. $result = $command->executeCommand(DemoCommand::class, [], $this->getMockIo($output));
  263. $this->assertNull($result);
  264. $this->assertEquals(['Quiet!', 'Demo Command!'], $output->messages());
  265. }
  266. /**
  267. * test executeCommand with an invalid string class
  268. *
  269. * @return void
  270. */
  271. public function testExecuteCommandStringInvalid()
  272. {
  273. $this->expectException(InvalidArgumentException::class);
  274. $this->expectExceptionMessage("Command class 'Nope' does not exist");
  275. $command = new Command();
  276. $command->executeCommand('Nope', [], $this->getMockIo(new ConsoleOutput()));
  277. }
  278. /**
  279. * test executeCommand with arguments
  280. *
  281. * @return void
  282. */
  283. public function testExecuteCommandArguments()
  284. {
  285. $output = new ConsoleOutput();
  286. $command = new Command();
  287. $command->executeCommand(DemoCommand::class, ['Jane'], $this->getMockIo($output));
  288. $this->assertEquals(['Quiet!', 'Demo Command!', 'Jane'], $output->messages());
  289. }
  290. /**
  291. * test executeCommand with arguments
  292. *
  293. * @return void
  294. */
  295. public function testExecuteCommandArgumentsOptions()
  296. {
  297. $output = new ConsoleOutput();
  298. $command = new Command();
  299. $command->executeCommand(DemoCommand::class, ['--quiet', 'Jane'], $this->getMockIo($output));
  300. $this->assertEquals(['Quiet!'], $output->messages());
  301. }
  302. /**
  303. * test executeCommand with an instance
  304. *
  305. * @return void
  306. */
  307. public function testExecuteCommandInstance()
  308. {
  309. $output = new ConsoleOutput();
  310. $command = new Command();
  311. $result = $command->executeCommand(new DemoCommand(), [], $this->getMockIo($output));
  312. $this->assertNull($result);
  313. $this->assertEquals(['Quiet!', 'Demo Command!'], $output->messages());
  314. }
  315. /**
  316. * test executeCommand with an abort
  317. *
  318. * @return void
  319. */
  320. public function testExecuteCommandAbort()
  321. {
  322. $output = new ConsoleOutput();
  323. $command = new Command();
  324. $result = $command->executeCommand(AbortCommand::class, [], $this->getMockIo($output));
  325. $this->assertSame(127, $result);
  326. $this->assertEquals(['<error>Command aborted</error>'], $output->messages());
  327. }
  328. /**
  329. * test executeCommand with an invalid instance
  330. *
  331. * @return void
  332. */
  333. public function testExecuteCommandInstanceInvalid()
  334. {
  335. $this->expectException(InvalidArgumentException::class);
  336. $this->expectExceptionMessage("Command 'stdClass' is not a subclass");
  337. $command = new Command();
  338. $command->executeCommand(new \stdClass(), [], $this->getMockIo(new ConsoleOutput()));
  339. }
  340. protected function getMockIo($output)
  341. {
  342. $io = $this->getMockBuilder(ConsoleIo::class)
  343. ->setConstructorArgs([$output, $output, null, null])
  344. ->setMethods(['in'])
  345. ->getMock();
  346. return $io;
  347. }
  348. }