CompletionShellTest.php 8.7 KB

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