CommandCollectionTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  12. * @link http://cakephp.org CakePHP(tm) Project
  13. * @since 3.5.0
  14. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Console;
  17. use Cake\Command\RoutesCommand;
  18. use Cake\Command\VersionCommand;
  19. use Cake\Console\CommandCollection;
  20. use Cake\Core\Configure;
  21. use Cake\TestSuite\TestCase;
  22. use InvalidArgumentException;
  23. use stdClass;
  24. use TestApp\Command\DemoCommand;
  25. use TestApp\Shell\SampleShell;
  26. /**
  27. * Test case for the CommandCollection
  28. */
  29. class CommandCollectionTest extends TestCase
  30. {
  31. public function setUp(): void
  32. {
  33. parent::setUp();
  34. Configure::write('App.namespace', 'TestApp');
  35. }
  36. /**
  37. * Test constructor with valid classnames
  38. *
  39. * @return void
  40. */
  41. public function testConstructor()
  42. {
  43. $collection = new CommandCollection([
  44. 'sample' => SampleShell::class,
  45. 'routes' => RoutesCommand::class,
  46. ]);
  47. $this->assertTrue($collection->has('routes'));
  48. $this->assertTrue($collection->has('sample'));
  49. $this->assertCount(2, $collection);
  50. }
  51. /**
  52. * Constructor with invalid class names should blow up
  53. *
  54. * @return void
  55. */
  56. public function testConstructorInvalidClass()
  57. {
  58. $this->expectException(\InvalidArgumentException::class);
  59. $this->expectExceptionMessage('Cannot use \'stdClass\' for command \'nope\'. It is not a subclass of Cake\Console\Shell');
  60. new CommandCollection([
  61. 'sample' => SampleShell::class,
  62. 'nope' => stdClass::class,
  63. ]);
  64. }
  65. /**
  66. * Test basic add/get
  67. *
  68. * @return void
  69. */
  70. public function testAdd()
  71. {
  72. $collection = new CommandCollection();
  73. $this->assertSame($collection, $collection->add('routes', RoutesCommand::class));
  74. $this->assertTrue($collection->has('routes'));
  75. $this->assertSame(RoutesCommand::class, $collection->get('routes'));
  76. }
  77. /**
  78. * test adding a command instance.
  79. *
  80. * @return void
  81. */
  82. public function testAddCommand()
  83. {
  84. $collection = new CommandCollection();
  85. $this->assertSame($collection, $collection->add('ex', DemoCommand::class));
  86. $this->assertTrue($collection->has('ex'));
  87. $this->assertSame(DemoCommand::class, $collection->get('ex'));
  88. }
  89. /**
  90. * Test that add() replaces.
  91. *
  92. * @return void
  93. */
  94. public function testAddReplace()
  95. {
  96. $collection = new CommandCollection();
  97. $this->assertSame($collection, $collection->add('routes', RoutesCommand::class));
  98. $this->assertSame($collection, $collection->add('routes', SampleShell::class));
  99. $this->assertTrue($collection->has('routes'));
  100. $this->assertSame(SampleShell::class, $collection->get('routes'));
  101. }
  102. /**
  103. * Test adding with instances
  104. *
  105. * @return void
  106. */
  107. public function testAddInstance()
  108. {
  109. $collection = new CommandCollection();
  110. $command = new RoutesCommand();
  111. $collection->add('routes', $command);
  112. $this->assertTrue($collection->has('routes'));
  113. $this->assertSame($command, $collection->get('routes'));
  114. }
  115. /**
  116. * Instances that are not shells should fail.
  117. */
  118. public function testAddInvalidInstance()
  119. {
  120. $this->expectException(\InvalidArgumentException::class);
  121. $this->expectExceptionMessage('Cannot use \'stdClass\' for command \'routes\'. It is not a subclass of Cake\Console\Shell');
  122. $collection = new CommandCollection();
  123. $shell = new stdClass();
  124. $collection->add('routes', $shell);
  125. }
  126. /**
  127. * Provider for invalid names.
  128. *
  129. * @return array
  130. */
  131. public function invalidNameProvider()
  132. {
  133. return [
  134. // Empty
  135. [''],
  136. // Leading spaces
  137. [' spaced'],
  138. // Trailing spaces
  139. ['spaced '],
  140. // Too many words
  141. ['one two three four'],
  142. ];
  143. }
  144. /**
  145. * test adding a command instance.
  146. *
  147. * @dataProvider invalidNameProvider
  148. * @return void
  149. */
  150. public function testAddCommandInvalidName(string $name)
  151. {
  152. $this->expectException(InvalidArgumentException::class);
  153. $this->expectExceptionMessage("The command name `$name` is invalid.");
  154. $collection = new CommandCollection();
  155. $collection->add($name, DemoCommand::class);
  156. }
  157. /**
  158. * Class names that are not shells should fail
  159. */
  160. public function testInvalidShellClassName()
  161. {
  162. $this->expectException(\InvalidArgumentException::class);
  163. $this->expectExceptionMessage('Cannot use \'stdClass\' for command \'routes\'. It is not a subclass of Cake\Console\Shell');
  164. $collection = new CommandCollection();
  165. $collection->add('routes', stdClass::class);
  166. }
  167. /**
  168. * Test removing a command
  169. *
  170. * @return void
  171. */
  172. public function testRemove()
  173. {
  174. $collection = new CommandCollection();
  175. $collection->add('routes', RoutesCommand::class);
  176. $this->assertSame($collection, $collection->remove('routes'));
  177. $this->assertFalse($collection->has('routes'));
  178. }
  179. /**
  180. * Removing an unknown command does not fail
  181. *
  182. * @return void
  183. */
  184. public function testRemoveUnknown()
  185. {
  186. $collection = new CommandCollection();
  187. $this->assertSame($collection, $collection->remove('nope'));
  188. $this->assertFalse($collection->has('nope'));
  189. }
  190. /**
  191. * test getIterator
  192. *
  193. * @return void
  194. */
  195. public function testGetIterator()
  196. {
  197. $in = [
  198. 'sample' => SampleShell::class,
  199. 'routes' => RoutesCommand::class,
  200. ];
  201. $collection = new CommandCollection($in);
  202. $out = [];
  203. foreach ($collection as $key => $value) {
  204. $out[$key] = $value;
  205. }
  206. $this->assertEquals($in, $out);
  207. }
  208. /**
  209. * test autodiscovering app shells
  210. *
  211. * @return void
  212. */
  213. public function testAutoDiscoverApp()
  214. {
  215. $collection = new CommandCollection();
  216. $collection->addMany($collection->autoDiscover());
  217. $this->assertTrue($collection->has('app'));
  218. $this->assertTrue($collection->has('demo'));
  219. $this->assertTrue($collection->has('i18m'));
  220. $this->assertTrue($collection->has('sample'));
  221. $this->assertTrue($collection->has('testing_dispatch'));
  222. $this->assertSame('TestApp\Shell\AppShell', $collection->get('app'));
  223. $this->assertSame('TestApp\Command\DemoCommand', $collection->get('demo'));
  224. $this->assertSame('TestApp\Shell\I18mShell', $collection->get('i18m'));
  225. $this->assertSame('TestApp\Shell\SampleShell', $collection->get('sample'));
  226. }
  227. /**
  228. * test autodiscovering core shells
  229. *
  230. * @return void
  231. */
  232. public function testAutoDiscoverCore()
  233. {
  234. $collection = new CommandCollection();
  235. $collection->addMany($collection->autoDiscover());
  236. $this->assertTrue($collection->has('version'));
  237. $this->assertTrue($collection->has('routes'));
  238. $this->assertTrue($collection->has('sample'));
  239. $this->assertTrue($collection->has('schema_cache build'));
  240. $this->assertTrue($collection->has('schema_cache clear'));
  241. $this->assertTrue($collection->has('server'));
  242. $this->assertTrue($collection->has('cache clear'));
  243. $this->assertFalse($collection->has('command_list'), 'Hidden commands should stay hidden');
  244. // These have to be strings as ::class uses the local namespace.
  245. $this->assertSame(RoutesCommand::class, $collection->get('routes'));
  246. $this->assertSame(SampleShell::class, $collection->get('sample'));
  247. $this->assertSame(VersionCommand::class, $collection->get('version'));
  248. }
  249. /**
  250. * test missing plugin discovery
  251. *
  252. * @return void
  253. */
  254. public function testDiscoverPluginUnknown()
  255. {
  256. $collection = new CommandCollection();
  257. $this->assertSame([], $collection->discoverPlugin('Nope'));
  258. }
  259. /**
  260. * test autodiscovering plugin shells
  261. *
  262. * @return void
  263. */
  264. public function testDiscoverPlugin()
  265. {
  266. $this->loadPlugins(['TestPlugin', 'Company/TestPluginThree']);
  267. $collection = new CommandCollection();
  268. // Add a dupe to test de-duping
  269. $collection->add('sample', DemoCommand::class);
  270. $result = $collection->discoverPlugin('TestPlugin');
  271. $this->assertArrayHasKey(
  272. 'example',
  273. $result,
  274. 'Used short name for unique plugin shell'
  275. );
  276. $this->assertArrayHasKey(
  277. 'test_plugin.example',
  278. $result,
  279. 'Long names are stored for unique shells'
  280. );
  281. $this->assertArrayNotHasKey('sample', $result, 'Existing command not output');
  282. $this->assertArrayHasKey(
  283. 'test_plugin.sample',
  284. $result,
  285. 'Duplicate shell was given a full alias'
  286. );
  287. $this->assertSame('TestPlugin\Shell\ExampleShell', $result['example']);
  288. $this->assertSame($result['example'], $result['test_plugin.example']);
  289. $this->assertSame('TestPlugin\Shell\SampleShell', $result['test_plugin.sample']);
  290. $result = $collection->discoverPlugin('Company/TestPluginThree');
  291. $this->assertArrayHasKey(
  292. 'company',
  293. $result,
  294. 'Used short name for unique plugin shell'
  295. );
  296. $this->assertArrayHasKey(
  297. 'company/test_plugin_three.company',
  298. $result,
  299. 'Long names are stored as well'
  300. );
  301. $this->assertSame($result['company'], $result['company/test_plugin_three.company']);
  302. $this->clearPlugins();
  303. }
  304. /**
  305. * Test keys
  306. *
  307. * @return void
  308. */
  309. public function testKeys()
  310. {
  311. $collection = new CommandCollection();
  312. $collection->add('demo', DemoCommand::class);
  313. $collection->add('demo sample', DemoCommand::class);
  314. $collection->add('dang', DemoCommand::class);
  315. $result = $collection->keys();
  316. $this->assertSame(['demo', 'demo sample', 'dang'], $result);
  317. }
  318. }