PluginTaskTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * PluginTask Test file
  4. *
  5. * Test Case for plugin generation shell task
  6. *
  7. * CakePHP : Rapid Development Framework (http://cakephp.org)
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP Project
  16. * @since CakePHP v 1.3.0
  17. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  18. */
  19. namespace Cake\Test\TestCase\Console\Command\Task;
  20. use Cake\Console\Command\Task\DbConfigTask;
  21. use Cake\Console\Command\Task\TemplateTask;
  22. use Cake\Core\App;
  23. use Cake\Core\Configure;
  24. use Cake\Core\Plugin;
  25. use Cake\TestSuite\TestCase;
  26. use Cake\Utility\Folder;
  27. /**
  28. * PluginTaskPlugin class
  29. *
  30. */
  31. class PluginTaskTest extends TestCase {
  32. /**
  33. * setUp method
  34. *
  35. * @return void
  36. */
  37. public function setUp() {
  38. parent::setUp();
  39. $this->out = $this->getMock('Cake\Console\ConsoleOutput', array(), array(), '', false);
  40. $this->in = $this->getMock('Cake\Console\ConsoleInput', array(), array(), '', false);
  41. $this->Task = $this->getMock('Cake\Console\Command\Task\PluginTask',
  42. array('in', 'err', 'createFile', '_stop', 'clear'),
  43. array($this->out, $this->out, $this->in)
  44. );
  45. $this->Task->Template = new TemplateTask($this->out, $this->out, $this->in);
  46. $this->Task->path = TMP . 'tests/';
  47. $this->Task->bootstrap = TMP . 'tests/bootstrap.php';
  48. if (!is_dir($this->Task->path)) {
  49. mkdir($this->Task->path);
  50. }
  51. touch($this->Task->bootstrap);
  52. $this->_path = App::path('Plugin');
  53. }
  54. /**
  55. * tearDown()
  56. *
  57. * @return void
  58. */
  59. public function tearDown() {
  60. if (file_exists($this->Task->bootstrap)) {
  61. unlink($this->Task->bootstrap);
  62. }
  63. parent::tearDown();
  64. }
  65. /**
  66. * test bake()
  67. *
  68. * @return void
  69. */
  70. public function testBakeFoldersAndFiles() {
  71. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('y'));
  72. $path = $this->Task->path . 'BakeTestPlugin';
  73. $file = $path . '/Controller/BakeTestPluginAppController.php';
  74. $this->Task->expects($this->at(1))->method('createFile')
  75. ->with($file, new \PHPUnit_Framework_Constraint_IsAnything());
  76. $this->Task->bake('BakeTestPlugin');
  77. $path = $this->Task->path . 'BakeTestPlugin';
  78. $this->assertTrue(is_dir($path), 'No plugin dir');
  79. $directories = array(
  80. 'Config/Schema',
  81. 'Model/Behavior',
  82. 'Model/Table',
  83. 'Model/Entity',
  84. 'Console/Command/Task',
  85. 'Controller/Component',
  86. 'Lib',
  87. 'View/Helper',
  88. 'Test/TestCase/Controller/Component',
  89. 'Test/TestCase/View/Helper',
  90. 'Test/TestCase/Model/Behavior',
  91. 'Test/Fixture',
  92. 'Template',
  93. 'webroot'
  94. );
  95. foreach ($directories as $dir) {
  96. $this->assertTrue(is_dir($path . DS . $dir), 'Missing directory for ' . $dir);
  97. }
  98. $Folder = new Folder($this->Task->path . 'BakeTestPlugin');
  99. $Folder->delete();
  100. }
  101. /**
  102. * test execute with no args, flowing into interactive,
  103. *
  104. * @return void
  105. */
  106. public function testExecuteWithNoArgs() {
  107. $this->Task->expects($this->at(0))
  108. ->method('in')
  109. ->will($this->returnValue('TestPlugin'));
  110. $this->Task->expects($this->at(1))
  111. ->method('in')
  112. ->will($this->returnValue('y'));
  113. $path = $this->Task->path . 'TestPlugin';
  114. $file = $path . '/Controller/TestPluginAppController.php';
  115. $this->Task->expects($this->at(2))->method('createFile')
  116. ->with($file, new \PHPUnit_Framework_Constraint_IsAnything());
  117. $file = $path . '/phpunit.xml';
  118. $this->Task->expects($this->at(3))->method('createFile')
  119. ->with($file, new \PHPUnit_Framework_Constraint_IsAnything());
  120. $file = $path . '/Test/bootstrap.php';
  121. $this->Task->expects($this->at(4))->method('createFile')
  122. ->with($file, new \PHPUnit_Framework_Constraint_IsAnything());
  123. $this->Task->args = array();
  124. $this->Task->execute();
  125. $Folder = new Folder($path);
  126. $Folder->delete();
  127. }
  128. /**
  129. * Test Execute
  130. *
  131. * @return void
  132. */
  133. public function testExecuteWithOneArg() {
  134. $this->Task->expects($this->at(0))->method('in')
  135. ->will($this->returnValue('y'));
  136. $path = $this->Task->path . 'BakeTestPlugin';
  137. $file = $path . DS . 'Controller/BakeTestPluginAppController.php';
  138. $this->Task->expects($this->at(1))->method('createFile')
  139. ->with($file, new \PHPUnit_Framework_Constraint_IsAnything());
  140. $file = $path . '/phpunit.xml';
  141. $this->Task->expects($this->at(2))->method('createFile')
  142. ->with($file, new \PHPUnit_Framework_Constraint_IsAnything());
  143. $file = $path . '/Test/bootstrap.php';
  144. $this->Task->expects($this->at(3))->method('createFile')
  145. ->with($file, new \PHPUnit_Framework_Constraint_IsAnything());
  146. $this->Task->args = array('BakeTestPlugin');
  147. $this->Task->execute();
  148. $Folder = new Folder($this->Task->path . 'BakeTestPlugin');
  149. $Folder->delete();
  150. }
  151. /**
  152. * Test that findPath ignores paths that don't exist.
  153. *
  154. * @return void
  155. */
  156. public function testFindPathNonExistant() {
  157. $paths = App::path('Plugin');
  158. $last = count($paths);
  159. array_unshift($paths, '/fake/path');
  160. $paths[] = '/fake/path2';
  161. $this->Task = $this->getMock('Cake\Console\Command\Task\PluginTask',
  162. array('in', 'out', 'err', 'createFile', '_stop'),
  163. array($this->out, $this->out, $this->in)
  164. );
  165. $this->Task->path = TMP . 'tests/';
  166. // Make sure the added path is filtered out.
  167. $this->Task->expects($this->exactly($last))
  168. ->method('out');
  169. $this->Task->expects($this->once())
  170. ->method('in')
  171. ->will($this->returnValue($last));
  172. $this->Task->findPath($paths);
  173. }
  174. }