CompletionShellTest.php 8.7 KB

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