CompletionCommandTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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\Console\Shell;
  18. use Cake\Core\Configure;
  19. use Cake\Routing\Router;
  20. use Cake\TestSuite\ConsoleIntegrationTestCase;
  21. /**
  22. * CompletionCommandTest
  23. */
  24. class CompletionCommandTest extends ConsoleIntegrationTestCase
  25. {
  26. /**
  27. * setUp method
  28. *
  29. * @return void
  30. */
  31. public function setUp(): void
  32. {
  33. parent::setUp();
  34. static::setAppNamespace();
  35. Configure::write('Plugins.autoload', ['TestPlugin', 'TestPluginTwo']);
  36. $this->useCommandRunner();
  37. }
  38. /**
  39. * tearDown
  40. *
  41. * @return void
  42. */
  43. public function tearDown(): void
  44. {
  45. parent::tearDown();
  46. Router::reload();
  47. $this->clearPlugins();
  48. }
  49. /**
  50. * test that the startup method suppresses the shell header
  51. *
  52. * @return void
  53. */
  54. public function testStartup()
  55. {
  56. $this->exec('completion');
  57. $this->assertExitCode(Shell::CODE_ERROR);
  58. $this->assertOutputNotContains('Welcome to CakePHP');
  59. }
  60. /**
  61. * test commands method that list all available commands
  62. *
  63. * @return void
  64. */
  65. public function testCommands()
  66. {
  67. $this->exec('completion commands');
  68. $this->assertExitCode(Shell::CODE_SUCCESS);
  69. $expected = [
  70. 'test_plugin.example',
  71. 'test_plugin.sample',
  72. 'test_plugin_two.example',
  73. 'unique',
  74. 'welcome',
  75. 'cache',
  76. 'help',
  77. 'i18n',
  78. 'plugin',
  79. 'routes',
  80. 'schema_cache',
  81. 'server',
  82. 'upgrade',
  83. 'version',
  84. 'abort',
  85. 'auto_load_model',
  86. 'demo',
  87. 'i18m',
  88. 'integration',
  89. 'merge',
  90. 'sample',
  91. 'shell_test',
  92. 'testing_dispatch',
  93. ];
  94. foreach ($expected as $value) {
  95. $this->assertOutputContains($value);
  96. }
  97. }
  98. /**
  99. * test that options without argument returns nothing
  100. *
  101. * @return void
  102. */
  103. public function testOptionsNoArguments()
  104. {
  105. $this->exec('completion options');
  106. $this->assertExitCode(Shell::CODE_SUCCESS);
  107. $this->assertOutputEmpty();
  108. }
  109. /**
  110. * test that options with a non-existing command returns nothing
  111. *
  112. * @return void
  113. */
  114. public function testOptionsNonExistingCommand()
  115. {
  116. $this->exec('completion options foo');
  117. $this->assertExitCode(Shell::CODE_SUCCESS);
  118. $this->assertOutputEmpty();
  119. }
  120. /**
  121. * test that options with an existing command returns the proper options
  122. *
  123. * @return void
  124. */
  125. public function testOptionsShell()
  126. {
  127. $this->exec('completion options schema_cache');
  128. $this->assertExitCode(Shell::CODE_SUCCESS);
  129. $expected = [
  130. '--connection -c',
  131. '--help -h',
  132. '--quiet -q',
  133. '--verbose -v',
  134. ];
  135. foreach ($expected as $value) {
  136. $this->assertOutputContains($value);
  137. }
  138. }
  139. /**
  140. * test that options with an existing command / subcommand pair returns the proper options
  141. *
  142. * @return void
  143. */
  144. public function testOptionsShellTask()
  145. {
  146. $this->exec('completion options sample sample');
  147. $this->assertExitCode(Shell::CODE_SUCCESS);
  148. $expected = [
  149. '--help -h',
  150. '--quiet -q',
  151. '--sample -s',
  152. '--verbose -v',
  153. ];
  154. foreach ($expected as $value) {
  155. $this->assertOutputContains($value);
  156. }
  157. }
  158. /**
  159. * test that options with an existing command / subcommand pair returns the proper options
  160. *
  161. * @return void
  162. */
  163. public function testOptionsShellCommand()
  164. {
  165. $this->exec('completion options cache list');
  166. $this->assertExitCode(Shell::CODE_SUCCESS);
  167. $expected = [
  168. '--help -h',
  169. '--quiet -q',
  170. '--verbose -v',
  171. ];
  172. foreach ($expected as $value) {
  173. $this->assertOutputContains($value);
  174. }
  175. }
  176. /**
  177. * test that subCommands with a existing CORE command returns the proper sub commands
  178. *
  179. * @return void
  180. */
  181. public function testSubCommandsCorePlugin()
  182. {
  183. $this->exec('completion subcommands schema_cache');
  184. $this->assertExitCode(Shell::CODE_SUCCESS);
  185. $expected = "build clear";
  186. $this->assertOutputContains($expected);
  187. }
  188. /**
  189. * test that subCommands with a existing APP command returns the proper sub commands (in this case none)
  190. *
  191. * @return void
  192. */
  193. public function testSubCommandsAppPlugin()
  194. {
  195. $this->exec('completion subcommands sample');
  196. $this->assertExitCode(Shell::CODE_SUCCESS);
  197. $expected = [
  198. 'derp',
  199. 'load',
  200. 'returnValue',
  201. 'sample',
  202. 'withAbort',
  203. ];
  204. foreach ($expected as $value) {
  205. $this->assertOutputContains($value);
  206. }
  207. //Methods overwritten from Shell class should not be included
  208. $notExpected = [
  209. 'runCommand',
  210. 'getOptionParser'
  211. ];
  212. foreach($notExpected as $method) {
  213. $this->assertOutputNotContains($method);
  214. }
  215. }
  216. /**
  217. * test that subCommands with a existing CORE command
  218. *
  219. * @return void
  220. */
  221. public function testSubCommandsCoreMultiwordCommand()
  222. {
  223. $this->exec('completion subcommands cache');
  224. $this->assertExitCode(Shell::CODE_SUCCESS);
  225. $expected = [
  226. 'list', 'clear', 'clearall',
  227. ];
  228. foreach ($expected as $value) {
  229. $this->assertOutputContains($value);
  230. }
  231. }
  232. /**
  233. * test that subCommands with an existing plugin command returns the proper sub commands
  234. * when the Shell name is unique and the dot notation not mandatory
  235. *
  236. * @return void
  237. */
  238. public function testSubCommandsPlugin()
  239. {
  240. $this->exec('completion subcommands welcome');
  241. $this->assertExitCode(Shell::CODE_SUCCESS);
  242. $expected = "say_hello";
  243. $this->assertOutputContains($expected);
  244. }
  245. /**
  246. * test that using the dot notation when not mandatory works to provide backward compatibility
  247. *
  248. * @return void
  249. */
  250. public function testSubCommandsPluginDotNotationBackwardCompatibility()
  251. {
  252. $this->exec('completion subcommands test_plugin_two.welcome');
  253. $this->assertExitCode(Shell::CODE_SUCCESS);
  254. $expected = "say_hello";
  255. $this->assertOutputContains($expected);
  256. }
  257. /**
  258. * test that subCommands with an existing plugin command returns the proper sub commands
  259. *
  260. * @return void
  261. */
  262. public function testSubCommandsPluginDotNotation()
  263. {
  264. $this->exec('completion subcommands test_plugin_two.example');
  265. $this->assertExitCode(Shell::CODE_SUCCESS);
  266. $expected = "say_hello";
  267. $this->assertOutputContains($expected);
  268. }
  269. /**
  270. * test that subCommands with an app shell that is also defined in a plugin and without the prefix "app."
  271. * returns proper sub commands
  272. *
  273. * @return void
  274. */
  275. public function testSubCommandsAppDuplicatePluginNoDot()
  276. {
  277. $this->exec('completion subcommands sample');
  278. $this->assertExitCode(Shell::CODE_SUCCESS);
  279. $expected = [
  280. 'derp',
  281. 'load',
  282. 'returnValue',
  283. 'sample',
  284. 'withAbort',
  285. ];
  286. foreach ($expected as $value) {
  287. $this->assertOutputContains($value);
  288. }
  289. }
  290. /**
  291. * test that subCommands with a plugin shell that is also defined in the returns proper sub commands
  292. *
  293. * @return void
  294. */
  295. public function testSubCommandsPluginDuplicateApp()
  296. {
  297. $this->exec('completion subcommands test_plugin.sample');
  298. $this->assertExitCode(Shell::CODE_SUCCESS);
  299. $expected = "example";
  300. $this->assertOutputContains($expected);
  301. }
  302. /**
  303. * test that subcommands without arguments returns nothing
  304. *
  305. * @return void
  306. */
  307. public function testSubCommandsNoArguments()
  308. {
  309. $this->exec('completion subcommands');
  310. $this->assertExitCode(Shell::CODE_SUCCESS);
  311. $this->assertOutputEmpty();
  312. }
  313. /**
  314. * test that subcommands with a non-existing command returns nothing
  315. *
  316. * @return void
  317. */
  318. public function testSubCommandsNonExistingCommand()
  319. {
  320. $this->exec('completion subcommands foo');
  321. $this->assertExitCode(Shell::CODE_SUCCESS);
  322. $this->assertOutputEmpty();
  323. }
  324. /**
  325. * test that subcommands returns the available subcommands for the given command
  326. *
  327. * @return void
  328. */
  329. public function testSubCommands()
  330. {
  331. $this->exec('completion subcommands schema_cache');
  332. $this->assertExitCode(Shell::CODE_SUCCESS);
  333. $expected = "build clear";
  334. $this->assertOutputContains($expected);
  335. }
  336. /**
  337. * test that fuzzy returns nothing
  338. *
  339. * @return void
  340. */
  341. public function testFuzzy()
  342. {
  343. $this->exec('completion fuzzy');
  344. $this->assertOutputEmpty();
  345. }
  346. /**
  347. * test that help returns content
  348. *
  349. * @return void
  350. */
  351. public function testHelp()
  352. {
  353. $this->exec('completion --help');
  354. $this->assertExitCode(Shell::CODE_SUCCESS);
  355. $this->assertOutputContains('Output a list of available commands');
  356. $this->assertOutputContains('Output a list of available sub-commands');
  357. }
  358. }