CompletionShellTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 " .
  107. "TestPluginTwo.example TestPluginTwo.welcome i18n orm_cache plugin routes server sample\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 a existing plugin command returns the proper sub commands
  172. *
  173. * @return void
  174. */
  175. public function testSubCommandsPlugin()
  176. {
  177. $this->Shell->runCommand(['subcommands', 'TestPluginTwo.welcome']);
  178. $output = $this->out->output;
  179. $expected = "say_hello\n";
  180. $this->assertTextEquals($expected, $output);
  181. }
  182. /**
  183. * test that subcommands without arguments returns nothing
  184. *
  185. * @return void
  186. */
  187. public function testSubCommandsNoArguments()
  188. {
  189. $this->Shell->runCommand(['subcommands']);
  190. $output = $this->out->output;
  191. $expected = '';
  192. $this->assertEquals($expected, $output);
  193. }
  194. /**
  195. * test that subcommands with a nonexisting command returns nothing
  196. *
  197. * @return void
  198. */
  199. public function testSubCommandsNonExistingCommand()
  200. {
  201. $this->Shell->runCommand(['subcommands', 'foo']);
  202. $output = $this->out->output;
  203. $expected = '';
  204. $this->assertEquals($expected, $output);
  205. }
  206. /**
  207. * test that subcommands returns the available subcommands for the given command
  208. *
  209. * @return void
  210. */
  211. public function testSubCommands()
  212. {
  213. $this->Shell->runCommand(['subcommands', 'orm_cache']);
  214. $output = $this->out->output;
  215. $expected = "build clear\n";
  216. $this->assertTextEquals($expected, $output);
  217. }
  218. /**
  219. * test that fuzzy returns nothing
  220. *
  221. * @return void
  222. */
  223. public function testFuzzy()
  224. {
  225. $this->Shell->runCommand(['fuzzy']);
  226. $output = $this->out->output;
  227. $expected = '';
  228. $this->assertEquals($expected, $output);
  229. }
  230. }