CompletionShellTest.php 8.7 KB

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