PluginTaskTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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-2011, 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-2011, 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->_paths = $paths = App::path('plugins');
  50. foreach ($paths as $i => $p) {
  51. if (!is_dir($p)) {
  52. array_splice($paths, $i, 1);
  53. }
  54. }
  55. $this->_testPath = array_push($paths, TMP . 'tests' . DS);
  56. App::build(array('plugins' => $paths));
  57. }
  58. /**
  59. * test bake()
  60. *
  61. * @return void
  62. */
  63. public function testBakeFoldersAndFiles() {
  64. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue($this->_testPath));
  65. $this->Task->expects($this->at(1))->method('in')->will($this->returnValue('y'));
  66. $path = $this->Task->path . 'BakeTestPlugin';
  67. $file = $path . DS . 'Controller' . DS . 'BakeTestPluginAppController.php';
  68. $this->Task->expects($this->at(2))->method('createFile')
  69. ->with($file, new PHPUnit_Framework_Constraint_IsAnything());
  70. $file = $path . DS . 'Model' . DS . 'BakeTestPluginAppModel.php';
  71. $this->Task->expects($this->at(3))->method('createFile')
  72. ->with($file, new PHPUnit_Framework_Constraint_IsAnything());
  73. $this->Task->bake('BakeTestPlugin');
  74. $path = $this->Task->path . 'BakeTestPlugin';
  75. $this->assertTrue(is_dir($path), 'No plugin dir %s');
  76. $directories = array(
  77. 'Config' . DS . 'Schema',
  78. 'Model' . DS . 'Behavior',
  79. 'Model' . DS . 'Datasource',
  80. 'Console' . DS . 'Command' . DS . 'Task',
  81. 'Controller' . DS . 'Component',
  82. 'Lib',
  83. 'View' . DS . 'Helper',
  84. 'Test' . DS . 'Case' . DS . 'Controller' . DS . 'Component',
  85. 'Test' . DS . 'Case' . DS . 'View' . DS . 'Helper',
  86. 'Test' . DS . 'Case' . DS . 'Model' . DS . 'Behavior',
  87. 'Test' . DS . 'Fixture',
  88. 'Vendor',
  89. 'webroot'
  90. );
  91. foreach ($directories as $dir) {
  92. $this->assertTrue(is_dir($path . DS . $dir), 'Missing directory for ' . $dir);
  93. }
  94. $Folder = new Folder($this->Task->path . 'BakeTestPlugin');
  95. $Folder->delete();
  96. }
  97. /**
  98. * test execute with no args, flowing into interactive,
  99. *
  100. * @return void
  101. */
  102. public function testExecuteWithNoArgs() {
  103. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('TestPlugin'));
  104. $this->Task->expects($this->at(1))->method('in')->will($this->returnValue($this->_testPath));
  105. $this->Task->expects($this->at(2))->method('in')->will($this->returnValue('y'));
  106. $path = $this->Task->path . 'TestPlugin';
  107. $file = $path . DS . 'Controller' . DS . 'TestPluginAppController.php';
  108. $this->Task->expects($this->at(3))->method('createFile')
  109. ->with($file, new PHPUnit_Framework_Constraint_IsAnything());
  110. $file = $path . DS . 'Model' . DS . 'TestPluginAppModel.php';
  111. $this->Task->expects($this->at(4))->method('createFile')
  112. ->with($file, new PHPUnit_Framework_Constraint_IsAnything());
  113. $this->Task->args = array();
  114. $this->Task->execute();
  115. $Folder = new Folder($path);
  116. $Folder->delete();
  117. }
  118. /**
  119. * Test Execute
  120. *
  121. * @return void
  122. */
  123. public function testExecuteWithOneArg() {
  124. $this->Task->expects($this->at(0))->method('in')
  125. ->will($this->returnValue($this->_testPath));
  126. $this->Task->expects($this->at(1))->method('in')
  127. ->will($this->returnValue('y'));
  128. $path = $this->Task->path . 'BakeTestPlugin';
  129. $file = $path . DS . 'Controller' . DS . 'BakeTestPluginAppController.php';
  130. $this->Task->expects($this->at(2))->method('createFile')
  131. ->with($file, new PHPUnit_Framework_Constraint_IsAnything());
  132. $path = $this->Task->path . 'BakeTestPlugin';
  133. $file = $path . DS . 'Model' . DS . 'BakeTestPluginAppModel.php';
  134. $this->Task->expects($this->at(3))->method('createFile')
  135. ->with($file, new PHPUnit_Framework_Constraint_IsAnything());
  136. $this->Task->args = array('BakeTestPlugin');
  137. $this->Task->execute();
  138. $Folder = new Folder($this->Task->path . 'BakeTestPlugin');
  139. $Folder->delete();
  140. }
  141. /**
  142. * Test that findPath ignores paths that don't exist.
  143. *
  144. * @return void
  145. */
  146. public function testFindPathNonExistant() {
  147. $paths = App::path('plugins');
  148. $last = count($paths);
  149. $paths[] = '/fake/path';
  150. $this->Task = $this->getMock('PluginTask',
  151. array('in', 'out', 'err', 'createFile', '_stop'),
  152. array($this->out, $this->out, $this->in)
  153. );
  154. $this->Task->path = TMP . 'tests' . DS;
  155. // Make sure the added path is filtered out.
  156. $this->Task->expects($this->exactly($last))
  157. ->method('out');
  158. $this->Task->expects($this->once())
  159. ->method('in')
  160. ->will($this->returnValue($last));
  161. $this->Task->findPath($paths);
  162. }
  163. }