CommandCollectionTest.php 11 KB

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