comparison cake/tests/cases/console/libs/shell.test.php @ 0:261e66bd5a0c

hg init
author Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
date Sun, 24 Jul 2011 21:08:31 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:261e66bd5a0c
1 <?php
2 /**
3 * ShellTest file
4 *
5 * Test Case for Shell
6 *
7 * PHP versions 4 and 5
8 *
9 * CakePHP : Rapid Development Framework (http://cakephp.org)
10 * Copyright 2006-2010, Cake Software Foundation, Inc.
11 *
12 * Licensed under The MIT License
13 * Redistributions of files must retain the above copyright notice.
14 *
15 * @copyright Copyright 2006-2010, Cake Software Foundation, Inc.
16 * @link http://cakephp.org CakePHP Project
17 * @package cake
18 * @subpackage cake.tests.cases.console.libs
19 * @since CakePHP v 1.2.0.7726
20 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
21 */
22 App::import('Core', 'Folder');
23 App::import('Shell', 'Shell', false);
24
25
26 if (!defined('DISABLE_AUTO_DISPATCH')) {
27 define('DISABLE_AUTO_DISPATCH', true);
28 }
29
30 if (!class_exists('ShellDispatcher')) {
31 ob_start();
32 $argv = false;
33 require CAKE . 'console' . DS . 'cake.php';
34 ob_end_clean();
35 }
36
37 Mock::generatePartial('ShellDispatcher', 'TestShellMockShellDispatcher', array(
38 'getInput', 'stdout', 'stderr', '_stop', '_initEnvironment'
39 ));
40
41 /**
42 * TestShell class
43 *
44 * @package cake
45 * @subpackage cake.tests.cases.console.libs
46 */
47 class TestShell extends Shell {
48
49 /**
50 * name property
51 *
52 * @var name
53 * @access public
54 */
55 var $name = 'TestShell';
56 /**
57 * stopped property
58 *
59 * @var integer
60 * @access public
61 */
62 var $stopped;
63
64 /**
65 * stop method
66 *
67 * @param integer $status
68 * @return void
69 * @access protected
70 */
71 function _stop($status = 0) {
72 $this->stopped = $status;
73 }
74 }
75
76 /**
77 * TestAppleTask class
78 *
79 * @package cake
80 * @subpackage cake.tests.cases.console.libs
81 */
82 class TestAppleTask extends Shell {
83 }
84
85 /**
86 * TestBananaTask class
87 *
88 * @package cake
89 * @subpackage cake.tests.cases.console.libs
90 */
91 class TestBananaTask extends Shell {
92 }
93
94 /**
95 * ShellTest class
96 *
97 * @package cake
98 * @subpackage cake.tests.cases.console.libs
99 */
100 class ShellTest extends CakeTestCase {
101
102 /**
103 * Fixtures used in this test case
104 *
105 * @var array
106 * @access public
107 */
108 var $fixtures = array(
109 'core.post', 'core.comment', 'core.article', 'core.user',
110 'core.tag', 'core.articles_tag', 'core.attachment'
111 );
112
113 /**
114 * setUp method
115 *
116 * @return void
117 * @access public
118 */
119 function setUp() {
120 $this->Dispatcher =& new TestShellMockShellDispatcher();
121 $this->Shell =& new TestShell($this->Dispatcher);
122 }
123
124 /**
125 * tearDown method
126 *
127 * @return void
128 * @access public
129 */
130 function tearDown() {
131 ClassRegistry::flush();
132 }
133
134 /**
135 * testConstruct method
136 *
137 * @return void
138 * @access public
139 */
140 function testConstruct() {
141 $this->assertIsA($this->Shell->Dispatch, 'TestShellMockShellDispatcher');
142 $this->assertEqual($this->Shell->name, 'TestShell');
143 $this->assertEqual($this->Shell->alias, 'TestShell');
144 }
145
146 /**
147 * testInitialize method
148 *
149 * @return void
150 * @access public
151 */
152 function testInitialize() {
153 App::build(array(
154 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS),
155 'models' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'models' . DS)
156 ), true);
157
158 $this->Shell->uses = array('TestPlugin.TestPluginPost');
159 $this->Shell->initialize();
160
161 $this->assertTrue(isset($this->Shell->TestPluginPost));
162 $this->assertIsA($this->Shell->TestPluginPost, 'TestPluginPost');
163 $this->assertEqual($this->Shell->modelClass, 'TestPluginPost');
164
165 $this->Shell->uses = array('Comment');
166 $this->Shell->initialize();
167 $this->assertTrue(isset($this->Shell->Comment));
168 $this->assertIsA($this->Shell->Comment, 'Comment');
169 $this->assertEqual($this->Shell->modelClass, 'Comment');
170
171 $this->Shell->uses = true;
172 $this->Shell->initialize();
173 $this->assertTrue(isset($this->Shell->AppModel));
174 $this->assertIsA($this->Shell->AppModel, 'AppModel');
175
176 App::build();
177 }
178
179 /**
180 * testIn method
181 *
182 * @return void
183 * @access public
184 */
185 function testIn() {
186 $this->Shell->Dispatch->setReturnValueAt(0, 'getInput', 'n');
187 $this->Shell->Dispatch->expectAt(0, 'getInput', array('Just a test?', array('y', 'n'), 'n'));
188 $result = $this->Shell->in('Just a test?', array('y', 'n'), 'n');
189 $this->assertEqual($result, 'n');
190
191 $this->Shell->Dispatch->setReturnValueAt(1, 'getInput', 'Y');
192 $this->Shell->Dispatch->expectAt(1, 'getInput', array('Just a test?', array('y', 'n'), 'n'));
193 $result = $this->Shell->in('Just a test?', array('y', 'n'), 'n');
194 $this->assertEqual($result, 'Y');
195
196 $this->Shell->Dispatch->setReturnValueAt(2, 'getInput', 'y');
197 $this->Shell->Dispatch->expectAt(2, 'getInput', array('Just a test?', 'y,n', 'n'));
198 $result = $this->Shell->in('Just a test?', 'y,n', 'n');
199 $this->assertEqual($result, 'y');
200
201 $this->Shell->Dispatch->setReturnValueAt(3, 'getInput', 'y');
202 $this->Shell->Dispatch->expectAt(3, 'getInput', array('Just a test?', 'y/n', 'n'));
203 $result = $this->Shell->in('Just a test?', 'y/n', 'n');
204 $this->assertEqual($result, 'y');
205
206 $this->Shell->Dispatch->setReturnValueAt(4, 'getInput', 'y');
207 $this->Shell->Dispatch->expectAt(4, 'getInput', array('Just a test?', 'y', 'y'));
208 $result = $this->Shell->in('Just a test?', 'y', 'y');
209 $this->assertEqual($result, 'y');
210
211 $this->Shell->interactive = false;
212
213 $result = $this->Shell->in('Just a test?', 'y/n', 'n');
214 $this->assertEqual($result, 'n');
215 }
216
217 /**
218 * testOut method
219 *
220 * @return void
221 * @access public
222 */
223 function testOut() {
224 $this->Shell->Dispatch->expectAt(0, 'stdout', array("Just a test\n", false));
225 $this->Shell->out('Just a test');
226
227 $this->Shell->Dispatch->expectAt(1, 'stdout', array("Just\na\ntest\n", false));
228 $this->Shell->out(array('Just', 'a', 'test'));
229
230 $this->Shell->Dispatch->expectAt(2, 'stdout', array("Just\na\ntest\n\n", false));
231 $this->Shell->out(array('Just', 'a', 'test'), 2);
232
233 $this->Shell->Dispatch->expectAt(3, 'stdout', array("\n", false));
234 $this->Shell->out();
235 }
236
237 /**
238 * testErr method
239 *
240 * @return void
241 * @access public
242 */
243 function testErr() {
244 $this->Shell->Dispatch->expectAt(0, 'stderr', array("Just a test\n"));
245 $this->Shell->err('Just a test');
246
247 $this->Shell->Dispatch->expectAt(1, 'stderr', array("Just\na\ntest\n"));
248 $this->Shell->err(array('Just', 'a', 'test'));
249
250 $this->Shell->Dispatch->expectAt(2, 'stderr', array("Just\na\ntest\n\n"));
251 $this->Shell->err(array('Just', 'a', 'test'), 2);
252
253 $this->Shell->Dispatch->expectAt(3, 'stderr', array("\n"));
254 $this->Shell->err();
255 }
256
257 /**
258 * testNl
259 *
260 * @return void
261 * @access public
262 */
263 function testNl() {
264 $this->assertEqual($this->Shell->nl(), "\n");
265 $this->assertEqual($this->Shell->nl(true), "\n");
266 $this->assertEqual($this->Shell->nl(false), "");
267 $this->assertEqual($this->Shell->nl(2), "\n\n");
268 $this->assertEqual($this->Shell->nl(1), "\n");
269 }
270
271 /**
272 * testHr
273 *
274 * @return void
275 * @access public
276 */
277 function testHr() {
278 $bar = '---------------------------------------------------------------';
279
280 $this->Shell->Dispatch->expectAt(0, 'stdout', array('', false));
281 $this->Shell->Dispatch->expectAt(1, 'stdout', array($bar . "\n", false));
282 $this->Shell->Dispatch->expectAt(2, 'stdout', array('', false));
283 $this->Shell->hr();
284
285 $this->Shell->Dispatch->expectAt(3, 'stdout', array("\n", false));
286 $this->Shell->Dispatch->expectAt(4, 'stdout', array($bar . "\n", false));
287 $this->Shell->Dispatch->expectAt(5, 'stdout', array("\n", false));
288 $this->Shell->hr(true);
289
290 $this->Shell->Dispatch->expectAt(3, 'stdout', array("\n\n", false));
291 $this->Shell->Dispatch->expectAt(4, 'stdout', array($bar . "\n", false));
292 $this->Shell->Dispatch->expectAt(5, 'stdout', array("\n\n", false));
293 $this->Shell->hr(2);
294 }
295
296 /**
297 * testError
298 *
299 * @return void
300 * @access public
301 */
302 function testError() {
303 $this->Shell->Dispatch->expectAt(0, 'stderr', array("Error: Foo Not Found\n"));
304 $this->Shell->error('Foo Not Found');
305 $this->assertIdentical($this->Shell->stopped, 1);
306
307 $this->Shell->stopped = null;
308
309 $this->Shell->Dispatch->expectAt(1, 'stderr', array("Error: Foo Not Found\n"));
310 $this->Shell->Dispatch->expectAt(2, 'stderr', array("Searched all...\n"));
311 $this->Shell->error('Foo Not Found', 'Searched all...');
312 $this->assertIdentical($this->Shell->stopped, 1);
313 }
314
315 /**
316 * testLoadTasks method
317 *
318 * @return void
319 * @access public
320 */
321 function testLoadTasks() {
322 $this->assertTrue($this->Shell->loadTasks());
323
324 $this->Shell->tasks = null;
325 $this->assertTrue($this->Shell->loadTasks());
326
327 $this->Shell->tasks = false;
328 $this->assertTrue($this->Shell->loadTasks());
329
330 $this->Shell->tasks = true;
331 $this->assertTrue($this->Shell->loadTasks());
332
333 $this->Shell->tasks = array();
334 $this->assertTrue($this->Shell->loadTasks());
335
336 // Fatal Error
337 // $this->Shell->tasks = 'TestIDontExist';
338 // $this->assertFalse($this->Shell->loadTasks());
339 // $this->assertFalse(isset($this->Shell->TestIDontExist));
340
341 $this->Shell->tasks = 'TestApple';
342 $this->assertTrue($this->Shell->loadTasks());
343 $this->assertIsA($this->Shell->TestApple, 'TestAppleTask');
344
345 $this->Shell->tasks = 'TestBanana';
346 $this->assertTrue($this->Shell->loadTasks());
347 $this->assertIsA($this->Shell->TestApple, 'TestAppleTask');
348 $this->assertIsA($this->Shell->TestBanana, 'TestBananaTask');
349
350 unset($this->Shell->ShellTestApple, $this->Shell->TestBanana);
351
352 $this->Shell->tasks = array('TestApple', 'TestBanana');
353 $this->assertTrue($this->Shell->loadTasks());
354 $this->assertIsA($this->Shell->TestApple, 'TestAppleTask');
355 $this->assertIsA($this->Shell->TestBanana, 'TestBananaTask');
356 }
357
358 /**
359 * testShortPath method
360 *
361 * @return void
362 * @access public
363 */
364 function testShortPath() {
365 $path = $expected = DS . 'tmp' . DS . 'ab' . DS . 'cd';
366 $this->assertEqual($this->Shell->shortPath($path), $expected);
367
368 $path = $expected = DS . 'tmp' . DS . 'ab' . DS . 'cd' . DS ;
369 $this->assertEqual($this->Shell->shortPath($path), $expected);
370
371 $path = $expected = DS . 'tmp' . DS . 'ab' . DS . 'index.php';
372 $this->assertEqual($this->Shell->shortPath($path), $expected);
373
374 // Shell::shortPath needs Folder::realpath
375 // $path = DS . 'tmp' . DS . 'ab' . DS . '..' . DS . 'cd';
376 // $expected = DS . 'tmp' . DS . 'cd';
377 // $this->assertEqual($this->Shell->shortPath($path), $expected);
378
379 $path = DS . 'tmp' . DS . 'ab' . DS . DS . 'cd';
380 $expected = DS . 'tmp' . DS . 'ab' . DS . 'cd';
381 $this->assertEqual($this->Shell->shortPath($path), $expected);
382
383 $path = 'tmp' . DS . 'ab';
384 $expected = 'tmp' . DS . 'ab';
385 $this->assertEqual($this->Shell->shortPath($path), $expected);
386
387 $path = 'tmp' . DS . 'ab';
388 $expected = 'tmp' . DS . 'ab';
389 $this->assertEqual($this->Shell->shortPath($path), $expected);
390
391 $path = APP;
392 $expected = DS . basename(APP) . DS;
393 $this->assertEqual($this->Shell->shortPath($path), $expected);
394
395 $path = APP . 'index.php';
396 $expected = DS . basename(APP) . DS . 'index.php';
397 $this->assertEqual($this->Shell->shortPath($path), $expected);
398 }
399
400 /**
401 * testCreateFile method
402 *
403 * @return void
404 * @access public
405 */
406 function testCreateFile() {
407 $this->skipIf(DIRECTORY_SEPARATOR === '\\', '%s Not supported on Windows');
408
409 $path = TMP . 'shell_test';
410 $file = $path . DS . 'file1.php';
411
412 new Folder($path, true);
413
414 $this->Shell->interactive = false;
415
416 $contents = "<?php\necho 'test';\n\$te = 'st';\n?>";
417 $result = $this->Shell->createFile($file, $contents);
418 $this->assertTrue($result);
419 $this->assertTrue(file_exists($file));
420 $this->assertEqual(file_get_contents($file), $contents);
421
422 $contents = "<?php\necho 'another test';\n\$te = 'st';\n?>";
423 $result = $this->Shell->createFile($file, $contents);
424 $this->assertTrue($result);
425 $this->assertTrue(file_exists($file));
426 $this->assertEqual(file_get_contents($file), $contents);
427
428 $this->Shell->interactive = true;
429
430 $this->Shell->Dispatch->setReturnValueAt(0, 'getInput', 'n');
431 $this->Shell->Dispatch->expectAt(1, 'stdout', array('File exists, overwrite?', '*'));
432
433 $contents = "<?php\necho 'yet another test';\n\$te = 'st';\n?>";
434 $result = $this->Shell->createFile($file, $contents);
435 $this->assertFalse($result);
436 $this->assertTrue(file_exists($file));
437 $this->assertNotEqual(file_get_contents($file), $contents);
438
439 $this->Shell->Dispatch->setReturnValueAt(1, 'getInput', 'y');
440 $this->Shell->Dispatch->expectAt(3, 'stdout', array('File exists, overwrite?', '*'));
441
442 $result = $this->Shell->createFile($file, $contents);
443 $this->assertTrue($result);
444 $this->assertTrue(file_exists($file));
445 $this->assertEqual(file_get_contents($file), $contents);
446
447 $Folder = new Folder($path);
448 $Folder->delete();
449 }
450
451 /**
452 * testCreateFileWindows method
453 *
454 * @return void
455 * @access public
456 */
457 function testCreateFileWindows() {
458 $this->skipUnless(DIRECTORY_SEPARATOR === '\\', 'testCreateFileWindows supported on Windows only');
459
460 $path = TMP . 'shell_test';
461 $file = $path . DS . 'file1.php';
462
463 new Folder($path, true);
464
465 $this->Shell->interactive = false;
466
467 $contents = "<?php\necho 'test';\r\n\$te = 'st';\r\n?>";
468 $result = $this->Shell->createFile($file, $contents);
469 $this->assertTrue($result);
470 $this->assertTrue(file_exists($file));
471 $this->assertEqual(file_get_contents($file), $contents);
472
473 $contents = "<?php\necho 'another test';\r\n\$te = 'st';\r\n?>";
474 $result = $this->Shell->createFile($file, $contents);
475 $this->assertTrue($result);
476 $this->assertTrue(file_exists($file));
477 $this->assertEqual(file_get_contents($file), $contents);
478
479 $this->Shell->interactive = true;
480
481 $this->Shell->Dispatch->setReturnValueAt(0, 'getInput', 'n');
482 $this->Shell->Dispatch->expectAt(1, 'stdout', array('File exists, overwrite?'));
483
484 $contents = "<?php\necho 'yet another test';\r\n\$te = 'st';\r\n?>";
485 $result = $this->Shell->createFile($file, $contents);
486 $this->assertFalse($result);
487 $this->assertTrue(file_exists($file));
488 $this->assertNotEqual(file_get_contents($file), $contents);
489
490 $this->Shell->Dispatch->setReturnValueAt(1, 'getInput', 'y');
491 $this->Shell->Dispatch->expectAt(3, 'stdout', array('File exists, overwrite?'));
492
493 $result = $this->Shell->createFile($file, $contents);
494 $this->assertTrue($result);
495 $this->assertTrue(file_exists($file));
496 $this->assertEqual(file_get_contents($file), $contents);
497
498 $Folder = new Folder($path);
499 $Folder->delete();
500 }
501 }