OSDN Git Service

restart
[umumu/umumu.git] / lib / Cake / Test / Case / Console / Command / Task / PluginTaskTest.php
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  * @package       Cake.Test.Case.Console.Command.Task
17  * @since         CakePHP v 1.3.0
18  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
19  */
20
21 App::uses('ShellDispatcher', 'Console');
22 App::uses('ConsoleOutput', 'Console');
23 App::uses('ConsoleInput', 'Console');
24 App::uses('Shell', 'Console');
25 App::uses('PluginTask', 'Console/Command/Task');
26 App::uses('ModelTask', 'Console/Command/Task');
27 App::uses('Folder', 'Utility');
28 App::uses('File', 'Utility');
29
30 /**
31  * PluginTaskPlugin class
32  *
33  * @package       Cake.Test.Case.Console.Command.Task
34  */
35 class PluginTaskTest extends CakeTestCase {
36
37 /**
38  * setUp method
39  *
40  * @return void
41  */
42         public function setUp() {
43                 parent::setUp();
44                 $this->out = $this->getMock('ConsoleOutput', array(), array(), '', false);
45                 $this->in = $this->getMock('ConsoleInput', array(), array(), '', false);
46
47                 $this->Task = $this->getMock('PluginTask',
48                         array('in', 'err', 'createFile', '_stop', 'clear'),
49                         array($this->out, $this->out, $this->in)
50                 );
51                 $this->Task->path = TMP . 'tests' . DS;
52                 $this->Task->bootstrap = TMP . 'tests' . DS . 'bootstrap.php';
53                 touch($this->Task->bootstrap);
54
55                 $this->_paths = $paths = App::path('plugins');
56                 foreach ($paths as $i => $p) {
57                         if (!is_dir($p)) {
58                                 array_splice($paths, $i, 1);
59                         }
60                 }
61                 $this->_testPath = array_push($paths, TMP . 'tests' . DS) - 1;
62                 App::build(array('plugins' => $paths));
63         }
64
65 /**
66  * tearDown()
67  *
68  * @return void
69  */
70         public function tearDown() {
71                 if (file_exists($this->Task->bootstrap)) {
72                         unlink($this->Task->bootstrap);
73                 }
74                 parent::tearDown();
75         }
76
77 /**
78  * test bake()
79  *
80  * @return void
81  */
82         public function testBakeFoldersAndFiles() {
83                 $this->Task->expects($this->at(0))
84                         ->method('in')
85                         ->will($this->returnValue($this->_testPath));
86                 $this->Task->expects($this->at(1))
87                         ->method('in')
88                         ->will($this->returnValue('y'));
89
90                 $path = $this->Task->path . 'BakeTestPlugin';
91
92                 $file = $path . DS . 'Controller' . DS . 'BakeTestPluginAppController.php';
93                 $this->Task->expects($this->at(2))
94                         ->method('createFile')
95                         ->with($file, new PHPUnit_Framework_Constraint_IsAnything());
96
97                 $file = $path . DS . 'Model' . DS . 'BakeTestPluginAppModel.php';
98                 $this->Task->expects($this->at(3))
99                         ->method('createFile')
100                         ->with($file, new PHPUnit_Framework_Constraint_IsAnything());
101
102                 $this->Task->bake('BakeTestPlugin');
103
104                 $path = $this->Task->path . 'BakeTestPlugin';
105                 $this->assertTrue(is_dir($path), 'No plugin dir %s');
106
107                 $directories = array(
108                         'Config' . DS . 'Schema',
109                         'Console' . DS . 'Command' . DS . 'Task',
110                         'Console' . DS . 'Templates',
111                         'Controller' . DS . 'Component',
112                         'Lib',
113                         'Locale' . DS . 'eng' . DS . 'LC_MESSAGES',
114                         'Model' . DS . 'Behavior',
115                         'Model' . DS . 'Datasource',
116                         'Test' . DS . 'Case' . DS . 'Controller' . DS . 'Component',
117                         'Test' . DS . 'Case' . DS . 'Lib',
118                         'Test' . DS . 'Case' . DS . 'Model' . DS . 'Behavior',
119                         'Test' . DS . 'Case' . DS . 'Model' . DS . 'Datasource',
120                         'Test' . DS . 'Case' . DS . 'View' . DS . 'Helper',
121                         'Test' . DS . 'Fixture',
122                         'View' . DS . 'Elements',
123                         'View' . DS . 'Helper',
124                         'View' . DS . 'Layouts',
125                         'webroot' . DS . 'css',
126                         'webroot' . DS . 'js',
127                         'webroot' . DS . 'img',
128                 );
129                 foreach ($directories as $dir) {
130                         $this->assertTrue(is_dir($path . DS . $dir), 'Missing directory for ' . $dir);
131                 }
132
133                 $Folder = new Folder($this->Task->path . 'BakeTestPlugin');
134                 $Folder->delete();
135         }
136
137 /**
138  * test execute with no args, flowing into interactive,
139  *
140  * @return void
141  */
142         public function testExecuteWithNoArgs() {
143                 $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('TestPlugin'));
144                 $this->Task->expects($this->at(1))->method('in')->will($this->returnValue($this->_testPath));
145                 $this->Task->expects($this->at(2))->method('in')->will($this->returnValue('y'));
146
147                 $path = $this->Task->path . 'TestPlugin';
148                 $file = $path . DS . 'Controller' . DS . 'TestPluginAppController.php';
149
150                 $this->Task->expects($this->at(3))->method('createFile')
151                         ->with($file, new PHPUnit_Framework_Constraint_IsAnything());
152
153                 $file = $path . DS . 'Model' . DS . 'TestPluginAppModel.php';
154                 $this->Task->expects($this->at(4))->method('createFile')
155                         ->with($file, new PHPUnit_Framework_Constraint_IsAnything());
156
157                 $this->Task->args = array();
158                 $this->Task->execute();
159
160                 $Folder = new Folder($path);
161                 $Folder->delete();
162         }
163
164 /**
165  * Test Execute
166  *
167  * @return void
168  */
169         public function testExecuteWithOneArg() {
170                 $this->Task->expects($this->at(0))->method('in')
171                         ->will($this->returnValue($this->_testPath));
172                 $this->Task->expects($this->at(1))->method('in')
173                         ->will($this->returnValue('y'));
174
175                 $path = $this->Task->path . 'BakeTestPlugin';
176                 $file = $path . DS . 'Controller' . DS . 'BakeTestPluginAppController.php';
177                 $this->Task->expects($this->at(2))->method('createFile')
178                         ->with($file, new PHPUnit_Framework_Constraint_IsAnything());
179
180                 $path = $this->Task->path . 'BakeTestPlugin';
181                 $file = $path . DS . 'Model' . DS . 'BakeTestPluginAppModel.php';
182                 $this->Task->expects($this->at(3))->method('createFile')
183                         ->with($file, new PHPUnit_Framework_Constraint_IsAnything());
184
185                 $this->Task->args = array('BakeTestPlugin');
186
187                 $this->Task->execute();
188
189                 $Folder = new Folder($this->Task->path . 'BakeTestPlugin');
190                 $Folder->delete();
191         }
192
193 /**
194  * Test that findPath ignores paths that don't exist.
195  *
196  * @return void
197  */
198         public function testFindPathNonExistant() {
199                 $paths = App::path('plugins');
200                 $last = count($paths);
201
202                 array_unshift($paths, '/fake/path');
203                 $paths[] = '/fake/path2';
204
205                 $this->Task = $this->getMock('PluginTask',
206                         array('in', 'out', 'err', 'createFile', '_stop'),
207                         array($this->out, $this->out, $this->in)
208                 );
209                 $this->Task->path = TMP . 'tests' . DS;
210
211                 // Make sure the added path is filtered out.
212                 $this->Task->expects($this->exactly($last))
213                         ->method('out');
214
215                 $this->Task->expects($this->once())
216                         ->method('in')
217                         ->will($this->returnValue($last));
218
219                 $this->Task->findPath($paths);
220         }
221 }