SimpleBakeTaskTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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\Console\Command\Task;
  16. use Cake\Console\Command\Task\TemplateTask;
  17. use Cake\Core\Configure;
  18. use Cake\Core\Plugin;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * SimpleBakeTaskTest class
  22. */
  23. class SimpleBakeTaskTest extends TestCase {
  24. /**
  25. * setup method
  26. *
  27. * @return void
  28. */
  29. public function setUp() {
  30. parent::setUp();
  31. $io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  32. $this->Task = $this->getMock(
  33. 'Cake\Console\Command\Task\SimpleBakeTask',
  34. ['in', 'err', 'createFile', '_stop', 'name', 'template', 'fileName'],
  35. [$io]
  36. );
  37. $this->Task->Test = $this->getMock('Cake\Console\Command\Task\TestTask',
  38. [],
  39. [$io]
  40. );
  41. $this->Task->Template = new TemplateTask($io);
  42. $this->Task->Template->initialize();
  43. $this->Task->Template->interactive = false;
  44. $this->Task->pathFragment = 'Model/Behavior/';
  45. $this->Task->expects($this->any())
  46. ->method('name')
  47. ->will($this->returnValue('behavior'));
  48. $this->Task->expects($this->any())
  49. ->method('template')
  50. ->will($this->returnValue('behavior'));
  51. $this->Task->expects($this->any())
  52. ->method('fileName')
  53. ->will($this->returnValue('ExampleBehavior.php'));
  54. }
  55. /**
  56. * Test the main method.
  57. *
  58. * @return void
  59. */
  60. public function testMain() {
  61. $this->Task->expects($this->once())
  62. ->method('createFile')
  63. ->with(
  64. $this->_normalizePath(APP . 'Model/Behavior/ExampleBehavior.php'),
  65. $this->stringContains('class ExampleBehavior extends Behavior')
  66. );
  67. $this->Task->Test->expects($this->once())
  68. ->method('bake')
  69. ->with('behavior', 'Example');
  70. $this->Task->main('Example');
  71. }
  72. /**
  73. * Test the main with plugin.name method.
  74. *
  75. * @return void
  76. */
  77. public function testMainWithPlugin() {
  78. Plugin::load('TestPlugin');
  79. $filename = $this->_normalizePath(TEST_APP . 'Plugin/TestPlugin/Model/Behavior/ExampleBehavior.php');
  80. $this->Task->expects($this->once())
  81. ->method('createFile')
  82. ->with(
  83. $filename,
  84. $this->stringContains('class ExampleBehavior extends Behavior')
  85. );
  86. $this->Task->Test->expects($this->once())
  87. ->method('bake')
  88. ->with('behavior', 'Example');
  89. $this->Task->main('TestPlugin.Example');
  90. }
  91. /**
  92. * Test generating code.
  93. *
  94. * @return void
  95. */
  96. public function testBake() {
  97. Configure::write('App.namespace', 'TestApp');
  98. $this->Task->expects($this->once())
  99. ->method('createFile')
  100. ->with(
  101. $this->_normalizePath(APP . 'Model/Behavior/ExampleBehavior.php'),
  102. $this->stringContains('class ExampleBehavior extends Behavior')
  103. );
  104. $result = $this->Task->bake('Example');
  105. $this->assertContains('namespace TestApp\Model\Behavior;', $result);
  106. $this->assertContains('use Cake\ORM\Behavior;', $result);
  107. $this->assertContains('class ExampleBehavior extends Behavior {', $result);
  108. }
  109. /**
  110. * Test bakeTest
  111. *
  112. * @return void
  113. */
  114. public function testBakeTest() {
  115. $this->Task->plugin = 'TestPlugin';
  116. $this->Task->Test->expects($this->once())
  117. ->method('bake')
  118. ->with('behavior', 'Example');
  119. $this->Task->bakeTest('Example');
  120. $this->assertEquals($this->Task->plugin, $this->Task->Test->plugin);
  121. }
  122. /**
  123. * Test the no-test option.
  124. *
  125. * @return void
  126. */
  127. public function testBakeTestNoTest() {
  128. $this->Task->params['no-test'] = true;
  129. $this->Task->Test->expects($this->never())
  130. ->method('bake');
  131. $result = $this->Task->bakeTest('Example');
  132. }
  133. /**
  134. * Test baking within a plugin.
  135. *
  136. * @return void
  137. */
  138. public function testBakePlugin() {
  139. Plugin::load('TestPlugin');
  140. $path = Plugin::path('TestPlugin');
  141. $this->Task->plugin = 'TestPlugin';
  142. $this->Task->expects($this->once())
  143. ->method('createFile')
  144. ->with(
  145. $this->_normalizePath($path . 'Model/Behavior/ExampleBehavior.php'),
  146. $this->stringContains('class ExampleBehavior extends Behavior')
  147. );
  148. $result = $this->Task->bake('Example');
  149. $this->assertContains('namespace TestPlugin\Model\Behavior;', $result);
  150. $this->assertContains('use Cake\ORM\Behavior;', $result);
  151. $this->assertContains('class ExampleBehavior extends Behavior {', $result);
  152. }
  153. /**
  154. * Provider for subclasses.
  155. *
  156. * @return array
  157. */
  158. public function subclassProvider() {
  159. return [
  160. ['Cake\Console\Command\Task\BehaviorTask'],
  161. ['Cake\Console\Command\Task\ComponentTask'],
  162. ['Cake\Console\Command\Task\HelperTask'],
  163. ['Cake\Console\Command\Task\ShellTask'],
  164. ];
  165. }
  166. /**
  167. * Test that the various implementations are sane.
  168. *
  169. * @dataProvider subclassProvider
  170. * @return void
  171. */
  172. public function testImplementations($class) {
  173. $io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  174. $task = new $class($io);
  175. $this->assertInternalType('string', $task->name());
  176. $this->assertInternalType('string', $task->fileName('Example'));
  177. $this->assertInternalType('string', $task->template());
  178. }
  179. }