PluginTaskTest.php 5.9 KB

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