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