PluginTaskTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. * @package Cake.Test.Case.Console.Command.Task
  17. * @since CakePHP v 1.3.0
  18. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  19. */
  20. App::uses('ShellDispatcher', 'Console');
  21. App::uses('ConsoleOutput', 'Console');
  22. App::uses('ConsoleInput', 'Console');
  23. App::uses('Shell', 'Console');
  24. App::uses('PluginTask', 'Console/Command/Task');
  25. App::uses('ModelTask', 'Console/Command/Task');
  26. App::uses('Folder', 'Utility');
  27. App::uses('File', 'Utility');
  28. /**
  29. * PluginTaskPlugin class
  30. *
  31. * @package Cake.Test.Case.Console.Command.Task
  32. */
  33. class PluginTaskTest extends CakeTestCase {
  34. /**
  35. * setUp method
  36. *
  37. * @return void
  38. */
  39. public function setUp() {
  40. parent::setUp();
  41. $this->out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  42. $this->in = $this->getMock('ConsoleInput', array(), array(), '', false);
  43. $this->Task = $this->getMock('PluginTask',
  44. array('in', 'err', 'createFile', '_stop', 'clear'),
  45. array($this->out, $this->out, $this->in)
  46. );
  47. $this->Task->path = TMP . 'tests' . DS;
  48. $this->Task->bootstrap = TMP . 'tests' . DS . 'bootstrap.php';
  49. touch($this->Task->bootstrap);
  50. $this->_paths = $paths = App::path('plugins');
  51. foreach ($paths as $i => $p) {
  52. if (!is_dir($p)) {
  53. array_splice($paths, $i, 1);
  54. }
  55. }
  56. $this->_testPath = array_push($paths, TMP . 'tests' . DS);
  57. App::build(array('plugins' => $paths));
  58. }
  59. /**
  60. * tearDown()
  61. *
  62. * @return void
  63. */
  64. public function tearDown() {
  65. if (file_exists($this->Task->bootstrap)) {
  66. unlink($this->Task->bootstrap);
  67. }
  68. parent::tearDown();
  69. }
  70. /**
  71. * test bake()
  72. *
  73. * @return void
  74. */
  75. public function testBakeFoldersAndFiles() {
  76. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue($this->_testPath));
  77. $this->Task->expects($this->at(1))->method('in')->will($this->returnValue('y'));
  78. $path = $this->Task->path . 'BakeTestPlugin';
  79. $file = $path . DS . 'Controller' . DS . 'BakeTestPluginAppController.php';
  80. $this->Task->expects($this->at(2))->method('createFile')
  81. ->with($file, new PHPUnit_Framework_Constraint_IsAnything());
  82. $file = $path . DS . 'Model' . DS . 'BakeTestPluginAppModel.php';
  83. $this->Task->expects($this->at(3))->method('createFile')
  84. ->with($file, new PHPUnit_Framework_Constraint_IsAnything());
  85. $this->Task->bake('BakeTestPlugin');
  86. $path = $this->Task->path . 'BakeTestPlugin';
  87. $this->assertTrue(is_dir($path), 'No plugin dir %s');
  88. $directories = array(
  89. 'Config' . DS . 'Schema',
  90. 'Model' . DS . 'Behavior',
  91. 'Model' . DS . 'Datasource',
  92. 'Console' . DS . 'Command' . DS . 'Task',
  93. 'Controller' . DS . 'Component',
  94. 'Lib',
  95. 'View' . DS . 'Helper',
  96. 'Test' . DS . 'Case' . DS . 'Controller' . DS . 'Component',
  97. 'Test' . DS . 'Case' . DS . 'View' . DS . 'Helper',
  98. 'Test' . DS . 'Case' . DS . 'Model' . DS . 'Behavior',
  99. 'Test' . DS . 'Fixture',
  100. 'Vendor',
  101. 'webroot'
  102. );
  103. foreach ($directories as $dir) {
  104. $this->assertTrue(is_dir($path . DS . $dir), 'Missing directory for ' . $dir);
  105. }
  106. $Folder = new Folder($this->Task->path . 'BakeTestPlugin');
  107. $Folder->delete();
  108. }
  109. /**
  110. * test execute with no args, flowing into interactive,
  111. *
  112. * @return void
  113. */
  114. public function testExecuteWithNoArgs() {
  115. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('TestPlugin'));
  116. $this->Task->expects($this->at(1))->method('in')->will($this->returnValue($this->_testPath));
  117. $this->Task->expects($this->at(2))->method('in')->will($this->returnValue('y'));
  118. $path = $this->Task->path . 'TestPlugin';
  119. $file = $path . DS . 'Controller' . DS . 'TestPluginAppController.php';
  120. $this->Task->expects($this->at(3))->method('createFile')
  121. ->with($file, new PHPUnit_Framework_Constraint_IsAnything());
  122. $file = $path . DS . 'Model' . DS . 'TestPluginAppModel.php';
  123. $this->Task->expects($this->at(4))->method('createFile')
  124. ->with($file, new PHPUnit_Framework_Constraint_IsAnything());
  125. $this->Task->args = array();
  126. $this->Task->execute();
  127. $Folder = new Folder($path);
  128. $Folder->delete();
  129. }
  130. /**
  131. * Test Execute
  132. *
  133. * @return void
  134. */
  135. public function testExecuteWithOneArg() {
  136. $this->Task->expects($this->at(0))->method('in')
  137. ->will($this->returnValue($this->_testPath));
  138. $this->Task->expects($this->at(1))->method('in')
  139. ->will($this->returnValue('y'));
  140. $path = $this->Task->path . 'BakeTestPlugin';
  141. $file = $path . DS . 'Controller' . DS . 'BakeTestPluginAppController.php';
  142. $this->Task->expects($this->at(2))->method('createFile')
  143. ->with($file, new PHPUnit_Framework_Constraint_IsAnything());
  144. $path = $this->Task->path . 'BakeTestPlugin';
  145. $file = $path . DS . 'Model' . DS . 'BakeTestPluginAppModel.php';
  146. $this->Task->expects($this->at(3))->method('createFile')
  147. ->with($file, new PHPUnit_Framework_Constraint_IsAnything());
  148. $this->Task->args = array('BakeTestPlugin');
  149. $this->Task->execute();
  150. $Folder = new Folder($this->Task->path . 'BakeTestPlugin');
  151. $Folder->delete();
  152. }
  153. /**
  154. * Test that findPath ignores paths that don't exist.
  155. *
  156. * @return void
  157. */
  158. public function testFindPathNonExistant() {
  159. $paths = App::path('plugins');
  160. $last = count($paths);
  161. array_unshift($paths, '/fake/path');
  162. $paths[] = '/fake/path2';
  163. $this->Task = $this->getMock('PluginTask',
  164. array('in', 'out', 'err', 'createFile', '_stop'),
  165. array($this->out, $this->out, $this->in)
  166. );
  167. $this->Task->path = TMP . 'tests' . DS;
  168. // Make sure the added path is filtered out.
  169. $this->Task->expects($this->exactly($last))
  170. ->method('out');
  171. $this->Task->expects($this->once())
  172. ->method('in')
  173. ->will($this->returnValue($last));
  174. $this->Task->findPath($paths);
  175. }
  176. }