CompletionShellTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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\Plugin;
  20. use Cake\Shell\CompletionShell;
  21. use Cake\Shell\Task\CommandTask;
  22. use Cake\TestSuite\TestCase;
  23. /**
  24. * Class TestCompletionStringOutput
  25. *
  26. */
  27. class TestCompletionStringOutput extends ConsoleOutput
  28. {
  29. public $output = '';
  30. protected function _write($message)
  31. {
  32. $this->output .= $message;
  33. }
  34. }
  35. /**
  36. * Class CompletionShellTest
  37. */
  38. class CompletionShellTest extends TestCase
  39. {
  40. /**
  41. * setUp method
  42. *
  43. * @return void
  44. */
  45. public function setUp()
  46. {
  47. parent::setUp();
  48. Plugin::load(['TestPlugin', 'TestPluginTwo']);
  49. $this->out = new TestCompletionStringOutput();
  50. $io = new ConsoleIo($this->out);
  51. $this->Shell = $this->getMock(
  52. 'Cake\Shell\CompletionShell',
  53. ['in', '_stop', 'clear'],
  54. [$io]
  55. );
  56. $this->Shell->Command = $this->getMock(
  57. 'Cake\Shell\Task\CommandTask',
  58. ['in', '_stop', 'clear'],
  59. [$io]
  60. );
  61. }
  62. /**
  63. * tearDown
  64. *
  65. * @return void
  66. */
  67. public function tearDown()
  68. {
  69. parent::tearDown();
  70. unset($this->Shell);
  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 the default options
  112. *
  113. * @return void
  114. */
  115. public function testOptionsNoArguments()
  116. {
  117. $this->Shell->runCommand(['options']);
  118. $output = $this->out->output;
  119. $expected = "--help -h --verbose -v --quiet -q\n";
  120. $this->assertTextEquals($expected, $output);
  121. }
  122. /**
  123. * test that options with a nonexisting command returns the default options
  124. *
  125. * @return void
  126. */
  127. public function testOptionsNonExistingCommand()
  128. {
  129. $this->Shell->runCommand(['options', 'foo']);
  130. $output = $this->out->output;
  131. $expected = "--help -h --verbose -v --quiet -q\n";
  132. $this->assertTextEquals($expected, $output);
  133. }
  134. /**
  135. * test that options with a 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 = "--help -h --verbose -v --quiet -q --connection -c\n";
  144. $this->assertTextEquals($expected, $output);
  145. }
  146. /**
  147. * test that subCommands with a existing CORE command returns the proper sub commands
  148. *
  149. * @return void
  150. */
  151. public function testSubCommandsCorePlugin()
  152. {
  153. $this->Shell->runCommand(['subcommands', 'CORE.orm_cache']);
  154. $output = $this->out->output;
  155. $expected = "build clear\n";
  156. $this->assertTextEquals($expected, $output);
  157. }
  158. /**
  159. * test that subCommands with a existing APP command returns the proper sub commands (in this case none)
  160. *
  161. * @return void
  162. */
  163. public function testSubCommandsAppPlugin()
  164. {
  165. $this->Shell->runCommand(['subcommands', 'app.sample']);
  166. $output = $this->out->output;
  167. $expected = '';
  168. $this->assertEquals($expected, $output);
  169. }
  170. /**
  171. * test that subCommands with an existing plugin command returns the proper sub commands
  172. * when the Shell name is unique and the dot notation not mandatory
  173. *
  174. * @return void
  175. */
  176. public function testSubCommandsPlugin()
  177. {
  178. $this->Shell->runCommand(['subcommands', 'welcome']);
  179. $output = $this->out->output;
  180. $expected = "say_hello\n";
  181. $this->assertTextEquals($expected, $output);
  182. }
  183. /**
  184. * test that subCommands with an existing plugin command returns the proper sub commands
  185. *
  186. * @return void
  187. */
  188. public function testSubCommandsPluginDotNotation()
  189. {
  190. $this->Shell->runCommand(['subcommands', 'TestPluginTwo.example']);
  191. $output = $this->out->output;
  192. $expected = "say_hello\n";
  193. $this->assertTextEquals($expected, $output);
  194. }
  195. /**
  196. * test that subcommands without arguments returns nothing
  197. *
  198. * @return void
  199. */
  200. public function testSubCommandsNoArguments()
  201. {
  202. $this->Shell->runCommand(['subcommands']);
  203. $output = $this->out->output;
  204. $expected = '';
  205. $this->assertEquals($expected, $output);
  206. }
  207. /**
  208. * test that subcommands with a nonexisting command returns nothing
  209. *
  210. * @return void
  211. */
  212. public function testSubCommandsNonExistingCommand()
  213. {
  214. $this->Shell->runCommand(['subcommands', 'foo']);
  215. $output = $this->out->output;
  216. $expected = '';
  217. $this->assertEquals($expected, $output);
  218. }
  219. /**
  220. * test that subcommands returns the available subcommands for the given command
  221. *
  222. * @return void
  223. */
  224. public function testSubCommands()
  225. {
  226. $this->Shell->runCommand(['subcommands', 'orm_cache']);
  227. $output = $this->out->output;
  228. $expected = "build clear\n";
  229. $this->assertTextEquals($expected, $output);
  230. }
  231. /**
  232. * test that fuzzy returns nothing
  233. *
  234. * @return void
  235. */
  236. public function testFuzzy()
  237. {
  238. $this->Shell->runCommand(['fuzzy']);
  239. $output = $this->out->output;
  240. $expected = '';
  241. $this->assertEquals($expected, $output);
  242. }
  243. }