CommandCollectionTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. * @expectedException InvalidArgumentException
  55. * @expectedExceptionMessage Cannot use 'stdClass' for command 'nope' it is not a subclass of Cake\Console\Shell
  56. */
  57. public function testConstructorInvalidClass()
  58. {
  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. * @expectedException InvalidArgumentException
  121. * @expectedExceptionMessage Cannot use 'stdClass' for command 'routes' it is not a subclass of Cake\Console\Shell
  122. */
  123. public function testAddInvalidInstance()
  124. {
  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. * @expectedException InvalidArgumentException
  133. * @expectedExceptionMessage Cannot use 'stdClass' for command 'routes' it is not a subclass of Cake\Console\Shell
  134. */
  135. public function testInvalidShellClassName()
  136. {
  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('i18m'));
  191. $this->assertTrue($collection->has('sample'));
  192. $this->assertTrue($collection->has('testing_dispatch'));
  193. $this->assertSame('TestApp\Shell\I18mShell', $collection->get('i18m'));
  194. $this->assertSame('TestApp\Shell\SampleShell', $collection->get('sample'));
  195. }
  196. /**
  197. * test autodiscovering core shells
  198. *
  199. * @return void
  200. */
  201. public function testAutoDiscoverCore()
  202. {
  203. $collection = new CommandCollection();
  204. $collection->addMany($collection->autoDiscover());
  205. $this->assertTrue($collection->has('routes'));
  206. $this->assertTrue($collection->has('i18n'));
  207. $this->assertTrue($collection->has('orm_cache'));
  208. $this->assertTrue($collection->has('server'));
  209. $this->assertTrue($collection->has('cache'));
  210. $this->assertFalse($collection->has('command_list'), 'Hidden commands should stay hidden');
  211. // These have to be strings as ::class uses the local namespace.
  212. $this->assertSame('Cake\Shell\RoutesShell', $collection->get('routes'));
  213. $this->assertSame('Cake\Shell\I18nShell', $collection->get('i18n'));
  214. }
  215. /**
  216. * test autodiscovering plugin shells
  217. *
  218. * @return void
  219. */
  220. public function testAutoDiscoverPlugin()
  221. {
  222. Plugin::load('TestPlugin');
  223. Plugin::load('Company/TestPluginThree');
  224. $collection = new CommandCollection();
  225. $collection->addMany($collection->autoDiscover());
  226. $this->assertTrue(
  227. $collection->has('example'),
  228. 'Used short name for unique plugin shell'
  229. );
  230. $this->assertTrue(
  231. $collection->has('test_plugin.example'),
  232. 'Long names are stored for unique shells'
  233. );
  234. $this->assertTrue(
  235. $collection->has('sample'),
  236. 'Has app shell'
  237. );
  238. $this->assertTrue(
  239. $collection->has('test_plugin.sample'),
  240. 'Duplicate shell was given a full alias'
  241. );
  242. $this->assertTrue(
  243. $collection->has('company'),
  244. 'Used short name for unique plugin shell'
  245. );
  246. $this->assertTrue(
  247. $collection->has('company/test_plugin_three.company'),
  248. 'Long names are stored as well'
  249. );
  250. $this->assertEquals('TestPlugin\Shell\ExampleShell', $collection->get('example'));
  251. $this->assertEquals($collection->get('example'), $collection->get('test_plugin.example'));
  252. $this->assertEquals(
  253. 'TestApp\Shell\SampleShell',
  254. $collection->get('sample'),
  255. 'Should prefer app shells over plugin ones'
  256. );
  257. $this->assertEquals('TestPlugin\Shell\SampleShell', $collection->get('test_plugin.sample'));
  258. }
  259. }