PluginTaskTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. * CakePHP : 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 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\Shell\Task\TemplateTask;
  17. use Cake\Core\App;
  18. use Cake\Core\Configure;
  19. use Cake\Core\Plugin;
  20. use Cake\TestSuite\TestCase;
  21. use Cake\Utility\File;
  22. use Cake\Utility\Folder;
  23. /**
  24. * PluginTaskPlugin class
  25. */
  26. class PluginTaskTest extends TestCase {
  27. /**
  28. * setUp method
  29. *
  30. * @return void
  31. */
  32. public function setUp() {
  33. parent::setUp();
  34. $this->io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  35. $this->Task = $this->getMock('Cake\Shell\Task\PluginTask',
  36. array('in', 'err', 'createFile', '_stop', 'clear', 'callProcess'),
  37. array($this->io)
  38. );
  39. $this->Task->Template = new TemplateTask($this->io);
  40. $this->Task->Template->interactive = false;
  41. $this->Task->path = TMP . 'tests' . DS;
  42. $this->Task->bootstrap = TMP . 'tests' . DS . 'bootstrap.php';
  43. if (!is_dir($this->Task->path)) {
  44. mkdir($this->Task->path);
  45. }
  46. touch($this->Task->bootstrap);
  47. $this->_path = App::path('Plugin');
  48. }
  49. /**
  50. * tearDown()
  51. *
  52. * @return void
  53. */
  54. public function tearDown() {
  55. if (file_exists($this->Task->bootstrap)) {
  56. unlink($this->Task->bootstrap);
  57. }
  58. parent::tearDown();
  59. }
  60. /**
  61. * test bake()
  62. *
  63. * @return void
  64. */
  65. public function testBakeFoldersAndFiles() {
  66. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('y'));
  67. $path = $this->Task->path . 'BakeTestPlugin';
  68. $file = $path . DS . 'src' . DS . 'Controller' . DS . 'AppController.php';
  69. $this->Task->expects($this->at(1))->method('createFile')
  70. ->with($file, $this->stringContains('namespace BakeTestPlugin\Controller;'));
  71. $this->Task->bake('BakeTestPlugin');
  72. $path = $this->Task->path . 'BakeTestPlugin';
  73. $this->assertTrue(is_dir($path), 'No plugin dir');
  74. $directories = array(
  75. 'config',
  76. 'src/Model/Behavior',
  77. 'src/Model/Table',
  78. 'src/Model/Entity',
  79. 'src/Shell/Task',
  80. 'src/Controller/Component',
  81. 'src/View/Helper',
  82. 'tests/TestCase/Controller/Component',
  83. 'tests/TestCase/View/Helper',
  84. 'tests/TestCase/Model/Behavior',
  85. 'tests/Fixture',
  86. 'src/Template',
  87. 'webroot'
  88. );
  89. foreach ($directories as $dir) {
  90. $this->assertTrue(is_dir($path . DS . $dir), 'Missing directory for ' . $dir);
  91. }
  92. $Folder = new Folder($this->Task->path . 'BakeTestPlugin');
  93. $Folder->delete();
  94. }
  95. /**
  96. * test execute with no args, flowing into interactive,
  97. *
  98. * @return void
  99. */
  100. public function testExecuteWithNoArgs() {
  101. $path = $this->Task->path . 'TestPlugin';
  102. $file = $path . DS . 'Controller' . DS . 'TestPluginAppController.php';
  103. $this->Task->expects($this->at(0))
  104. ->method('err')
  105. ->with($this->stringContains('You must'));
  106. $this->Task->expects($this->never())
  107. ->method('createFile');
  108. $this->Task->main();
  109. $Folder = new Folder($path);
  110. $Folder->delete();
  111. }
  112. /**
  113. * Test Execute
  114. *
  115. * @return void
  116. */
  117. public function testExecuteWithOneArg() {
  118. $this->Task->expects($this->at(0))->method('in')
  119. ->will($this->returnValue('y'));
  120. $path = $this->Task->path . 'BakeTestPlugin';
  121. $file = $path . DS . 'src' . DS . 'Controller' . DS . 'AppController.php';
  122. $this->Task->expects($this->at(1))->method('createFile')
  123. ->with($file, $this->stringContains('class AppController extends BaseController {'));
  124. $file = $path . DS . 'config' . DS . 'routes.php';
  125. $this->Task->expects($this->at(2))->method('createFile')
  126. ->with($file, $this->stringContains("Router::plugin('BakeTestPlugin', function(\$routes)"));
  127. $file = $path . DS . 'phpunit.xml';
  128. $this->Task->expects($this->at(3))->method('createFile')
  129. ->with($file, $this->anything());
  130. $file = $path . DS . 'tests' . DS . 'bootstrap.php';
  131. $this->Task->expects($this->at(4))->method('createFile')
  132. ->with($file, $this->anything());
  133. $this->Task->main('BakeTestPlugin');
  134. $Folder = new Folder($this->Task->path . 'BakeTestPlugin');
  135. $Folder->delete();
  136. }
  137. /**
  138. * Test that baking a plugin for a project that contains a composer.json, the later
  139. * will be updated
  140. *
  141. * @return void
  142. */
  143. public function testExecuteUpdateComposer() {
  144. $this->Task->expects($this->at(0))->method('in')
  145. ->will($this->returnValue('y'));
  146. $this->Task->Project = $this->getMock('ComposerProject', ['findComposer']);
  147. $this->Task->Project->expects($this->at(0))
  148. ->method('findComposer')
  149. ->will($this->returnValue('composer.phar'));
  150. $path = dirname($this->Task->path);
  151. $file = $path . DS . 'composer.json';
  152. file_put_contents($file, '{}');
  153. $config = [
  154. 'autoload' => [
  155. 'psr-4' => [
  156. 'BakeTestPlugin\\' => './plugins/BakeTestPlugin/src',
  157. ],
  158. ],
  159. 'autoload-dev' => [
  160. 'psr-4' => [
  161. 'BakeTestPlugin\\Test\\' => './plugins/BakeTestPlugin/tests',
  162. ],
  163. ],
  164. ];
  165. $config = json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
  166. $this->Task->expects($this->at(2))
  167. ->method('createFile')
  168. ->with($file, $config);
  169. $this->Task->expects($this->at(3))
  170. ->method('callProcess')
  171. ->with('cd ' . escapeshellarg($path) . '; php ' . escapeshellarg('composer.phar') . ' dump-autoload');
  172. $this->Task->main('BakeTestPlugin');
  173. $Folder = new Folder($this->Task->path . 'BakeTestPlugin');
  174. $Folder->delete();
  175. $File = new File($file);
  176. $File->delete();
  177. }
  178. /**
  179. * Test that findPath ignores paths that don't exist.
  180. *
  181. * @return void
  182. */
  183. public function testFindPathNonExistant() {
  184. $paths = App::path('Plugin');
  185. $last = count($paths);
  186. array_unshift($paths, '/fake/path');
  187. $paths[] = '/fake/path2';
  188. $this->Task = $this->getMock('Cake\Shell\Task\PluginTask',
  189. array('in', 'out', 'err', 'createFile', '_stop'),
  190. array($this->io)
  191. );
  192. $this->Task->path = TMP . 'tests' . DS;
  193. // Make sure the added path is filtered out.
  194. $this->Task->expects($this->exactly($last))
  195. ->method('out');
  196. $this->Task->expects($this->once())
  197. ->method('in')
  198. ->will($this->returnValue($last));
  199. $this->Task->findPath($paths);
  200. }
  201. }