PluginTaskTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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\Core\App;
  17. use Cake\Core\Configure;
  18. use Cake\Core\Plugin;
  19. use Cake\Filesystem\File;
  20. use Cake\Filesystem\Folder;
  21. use Cake\Shell\Task\TemplateTask;
  22. use Cake\TestSuite\TestCase;
  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. $this->Task->expects($this->at(0))
  103. ->method('err')
  104. ->with($this->stringContains('You must'));
  105. $this->Task->expects($this->never())
  106. ->method('createFile');
  107. $this->Task->main();
  108. $Folder = new Folder($path);
  109. $Folder->delete();
  110. }
  111. /**
  112. * Test Execute
  113. *
  114. * @return void
  115. */
  116. public function testExecuteWithOneArg() {
  117. $this->Task->expects($this->at(0))->method('in')
  118. ->will($this->returnValue('y'));
  119. $path = $this->Task->path . 'BakeTestPlugin';
  120. $file = $path . DS . 'src' . DS . 'Controller' . DS . 'AppController.php';
  121. $this->Task->expects($this->at(1))->method('createFile')
  122. ->with($file, $this->stringContains('class AppController extends BaseController {'));
  123. $file = $path . DS . 'config' . DS . 'routes.php';
  124. $this->Task->expects($this->at(2))->method('createFile')
  125. ->with($file, $this->stringContains("Router::plugin('BakeTestPlugin', function(\$routes)"));
  126. $file = $path . DS . 'phpunit.xml';
  127. $this->Task->expects($this->at(3))->method('createFile')
  128. ->with($file, $this->anything());
  129. $file = $path . DS . 'tests' . DS . 'bootstrap.php';
  130. $this->Task->expects($this->at(4))->method('createFile')
  131. ->with($file, $this->anything());
  132. $this->Task->main('BakeTestPlugin');
  133. $Folder = new Folder($this->Task->path . 'BakeTestPlugin');
  134. $Folder->delete();
  135. }
  136. /**
  137. * Test that baking a plugin for a project that contains a composer.json, the later
  138. * will be updated
  139. *
  140. * @return void
  141. */
  142. public function testExecuteUpdateComposer() {
  143. $this->Task->expects($this->at(0))->method('in')
  144. ->will($this->returnValue('y'));
  145. $this->Task->Project = $this->getMock('ComposerProject', ['findComposer']);
  146. $this->Task->Project->expects($this->at(0))
  147. ->method('findComposer')
  148. ->will($this->returnValue('composer.phar'));
  149. $path = dirname($this->Task->path);
  150. $file = $path . DS . 'composer.json';
  151. file_put_contents($file, '{}');
  152. $config = [
  153. 'autoload' => [
  154. 'psr-4' => [
  155. 'BakeTestPlugin\\' => './plugins/BakeTestPlugin/src',
  156. ],
  157. ],
  158. 'autoload-dev' => [
  159. 'psr-4' => [
  160. 'BakeTestPlugin\\Test\\' => './plugins/BakeTestPlugin/tests',
  161. ],
  162. ],
  163. ];
  164. $config = json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
  165. $this->Task->expects($this->at(2))
  166. ->method('createFile')
  167. ->with($file, $config);
  168. $this->Task->expects($this->at(3))
  169. ->method('callProcess')
  170. ->with('cd ' . escapeshellarg($path) . '; php ' . escapeshellarg('composer.phar') . ' dump-autoload');
  171. $this->Task->main('BakeTestPlugin');
  172. $Folder = new Folder($this->Task->path . 'BakeTestPlugin');
  173. $Folder->delete();
  174. $File = new File($file);
  175. $File->delete();
  176. }
  177. /**
  178. * Test that findPath ignores paths that don't exist.
  179. *
  180. * @return void
  181. */
  182. public function testFindPathNonExistant() {
  183. $paths = App::path('Plugin');
  184. $last = count($paths);
  185. array_unshift($paths, '/fake/path');
  186. $paths[] = '/fake/path2';
  187. $this->Task = $this->getMock('Cake\Shell\Task\PluginTask',
  188. array('in', 'out', 'err', 'createFile', '_stop'),
  189. array($this->io)
  190. );
  191. $this->Task->path = TMP . 'tests' . DS;
  192. // Make sure the added path is filtered out.
  193. $this->Task->expects($this->exactly($last))
  194. ->method('out');
  195. $this->Task->expects($this->once())
  196. ->method('in')
  197. ->will($this->returnValue($last));
  198. $this->Task->findPath($paths);
  199. }
  200. }