CommandTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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\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 option parser fetching
  112. *
  113. * @expectedException RuntimeException
  114. * @return void
  115. */
  116. public function testGetOptionParserInvalid()
  117. {
  118. $command = $this->getMockBuilder(Command::class)
  119. ->setMethods(['buildOptionParser'])
  120. ->getMock();
  121. $command->expects($this->once())
  122. ->method('buildOptionParser')
  123. ->will($this->returnValue(null));
  124. $command->getOptionParser();
  125. }
  126. /**
  127. * Test that initialize is called.
  128. *
  129. * @return void
  130. */
  131. public function testRunCallsInitialize()
  132. {
  133. $command = $this->getMockBuilder(Command::class)
  134. ->setMethods(['initialize'])
  135. ->getMock();
  136. $command->setName('cake example');
  137. $command->expects($this->once())->method('initialize');
  138. $command->run([], $this->getMockIo(new ConsoleOutput()));
  139. }
  140. /**
  141. * Test run() outputs help
  142. *
  143. * @return void
  144. */
  145. public function testRunOutputHelp()
  146. {
  147. $command = new Command();
  148. $command->setName('cake demo');
  149. $output = new ConsoleOutput();
  150. $this->assertSame(
  151. Command::CODE_SUCCESS,
  152. $command->run(['-h'], $this->getMockIo($output))
  153. );
  154. $messages = implode("\n", $output->messages());
  155. $this->assertNotContains('Demo', $messages);
  156. $this->assertContains('cake demo [-h]', $messages);
  157. }
  158. /**
  159. * Test run() outputs help
  160. *
  161. * @return void
  162. */
  163. public function testRunOutputHelpLongOption()
  164. {
  165. $command = new Command();
  166. $command->setName('cake demo');
  167. $output = new ConsoleOutput();
  168. $this->assertSame(
  169. Command::CODE_SUCCESS,
  170. $command->run(['--help'], $this->getMockIo($output))
  171. );
  172. $messages = implode("\n", $output->messages());
  173. $this->assertNotContains('Demo', $messages);
  174. $this->assertContains('cake demo [-h]', $messages);
  175. }
  176. /**
  177. * Test run() sets output level
  178. *
  179. * @return void
  180. */
  181. public function testRunVerboseOption()
  182. {
  183. $command = new DemoCommand();
  184. $command->setName('cake demo');
  185. $output = new ConsoleOutput();
  186. $this->assertNull($command->run(['--verbose'], $this->getMockIo($output)));
  187. $messages = implode("\n", $output->messages());
  188. $this->assertContains('Verbose!', $messages);
  189. $this->assertContains('Demo Command!', $messages);
  190. $this->assertContains('Quiet!', $messages);
  191. $this->assertNotContains('cake demo [-h]', $messages);
  192. }
  193. /**
  194. * Test run() sets output level
  195. *
  196. * @return void
  197. */
  198. public function testRunQuietOption()
  199. {
  200. $command = new DemoCommand();
  201. $command->setName('cake demo');
  202. $output = new ConsoleOutput();
  203. $this->assertNull($command->run(['--quiet'], $this->getMockIo($output)));
  204. $messages = implode("\n", $output->messages());
  205. $this->assertContains('Quiet!', $messages);
  206. $this->assertNotContains('Verbose!', $messages);
  207. $this->assertNotContains('Demo Command!', $messages);
  208. }
  209. /**
  210. * Test run() sets option parser failure
  211. *
  212. * @return void
  213. */
  214. public function testRunOptionParserFailure()
  215. {
  216. $command = $this->getMockBuilder(Command::class)
  217. ->setMethods(['getOptionParser'])
  218. ->getMock();
  219. $parser = new ConsoleOptionParser('cake example');
  220. $parser->addArgument('name', ['required' => true]);
  221. $command->method('getOptionParser')->will($this->returnValue($parser));
  222. $output = new ConsoleOutput();
  223. $result = $command->run([], $this->getMockIo($output));
  224. $this->assertSame(Command::CODE_ERROR, $result);
  225. $messages = implode("\n", $output->messages());
  226. $this->assertContains('Error: Missing required arguments. name is required', $messages);
  227. }
  228. /**
  229. * Test abort()
  230. *
  231. * @expectedException \Cake\Console\Exception\StopException
  232. * @expectedExceptionCode 1
  233. * @return void
  234. */
  235. public function testAbort()
  236. {
  237. $command = new Command();
  238. $command->abort();
  239. }
  240. /**
  241. * Test abort()
  242. *
  243. * @expectedException \Cake\Console\Exception\StopException
  244. * @expectedExceptionCode 99
  245. * @return void
  246. */
  247. public function testAbortCustomCode()
  248. {
  249. $command = new Command();
  250. $command->abort(99);
  251. }
  252. /**
  253. * test executeCommand with a string class
  254. *
  255. * @return void
  256. */
  257. public function testExecuteCommandString()
  258. {
  259. $output = new ConsoleOutput();
  260. $command = new Command();
  261. $result = $command->executeCommand(DemoCommand::class, $this->getMockIo($output));
  262. $this->assertNull($result);
  263. $this->assertEquals(['Quiet!', 'Demo Command!'], $output->messages());
  264. }
  265. /**
  266. * test executeCommand with an invalid string class
  267. *
  268. * @return void
  269. */
  270. public function testExecuteCommandStringInvalid()
  271. {
  272. $this->expectException(InvalidArgumentException::class);
  273. $this->expectExceptionMessage("Command class 'Nope' does not exist");
  274. $command = new Command();
  275. $command->executeCommand('Nope', $this->getMockIo(new ConsoleOutput()));
  276. }
  277. /**
  278. * test executeCommand with arguments
  279. *
  280. * @return void
  281. */
  282. public function testExecuteCommandArguments()
  283. {
  284. $output = new ConsoleOutput();
  285. $command = new Command();
  286. $command->executeCommand(DemoCommand::class, $this->getMockIo($output), ['Jane']);
  287. $this->assertEquals(['Quiet!', 'Demo Command!', 'Jane'], $output->messages());
  288. }
  289. /**
  290. * test executeCommand with arguments
  291. *
  292. * @return void
  293. */
  294. public function testExecuteCommandArgumentsOptions()
  295. {
  296. $output = new ConsoleOutput();
  297. $command = new Command();
  298. $command->executeCommand(DemoCommand::class, $this->getMockIo($output), ['--quiet', 'Jane']);
  299. $this->assertEquals(['Quiet!'], $output->messages());
  300. }
  301. /**
  302. * test executeCommand with an instance
  303. *
  304. * @return void
  305. */
  306. public function testExecuteCommandInstance()
  307. {
  308. $output = new ConsoleOutput();
  309. $command = new Command();
  310. $result = $command->executeCommand(new DemoCommand(), $this->getMockIo($output));
  311. $this->assertNull($result);
  312. $this->assertEquals(['Quiet!', 'Demo Command!'], $output->messages());
  313. }
  314. /**
  315. * test executeCommand with an invalid instance
  316. *
  317. * @return void
  318. */
  319. public function testExecuteCommandInstanceInvalid()
  320. {
  321. $this->expectException(InvalidArgumentException::class);
  322. $this->expectExceptionMessage("Command 'stdClass' is not a subclass");
  323. $command = new Command();
  324. $command->executeCommand(new \stdClass, $this->getMockIo(new ConsoleOutput()));
  325. }
  326. protected function getMockIo($output)
  327. {
  328. $io = $this->getMockBuilder(ConsoleIo::class)
  329. ->setConstructorArgs([$output, $output, null, null])
  330. ->setMethods(['in'])
  331. ->getMock();
  332. return $io;
  333. }
  334. }