CompletionCommandTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP Project
  13. * @since 2.5.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Command;
  17. use Cake\Command\Command;
  18. use Cake\Core\Configure;
  19. use Cake\Routing\Router;
  20. use Cake\TestSuite\ConsoleIntegrationTestTrait;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * CompletionCommandTest
  24. */
  25. class CompletionCommandTest extends TestCase
  26. {
  27. use ConsoleIntegrationTestTrait;
  28. /**
  29. * setUp method
  30. *
  31. * @return void
  32. */
  33. public function setUp(): void
  34. {
  35. parent::setUp();
  36. static::setAppNamespace();
  37. Configure::write('Plugins.autoload', ['TestPlugin', 'TestPluginTwo']);
  38. }
  39. /**
  40. * tearDown
  41. *
  42. * @return void
  43. */
  44. public function tearDown(): void
  45. {
  46. parent::tearDown();
  47. Router::reload();
  48. $this->clearPlugins();
  49. }
  50. /**
  51. * test that the startup method suppresses the command header
  52. *
  53. * @return void
  54. */
  55. public function testStartup()
  56. {
  57. $this->exec('completion');
  58. $this->assertExitCode(Command::CODE_ERROR);
  59. $this->assertOutputNotContains('Welcome to CakePHP');
  60. }
  61. /**
  62. * test commands method that list all available commands
  63. *
  64. * @return void
  65. */
  66. public function testCommands()
  67. {
  68. $this->exec('completion commands');
  69. $this->assertExitCode(Command::CODE_SUCCESS);
  70. $expected = [
  71. 'test_plugin.example',
  72. 'test_plugin.sample',
  73. 'test_plugin_two.example',
  74. 'unique',
  75. 'welcome',
  76. 'cache',
  77. 'help',
  78. 'i18n',
  79. 'plugin',
  80. 'routes',
  81. 'schema_cache',
  82. 'server',
  83. 'version',
  84. 'abort',
  85. 'auto_load_model',
  86. 'demo',
  87. 'integration',
  88. 'sample',
  89. ];
  90. foreach ($expected as $value) {
  91. $this->assertOutputContains($value);
  92. }
  93. }
  94. /**
  95. * test that options without argument returns nothing
  96. *
  97. * @return void
  98. */
  99. public function testOptionsNoArguments()
  100. {
  101. $this->exec('completion options');
  102. $this->assertExitCode(Command::CODE_SUCCESS);
  103. $this->assertOutputEmpty();
  104. }
  105. /**
  106. * test that options with a nonexistent command returns nothing
  107. *
  108. * @return void
  109. */
  110. public function testOptionsNonExistentCommand()
  111. {
  112. $this->exec('completion options foo');
  113. $this->assertExitCode(Command::CODE_SUCCESS);
  114. $this->assertOutputEmpty();
  115. }
  116. /**
  117. * test that options with an existing command returns the proper options
  118. *
  119. * @return void
  120. */
  121. public function testOptionsCommand()
  122. {
  123. $this->exec('completion options schema_cache');
  124. $this->assertExitCode(Command::CODE_SUCCESS);
  125. $expected = [
  126. '--connection -c',
  127. '--help -h',
  128. '--quiet -q',
  129. '--verbose -v',
  130. ];
  131. foreach ($expected as $value) {
  132. $this->assertOutputContains($value);
  133. }
  134. }
  135. /**
  136. * test that options with an existing command / subcommand pair returns the proper options
  137. *
  138. * @return void
  139. */
  140. public function testOptionsSubCommand()
  141. {
  142. $this->exec('completion options cache list');
  143. $this->assertExitCode(Command::CODE_SUCCESS);
  144. $expected = [
  145. '--help -h',
  146. '--quiet -q',
  147. '--verbose -v',
  148. ];
  149. foreach ($expected as $value) {
  150. $this->assertOutputContains($value);
  151. }
  152. }
  153. /**
  154. * test that nested command returns subcommand's options not command.
  155. *
  156. * @return void
  157. */
  158. public function testOptionsNestedCommand()
  159. {
  160. $this->exec('completion options i18n extract');
  161. $this->assertExitCode(Command::CODE_SUCCESS);
  162. $expected = [
  163. '--plugin',
  164. '--app',
  165. ];
  166. foreach ($expected as $value) {
  167. $this->assertOutputContains($value);
  168. }
  169. }
  170. /**
  171. * test that subCommands with a existing CORE command returns the proper sub commands
  172. *
  173. * @return void
  174. */
  175. public function testSubCommandsCorePlugin()
  176. {
  177. $this->exec('completion subcommands schema_cache');
  178. $this->assertExitCode(Command::CODE_SUCCESS);
  179. $expected = 'build clear';
  180. $this->assertOutputContains($expected);
  181. }
  182. /**
  183. * test that subCommands with a existing APP command returns the proper sub commands (in this case none)
  184. *
  185. * @return void
  186. */
  187. public function testSubCommandsAppPlugin()
  188. {
  189. $this->exec('completion subcommands sample');
  190. $this->assertExitCode(Command::CODE_SUCCESS);
  191. $this->assertOutputContains('sub');
  192. }
  193. /**
  194. * test that subCommands with a existing CORE command
  195. *
  196. * @return void
  197. */
  198. public function testSubCommandsCoreMultiwordCommand()
  199. {
  200. $this->exec('completion subcommands cache');
  201. $this->assertExitCode(Command::CODE_SUCCESS);
  202. $expected = [
  203. 'list', 'clear', 'clear_all',
  204. ];
  205. foreach ($expected as $value) {
  206. $this->assertOutputContains($value);
  207. }
  208. }
  209. /**
  210. * test that subCommands with an existing plugin command returns the proper sub commands
  211. * when the command name is unique and the dot notation not mandatory
  212. *
  213. * @return void
  214. */
  215. public function testSubCommandsPlugin()
  216. {
  217. $this->exec('completion subcommands welcome');
  218. $this->assertExitCode(Command::CODE_SUCCESS);
  219. $expected = 'say_hello';
  220. $this->assertOutputContains($expected);
  221. }
  222. /**
  223. * test that using the dot notation when not mandatory works to provide backward compatibility
  224. *
  225. * @return void
  226. */
  227. public function testSubCommandsPluginDotNotationBackwardCompatibility()
  228. {
  229. $this->exec('completion subcommands test_plugin_two.welcome');
  230. $this->assertExitCode(Command::CODE_SUCCESS);
  231. $expected = 'say_hello';
  232. $this->assertOutputContains($expected);
  233. }
  234. /**
  235. * test that subCommands with an app command that is also defined in a plugin and without the prefix "app."
  236. * returns proper sub commands
  237. *
  238. * @return void
  239. */
  240. public function testSubCommandsAppDuplicatePluginNoDot()
  241. {
  242. $this->exec('completion subcommands sample');
  243. $this->assertExitCode(Command::CODE_SUCCESS);
  244. $this->assertOutputContains('sub');
  245. }
  246. /**
  247. * test that subCommands with a plugin command that is also defined in the returns proper sub commands
  248. *
  249. * @return void
  250. */
  251. public function testSubCommandsPluginDuplicateApp()
  252. {
  253. $this->exec('completion subcommands test_plugin.sample');
  254. $this->assertExitCode(Command::CODE_SUCCESS);
  255. $expected = 'sub';
  256. $this->assertOutputContains($expected);
  257. }
  258. /**
  259. * test that subcommands without arguments returns nothing
  260. *
  261. * @return void
  262. */
  263. public function testSubCommandsNoArguments()
  264. {
  265. $this->exec('completion subcommands');
  266. $this->assertExitCode(Command::CODE_SUCCESS);
  267. $this->assertOutputEmpty();
  268. }
  269. /**
  270. * test that subcommands with a nonexistent command returns nothing
  271. *
  272. * @return void
  273. */
  274. public function testSubCommandsNonExistentCommand()
  275. {
  276. $this->exec('completion subcommands foo');
  277. $this->assertExitCode(Command::CODE_SUCCESS);
  278. $this->assertOutputEmpty();
  279. }
  280. /**
  281. * test that subcommands returns the available subcommands for the given command
  282. *
  283. * @return void
  284. */
  285. public function testSubCommands()
  286. {
  287. $this->exec('completion subcommands schema_cache');
  288. $this->assertExitCode(Command::CODE_SUCCESS);
  289. $expected = 'build clear';
  290. $this->assertOutputContains($expected);
  291. }
  292. /**
  293. * test that fuzzy returns nothing
  294. *
  295. * @return void
  296. */
  297. public function testFuzzy()
  298. {
  299. $this->exec('completion fuzzy');
  300. $this->assertOutputEmpty();
  301. }
  302. /**
  303. * test that help returns content
  304. *
  305. * @return void
  306. */
  307. public function testHelp()
  308. {
  309. $this->exec('completion --help');
  310. $this->assertExitCode(Command::CODE_SUCCESS);
  311. $this->assertOutputContains('Output a list of available commands');
  312. $this->assertOutputContains('Output a list of available sub-commands');
  313. }
  314. }