ShellDispatcherTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  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://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  12. * @since 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Console;
  16. use Cake\Console\ShellDispatcher;
  17. use Cake\Core\App;
  18. use Cake\Core\Configure;
  19. use Cake\Core\Plugin;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * ShellDispatcherTest
  23. *
  24. */
  25. class ShellDispatcherTest extends TestCase {
  26. /**
  27. * setUp method
  28. *
  29. * @return void
  30. */
  31. public function setUp() {
  32. parent::setUp();
  33. Plugin::load('TestPlugin');
  34. Configure::write('App.namespace', 'TestApp');
  35. $this->dispatcher = $this->getMock('Cake\Console\ShellDispatcher', ['_stop']);
  36. }
  37. /**
  38. * teardown
  39. *
  40. * @return void
  41. */
  42. public function tearDown() {
  43. parent::tearDown();
  44. ShellDispatcher::resetAliases();
  45. }
  46. /**
  47. * Test error on missing shell
  48. *
  49. * @expectedException Cake\Console\Error\MissingShellException
  50. * @return void
  51. */
  52. public function testFindShellMissing() {
  53. $this->dispatcher->findShell('nope');
  54. }
  55. /**
  56. * Test error on missing plugin shell
  57. *
  58. * @expectedException Cake\Console\Error\MissingShellException
  59. * @return void
  60. */
  61. public function testFindShellMissingPlugin() {
  62. $this->dispatcher->findShell('test_plugin.nope');
  63. }
  64. /**
  65. * Verify loading of (plugin-) shells
  66. *
  67. * @return void
  68. */
  69. public function testFindShell() {
  70. $result = $this->dispatcher->findShell('sample');
  71. $this->assertInstanceOf('TestApp\Console\Command\SampleShell', $result);
  72. $result = $this->dispatcher->findShell('test_plugin.example');
  73. $this->assertInstanceOf('TestPlugin\Console\Command\ExampleShell', $result);
  74. $this->assertEquals('TestPlugin', $result->plugin);
  75. $this->assertEquals('Example', $result->name);
  76. $result = $this->dispatcher->findShell('TestPlugin.example');
  77. $this->assertInstanceOf('TestPlugin\Console\Command\ExampleShell', $result);
  78. $result = $this->dispatcher->findShell('TestPlugin.Example');
  79. $this->assertInstanceOf('TestPlugin\Console\Command\ExampleShell', $result);
  80. }
  81. /**
  82. * Test getting shells with aliases.
  83. *
  84. * @return void
  85. */
  86. public function testFindShellAliased() {
  87. ShellDispatcher::alias('short', 'test_plugin.example');
  88. $result = $this->dispatcher->findShell('short');
  89. $this->assertInstanceOf('TestPlugin\Console\Command\ExampleShell', $result);
  90. $this->assertEquals('TestPlugin', $result->plugin);
  91. $this->assertEquals('Example', $result->name);
  92. }
  93. /**
  94. * Test finding a shell that has a matching alias.
  95. *
  96. * Aliases should not overload concrete shells.
  97. *
  98. * @return void
  99. */
  100. public function testFindShellAliasedAppShadow() {
  101. ShellDispatcher::alias('sample', 'test_plugin.example');
  102. $result = $this->dispatcher->findShell('sample');
  103. $this->assertInstanceOf('TestApp\Console\Command\SampleShell', $result);
  104. $this->assertEmpty($result->plugin);
  105. $this->assertEquals('Sample', $result->name);
  106. }
  107. /**
  108. * Verify correct dispatch of Shell subclasses with a main method
  109. *
  110. * @return void
  111. */
  112. public function testDispatchShellWithMain() {
  113. $dispatcher = $this->getMock('Cake\Console\ShellDispatcher', ['findShell']);
  114. $Shell = $this->getMock('Cake\Console\Shell');
  115. $Shell->expects($this->once())->method('initialize');
  116. $Shell->expects($this->once())->method('runCommand')
  117. ->with([])
  118. ->will($this->returnValue(true));
  119. $dispatcher->expects($this->any())
  120. ->method('findShell')
  121. ->with('mock_with_main')
  122. ->will($this->returnValue($Shell));
  123. $dispatcher->args = array('mock_with_main');
  124. $result = $dispatcher->dispatch();
  125. $this->assertEquals(0, $result);
  126. $this->assertEquals(array(), $dispatcher->args);
  127. }
  128. /**
  129. * Verify correct dispatch of Shell subclasses without a main method
  130. *
  131. * @return void
  132. */
  133. public function testDispatchShellWithoutMain() {
  134. $dispatcher = $this->getMock('Cake\Console\ShellDispatcher', ['findShell']);
  135. $Shell = $this->getMock('Cake\Console\Shell');
  136. $Shell->expects($this->once())->method('initialize');
  137. $Shell->expects($this->once())->method('runCommand')
  138. ->with(['initdb'])
  139. ->will($this->returnValue(true));
  140. $dispatcher->expects($this->any())
  141. ->method('findShell')
  142. ->with('mock_without_main')
  143. ->will($this->returnValue($Shell));
  144. $dispatcher->args = array('mock_without_main', 'initdb');
  145. $result = $dispatcher->dispatch();
  146. $this->assertEquals(0, $result);
  147. }
  148. /**
  149. * Verify shifting of arguments
  150. *
  151. * @return void
  152. */
  153. public function testShiftArgs() {
  154. $this->dispatcher->args = array('a', 'b', 'c');
  155. $this->assertEquals('a', $this->dispatcher->shiftArgs());
  156. $this->assertSame($this->dispatcher->args, array('b', 'c'));
  157. $this->dispatcher->args = array('a' => 'b', 'c', 'd');
  158. $this->assertEquals('b', $this->dispatcher->shiftArgs());
  159. $this->assertSame($this->dispatcher->args, array('c', 'd'));
  160. $this->dispatcher->args = array('a', 'b' => 'c', 'd');
  161. $this->assertEquals('a', $this->dispatcher->shiftArgs());
  162. $this->assertSame($this->dispatcher->args, array('b' => 'c', 'd'));
  163. $this->dispatcher->args = array(0 => 'a', 2 => 'b', 30 => 'c');
  164. $this->assertEquals('a', $this->dispatcher->shiftArgs());
  165. $this->assertSame($this->dispatcher->args, array(0 => 'b', 1 => 'c'));
  166. $this->dispatcher->args = array();
  167. $this->assertNull($this->dispatcher->shiftArgs());
  168. $this->assertSame(array(), $this->dispatcher->args);
  169. }
  170. }