CompletionCommandTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. 'returnValue',
  200. 'sample',
  201. 'withAbort',
  202. ];
  203. foreach ($expected as $value) {
  204. $this->assertOutputContains($value);
  205. }
  206. //Methods overwritten from Shell class should not be included
  207. $notExpected = [
  208. 'runCommand',
  209. 'getOptionParser',
  210. ];
  211. foreach ($notExpected as $method) {
  212. $this->assertOutputNotContains($method);
  213. }
  214. }
  215. /**
  216. * test that subCommands with a existing CORE command
  217. *
  218. * @return void
  219. */
  220. public function testSubCommandsCoreMultiwordCommand()
  221. {
  222. $this->exec('completion subcommands cache');
  223. $this->assertExitCode(Shell::CODE_SUCCESS);
  224. $expected = [
  225. 'list', 'clear', 'clear_all',
  226. ];
  227. foreach ($expected as $value) {
  228. $this->assertOutputContains($value);
  229. }
  230. }
  231. /**
  232. * test that subCommands with an existing plugin command returns the proper sub commands
  233. * when the Shell name is unique and the dot notation not mandatory
  234. *
  235. * @return void
  236. */
  237. public function testSubCommandsPlugin()
  238. {
  239. $this->exec('completion subcommands welcome');
  240. $this->assertExitCode(Shell::CODE_SUCCESS);
  241. $expected = "say_hello";
  242. $this->assertOutputContains($expected);
  243. }
  244. /**
  245. * test that using the dot notation when not mandatory works to provide backward compatibility
  246. *
  247. * @return void
  248. */
  249. public function testSubCommandsPluginDotNotationBackwardCompatibility()
  250. {
  251. $this->exec('completion subcommands test_plugin_two.welcome');
  252. $this->assertExitCode(Shell::CODE_SUCCESS);
  253. $expected = "say_hello";
  254. $this->assertOutputContains($expected);
  255. }
  256. /**
  257. * test that subCommands with an existing plugin command returns the proper sub commands
  258. *
  259. * @return void
  260. */
  261. public function testSubCommandsPluginDotNotation()
  262. {
  263. $this->exec('completion subcommands test_plugin_two.example');
  264. $this->assertExitCode(Shell::CODE_SUCCESS);
  265. $expected = "say_hello";
  266. $this->assertOutputContains($expected);
  267. }
  268. /**
  269. * test that subCommands with an app shell that is also defined in a plugin and without the prefix "app."
  270. * returns proper sub commands
  271. *
  272. * @return void
  273. */
  274. public function testSubCommandsAppDuplicatePluginNoDot()
  275. {
  276. $this->exec('completion subcommands sample');
  277. $this->assertExitCode(Shell::CODE_SUCCESS);
  278. $expected = [
  279. 'derp',
  280. 'returnValue',
  281. 'sample',
  282. 'withAbort',
  283. ];
  284. foreach ($expected as $value) {
  285. $this->assertOutputContains($value);
  286. }
  287. }
  288. /**
  289. * test that subCommands with a plugin shell that is also defined in the returns proper sub commands
  290. *
  291. * @return void
  292. */
  293. public function testSubCommandsPluginDuplicateApp()
  294. {
  295. $this->exec('completion subcommands test_plugin.sample');
  296. $this->assertExitCode(Shell::CODE_SUCCESS);
  297. $expected = "example";
  298. $this->assertOutputContains($expected);
  299. }
  300. /**
  301. * test that subcommands without arguments returns nothing
  302. *
  303. * @return void
  304. */
  305. public function testSubCommandsNoArguments()
  306. {
  307. $this->exec('completion subcommands');
  308. $this->assertExitCode(Shell::CODE_SUCCESS);
  309. $this->assertOutputEmpty();
  310. }
  311. /**
  312. * test that subcommands with a non-existing command returns nothing
  313. *
  314. * @return void
  315. */
  316. public function testSubCommandsNonExistingCommand()
  317. {
  318. $this->exec('completion subcommands foo');
  319. $this->assertExitCode(Shell::CODE_SUCCESS);
  320. $this->assertOutputEmpty();
  321. }
  322. /**
  323. * test that subcommands returns the available subcommands for the given command
  324. *
  325. * @return void
  326. */
  327. public function testSubCommands()
  328. {
  329. $this->exec('completion subcommands schema_cache');
  330. $this->assertExitCode(Shell::CODE_SUCCESS);
  331. $expected = "build clear";
  332. $this->assertOutputContains($expected);
  333. }
  334. /**
  335. * test that fuzzy returns nothing
  336. *
  337. * @return void
  338. */
  339. public function testFuzzy()
  340. {
  341. $this->exec('completion fuzzy');
  342. $this->assertOutputEmpty();
  343. }
  344. /**
  345. * test that help returns content
  346. *
  347. * @return void
  348. */
  349. public function testHelp()
  350. {
  351. $this->exec('completion --help');
  352. $this->assertExitCode(Shell::CODE_SUCCESS);
  353. $this->assertOutputContains('Output a list of available commands');
  354. $this->assertOutputContains('Output a list of available sub-commands');
  355. }
  356. }