SimpleBakeTaskTest.php 4.7 KB

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