CompletionShellTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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\Shell\CompletionShell;
  17. use Cake\Shell\Task\CommandTask;
  18. use Cake\Console\ConsoleIo;
  19. use Cake\Console\ConsoleOutput;
  20. use Cake\Console\Shell;
  21. use Cake\Core\Plugin;
  22. use Cake\TestSuite\TestCase;
  23. /**
  24. * Class TestCompletionStringOutput
  25. *
  26. */
  27. class TestCompletionStringOutput extends ConsoleOutput {
  28. public $output = '';
  29. protected function _write($message) {
  30. $this->output .= $message;
  31. }
  32. }
  33. /**
  34. * Class CompletionShellTest
  35. */
  36. class CompletionShellTest extends TestCase {
  37. /**
  38. * setUp method
  39. *
  40. * @return void
  41. */
  42. public function setUp() {
  43. parent::setUp();
  44. Plugin::load(array('TestPlugin', 'TestPluginTwo'));
  45. $this->out = new TestCompletionStringOutput();
  46. $io = new ConsoleIo($this->out);
  47. $this->Shell = $this->getMock(
  48. 'Cake\Shell\CompletionShell',
  49. ['in', '_stop', 'clear'],
  50. [$io]
  51. );
  52. $this->Shell->Command = $this->getMock(
  53. 'Cake\Shell\Task\CommandTask',
  54. ['in', '_stop', 'clear'],
  55. [$io]
  56. );
  57. }
  58. /**
  59. * tearDown
  60. *
  61. * @return void
  62. */
  63. public function tearDown() {
  64. parent::tearDown();
  65. unset($this->Shell);
  66. Plugin::unload();
  67. }
  68. /**
  69. * test that the startup method supresses the shell header
  70. *
  71. * @return void
  72. */
  73. public function testStartup() {
  74. $this->Shell->runCommand(['main']);
  75. $output = $this->out->output;
  76. $needle = 'Welcome to CakePHP';
  77. $this->assertTextNotContains($needle, $output);
  78. }
  79. /**
  80. * test that main displays a warning
  81. *
  82. * @return void
  83. */
  84. public function testMain() {
  85. $this->Shell->runCommand(['main']);
  86. $output = $this->out->output;
  87. $expected = "/This command is not intended to be called manually/";
  88. $this->assertRegExp($expected, $output);
  89. }
  90. /**
  91. * test commands method that list all available commands
  92. *
  93. * @return void
  94. */
  95. public function testCommands() {
  96. $this->Shell->runCommand(['commands']);
  97. $output = $this->out->output;
  98. $expected = "TestPlugin.example TestPluginTwo.example TestPluginTwo.welcome bake i18n orm_cache server test sample\n";
  99. $this->assertTextEquals($expected, $output);
  100. }
  101. /**
  102. * test that options without argument returns the default options
  103. *
  104. * @return void
  105. */
  106. public function testOptionsNoArguments() {
  107. $this->Shell->runCommand(['options']);
  108. $output = $this->out->output;
  109. $expected = "--help -h --verbose -v --quiet -q\n";
  110. $this->assertTextEquals($expected, $output);
  111. }
  112. /**
  113. * test that options with a nonexisting command returns the default options
  114. *
  115. * @return void
  116. */
  117. public function testOptionsNonExistingCommand() {
  118. $this->Shell->runCommand(['options', 'foo']);
  119. $output = $this->out->output;
  120. $expected = "--help -h --verbose -v --quiet -q\n";
  121. $this->assertTextEquals($expected, $output);
  122. }
  123. /**
  124. * test that options with a existing command returns the proper options
  125. *
  126. * @return void
  127. */
  128. public function testOptions() {
  129. $this->Shell->runCommand(['options', 'bake']);
  130. $output = $this->out->output;
  131. $expected = "--help -h --verbose -v --quiet -q --connection -c --template -t\n";
  132. $this->assertTextEquals($expected, $output);
  133. }
  134. /**
  135. * test that subCommands with a existing CORE command returns the proper sub commands
  136. *
  137. * @return void
  138. */
  139. public function testSubCommandsCorePlugin() {
  140. $this->Shell->runCommand(['subcommands', 'CORE.bake']);
  141. $output = $this->out->output;
  142. $expected = "behavior cell component controller fixture helper model plugin project shell test view widget zerg\n";
  143. $this->assertTextEquals($expected, $output);
  144. }
  145. /**
  146. * test that subCommands with a existing APP command returns the proper sub commands (in this case none)
  147. *
  148. * @return void
  149. */
  150. public function testSubCommandsAppPlugin() {
  151. $this->Shell->runCommand(['subcommands', 'app.sample']);
  152. $output = $this->out->output;
  153. $expected = '';
  154. $this->assertEquals($expected, $output);
  155. }
  156. /**
  157. * test that subCommands with a existing plugin command returns the proper sub commands
  158. *
  159. * @return void
  160. */
  161. public function testSubCommandsPlugin() {
  162. $this->Shell->runCommand(['subcommands', 'TestPluginTwo.welcome']);
  163. $output = $this->out->output;
  164. $expected = "say_hello\n";
  165. $this->assertTextEquals($expected, $output);
  166. }
  167. /**
  168. * test that subcommands without arguments returns nothing
  169. *
  170. * @return void
  171. */
  172. public function testSubCommandsNoArguments() {
  173. $this->Shell->runCommand(['subcommands']);
  174. $output = $this->out->output;
  175. $expected = '';
  176. $this->assertEquals($expected, $output);
  177. }
  178. /**
  179. * test that subcommands with a nonexisting command returns nothing
  180. *
  181. * @return void
  182. */
  183. public function testSubCommandsNonExistingCommand() {
  184. $this->Shell->runCommand(['subcommands', 'foo']);
  185. $output = $this->out->output;
  186. $expected = '';
  187. $this->assertEquals($expected, $output);
  188. }
  189. /**
  190. * test that subcommands returns the available subcommands for the given command
  191. *
  192. * @return void
  193. */
  194. public function testSubCommands() {
  195. $this->Shell->runCommand(['subcommands', 'bake']);
  196. $output = $this->out->output;
  197. $expected = "behavior cell component controller fixture helper model plugin project shell test view widget zerg\n";
  198. $this->assertTextEquals($expected, $output);
  199. }
  200. /**
  201. * test that fuzzy returns nothing
  202. *
  203. * @return void
  204. */
  205. public function testFuzzy() {
  206. $this->Shell->runCommand(['fuzzy']);
  207. $output = $this->out->output;
  208. $expected = '';
  209. $this->assertEquals($expected, $output);
  210. }
  211. }