CommandCollectionTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. $io = $this->getMockBuilder('Cake\Console\ConsoleIo')
  111. ->disableOriginalConstructor()
  112. ->getMock();
  113. $shell = new RoutesCommand($io);
  114. $collection->add('routes', $shell);
  115. $this->assertTrue($collection->has('routes'));
  116. $this->assertSame($shell, $collection->get('routes'));
  117. }
  118. /**
  119. * Instances that are not shells should fail.
  120. */
  121. public function testAddInvalidInstance()
  122. {
  123. $this->expectException(\InvalidArgumentException::class);
  124. $this->expectExceptionMessage('Cannot use \'stdClass\' for command \'routes\'. It is not a subclass of Cake\Console\Shell');
  125. $collection = new CommandCollection();
  126. $shell = new stdClass();
  127. $collection->add('routes', $shell);
  128. }
  129. /**
  130. * Provider for invalid names.
  131. *
  132. * @return array
  133. */
  134. public function invalidNameProvider()
  135. {
  136. return [
  137. // Empty
  138. [''],
  139. // Leading spaces
  140. [' spaced'],
  141. // Trailing spaces
  142. ['spaced '],
  143. // Too many words
  144. ['one two three four'],
  145. ];
  146. }
  147. /**
  148. * test adding a command instance.
  149. *
  150. * @dataProvider invalidNameProvider
  151. * @return void
  152. */
  153. public function testAddCommandInvalidName($name)
  154. {
  155. $this->expectException(InvalidArgumentException::class);
  156. $this->expectExceptionMessage("The command name `$name` is invalid.");
  157. $collection = new CommandCollection();
  158. $collection->add($name, DemoCommand::class);
  159. }
  160. /**
  161. * Class names that are not shells should fail
  162. */
  163. public function testInvalidShellClassName()
  164. {
  165. $this->expectException(\InvalidArgumentException::class);
  166. $this->expectExceptionMessage('Cannot use \'stdClass\' for command \'routes\'. It is not a subclass of Cake\Console\Shell');
  167. $collection = new CommandCollection();
  168. $collection->add('routes', stdClass::class);
  169. }
  170. /**
  171. * Test removing a command
  172. *
  173. * @return void
  174. */
  175. public function testRemove()
  176. {
  177. $collection = new CommandCollection();
  178. $collection->add('routes', RoutesCommand::class);
  179. $this->assertSame($collection, $collection->remove('routes'));
  180. $this->assertFalse($collection->has('routes'));
  181. }
  182. /**
  183. * Removing an unknown command does not fail
  184. *
  185. * @return void
  186. */
  187. public function testRemoveUnknown()
  188. {
  189. $collection = new CommandCollection();
  190. $this->assertSame($collection, $collection->remove('nope'));
  191. $this->assertFalse($collection->has('nope'));
  192. }
  193. /**
  194. * test getIterator
  195. *
  196. * @return void
  197. */
  198. public function testGetIterator()
  199. {
  200. $in = [
  201. 'sample' => SampleShell::class,
  202. 'routes' => RoutesCommand::class,
  203. ];
  204. $collection = new CommandCollection($in);
  205. $out = [];
  206. foreach ($collection as $key => $value) {
  207. $out[$key] = $value;
  208. }
  209. $this->assertEquals($in, $out);
  210. }
  211. /**
  212. * test autodiscovering app shells
  213. *
  214. * @return void
  215. */
  216. public function testAutoDiscoverApp()
  217. {
  218. $collection = new CommandCollection();
  219. $collection->addMany($collection->autoDiscover());
  220. $this->assertTrue($collection->has('app'));
  221. $this->assertTrue($collection->has('demo'));
  222. $this->assertTrue($collection->has('i18m'));
  223. $this->assertTrue($collection->has('sample'));
  224. $this->assertTrue($collection->has('testing_dispatch'));
  225. $this->assertSame('TestApp\Shell\AppShell', $collection->get('app'));
  226. $this->assertSame('TestApp\Command\DemoCommand', $collection->get('demo'));
  227. $this->assertSame('TestApp\Shell\I18mShell', $collection->get('i18m'));
  228. $this->assertSame('TestApp\Shell\SampleShell', $collection->get('sample'));
  229. }
  230. /**
  231. * test autodiscovering core shells
  232. *
  233. * @return void
  234. */
  235. public function testAutoDiscoverCore()
  236. {
  237. $collection = new CommandCollection();
  238. $collection->addMany($collection->autoDiscover());
  239. $this->assertTrue($collection->has('version'));
  240. $this->assertTrue($collection->has('routes'));
  241. $this->assertTrue($collection->has('sample'));
  242. $this->assertTrue($collection->has('schema_cache build'));
  243. $this->assertTrue($collection->has('schema_cache clear'));
  244. $this->assertTrue($collection->has('server'));
  245. $this->assertTrue($collection->has('cache clear'));
  246. $this->assertFalse($collection->has('command_list'), 'Hidden commands should stay hidden');
  247. // These have to be strings as ::class uses the local namespace.
  248. $this->assertSame(RoutesCommand::class, $collection->get('routes'));
  249. $this->assertSame(SampleShell::class, $collection->get('sample'));
  250. $this->assertSame(VersionCommand::class, $collection->get('version'));
  251. }
  252. /**
  253. * test missing plugin discovery
  254. *
  255. * @return void
  256. */
  257. public function testDiscoverPluginUnknown()
  258. {
  259. $collection = new CommandCollection();
  260. $this->assertSame([], $collection->discoverPlugin('Nope'));
  261. }
  262. /**
  263. * test autodiscovering plugin shells
  264. *
  265. * @return void
  266. */
  267. public function testDiscoverPlugin()
  268. {
  269. $this->loadPlugins(['TestPlugin', 'Company/TestPluginThree']);
  270. $collection = new CommandCollection();
  271. // Add a dupe to test de-duping
  272. $collection->add('sample', DemoCommand::class);
  273. $result = $collection->discoverPlugin('TestPlugin');
  274. $this->assertArrayHasKey(
  275. 'example',
  276. $result,
  277. 'Used short name for unique plugin shell'
  278. );
  279. $this->assertArrayHasKey(
  280. 'test_plugin.example',
  281. $result,
  282. 'Long names are stored for unique shells'
  283. );
  284. $this->assertArrayNotHasKey('sample', $result, 'Existing command not output');
  285. $this->assertArrayHasKey(
  286. 'test_plugin.sample',
  287. $result,
  288. 'Duplicate shell was given a full alias'
  289. );
  290. $this->assertSame('TestPlugin\Shell\ExampleShell', $result['example']);
  291. $this->assertEquals($result['example'], $result['test_plugin.example']);
  292. $this->assertSame('TestPlugin\Shell\SampleShell', $result['test_plugin.sample']);
  293. $result = $collection->discoverPlugin('Company/TestPluginThree');
  294. $this->assertArrayHasKey(
  295. 'company',
  296. $result,
  297. 'Used short name for unique plugin shell'
  298. );
  299. $this->assertArrayHasKey(
  300. 'company/test_plugin_three.company',
  301. $result,
  302. 'Long names are stored as well'
  303. );
  304. $this->assertSame($result['company'], $result['company/test_plugin_three.company']);
  305. $this->clearPlugins();
  306. }
  307. /**
  308. * Test keys
  309. *
  310. * @return void
  311. */
  312. public function testKeys()
  313. {
  314. $collection = new CommandCollection();
  315. $collection->add('demo', DemoCommand::class);
  316. $collection->add('demo sample', DemoCommand::class);
  317. $collection->add('dang', DemoCommand::class);
  318. $result = $collection->keys();
  319. $this->assertSame(['demo', 'demo sample', 'dang'], $result);
  320. }
  321. }