BakeShellTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 1.3.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Shell;
  16. use Cake\Controller\Controller;
  17. use Cake\Core\App;
  18. use Cake\Core\Configure;
  19. use Cake\Core\Plugin;
  20. use Cake\Shell\BakeShellShell;
  21. use Cake\TestSuite\TestCase;
  22. class BakeShellTest extends TestCase {
  23. /**
  24. * fixtures
  25. *
  26. * @var array
  27. */
  28. public $fixtures = array('core.comments');
  29. /**
  30. * setup test
  31. *
  32. * @return void
  33. */
  34. public function setUp() {
  35. parent::setUp();
  36. $this->io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  37. $this->Shell = $this->getMock(
  38. 'Cake\Shell\BakeShell',
  39. ['in', 'out', 'hr', 'err', 'createFile', '_stop'],
  40. [$this->io]
  41. );
  42. Configure::write('App.namespace', 'TestApp');
  43. }
  44. /**
  45. * tearDown method
  46. *
  47. * @return void
  48. */
  49. public function tearDown() {
  50. parent::tearDown();
  51. unset($this->Shell);
  52. }
  53. /**
  54. * test bake all
  55. *
  56. * @return void
  57. */
  58. public function testAllWithModelName() {
  59. $this->Shell->Model = $this->getMock('Cake\Shell\Task\ModelTask');
  60. $this->Shell->Controller = $this->getMock('Cake\Shell\Task\ControllerTask');
  61. $this->Shell->View = $this->getMock('Cake\Shell\Task\ModelTask');
  62. $this->Shell->Model->expects($this->once())
  63. ->method('bake')
  64. ->with('Comments')
  65. ->will($this->returnValue(true));
  66. $this->Shell->Controller->expects($this->once())
  67. ->method('bake')
  68. ->with('Comments')
  69. ->will($this->returnValue(true));
  70. $this->Shell->View->expects($this->once())
  71. ->method('main')
  72. ->with('Comments');
  73. $this->Shell->expects($this->at(0))
  74. ->method('out')
  75. ->with('Bake All');
  76. $this->Shell->expects($this->at(2))
  77. ->method('out')
  78. ->with('<success>Bake All complete.</success>');
  79. $this->Shell->connection = '';
  80. $this->Shell->params = [];
  81. $this->Shell->all('Comments');
  82. }
  83. /**
  84. * Test the main function.
  85. *
  86. * @return void
  87. */
  88. public function testMain() {
  89. $this->Shell->expects($this->at(0))
  90. ->method('out')
  91. ->with($this->stringContains('The following commands'));
  92. $this->Shell->expects($this->exactly(18))
  93. ->method('out');
  94. $this->Shell->loadTasks();
  95. $this->Shell->main();
  96. }
  97. /**
  98. * Test that the generated option parser reflects all tasks.
  99. *
  100. * @return void
  101. */
  102. public function testGetOptionParser() {
  103. $this->Shell->loadTasks();
  104. $parser = $this->Shell->getOptionParser();
  105. $commands = $parser->subcommands();
  106. $this->assertArrayHasKey('fixture', $commands);
  107. $this->assertArrayHasKey('view', $commands);
  108. $this->assertArrayHasKey('controller', $commands);
  109. $this->assertArrayHasKey('model', $commands);
  110. }
  111. /**
  112. * Test loading tasks from core directories.
  113. *
  114. * @return void
  115. */
  116. public function testLoadTasksCoreAndApp() {
  117. $this->Shell->loadTasks();
  118. $expected = [
  119. 'Behavior',
  120. 'Cell',
  121. 'Component',
  122. 'Controller',
  123. 'Fixture',
  124. 'Helper',
  125. 'Model',
  126. 'Plugin',
  127. 'Project',
  128. 'Shell',
  129. 'Test',
  130. 'View',
  131. 'Zerg',
  132. ];
  133. sort($this->Shell->tasks);
  134. sort($expected);
  135. $this->assertEquals($expected, $this->Shell->tasks);
  136. }
  137. /**
  138. * Test loading tasks from plugins
  139. *
  140. * @return void
  141. */
  142. public function testLoadTasksPlugin() {
  143. Plugin::load('TestPlugin');
  144. $this->Shell->loadTasks();
  145. $this->assertContains('TestPlugin.Widget', $this->Shell->tasks);
  146. $this->assertContains('TestPlugin.Zerg', $this->Shell->tasks);
  147. }
  148. }