PluginTaskTest.php 5.4 KB

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