PluginTaskTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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\Console\Command\Task;
  16. use Cake\Console\Command\Task\TemplateTask;
  17. use Cake\Core\App;
  18. use Cake\Core\Configure;
  19. use Cake\Core\Plugin;
  20. use Cake\TestSuite\TestCase;
  21. use Cake\Utility\Folder;
  22. /**
  23. * PluginTaskPlugin class
  24. */
  25. class PluginTaskTest extends TestCase {
  26. /**
  27. * setUp method
  28. *
  29. * @return void
  30. */
  31. public function setUp() {
  32. parent::setUp();
  33. $this->out = $this->getMock('Cake\Console\ConsoleOutput', array(), array(), '', false);
  34. $this->in = $this->getMock('Cake\Console\ConsoleInput', array(), array(), '', false);
  35. $this->Task = $this->getMock('Cake\Console\Command\Task\PluginTask',
  36. array('in', 'err', 'createFile', '_stop', 'clear'),
  37. array($this->out, $this->out, $this->in)
  38. );
  39. $this->Task->Template = new TemplateTask($this->out, $this->out, $this->in);
  40. $this->Task->path = TMP . 'tests' . DS;
  41. $this->Task->bootstrap = TMP . 'tests' . DS . 'bootstrap.php';
  42. if (!is_dir($this->Task->path)) {
  43. mkdir($this->Task->path);
  44. }
  45. touch($this->Task->bootstrap);
  46. $this->_path = App::path('Plugin');
  47. }
  48. /**
  49. * tearDown()
  50. *
  51. * @return void
  52. */
  53. public function tearDown() {
  54. if (file_exists($this->Task->bootstrap)) {
  55. unlink($this->Task->bootstrap);
  56. }
  57. parent::tearDown();
  58. }
  59. /**
  60. * test bake()
  61. *
  62. * @return void
  63. */
  64. public function testBakeFoldersAndFiles() {
  65. $this->Task->expects($this->at(0))->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(1))->method('createFile')
  69. ->with($file, new \PHPUnit_Framework_Constraint_IsAnything());
  70. $this->Task->bake('BakeTestPlugin');
  71. $path = $this->Task->path . 'BakeTestPlugin';
  72. $this->assertTrue(is_dir($path), 'No plugin dir');
  73. $directories = array(
  74. 'Config/Schema',
  75. 'Model/Behavior',
  76. 'Model/Table',
  77. 'Model/Entity',
  78. 'Console/Command/Task',
  79. 'Controller/Component',
  80. 'Lib',
  81. 'View/Helper',
  82. 'Test/TestCase/Controller/Component',
  83. 'Test/TestCase/View/Helper',
  84. 'Test/TestCase/Model/Behavior',
  85. 'Test/Fixture',
  86. '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. $this->Task->expects($this->at(0))
  102. ->method('in')
  103. ->will($this->returnValue('TestPlugin'));
  104. $this->Task->expects($this->at(1))
  105. ->method('in')
  106. ->will($this->returnValue('y'));
  107. $path = $this->Task->path . 'TestPlugin';
  108. $file = $path . DS . 'Controller' . DS . 'TestPluginAppController.php';
  109. $this->Task->expects($this->at(2))->method('createFile')
  110. ->with($file, new \PHPUnit_Framework_Constraint_IsAnything());
  111. $file = $path . DS . 'phpunit.xml';
  112. $this->Task->expects($this->at(3))->method('createFile')
  113. ->with($file, new \PHPUnit_Framework_Constraint_IsAnything());
  114. $file = $path . DS . 'Test' . DS . 'bootstrap.php';
  115. $this->Task->expects($this->at(4))->method('createFile')
  116. ->with($file, new \PHPUnit_Framework_Constraint_IsAnything());
  117. $this->Task->args = array();
  118. $this->Task->execute();
  119. $Folder = new Folder($path);
  120. $Folder->delete();
  121. }
  122. /**
  123. * Test Execute
  124. *
  125. * @return void
  126. */
  127. public function testExecuteWithOneArg() {
  128. $this->Task->expects($this->at(0))->method('in')
  129. ->will($this->returnValue('y'));
  130. $path = $this->Task->path . 'BakeTestPlugin';
  131. $file = $path . DS . 'Controller' . DS . 'BakeTestPluginAppController.php';
  132. $this->Task->expects($this->at(1))->method('createFile')
  133. ->with($file, new \PHPUnit_Framework_Constraint_IsAnything());
  134. $file = $path . DS . 'phpunit.xml';
  135. $this->Task->expects($this->at(2))->method('createFile')
  136. ->with($file, new \PHPUnit_Framework_Constraint_IsAnything());
  137. $file = $path . DS. 'Test' . DS . 'bootstrap.php';
  138. $this->Task->expects($this->at(3))->method('createFile')
  139. ->with($file, new \PHPUnit_Framework_Constraint_IsAnything());
  140. $this->Task->args = array('BakeTestPlugin');
  141. $this->Task->execute();
  142. $Folder = new Folder($this->Task->path . 'BakeTestPlugin');
  143. $Folder->delete();
  144. }
  145. /**
  146. * Test that findPath ignores paths that don't exist.
  147. *
  148. * @return void
  149. */
  150. public function testFindPathNonExistant() {
  151. $paths = App::path('Plugin');
  152. $last = count($paths);
  153. array_unshift($paths, '/fake/path');
  154. $paths[] = '/fake/path2';
  155. $this->Task = $this->getMock('Cake\Console\Command\Task\PluginTask',
  156. array('in', 'out', 'err', 'createFile', '_stop'),
  157. array($this->out, $this->out, $this->in)
  158. );
  159. $this->Task->path = TMP . 'tests' . DS;
  160. // Make sure the added path is filtered out.
  161. $this->Task->expects($this->exactly($last))
  162. ->method('out');
  163. $this->Task->expects($this->once())
  164. ->method('in')
  165. ->will($this->returnValue($last));
  166. $this->Task->findPath($paths);
  167. }
  168. }