PluginTaskTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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->io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  34. $this->Task = $this->getMock('Cake\Console\Command\Task\PluginTask',
  35. array('in', 'err', 'createFile', '_stop', 'clear'),
  36. array($this->io)
  37. );
  38. $this->Task->Template = new TemplateTask($this->io);
  39. $this->Task->Template->interactive = false;
  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 . 'AppController.php';
  68. $this->Task->expects($this->at(1))->method('createFile')
  69. ->with($file, $this->stringContains('namespace BakeTestPlugin\Controller;'));
  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->main();
  109. $Folder = new Folder($path);
  110. $Folder->delete();
  111. }
  112. /**
  113. * Test Execute
  114. *
  115. * @return void
  116. */
  117. public function testExecuteWithOneArg() {
  118. $this->Task->expects($this->at(0))->method('in')
  119. ->will($this->returnValue('y'));
  120. $path = $this->Task->path . 'BakeTestPlugin';
  121. $file = $path . DS . 'Controller' . DS . 'AppController.php';
  122. $this->Task->expects($this->at(1))->method('createFile')
  123. ->with($file, $this->stringContains('class AppController extends BaseController {'));
  124. $file = $path . DS . 'phpunit.xml';
  125. $this->Task->expects($this->at(2))->method('createFile')
  126. ->with($file, new \PHPUnit_Framework_Constraint_IsAnything());
  127. $file = $path . DS . 'Test' . DS . 'bootstrap.php';
  128. $this->Task->expects($this->at(3))->method('createFile')
  129. ->with($file, new \PHPUnit_Framework_Constraint_IsAnything());
  130. $this->Task->main('BakeTestPlugin');
  131. $Folder = new Folder($this->Task->path . 'BakeTestPlugin');
  132. $Folder->delete();
  133. }
  134. /**
  135. * Test that findPath ignores paths that don't exist.
  136. *
  137. * @return void
  138. */
  139. public function testFindPathNonExistant() {
  140. $paths = App::path('Plugin');
  141. $last = count($paths);
  142. array_unshift($paths, '/fake/path');
  143. $paths[] = '/fake/path2';
  144. $this->Task = $this->getMock('Cake\Console\Command\Task\PluginTask',
  145. array('in', 'out', 'err', 'createFile', '_stop'),
  146. array($this->io)
  147. );
  148. $this->Task->path = TMP . 'tests' . DS;
  149. // Make sure the added path is filtered out.
  150. $this->Task->expects($this->exactly($last))
  151. ->method('out');
  152. $this->Task->expects($this->once())
  153. ->method('in')
  154. ->will($this->returnValue($last));
  155. $this->Task->findPath($paths);
  156. }
  157. }