CompletionShellTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 2.5.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Shell;
  17. use Cake\Console\ConsoleIo;
  18. use Cake\TestSuite\Stub\ConsoleOutput as StubOutput;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * CompletionShellTest
  22. */
  23. class CompletionShellTest extends TestCase
  24. {
  25. /**
  26. * setUp method
  27. *
  28. * @return void
  29. */
  30. public function setUp()
  31. {
  32. parent::setUp();
  33. static::setAppNamespace();
  34. $this->loadPlugins(['TestPlugin', 'TestPluginTwo']);
  35. $this->out = new StubOutput();
  36. $io = new ConsoleIo($this->out);
  37. $this->Shell = $this->getMockBuilder('Cake\Shell\CompletionShell')
  38. ->setMethods(['in', '_stop', 'clear'])
  39. ->setConstructorArgs([$io])
  40. ->getMock();
  41. $this->Shell->Command = $this->getMockBuilder('Cake\Shell\Task\CommandTask')
  42. ->setMethods(['in', '_stop', 'clear'])
  43. ->setConstructorArgs([$io])
  44. ->getMock();
  45. }
  46. /**
  47. * tearDown
  48. *
  49. * @return void
  50. */
  51. public function tearDown()
  52. {
  53. parent::tearDown();
  54. unset($this->Shell);
  55. static::setAppNamespace('App');
  56. $this->clearPlugins();
  57. }
  58. /**
  59. * test that the startup method suppresses the shell header
  60. *
  61. * @return void
  62. */
  63. public function testStartup()
  64. {
  65. $this->Shell->runCommand(['main']);
  66. $output = $this->out->output();
  67. $needle = 'Welcome to CakePHP';
  68. $this->assertTextNotContains($needle, $output);
  69. }
  70. /**
  71. * test that main displays a warning
  72. *
  73. * @return void
  74. */
  75. public function testMain()
  76. {
  77. $this->Shell->runCommand(['main']);
  78. $output = $this->out->output();
  79. $expected = '/This command is not intended to be called manually/';
  80. $this->assertRegExp($expected, $output);
  81. }
  82. /**
  83. * test commands method that list all available commands
  84. *
  85. * @return void
  86. */
  87. public function testCommands()
  88. {
  89. $this->Shell->runCommand(['commands']);
  90. $output = $this->out->output();
  91. $expected = 'TestPlugin.example TestPlugin.sample TestPluginTwo.example unique welcome ' .
  92. 'cache help plugin routes schema_cache server upgrade version ' .
  93. "abort auto_load_model demo i18n integration merge sample shell_test testing_dispatch";
  94. $this->assertTextEquals($expected, $output);
  95. }
  96. /**
  97. * test that options without argument returns nothing
  98. *
  99. * @return void
  100. */
  101. public function testOptionsNoArguments()
  102. {
  103. $this->Shell->runCommand(['options']);
  104. $output = $this->out->output();
  105. $expected = '';
  106. $this->assertTextEquals($expected, $output);
  107. }
  108. /**
  109. * test that options with a non-existing command returns nothing
  110. *
  111. * @return void
  112. */
  113. public function testOptionsNonExistingCommand()
  114. {
  115. $this->Shell->runCommand(['options', 'foo']);
  116. $output = $this->out->output();
  117. $expected = '';
  118. $this->assertTextEquals($expected, $output);
  119. }
  120. /**
  121. * test that options with an existing command returns the proper options
  122. *
  123. * @return void
  124. */
  125. public function testOptions()
  126. {
  127. $this->Shell->runCommand(['options', 'schema_cache']);
  128. $output = $this->out->output();
  129. $expected = "--connection -c --help -h --quiet -q --verbose -v";
  130. $this->assertTextEquals($expected, $output);
  131. }
  132. /**
  133. * test that options with an existing command / subcommand pair returns the proper options
  134. *
  135. * @return void
  136. */
  137. public function testOptionsTask()
  138. {
  139. $this->Shell->runCommand(['options', 'sample', 'sample']);
  140. $output = $this->out->output();
  141. $expected = "--help -h --quiet -q --sample -s --verbose -v";
  142. $this->assertTextEquals($expected, $output);
  143. }
  144. /**
  145. * test that subCommands with a existing CORE command returns the proper sub commands
  146. *
  147. * @return void
  148. */
  149. public function testSubCommandsCorePlugin()
  150. {
  151. $this->Shell->runCommand(['subcommands', 'CORE.schema_cache']);
  152. $output = $this->out->output();
  153. $expected = "build clear";
  154. $this->assertTextEquals($expected, $output);
  155. }
  156. /**
  157. * test that subCommands with a existing APP command returns the proper sub commands (in this case none)
  158. *
  159. * @return void
  160. */
  161. public function testSubCommandsAppPlugin()
  162. {
  163. $this->Shell->runCommand(['subcommands', 'app.sample']);
  164. $output = $this->out->output();
  165. $expected = "derp load returnValue sample withAbort";
  166. $this->assertTextEquals($expected, $output);
  167. }
  168. /**
  169. * test that subCommands with an existing plugin command returns the proper sub commands
  170. * when the Shell name is unique and the dot notation not mandatory
  171. *
  172. * @return void
  173. */
  174. public function testSubCommandsPlugin()
  175. {
  176. $this->Shell->runCommand(['subcommands', 'welcome']);
  177. $output = $this->out->output();
  178. $expected = "say_hello";
  179. $this->assertTextEquals($expected, $output);
  180. }
  181. /**
  182. * test that using the dot notation when not mandatory works to provide backward compatibility
  183. *
  184. * @return void
  185. */
  186. public function testSubCommandsPluginDotNotationBackwardCompatibility()
  187. {
  188. $this->Shell->runCommand(['subcommands', 'TestPluginTwo.welcome']);
  189. $output = $this->out->output();
  190. $expected = "say_hello";
  191. $this->assertTextEquals($expected, $output);
  192. }
  193. /**
  194. * test that subCommands with an existing plugin command returns the proper sub commands
  195. *
  196. * @return void
  197. */
  198. public function testSubCommandsPluginDotNotation()
  199. {
  200. $this->Shell->runCommand(['subcommands', 'TestPluginTwo.example']);
  201. $output = $this->out->output();
  202. $expected = "say_hello";
  203. $this->assertTextEquals($expected, $output);
  204. }
  205. /**
  206. * test that subCommands with an app shell that is also defined in a plugin and without the prefix "app."
  207. * returns proper sub commands
  208. *
  209. * @return void
  210. */
  211. public function testSubCommandsAppDuplicatePluginNoDot()
  212. {
  213. $this->Shell->runCommand(['subcommands', 'sample']);
  214. $output = $this->out->output();
  215. $expected = "derp load returnValue sample withAbort";
  216. $this->assertTextEquals($expected, $output);
  217. }
  218. /**
  219. * test that subCommands with a plugin shell that is also defined in the returns proper sub commands
  220. *
  221. * @return void
  222. */
  223. public function testSubCommandsPluginDuplicateApp()
  224. {
  225. $this->Shell->runCommand(['subcommands', 'TestPlugin.sample']);
  226. $output = $this->out->output();
  227. $expected = "example";
  228. $this->assertTextEquals($expected, $output);
  229. }
  230. /**
  231. * test that subcommands without arguments returns nothing
  232. *
  233. * @return void
  234. */
  235. public function testSubCommandsNoArguments()
  236. {
  237. $this->Shell->runCommand(['subcommands']);
  238. $output = $this->out->output();
  239. $expected = '';
  240. $this->assertEquals($expected, $output);
  241. }
  242. /**
  243. * test that subcommands with a non-existing command returns nothing
  244. *
  245. * @return void
  246. */
  247. public function testSubCommandsNonExistingCommand()
  248. {
  249. $this->Shell->runCommand(['subcommands', 'foo']);
  250. $output = $this->out->output();
  251. $expected = '';
  252. $this->assertEquals($expected, $output);
  253. }
  254. /**
  255. * test that subcommands returns the available subcommands for the given command
  256. *
  257. * @return void
  258. */
  259. public function testSubCommands()
  260. {
  261. $this->Shell->runCommand(['subcommands', 'schema_cache']);
  262. $output = $this->out->output();
  263. $expected = "build clear";
  264. $this->assertTextEquals($expected, $output);
  265. }
  266. /**
  267. * test that fuzzy returns nothing
  268. *
  269. * @return void
  270. */
  271. public function testFuzzy()
  272. {
  273. $this->Shell->runCommand(['fuzzy']);
  274. $output = $this->out->output();
  275. $expected = '';
  276. $this->assertEquals($expected, $output);
  277. }
  278. }