CommandTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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\ExampleCommand;
  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 option parser fetching
  76. *
  77. * @return void
  78. */
  79. public function testGetOptionParser()
  80. {
  81. $command = new Command();
  82. $command->setName('cake routes show');
  83. $parser = $command->getOptionParser();
  84. $this->assertInstanceOf(ConsoleOptionParser::class, $parser);
  85. $this->assertSame('routes show', $parser->getCommand());
  86. }
  87. /**
  88. * Test option parser fetching
  89. *
  90. * @expectedException RuntimeException
  91. * @return void
  92. */
  93. public function testGetOptionParserInvalid()
  94. {
  95. $command = $this->getMockBuilder(Command::class)
  96. ->setMethods(['buildOptionParser'])
  97. ->getMock();
  98. $command->expects($this->once())
  99. ->method('buildOptionParser')
  100. ->will($this->returnValue(null));
  101. $command->getOptionParser();
  102. }
  103. /**
  104. * Test that initialize is called.
  105. *
  106. * @return void
  107. */
  108. public function testRunCallsInitialize()
  109. {
  110. $command = $this->getMockBuilder(Command::class)
  111. ->setMethods(['initialize'])
  112. ->getMock();
  113. $command->setName('cake example');
  114. $command->expects($this->once())->method('initialize');
  115. $command->run([], $this->getMockIo(new ConsoleOutput()));
  116. }
  117. /**
  118. * Test run() outputs help
  119. *
  120. * @return void
  121. */
  122. public function testRunOutputHelp()
  123. {
  124. $command = new Command();
  125. $command->setName('cake example');
  126. $output = new ConsoleOutput();
  127. $this->assertSame(
  128. Command::CODE_SUCCESS,
  129. $command->run(['-h'], $this->getMockIo($output))
  130. );
  131. $messages = implode("\n", $output->messages());
  132. $this->assertNotContains('Example', $messages);
  133. $this->assertContains('cake example [-h]', $messages);
  134. }
  135. /**
  136. * Test run() outputs help
  137. *
  138. * @return void
  139. */
  140. public function testRunOutputHelpLongOption()
  141. {
  142. $command = new Command();
  143. $command->setName('cake example');
  144. $output = new ConsoleOutput();
  145. $this->assertSame(
  146. Command::CODE_SUCCESS,
  147. $command->run(['--help'], $this->getMockIo($output))
  148. );
  149. $messages = implode("\n", $output->messages());
  150. $this->assertNotContains('Example', $messages);
  151. $this->assertContains('cake example [-h]', $messages);
  152. }
  153. /**
  154. * Test run() sets output level
  155. *
  156. * @return void
  157. */
  158. public function testRunVerboseOption()
  159. {
  160. $command = new ExampleCommand();
  161. $command->setName('cake example');
  162. $output = new ConsoleOutput();
  163. $this->assertNull($command->run(['--verbose'], $this->getMockIo($output)));
  164. $messages = implode("\n", $output->messages());
  165. $this->assertContains('Verbose!', $messages);
  166. $this->assertContains('Example Command!', $messages);
  167. $this->assertContains('Quiet!', $messages);
  168. $this->assertNotContains('cake example [-h]', $messages);
  169. }
  170. /**
  171. * Test run() sets output level
  172. *
  173. * @return void
  174. */
  175. public function testRunQuietOption()
  176. {
  177. $command = new ExampleCommand();
  178. $command->setName('cake example');
  179. $output = new ConsoleOutput();
  180. $this->assertNull($command->run(['--quiet'], $this->getMockIo($output)));
  181. $messages = implode("\n", $output->messages());
  182. $this->assertContains('Quiet!', $messages);
  183. $this->assertNotContains('Verbose!', $messages);
  184. $this->assertNotContains('Example Command!', $messages);
  185. }
  186. /**
  187. * Test run() sets option parser failure
  188. *
  189. * @return void
  190. */
  191. public function testRunOptionParserFailure()
  192. {
  193. $command = $this->getMockBuilder(Command::class)
  194. ->setMethods(['getOptionParser'])
  195. ->getMock();
  196. $parser = new ConsoleOptionParser('cake example');
  197. $parser->addArgument('name', ['required' => true]);
  198. $command->method('getOptionParser')->will($this->returnValue($parser));
  199. $output = new ConsoleOutput();
  200. $result = $command->run([], $this->getMockIo($output));
  201. $this->assertSame(Command::CODE_ERROR, $result);
  202. $messages = implode("\n", $output->messages());
  203. $this->assertContains('Error: Missing required arguments. name is required', $messages);
  204. }
  205. protected function getMockIo($output)
  206. {
  207. $io = $this->getMockBuilder(ConsoleIo::class)
  208. ->setConstructorArgs([$output, $output, null, null])
  209. ->setMethods(['in'])
  210. ->getMock();
  211. return $io;
  212. }
  213. }