PluginTaskTest.php 6.1 KB

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