PluginTaskTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * PluginTask Test file
  4. *
  5. * Test Case for plugin generation shell task
  6. *
  7. * CakePHP : Rapid Development Framework (http://cakephp.org)
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP Project
  16. * @since CakePHP v 1.3.0
  17. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  18. */
  19. namespace Cake\Test\TestCase\Console\Command\Task;
  20. use Cake\Console\Command\Task\DbConfigTask;
  21. use Cake\Core\App;
  22. use Cake\Core\Configure;
  23. use Cake\Core\Plugin;
  24. use Cake\TestSuite\TestCase;
  25. use Cake\Utility\Folder;
  26. /**
  27. * PluginTaskPlugin class
  28. *
  29. */
  30. class PluginTaskTest extends TestCase {
  31. /**
  32. * setUp method
  33. *
  34. * @return void
  35. */
  36. public function setUp() {
  37. parent::setUp();
  38. $this->out = $this->getMock('Cake\Console\ConsoleOutput', array(), array(), '', false);
  39. $this->in = $this->getMock('Cake\Console\ConsoleInput', array(), array(), '', false);
  40. $this->Task = $this->getMock('Cake\Console\Command\Task\PluginTask',
  41. array('in', 'err', 'createFile', '_stop', 'clear'),
  42. array($this->out, $this->out, $this->in)
  43. );
  44. $this->Task->path = TMP . 'tests/';
  45. $this->Task->bootstrap = TMP . 'tests/bootstrap.php';
  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 . DS . 'Controller/BakeTestPluginAppController.php';
  69. $this->Task->expects($this->at(1))->method('createFile')
  70. ->with($file, new \PHPUnit_Framework_Constraint_IsAnything());
  71. $file = $path . DS . 'Model/BakeTestPluginAppModel.php';
  72. $this->Task->expects($this->at(2))->method('createFile')
  73. ->with($file, new \PHPUnit_Framework_Constraint_IsAnything());
  74. $this->Task->bake('BakeTestPlugin');
  75. $path = $this->Task->path . 'BakeTestPlugin';
  76. $this->assertTrue(is_dir($path), 'No plugin dir');
  77. $directories = array(
  78. 'Config/Schema',
  79. 'Model/Behavior',
  80. 'Model/Datasource',
  81. 'Console/Command/Task',
  82. 'Controller/Component',
  83. 'Lib',
  84. 'View/Helper',
  85. 'Test/Case/Controller/Component',
  86. 'Test/Case/View/Helper',
  87. 'Test/Case/Model/Behavior',
  88. 'Test/Fixture',
  89. 'vendor',
  90. 'webroot'
  91. );
  92. foreach ($directories as $dir) {
  93. $this->assertTrue(is_dir($path . DS . $dir), 'Missing directory for ' . $dir);
  94. }
  95. $Folder = new Folder($this->Task->path . 'BakeTestPlugin');
  96. $Folder->delete();
  97. }
  98. /**
  99. * test execute with no args, flowing into interactive,
  100. *
  101. * @return void
  102. */
  103. public function testExecuteWithNoArgs() {
  104. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('TestPlugin'));
  105. $this->Task->expects($this->at(1))->method('in')->will($this->returnValue('y'));
  106. $path = $this->Task->path . 'TestPlugin';
  107. $file = $path . DS . 'Controller/TestPluginAppController.php';
  108. $this->Task->expects($this->at(2))->method('createFile')
  109. ->with($file, new \PHPUnit_Framework_Constraint_IsAnything());
  110. $file = $path . DS . 'Model/TestPluginAppModel.php';
  111. $this->Task->expects($this->at(3))->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('y'));
  126. $path = $this->Task->path . 'BakeTestPlugin';
  127. $file = $path . DS . 'Controller/BakeTestPluginAppController.php';
  128. $this->Task->expects($this->at(1))->method('createFile')
  129. ->with($file, new \PHPUnit_Framework_Constraint_IsAnything());
  130. $path = $this->Task->path . 'BakeTestPlugin';
  131. $file = $path . DS . 'Model/BakeTestPluginAppModel.php';
  132. $this->Task->expects($this->at(2))->method('createFile')
  133. ->with($file, new \PHPUnit_Framework_Constraint_IsAnything());
  134. $this->Task->args = array('BakeTestPlugin');
  135. $this->Task->execute();
  136. $Folder = new Folder($this->Task->path . 'BakeTestPlugin');
  137. $Folder->delete();
  138. }
  139. /**
  140. * Test that findPath ignores paths that don't exist.
  141. *
  142. * @return void
  143. */
  144. public function testFindPathNonExistant() {
  145. $paths = App::path('Plugin');
  146. $last = count($paths);
  147. $paths[] = '/fake/path';
  148. $this->Task = $this->getMock('Cake\Console\Command\Task\PluginTask',
  149. array('in', 'out', 'err', 'createFile', '_stop'),
  150. array($this->out, $this->out, $this->in)
  151. );
  152. $this->Task->path = TMP . 'tests/';
  153. // Make sure the added path is filtered out.
  154. $this->Task->expects($this->exactly($last))
  155. ->method('out');
  156. $this->Task->expects($this->once())
  157. ->method('in')
  158. ->will($this->returnValue($last));
  159. $this->Task->findPath($paths);
  160. }
  161. }