CommandCollectionTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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\CommandCollection;
  17. use Cake\Shell\I18nShell;
  18. use Cake\Shell\RoutesShell;
  19. use Cake\TestSuite\TestCase;
  20. use stdClass;
  21. /**
  22. * Test case for the CommandCollection
  23. */
  24. class CommandCollectionTest extends TestCase
  25. {
  26. /**
  27. * Test constructor with valid classnames
  28. *
  29. * @return void
  30. */
  31. public function testConstructor()
  32. {
  33. $collection = new CommandCollection([
  34. 'i18n' => I18nShell::class,
  35. 'routes' => RoutesShell::class
  36. ]);
  37. $this->assertTrue($collection->has('routes'));
  38. $this->assertTrue($collection->has('i18n'));
  39. $this->assertCount(2, $collection);
  40. }
  41. /**
  42. * Constructor with invalid class names should blow up
  43. *
  44. * @return void
  45. * @expectedException InvalidArgumentException
  46. * @expectedExceptionMessage 'nope' is not a subclass of Cake\Console\Shell
  47. */
  48. public function testConstructorInvalidClass()
  49. {
  50. new CommandCollection([
  51. 'i18n' => I18nShell::class,
  52. 'nope' => stdClass::class
  53. ]);
  54. }
  55. /**
  56. * Test basic add/get
  57. *
  58. * @return void
  59. */
  60. public function testAdd()
  61. {
  62. $collection = new CommandCollection();
  63. $this->assertSame($collection, $collection->add('routes', RoutesShell::class));
  64. $this->assertTrue($collection->has('routes'));
  65. $this->assertSame(RoutesShell::class, $collection->get('routes'));
  66. }
  67. /**
  68. * Test that add() replaces.
  69. *
  70. * @return void
  71. */
  72. public function testAddReplace()
  73. {
  74. $collection = new CommandCollection();
  75. $this->assertSame($collection, $collection->add('routes', RoutesShell::class));
  76. $this->assertSame($collection, $collection->add('routes', I18nShell::class));
  77. $this->assertTrue($collection->has('routes'));
  78. $this->assertSame(I18nShell::class, $collection->get('routes'));
  79. }
  80. /**
  81. * Test adding with instances
  82. *
  83. * @return void
  84. */
  85. public function testAddInstance()
  86. {
  87. $collection = new CommandCollection();
  88. $io = $this->getMockBuilder('Cake\Console\ConsoleIo')
  89. ->disableOriginalConstructor()
  90. ->getMock();
  91. $shell = new RoutesShell($io);
  92. $collection->add('routes', $shell);
  93. $this->assertTrue($collection->has('routes'));
  94. $this->assertSame($shell, $collection->get('routes'));
  95. }
  96. /**
  97. * Instances that are not shells should fail.
  98. *
  99. * @expectedException InvalidArgumentException
  100. * @expectedExceptionMessage 'routes' is not a subclass of Cake\Console\Shell
  101. */
  102. public function testAddInvalidInstance()
  103. {
  104. $collection = new CommandCollection();
  105. $shell = new stdClass();
  106. $collection->add('routes', $shell);
  107. }
  108. /**
  109. * Class names that are not shells should fail
  110. *
  111. * @expectedException InvalidArgumentException
  112. * @expectedExceptionMessage 'routes' is not a subclass of Cake\Console\Shell
  113. */
  114. public function testInvalidShellClassName()
  115. {
  116. $collection = new CommandCollection();
  117. $collection->add('routes', stdClass::class);
  118. }
  119. /**
  120. * Test removing a command
  121. *
  122. * @return void
  123. */
  124. public function testRemove()
  125. {
  126. $collection = new CommandCollection();
  127. $collection->add('routes', RoutesShell::class);
  128. $this->assertSame($collection, $collection->remove('routes'));
  129. $this->assertFalse($collection->has('routes'));
  130. }
  131. /**
  132. * Removing an unknown command does not fail
  133. *
  134. * @return void
  135. */
  136. public function testRemoveUnknown()
  137. {
  138. $collection = new CommandCollection();
  139. $this->assertSame($collection, $collection->remove('nope'));
  140. $this->assertFalse($collection->has('nope'));
  141. }
  142. /**
  143. * test getIterator
  144. *
  145. * @return void
  146. */
  147. public function testGetIterator()
  148. {
  149. $in = [
  150. 'i18n' => I18nShell::class,
  151. 'routes' => RoutesShell::class
  152. ];
  153. $collection = new CommandCollection($in);
  154. $out = [];
  155. foreach ($collection as $key => $value) {
  156. $out[$key] = $value;
  157. }
  158. $this->assertEquals($in, $out);
  159. }
  160. }