CommandCollectionTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : 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(tm) Project
  13. * @since 3.5.0
  14. * @license https://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 PHPUnit\Framework\Attributes\DataProvider;
  24. use TestApp\Command\DemoCommand;
  25. use TestApp\Command\SampleCommand;
  26. use TestPlugin\Command\ExampleCommand;
  27. use TestPlugin\Command\SampleCommand as PluginSampleCommand;
  28. /**
  29. * Test case for the CommandCollection
  30. */
  31. class CommandCollectionTest extends TestCase
  32. {
  33. public function setUp(): void
  34. {
  35. parent::setUp();
  36. Configure::write('App.namespace', 'TestApp');
  37. $this->clearPlugins();
  38. }
  39. /**
  40. * Test constructor with valid classnames
  41. */
  42. public function testConstructor(): void
  43. {
  44. $collection = new CommandCollection([
  45. 'sample' => SampleCommand::class,
  46. 'routes' => RoutesCommand::class,
  47. ]);
  48. $this->assertTrue($collection->has('routes'));
  49. $this->assertTrue($collection->has('sample'));
  50. $this->assertCount(2, $collection);
  51. }
  52. /**
  53. * Test basic add/get
  54. */
  55. public function testAdd(): void
  56. {
  57. $collection = new CommandCollection();
  58. $this->assertSame($collection, $collection->add('routes', RoutesCommand::class));
  59. $this->assertTrue($collection->has('routes'));
  60. $this->assertSame(RoutesCommand::class, $collection->get('routes'));
  61. }
  62. /**
  63. * test adding a command instance.
  64. */
  65. public function testAddCommand(): void
  66. {
  67. $collection = new CommandCollection();
  68. $this->assertSame($collection, $collection->add('ex', DemoCommand::class));
  69. $this->assertTrue($collection->has('ex'));
  70. $this->assertSame(DemoCommand::class, $collection->get('ex'));
  71. }
  72. /**
  73. * Test that add() replaces.
  74. */
  75. public function testAddReplace(): void
  76. {
  77. $collection = new CommandCollection();
  78. $this->assertSame($collection, $collection->add('routes', RoutesCommand::class));
  79. $this->assertSame($collection, $collection->add('routes', SampleCommand::class));
  80. $this->assertTrue($collection->has('routes'));
  81. $this->assertSame(SampleCommand::class, $collection->get('routes'));
  82. }
  83. /**
  84. * Test adding with instances
  85. */
  86. public function testAddInstance(): void
  87. {
  88. $collection = new CommandCollection();
  89. $command = new RoutesCommand();
  90. $collection->add('routes', $command);
  91. $this->assertTrue($collection->has('routes'));
  92. $this->assertSame($command, $collection->get('routes'));
  93. }
  94. /**
  95. * Provider for invalid names.
  96. *
  97. * @return array
  98. */
  99. public static function invalidNameProvider(): array
  100. {
  101. return [
  102. // Empty
  103. [''],
  104. // Leading spaces
  105. [' spaced'],
  106. // Trailing spaces
  107. ['spaced '],
  108. // Too many words
  109. ['one two three four'],
  110. ];
  111. }
  112. /**
  113. * test adding a command instance.
  114. */
  115. #[DataProvider('invalidNameProvider')]
  116. public function testAddCommandInvalidName(string $name): void
  117. {
  118. $this->expectException(InvalidArgumentException::class);
  119. $this->expectExceptionMessage("The command name `{$name}` is invalid.");
  120. $collection = new CommandCollection();
  121. $collection->add($name, DemoCommand::class);
  122. }
  123. /**
  124. * Test removing a command
  125. */
  126. public function testRemove(): void
  127. {
  128. $collection = new CommandCollection();
  129. $collection->add('routes', RoutesCommand::class);
  130. $this->assertSame($collection, $collection->remove('routes'));
  131. $this->assertFalse($collection->has('routes'));
  132. }
  133. /**
  134. * Removing an unknown command does not fail
  135. */
  136. public function testRemoveUnknown(): void
  137. {
  138. $collection = new CommandCollection();
  139. $this->assertSame($collection, $collection->remove('nope'));
  140. $this->assertFalse($collection->has('nope'));
  141. }
  142. /**
  143. * test getIterator
  144. */
  145. public function testGetIterator(): void
  146. {
  147. $in = [
  148. 'sample' => SampleCommand::class,
  149. 'routes' => RoutesCommand::class,
  150. ];
  151. $collection = new CommandCollection($in);
  152. $out = [];
  153. foreach ($collection as $key => $value) {
  154. $out[$key] = $value;
  155. }
  156. $this->assertEquals($in, $out);
  157. }
  158. /**
  159. * test autodiscovering app shells
  160. */
  161. public function testAutoDiscoverApp(): void
  162. {
  163. $collection = new CommandCollection();
  164. $collection->addMany($collection->autoDiscover());
  165. $this->assertTrue($collection->has('demo'));
  166. $this->assertTrue($collection->has('sample'));
  167. $this->assertSame(DemoCommand::class, $collection->get('demo'));
  168. $this->assertSame(SampleCommand::class, $collection->get('sample'));
  169. }
  170. /**
  171. * test autodiscovering core shells
  172. */
  173. public function testAutoDiscoverCore(): void
  174. {
  175. $collection = new CommandCollection();
  176. $collection->addMany($collection->autoDiscover());
  177. $this->assertTrue($collection->has('version'));
  178. $this->assertTrue($collection->has('routes'));
  179. $this->assertTrue($collection->has('sample'));
  180. $this->assertTrue($collection->has('schema_cache build'));
  181. $this->assertTrue($collection->has('schema_cache clear'));
  182. $this->assertTrue($collection->has('server'));
  183. $this->assertTrue($collection->has('cache clear'));
  184. $this->assertFalse($collection->has('command_list'), 'Hidden commands should stay hidden');
  185. // These have to be strings as ::class uses the local namespace.
  186. $this->assertSame(RoutesCommand::class, $collection->get('routes'));
  187. $this->assertSame(SampleCommand::class, $collection->get('sample'));
  188. $this->assertSame(VersionCommand::class, $collection->get('version'));
  189. }
  190. /**
  191. * test missing plugin discovery
  192. */
  193. public function testDiscoverPluginUnknown(): void
  194. {
  195. $collection = new CommandCollection();
  196. $this->assertSame([], $collection->discoverPlugin('Nope'));
  197. }
  198. /**
  199. * test autodiscovering plugin shells
  200. */
  201. public function testDiscoverPlugin(): void
  202. {
  203. $this->loadPlugins(['TestPlugin', 'Company/TestPluginThree']);
  204. $collection = new CommandCollection();
  205. // Add a dupe to test de-duping
  206. $collection->add('sample', DemoCommand::class);
  207. $result = $collection->discoverPlugin('TestPlugin');
  208. $this->assertArrayHasKey(
  209. 'example',
  210. $result,
  211. 'Used short name for unique plugin shell'
  212. );
  213. $this->assertArrayHasKey(
  214. 'test_plugin.example',
  215. $result,
  216. 'Long names are stored for unique shells'
  217. );
  218. $this->assertArrayNotHasKey('sample', $result, 'Existing command not output');
  219. $this->assertArrayHasKey(
  220. 'test_plugin.sample',
  221. $result,
  222. 'Duplicate shell was given a full alias'
  223. );
  224. $this->assertSame(ExampleCommand::class, $result['example']);
  225. $this->assertSame($result['example'], $result['test_plugin.example']);
  226. $this->assertSame(PluginSampleCommand::class, $result['test_plugin.sample']);
  227. $result = $collection->discoverPlugin('Company/TestPluginThree');
  228. $this->assertArrayHasKey(
  229. 'company',
  230. $result,
  231. 'Used short name for unique plugin shell'
  232. );
  233. $this->assertArrayHasKey(
  234. 'company/test_plugin_three.company',
  235. $result,
  236. 'Long names are stored as well'
  237. );
  238. $this->assertSame($result['company'], $result['company/test_plugin_three.company']);
  239. $this->clearPlugins();
  240. }
  241. /**
  242. * Test keys
  243. */
  244. public function testKeys(): void
  245. {
  246. $collection = new CommandCollection();
  247. $collection->add('demo', DemoCommand::class);
  248. $collection->add('demo sample', DemoCommand::class);
  249. $collection->add('dang', DemoCommand::class);
  250. $result = $collection->keys();
  251. $this->assertSame(['demo', 'demo sample', 'dang'], $result);
  252. }
  253. }