CompletionShellTest.php 8.7 KB

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