SimpleBakeTaskTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. $out = $this->getMock('Cake\Console\ConsoleOutput', [], [], '', false);
  32. $in = $this->getMock('Cake\Console\ConsoleInput', [], [], '', false);
  33. $this->Task = $this->getMock(
  34. 'Cake\Console\Command\Task\SimpleBakeTask',
  35. ['in', 'err', 'createFile', '_stop', 'name', 'template', 'fileName'],
  36. [$out, $out, $in]
  37. );
  38. $this->Task->Test = $this->getMock('Cake\Console\Command\Task\TestTask',
  39. [],
  40. [$out, $out, $in]
  41. );
  42. $this->Task->Template = new TemplateTask($out, $out, $in);
  43. $this->Task->Template->initialize();
  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 excute method.
  57. *
  58. * @return void
  59. */
  60. public function testExecute() {
  61. $this->Task->args = ['Example'];
  62. $this->Task->expects($this->once())
  63. ->method('createFile')
  64. ->with(
  65. APP . 'Model/Behavior/ExampleBehavior.php',
  66. $this->stringContains('class ExampleBehavior extends Behavior')
  67. );
  68. $this->Task->Test->expects($this->once())
  69. ->method('bake')
  70. ->with('behavior', 'Example');
  71. $this->Task->execute();
  72. }
  73. /**
  74. * Test generating code.
  75. *
  76. * @return void
  77. */
  78. public function testBake() {
  79. Configure::write('App.namespace', 'TestApp');
  80. $this->Task->expects($this->once())
  81. ->method('createFile')
  82. ->with(
  83. APP . 'Model/Behavior/ExampleBehavior.php',
  84. $this->stringContains('class ExampleBehavior extends Behavior')
  85. );
  86. $result = $this->Task->bake('Example');
  87. $this->assertContains('namespace TestApp\Model\Behavior;', $result);
  88. $this->assertContains('use Cake\ORM\Behavior;', $result);
  89. $this->assertContains('class ExampleBehavior extends Behavior {', $result);
  90. }
  91. /**
  92. * Test bakeTest
  93. *
  94. * @return void
  95. */
  96. public function testBakeTest() {
  97. $this->Task->plugin = 'TestPlugin';
  98. $this->Task->Test->expects($this->once())
  99. ->method('bake')
  100. ->with('behavior', 'Example');
  101. $this->Task->bakeTest('Example');
  102. $this->assertEquals($this->Task->plugin, $this->Task->Test->plugin);
  103. }
  104. /**
  105. * Test the no-test option.
  106. *
  107. * @return void
  108. */
  109. public function testBakeTestNoTest() {
  110. $this->Task->params['no-test'] = true;
  111. $this->Task->Test->expects($this->never())
  112. ->method('bake');
  113. $result = $this->Task->bakeTest('Example');
  114. }
  115. /**
  116. * Test baking within a plugin.
  117. *
  118. * @return void
  119. */
  120. public function testBakePlugin() {
  121. Plugin::load('TestPlugin');
  122. $path = Plugin::path('TestPlugin');
  123. $this->Task->plugin = 'TestPlugin';
  124. $this->Task->expects($this->once())
  125. ->method('createFile')
  126. ->with(
  127. $path . 'Model/Behavior/ExampleBehavior.php',
  128. $this->stringContains('class ExampleBehavior extends Behavior')
  129. );
  130. $result = $this->Task->bake('Example');
  131. $this->assertContains('namespace TestPlugin\Model\Behavior;', $result);
  132. $this->assertContains('use Cake\ORM\Behavior;', $result);
  133. $this->assertContains('class ExampleBehavior extends Behavior {', $result);
  134. }
  135. /**
  136. * Provider for subclasses.
  137. *
  138. * @return array
  139. */
  140. public function subclassProvider() {
  141. return [
  142. ['Cake\Console\Command\Task\BehaviorTask'],
  143. ['Cake\Console\Command\Task\ComponentTask'],
  144. ['Cake\Console\Command\Task\HelperTask'],
  145. ];
  146. }
  147. /**
  148. * Test that the various implementations are sane.
  149. *
  150. * @dataProvider subclassProvider
  151. * @return void
  152. */
  153. public function testImplementations($class) {
  154. $out = $this->getMock('Cake\Console\ConsoleOutput', [], [], '', false);
  155. $in = $this->getMock('Cake\Console\ConsoleInput', [], [], '', false);
  156. $task = new $class($out, $out, $in);
  157. $this->assertInternalType('string', $task->name());
  158. $this->assertInternalType('string', $task->fileName('Example'));
  159. $this->assertInternalType('string', $task->template());
  160. }
  161. }