CompletionCommandTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. //details: https://github.com/cakephp/cakephp/pull/13533
  147. $this->markTestIncomplete(
  148. 'This test does not work correctly with shells.'
  149. );
  150. $this->exec('completion options sample sample');
  151. $this->assertExitCode(Shell::CODE_SUCCESS);
  152. $expected = [
  153. '--help -h',
  154. '--quiet -q',
  155. '--sample -s',
  156. '--verbose -v',
  157. ];
  158. foreach ($expected as $value) {
  159. $this->assertOutputContains($value);
  160. }
  161. }
  162. /**
  163. * test that options with an existing command / subcommand pair returns the proper options
  164. *
  165. * @return void
  166. */
  167. public function testOptionsShellCommand()
  168. {
  169. $this->exec('completion options cache list');
  170. $this->assertExitCode(Shell::CODE_SUCCESS);
  171. $expected = [
  172. '--help -h',
  173. '--quiet -q',
  174. '--verbose -v',
  175. ];
  176. foreach ($expected as $value) {
  177. $this->assertOutputContains($value);
  178. }
  179. }
  180. /**
  181. * test that nested command returns subcommand's options not command.
  182. *
  183. * @return void
  184. */
  185. public function testOptionsNestedCommand()
  186. {
  187. $this->exec('completion options i18n extract');
  188. $this->assertExitCode(Shell::CODE_SUCCESS);
  189. $expected = [
  190. '--plugin',
  191. '--app',
  192. ];
  193. foreach ($expected as $value) {
  194. $this->assertOutputContains($value);
  195. }
  196. }
  197. /**
  198. * test that subCommands with a existing CORE command returns the proper sub commands
  199. *
  200. * @return void
  201. */
  202. public function testSubCommandsCorePlugin()
  203. {
  204. $this->exec('completion subcommands schema_cache');
  205. $this->assertExitCode(Shell::CODE_SUCCESS);
  206. $expected = "build clear";
  207. $this->assertOutputContains($expected);
  208. }
  209. /**
  210. * test that subCommands with a existing APP command returns the proper sub commands (in this case none)
  211. *
  212. * @return void
  213. */
  214. public function testSubCommandsAppPlugin()
  215. {
  216. $this->exec('completion subcommands sample');
  217. $this->assertExitCode(Shell::CODE_SUCCESS);
  218. $expected = [
  219. 'derp',
  220. 'returnValue',
  221. 'sample',
  222. 'withAbort',
  223. ];
  224. foreach ($expected as $value) {
  225. $this->assertOutputContains($value);
  226. }
  227. //Methods overwritten from Shell class should not be included
  228. $notExpected = [
  229. 'runCommand',
  230. 'getOptionParser',
  231. ];
  232. foreach ($notExpected as $method) {
  233. $this->assertOutputNotContains($method);
  234. }
  235. }
  236. /**
  237. * test that subCommands with a existing CORE command
  238. *
  239. * @return void
  240. */
  241. public function testSubCommandsCoreMultiwordCommand()
  242. {
  243. $this->exec('completion subcommands cache');
  244. $this->assertExitCode(Shell::CODE_SUCCESS);
  245. $expected = [
  246. 'list', 'clear', 'clear_all',
  247. ];
  248. foreach ($expected as $value) {
  249. $this->assertOutputContains($value);
  250. }
  251. }
  252. /**
  253. * test that subCommands with an existing plugin command returns the proper sub commands
  254. * when the Shell name is unique and the dot notation not mandatory
  255. *
  256. * @return void
  257. */
  258. public function testSubCommandsPlugin()
  259. {
  260. $this->exec('completion subcommands welcome');
  261. $this->assertExitCode(Shell::CODE_SUCCESS);
  262. $expected = "say_hello";
  263. $this->assertOutputContains($expected);
  264. }
  265. /**
  266. * test that using the dot notation when not mandatory works to provide backward compatibility
  267. *
  268. * @return void
  269. */
  270. public function testSubCommandsPluginDotNotationBackwardCompatibility()
  271. {
  272. $this->exec('completion subcommands test_plugin_two.welcome');
  273. $this->assertExitCode(Shell::CODE_SUCCESS);
  274. $expected = "say_hello";
  275. $this->assertOutputContains($expected);
  276. }
  277. /**
  278. * test that subCommands with an existing plugin command returns the proper sub commands
  279. *
  280. * @return void
  281. */
  282. public function testSubCommandsPluginDotNotation()
  283. {
  284. $this->exec('completion subcommands test_plugin_two.example');
  285. $this->assertExitCode(Shell::CODE_SUCCESS);
  286. $expected = "say_hello";
  287. $this->assertOutputContains($expected);
  288. }
  289. /**
  290. * test that subCommands with an app shell that is also defined in a plugin and without the prefix "app."
  291. * returns proper sub commands
  292. *
  293. * @return void
  294. */
  295. public function testSubCommandsAppDuplicatePluginNoDot()
  296. {
  297. $this->exec('completion subcommands sample');
  298. $this->assertExitCode(Shell::CODE_SUCCESS);
  299. $expected = [
  300. 'derp',
  301. 'returnValue',
  302. 'sample',
  303. 'withAbort',
  304. ];
  305. foreach ($expected as $value) {
  306. $this->assertOutputContains($value);
  307. }
  308. }
  309. /**
  310. * test that subCommands with a plugin shell that is also defined in the returns proper sub commands
  311. *
  312. * @return void
  313. */
  314. public function testSubCommandsPluginDuplicateApp()
  315. {
  316. $this->exec('completion subcommands test_plugin.sample');
  317. $this->assertExitCode(Shell::CODE_SUCCESS);
  318. $expected = "example";
  319. $this->assertOutputContains($expected);
  320. }
  321. /**
  322. * test that subcommands without arguments returns nothing
  323. *
  324. * @return void
  325. */
  326. public function testSubCommandsNoArguments()
  327. {
  328. $this->exec('completion subcommands');
  329. $this->assertExitCode(Shell::CODE_SUCCESS);
  330. $this->assertOutputEmpty();
  331. }
  332. /**
  333. * test that subcommands with a non-existing command returns nothing
  334. *
  335. * @return void
  336. */
  337. public function testSubCommandsNonExistingCommand()
  338. {
  339. $this->exec('completion subcommands foo');
  340. $this->assertExitCode(Shell::CODE_SUCCESS);
  341. $this->assertOutputEmpty();
  342. }
  343. /**
  344. * test that subcommands returns the available subcommands for the given command
  345. *
  346. * @return void
  347. */
  348. public function testSubCommands()
  349. {
  350. $this->exec('completion subcommands schema_cache');
  351. $this->assertExitCode(Shell::CODE_SUCCESS);
  352. $expected = "build clear";
  353. $this->assertOutputContains($expected);
  354. }
  355. /**
  356. * test that fuzzy returns nothing
  357. *
  358. * @return void
  359. */
  360. public function testFuzzy()
  361. {
  362. $this->exec('completion fuzzy');
  363. $this->assertOutputEmpty();
  364. }
  365. /**
  366. * test that help returns content
  367. *
  368. * @return void
  369. */
  370. public function testHelp()
  371. {
  372. $this->exec('completion --help');
  373. $this->assertExitCode(Shell::CODE_SUCCESS);
  374. $this->assertOutputContains('Output a list of available commands');
  375. $this->assertOutputContains('Output a list of available sub-commands');
  376. }
  377. }