CompletionShellTest.php 8.8 KB

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