CommandCollectionTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.5.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\Console;
  16. use Cake\Console\Command;
  17. use Cake\Console\CommandCollection;
  18. use Cake\Core\Configure;
  19. use Cake\Core\Plugin;
  20. use Cake\Shell\I18nShell;
  21. use Cake\Shell\RoutesShell;
  22. use Cake\TestSuite\TestCase;
  23. use InvalidArgumentException;
  24. use stdClass;
  25. use TestApp\Command\DemoCommand;
  26. /**
  27. * Test case for the CommandCollection
  28. */
  29. class CommandCollectionTest extends TestCase
  30. {
  31. public function setUp()
  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. 'i18n' => I18nShell::class,
  45. 'routes' => RoutesShell::class
  46. ]);
  47. $this->assertTrue($collection->has('routes'));
  48. $this->assertTrue($collection->has('i18n'));
  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. 'i18n' => I18nShell::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', RoutesShell::class));
  74. $this->assertTrue($collection->has('routes'));
  75. $this->assertSame(RoutesShell::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', RoutesShell::class));
  98. $this->assertSame($collection, $collection->add('routes', I18nShell::class));
  99. $this->assertTrue($collection->has('routes'));
  100. $this->assertSame(I18nShell::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 RoutesShell($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. * Class names that are not shells should fail
  132. *
  133. */
  134. public function testInvalidShellClassName()
  135. {
  136. $this->expectException(\InvalidArgumentException::class);
  137. $this->expectExceptionMessage('Cannot use \'stdClass\' for command \'routes\' it is not a subclass of Cake\Console\Shell');
  138. $collection = new CommandCollection();
  139. $collection->add('routes', stdClass::class);
  140. }
  141. /**
  142. * Test removing a command
  143. *
  144. * @return void
  145. */
  146. public function testRemove()
  147. {
  148. $collection = new CommandCollection();
  149. $collection->add('routes', RoutesShell::class);
  150. $this->assertSame($collection, $collection->remove('routes'));
  151. $this->assertFalse($collection->has('routes'));
  152. }
  153. /**
  154. * Removing an unknown command does not fail
  155. *
  156. * @return void
  157. */
  158. public function testRemoveUnknown()
  159. {
  160. $collection = new CommandCollection();
  161. $this->assertSame($collection, $collection->remove('nope'));
  162. $this->assertFalse($collection->has('nope'));
  163. }
  164. /**
  165. * test getIterator
  166. *
  167. * @return void
  168. */
  169. public function testGetIterator()
  170. {
  171. $in = [
  172. 'i18n' => I18nShell::class,
  173. 'routes' => RoutesShell::class
  174. ];
  175. $collection = new CommandCollection($in);
  176. $out = [];
  177. foreach ($collection as $key => $value) {
  178. $out[$key] = $value;
  179. }
  180. $this->assertEquals($in, $out);
  181. }
  182. /**
  183. * test autodiscovering app shells
  184. *
  185. * @return void
  186. */
  187. public function testAutoDiscoverApp()
  188. {
  189. $collection = new CommandCollection();
  190. $collection->addMany($collection->autoDiscover());
  191. $this->assertTrue($collection->has('app'));
  192. $this->assertTrue($collection->has('demo'));
  193. $this->assertTrue($collection->has('i18m'));
  194. $this->assertTrue($collection->has('sample'));
  195. $this->assertTrue($collection->has('testing_dispatch'));
  196. $this->assertSame('TestApp\Shell\AppShell', $collection->get('app'));
  197. $this->assertSame('TestApp\Command\DemoCommand', $collection->get('demo'));
  198. $this->assertSame('TestApp\Shell\I18mShell', $collection->get('i18m'));
  199. $this->assertSame('TestApp\Shell\SampleShell', $collection->get('sample'));
  200. }
  201. /**
  202. * test autodiscovering core shells
  203. *
  204. * @return void
  205. */
  206. public function testAutoDiscoverCore()
  207. {
  208. $collection = new CommandCollection();
  209. $collection->addMany($collection->autoDiscover());
  210. $this->assertTrue($collection->has('version'));
  211. $this->assertTrue($collection->has('routes'));
  212. $this->assertTrue($collection->has('i18n'));
  213. $this->assertTrue($collection->has('orm_cache'));
  214. $this->assertTrue($collection->has('server'));
  215. $this->assertTrue($collection->has('cache'));
  216. $this->assertFalse($collection->has('command_list'), 'Hidden commands should stay hidden');
  217. // These have to be strings as ::class uses the local namespace.
  218. $this->assertSame('Cake\Shell\RoutesShell', $collection->get('routes'));
  219. $this->assertSame('Cake\Shell\I18nShell', $collection->get('i18n'));
  220. $this->assertSame('Cake\Command\VersionCommand', $collection->get('version'));
  221. }
  222. /**
  223. * test missing plugin discovery
  224. *
  225. * @return void
  226. */
  227. public function testDiscoverPluginUnknown()
  228. {
  229. $collection = new CommandCollection();
  230. $this->assertSame([], $collection->discoverPlugin('Nope'));
  231. }
  232. /**
  233. * test autodiscovering plugin shells
  234. *
  235. * @return void
  236. */
  237. public function testDiscoverPlugin()
  238. {
  239. Plugin::load('TestPlugin');
  240. Plugin::load('Company/TestPluginThree');
  241. $collection = new CommandCollection();
  242. // Add a dupe to test de-duping
  243. $collection->add('sample', DemoCommand::class);
  244. $result = $collection->discoverPlugin('TestPlugin');
  245. $this->assertArrayHasKey(
  246. 'example',
  247. $result,
  248. 'Used short name for unique plugin shell'
  249. );
  250. $this->assertArrayHasKey(
  251. 'test_plugin.example',
  252. $result,
  253. 'Long names are stored for unique shells'
  254. );
  255. $this->assertArrayNotHasKey('sample', $result, 'Existing command not output');
  256. $this->assertArrayHasKey(
  257. 'test_plugin.sample',
  258. $result,
  259. 'Duplicate shell was given a full alias'
  260. );
  261. $this->assertEquals('TestPlugin\Shell\ExampleShell', $result['example']);
  262. $this->assertEquals($result['example'], $result['test_plugin.example']);
  263. $this->assertEquals('TestPlugin\Shell\SampleShell', $result['test_plugin.sample']);
  264. $result = $collection->discoverPlugin('Company/TestPluginThree');
  265. $this->assertArrayHasKey(
  266. 'company',
  267. $result,
  268. 'Used short name for unique plugin shell'
  269. );
  270. $this->assertArrayHasKey(
  271. 'company/test_plugin_three.company',
  272. $result,
  273. 'Long names are stored as well'
  274. );
  275. $this->assertSame($result['company'], $result['company/test_plugin_three.company']);
  276. }
  277. }