CommandCollectionTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 stdClass;
  24. use TestApp\Command\DemoCommand;
  25. /**
  26. * Test case for the CommandCollection
  27. */
  28. class CommandCollectionTest extends TestCase
  29. {
  30. public function setUp()
  31. {
  32. parent::setUp();
  33. Configure::write('App.namespace', 'TestApp');
  34. }
  35. /**
  36. * Test constructor with valid classnames
  37. *
  38. * @return void
  39. */
  40. public function testConstructor()
  41. {
  42. $collection = new CommandCollection([
  43. 'i18n' => I18nShell::class,
  44. 'routes' => RoutesShell::class
  45. ]);
  46. $this->assertTrue($collection->has('routes'));
  47. $this->assertTrue($collection->has('i18n'));
  48. $this->assertCount(2, $collection);
  49. }
  50. /**
  51. * Constructor with invalid class names should blow up
  52. *
  53. * @return void
  54. */
  55. public function testConstructorInvalidClass()
  56. {
  57. $this->expectException(\InvalidArgumentException::class);
  58. $this->expectExceptionMessage('Cannot use \'stdClass\' for command \'nope\' it is not a subclass of Cake\Console\Shell');
  59. new CommandCollection([
  60. 'i18n' => I18nShell::class,
  61. 'nope' => stdClass::class
  62. ]);
  63. }
  64. /**
  65. * Test basic add/get
  66. *
  67. * @return void
  68. */
  69. public function testAdd()
  70. {
  71. $collection = new CommandCollection();
  72. $this->assertSame($collection, $collection->add('routes', RoutesShell::class));
  73. $this->assertTrue($collection->has('routes'));
  74. $this->assertSame(RoutesShell::class, $collection->get('routes'));
  75. }
  76. /**
  77. * test adding a command instance.
  78. *
  79. * @return void
  80. */
  81. public function testAddCommand()
  82. {
  83. $collection = new CommandCollection();
  84. $this->assertSame($collection, $collection->add('ex', DemoCommand::class));
  85. $this->assertTrue($collection->has('ex'));
  86. $this->assertSame(DemoCommand::class, $collection->get('ex'));
  87. }
  88. /**
  89. * Test that add() replaces.
  90. *
  91. * @return void
  92. */
  93. public function testAddReplace()
  94. {
  95. $collection = new CommandCollection();
  96. $this->assertSame($collection, $collection->add('routes', RoutesShell::class));
  97. $this->assertSame($collection, $collection->add('routes', I18nShell::class));
  98. $this->assertTrue($collection->has('routes'));
  99. $this->assertSame(I18nShell::class, $collection->get('routes'));
  100. }
  101. /**
  102. * Test adding with instances
  103. *
  104. * @return void
  105. */
  106. public function testAddInstance()
  107. {
  108. $collection = new CommandCollection();
  109. $io = $this->getMockBuilder('Cake\Console\ConsoleIo')
  110. ->disableOriginalConstructor()
  111. ->getMock();
  112. $shell = new RoutesShell($io);
  113. $collection->add('routes', $shell);
  114. $this->assertTrue($collection->has('routes'));
  115. $this->assertSame($shell, $collection->get('routes'));
  116. }
  117. /**
  118. * Instances that are not shells should fail.
  119. *
  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. * Class names that are not shells should fail
  131. *
  132. */
  133. public function testInvalidShellClassName()
  134. {
  135. $this->expectException(\InvalidArgumentException::class);
  136. $this->expectExceptionMessage('Cannot use \'stdClass\' for command \'routes\' it is not a subclass of Cake\Console\Shell');
  137. $collection = new CommandCollection();
  138. $collection->add('routes', stdClass::class);
  139. }
  140. /**
  141. * Test removing a command
  142. *
  143. * @return void
  144. */
  145. public function testRemove()
  146. {
  147. $collection = new CommandCollection();
  148. $collection->add('routes', RoutesShell::class);
  149. $this->assertSame($collection, $collection->remove('routes'));
  150. $this->assertFalse($collection->has('routes'));
  151. }
  152. /**
  153. * Removing an unknown command does not fail
  154. *
  155. * @return void
  156. */
  157. public function testRemoveUnknown()
  158. {
  159. $collection = new CommandCollection();
  160. $this->assertSame($collection, $collection->remove('nope'));
  161. $this->assertFalse($collection->has('nope'));
  162. }
  163. /**
  164. * test getIterator
  165. *
  166. * @return void
  167. */
  168. public function testGetIterator()
  169. {
  170. $in = [
  171. 'i18n' => I18nShell::class,
  172. 'routes' => RoutesShell::class
  173. ];
  174. $collection = new CommandCollection($in);
  175. $out = [];
  176. foreach ($collection as $key => $value) {
  177. $out[$key] = $value;
  178. }
  179. $this->assertEquals($in, $out);
  180. }
  181. /**
  182. * test autodiscovering app shells
  183. *
  184. * @return void
  185. */
  186. public function testAutoDiscoverApp()
  187. {
  188. $collection = new CommandCollection();
  189. $collection->addMany($collection->autoDiscover());
  190. $this->assertTrue($collection->has('demo'));
  191. $this->assertTrue($collection->has('i18m'));
  192. $this->assertTrue($collection->has('sample'));
  193. $this->assertTrue($collection->has('testing_dispatch'));
  194. $this->assertSame('TestApp\Command\DemoCommand', $collection->get('demo'));
  195. $this->assertSame('TestApp\Shell\I18mShell', $collection->get('i18m'));
  196. $this->assertSame('TestApp\Shell\SampleShell', $collection->get('sample'));
  197. }
  198. /**
  199. * test autodiscovering core shells
  200. *
  201. * @return void
  202. */
  203. public function testAutoDiscoverCore()
  204. {
  205. $collection = new CommandCollection();
  206. $collection->addMany($collection->autoDiscover());
  207. $this->assertTrue($collection->has('version'));
  208. $this->assertTrue($collection->has('routes'));
  209. $this->assertTrue($collection->has('i18n'));
  210. $this->assertTrue($collection->has('orm_cache'));
  211. $this->assertTrue($collection->has('server'));
  212. $this->assertTrue($collection->has('cache'));
  213. $this->assertFalse($collection->has('command_list'), 'Hidden commands should stay hidden');
  214. // These have to be strings as ::class uses the local namespace.
  215. $this->assertSame('Cake\Shell\RoutesShell', $collection->get('routes'));
  216. $this->assertSame('Cake\Shell\I18nShell', $collection->get('i18n'));
  217. $this->assertSame('Cake\Command\VersionCommand', $collection->get('version'));
  218. }
  219. /**
  220. * test autodiscovering plugin shells
  221. *
  222. * @return void
  223. */
  224. public function testAutoDiscoverPlugin()
  225. {
  226. Plugin::load('TestPlugin');
  227. Plugin::load('Company/TestPluginThree');
  228. $collection = new CommandCollection();
  229. $collection->addMany($collection->autoDiscover());
  230. $this->assertTrue(
  231. $collection->has('example'),
  232. 'Used short name for unique plugin shell'
  233. );
  234. $this->assertTrue(
  235. $collection->has('test_plugin.example'),
  236. 'Long names are stored for unique shells'
  237. );
  238. $this->assertTrue(
  239. $collection->has('sample'),
  240. 'Has app shell'
  241. );
  242. $this->assertTrue(
  243. $collection->has('test_plugin.sample'),
  244. 'Duplicate shell was given a full alias'
  245. );
  246. $this->assertTrue(
  247. $collection->has('company'),
  248. 'Used short name for unique plugin shell'
  249. );
  250. $this->assertTrue(
  251. $collection->has('company/test_plugin_three.company'),
  252. 'Long names are stored as well'
  253. );
  254. $this->assertEquals('TestPlugin\Shell\ExampleShell', $collection->get('example'));
  255. $this->assertEquals($collection->get('example'), $collection->get('test_plugin.example'));
  256. $this->assertEquals(
  257. 'TestApp\Shell\SampleShell',
  258. $collection->get('sample'),
  259. 'Should prefer app shells over plugin ones'
  260. );
  261. $this->assertEquals('TestPlugin\Shell\SampleShell', $collection->get('test_plugin.sample'));
  262. }
  263. }