PluginTaskTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. $path = $this->Task->path . 'TestPlugin';
  102. $file = $path . DS . 'Controller' . DS . 'TestPluginAppController.php';
  103. $this->Task->expects($this->at(0))
  104. ->method('err')
  105. ->with($this->stringContains('You must'));
  106. $this->Task->expects($this->never())
  107. ->method('createFile');
  108. $this->Task->args = array();
  109. $this->Task->execute();
  110. $Folder = new Folder($path);
  111. $Folder->delete();
  112. }
  113. /**
  114. * Test Execute
  115. *
  116. * @return void
  117. */
  118. public function testExecuteWithOneArg() {
  119. $this->Task->expects($this->at(0))->method('in')
  120. ->will($this->returnValue('y'));
  121. $path = $this->Task->path . 'BakeTestPlugin';
  122. $file = $path . DS . 'Controller' . DS . 'BakeTestPluginAppController.php';
  123. $this->Task->expects($this->at(1))->method('createFile')
  124. ->with($file, new \PHPUnit_Framework_Constraint_IsAnything());
  125. $file = $path . DS . 'phpunit.xml';
  126. $this->Task->expects($this->at(2))->method('createFile')
  127. ->with($file, new \PHPUnit_Framework_Constraint_IsAnything());
  128. $file = $path . DS . 'Test' . DS . 'bootstrap.php';
  129. $this->Task->expects($this->at(3))->method('createFile')
  130. ->with($file, new \PHPUnit_Framework_Constraint_IsAnything());
  131. $this->Task->args = array('BakeTestPlugin');
  132. $this->Task->execute();
  133. $Folder = new Folder($this->Task->path . 'BakeTestPlugin');
  134. $Folder->delete();
  135. }
  136. /**
  137. * Test that findPath ignores paths that don't exist.
  138. *
  139. * @return void
  140. */
  141. public function testFindPathNonExistant() {
  142. $paths = App::path('Plugin');
  143. $last = count($paths);
  144. array_unshift($paths, '/fake/path');
  145. $paths[] = '/fake/path2';
  146. $this->Task = $this->getMock('Cake\Console\Command\Task\PluginTask',
  147. array('in', 'out', 'err', 'createFile', '_stop'),
  148. array($this->out, $this->out, $this->in)
  149. );
  150. $this->Task->path = TMP . 'tests' . DS;
  151. // Make sure the added path is filtered out.
  152. $this->Task->expects($this->exactly($last))
  153. ->method('out');
  154. $this->Task->expects($this->once())
  155. ->method('in')
  156. ->will($this->returnValue($last));
  157. $this->Task->findPath($paths);
  158. }
  159. }