comparison cake/tests/cases/console/cake.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 * ShellDispatcherTest file
4 *
5 * PHP versions 4 and 5
6 *
7 * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
8 * Copyright 2005-2010, Cake Software Foundation, Inc.
9 *
10 * Licensed under The Open Group Test Suite License
11 * Redistributions of files must retain the above copyright notice.
12 *
13 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc.
14 * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
15 * @package cake
16 * @subpackage cake.tests.cases.console
17 * @since CakePHP(tm) v 1.2.0.5432
18 * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
19 */
20 if (!defined('DISABLE_AUTO_DISPATCH')) {
21 define('DISABLE_AUTO_DISPATCH', true);
22 }
23
24 if (!class_exists('ShellDispatcher')) {
25 ob_start();
26 $argv = false;
27 require CAKE . 'console' . DS . 'cake.php';
28 ob_end_clean();
29 }
30
31 require_once CONSOLE_LIBS . 'shell.php';
32
33 /**
34 * TestShellDispatcher class
35 *
36 * @package cake
37 * @subpackage cake.tests.cases.console
38 */
39 class TestShellDispatcher extends ShellDispatcher {
40
41 /**
42 * params property
43 *
44 * @var array
45 * @access public
46 */
47 var $params = array();
48
49 /**
50 * stdout property
51 *
52 * @var string
53 * @access public
54 */
55 var $stdout = '';
56
57 /**
58 * stderr property
59 *
60 * @var string
61 * @access public
62 */
63 var $stderr = '';
64
65 /**
66 * stopped property
67 *
68 * @var string
69 * @access public
70 */
71 var $stopped = null;
72
73 /**
74 * TestShell
75 *
76 * @var mixed
77 * @access public
78 */
79 var $TestShell;
80
81 /**
82 * _initEnvironment method
83 *
84 * @return void
85 * @access protected
86 */
87 function _initEnvironment() {
88 }
89
90 /**
91 * stderr method
92 *
93 * @return void
94 * @access public
95 */
96 function stderr($string) {
97 $this->stderr .= rtrim($string, ' ');
98 }
99
100 /**
101 * stdout method
102 *
103 * @return void
104 * @access public
105 */
106 function stdout($string, $newline = true) {
107 if ($newline) {
108 $this->stdout .= rtrim($string, ' ') . "\n";
109 } else {
110 $this->stdout .= rtrim($string, ' ');
111 }
112 }
113
114 /**
115 * clear method
116 *
117 * @return void
118 * @access public
119 */
120 function clear() {
121
122 }
123
124 /**
125 * _stop method
126 *
127 * @return void
128 * @access protected
129 */
130 function _stop($status = 0) {
131 $this->stopped = 'Stopped with status: ' . $status;
132 return $status;
133 }
134
135 /**
136 * getShell
137 *
138 * @param mixed $plugin
139 * @return mixed
140 * @access public
141 */
142 function getShell($plugin = null) {
143 return $this->_getShell($plugin);
144 }
145
146 /**
147 * _getShell
148 *
149 * @param mixed $plugin
150 * @return mixed
151 * @access protected
152 */
153 function _getShell($plugin = null) {
154 if (isset($this->TestShell)) {
155 return $this->TestShell;
156 }
157 return parent::_getShell($plugin);
158 }
159 }
160
161 /**
162 * ShellDispatcherTest
163 *
164 * @package cake
165 * @subpackage cake.tests.cases.libs
166 */
167 class ShellDispatcherTest extends CakeTestCase {
168
169 /**
170 * setUp method
171 *
172 * @return void
173 * @access public
174 */
175 function setUp() {
176 App::build(array(
177 'plugins' => array(
178 TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS
179 ),
180 'shells' => array(
181 CORE_PATH ? CONSOLE_LIBS : ROOT . DS . CONSOLE_LIBS,
182 TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'vendors' . DS . 'shells' . DS
183 )
184 ), true);
185 }
186
187 /**
188 * tearDown method
189 *
190 * @return void
191 * @access public
192 */
193 function tearDown() {
194 App::build();
195 }
196
197 /**
198 * testParseParams method
199 *
200 * @return void
201 * @access public
202 */
203 function testParseParams() {
204 $Dispatcher =& new TestShellDispatcher();
205
206 $params = array(
207 '/cake/1.2.x.x/cake/console/cake.php',
208 'bake',
209 '-app',
210 'new',
211 '-working',
212 '/var/www/htdocs'
213 );
214 $expected = array(
215 'app' => 'new',
216 'webroot' => 'webroot',
217 'working' => '/var/www/htdocs/new',
218 'root' => '/var/www/htdocs'
219 );
220 $Dispatcher->parseParams($params);
221 $this->assertEqual($expected, $Dispatcher->params);
222
223 $params = array('cake.php');
224 $expected = array(
225 'app' => 'app',
226 'webroot' => 'webroot',
227 'working' => str_replace('\\', '/', CAKE_CORE_INCLUDE_PATH . DS . 'app'),
228 'root' => str_replace('\\', '/', CAKE_CORE_INCLUDE_PATH),
229 );
230 $Dispatcher->params = $Dispatcher->args = array();
231 $Dispatcher->parseParams($params);
232 $this->assertEqual($expected, $Dispatcher->params);
233
234 $params = array(
235 'cake.php',
236 '-app',
237 'new',
238 );
239 $expected = array(
240 'app' => 'new',
241 'webroot' => 'webroot',
242 'working' => str_replace('\\', '/', CAKE_CORE_INCLUDE_PATH . DS . 'new'),
243 'root' => str_replace('\\', '/', CAKE_CORE_INCLUDE_PATH)
244 );
245 $Dispatcher->params = $Dispatcher->args = array();
246 $Dispatcher->parseParams($params);
247 $this->assertEqual($expected, $Dispatcher->params);
248
249 $params = array(
250 './cake.php',
251 'bake',
252 '-app',
253 'new',
254 '-working',
255 '/cake/1.2.x.x/cake/console'
256 );
257
258 $expected = array(
259 'app' => 'new',
260 'webroot' => 'webroot',
261 'working' => str_replace('\\', '/', CAKE_CORE_INCLUDE_PATH . DS . 'new'),
262 'root' => str_replace('\\', '/', CAKE_CORE_INCLUDE_PATH)
263 );
264
265 $Dispatcher->params = $Dispatcher->args = array();
266 $Dispatcher->parseParams($params);
267 $this->assertEqual($expected, $Dispatcher->params);
268
269 $params = array(
270 './console/cake.php',
271 'bake',
272 '-app',
273 'new',
274 '-working',
275 '/cake/1.2.x.x/cake'
276 );
277 $expected = array(
278 'app' => 'new',
279 'webroot' => 'webroot',
280 'working' => str_replace('\\', '/', CAKE_CORE_INCLUDE_PATH . DS . 'new'),
281 'root' => str_replace('\\', '/', CAKE_CORE_INCLUDE_PATH)
282 );
283 $Dispatcher->params = $Dispatcher->args = array();
284 $Dispatcher->parseParams($params);
285 $this->assertEqual($expected, $Dispatcher->params);
286
287 $params = array(
288 './console/cake.php',
289 'bake',
290 '-app',
291 'new',
292 '-dry',
293 '-working',
294 '/cake/1.2.x.x/cake'
295 );
296 $expected = array(
297 'app' => 'new',
298 'webroot' => 'webroot',
299 'working' => str_replace('\\', '/', CAKE_CORE_INCLUDE_PATH . DS . 'new'),
300 'root' => str_replace('\\', '/', CAKE_CORE_INCLUDE_PATH),
301 'dry' => 1
302 );
303 $Dispatcher->params = $Dispatcher->args = array();
304 $Dispatcher->parseParams($params);
305 $this->assertEqual($expected, $Dispatcher->params);
306
307 $params = array(
308 './console/cake.php',
309 '-working',
310 '/cake/1.2.x.x/cake',
311 'schema',
312 'run',
313 'create',
314 '-dry',
315 '-f',
316 '-name',
317 'DbAcl'
318 );
319 $expected = array(
320 'app' => 'app',
321 'webroot' => 'webroot',
322 'working' => str_replace('\\', '/', CAKE_CORE_INCLUDE_PATH . DS . 'app'),
323 'root' => str_replace('\\', '/', CAKE_CORE_INCLUDE_PATH),
324 'dry' => 1,
325 'f' => 1,
326 'name' => 'DbAcl'
327 );
328 $Dispatcher->params = $Dispatcher->args = array();
329 $Dispatcher->parseParams($params);
330 $this->assertEqual($expected, $Dispatcher->params);
331
332 $expected = array('./console/cake.php', 'schema', 'run', 'create');
333 $this->assertEqual($expected, $Dispatcher->args);
334
335 $params = array(
336 '/cake/1.2.x.x/cake/console/cake.php',
337 '-working',
338 '/cake/1.2.x.x/app',
339 'schema',
340 'run',
341 'create',
342 '-dry',
343 '-name',
344 'DbAcl'
345 );
346 $expected = array(
347 'app' => 'app',
348 'webroot' => 'webroot',
349 'working' => '/cake/1.2.x.x/app',
350 'root' => '/cake/1.2.x.x',
351 'dry' => 1,
352 'name' => 'DbAcl'
353 );
354 $Dispatcher->params = $Dispatcher->args = array();
355 $Dispatcher->parseParams($params);
356 $this->assertEqual($expected, $Dispatcher->params);
357
358 $expected = array('/cake/1.2.x.x/cake/console/cake.php', 'schema', 'run', 'create');
359 $this->assertEqual($expected, $Dispatcher->args);
360 $params = array(
361 'cake.php',
362 '-working',
363 'C:/wamp/www/cake/app',
364 'bake',
365 '-app',
366 'C:/wamp/www/apps/cake/app',
367 );
368 $expected = array(
369 'app' => 'app',
370 'webroot' => 'webroot',
371 'working' => 'C:\wamp\www\apps\cake\app',
372 'root' => 'C:\wamp\www\apps\cake'
373 );
374
375 $Dispatcher->params = $Dispatcher->args = array();
376 $Dispatcher->parseParams($params);
377 $this->assertEqual($expected, $Dispatcher->params);
378
379 $params = array(
380 'cake.php',
381 '-working',
382 'C:\wamp\www\cake\app',
383 'bake',
384 '-app',
385 'C:\wamp\www\apps\cake\app',
386 );
387 $expected = array(
388 'app' => 'app',
389 'webroot' => 'webroot',
390 'working' => 'C:\wamp\www\apps\cake\app',
391 'root' => 'C:\wamp\www\apps\cake'
392 );
393 $Dispatcher->params = $Dispatcher->args = array();
394 $Dispatcher->parseParams($params);
395 $this->assertEqual($expected, $Dispatcher->params);
396
397 $params = array(
398 'cake.php',
399 '-working',
400 'C:\wamp\www\apps',
401 'bake',
402 '-app',
403 'cake\app',
404 '-url',
405 'http://example.com/some/url/with/a/path'
406 );
407 $expected = array(
408 'app' => 'app',
409 'webroot' => 'webroot',
410 'working' => 'C:\wamp\www\apps\cake\app',
411 'root' => 'C:\wamp\www\apps\cake',
412 'url' => 'http://example.com/some/url/with/a/path'
413 );
414 $Dispatcher->params = $Dispatcher->args = array();
415 $Dispatcher->parseParams($params);
416 $this->assertEqual($expected, $Dispatcher->params);
417
418 $params = array(
419 '/home/amelo/dev/cake-common/cake/console/cake.php',
420 '-root',
421 '/home/amelo/dev/lsbu-vacancy',
422 '-working',
423 '/home/amelo/dev/lsbu-vacancy',
424 '-app',
425 'app',
426 );
427 $expected = array(
428 'app' => 'app',
429 'webroot' => 'webroot',
430 'working' => '/home/amelo/dev/lsbu-vacancy/app',
431 'root' => '/home/amelo/dev/lsbu-vacancy',
432 );
433 $Dispatcher->params = $Dispatcher->args = array();
434 $Dispatcher->parseParams($params);
435 $this->assertEqual($expected, $Dispatcher->params);
436
437 $params = array(
438 'cake.php',
439 '-working',
440 'D:\www',
441 'bake',
442 'my_app',
443 );
444 $expected = array(
445 'working' => 'D:\www',
446 'app' => 'www',
447 'root' => 'D:',
448 'webroot' => 'webroot'
449 );
450
451 $Dispatcher->params = $Dispatcher->args = array();
452 $Dispatcher->parseParams($params);
453 $this->assertEqual($expected, $Dispatcher->params);
454
455 $params = array(
456 'cake.php',
457 '-working',
458 'D:\ ',
459 'bake',
460 'my_app',
461 );
462 $expected = array(
463 'working' => '.',
464 'app' => 'D:',
465 'root' => '.',
466 'webroot' => 'webroot'
467 );
468 $Dispatcher->params = $Dispatcher->args = array();
469 $Dispatcher->parseParams($params);
470 $this->assertEqual($expected, $Dispatcher->params);
471 }
472
473 /**
474 * testBuildPaths method
475 *
476 * @return void
477 * @access public
478 */
479 function testBuildPaths() {
480 $Dispatcher =& new TestShellDispatcher();
481
482 $result = $Dispatcher->shellPaths;
483
484 $expected = array(
485 TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS . 'test_plugin' . DS . 'vendors' . DS . 'shells' . DS,
486 TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS . 'test_plugin_two' . DS . 'vendors' . DS . 'shells' . DS,
487 APP . 'vendors' . DS . 'shells' . DS,
488 VENDORS . 'shells' . DS,
489 CORE_PATH ? CONSOLE_LIBS : ROOT . DS . CONSOLE_LIBS,
490 TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'vendors' . DS . 'shells' . DS,
491 );
492 $this->assertIdentical(array_diff($result, $expected), array());
493 $this->assertIdentical(array_diff($expected, $result), array());
494 }
495
496 /**
497 * Verify loading of (plugin-) shells
498 *
499 * @return void
500 * @access public
501 */
502 function testGetShell() {
503 $this->skipIf(class_exists('SampleShell'), '%s SampleShell Class already loaded');
504 $this->skipIf(class_exists('ExampleShell'), '%s ExampleShell Class already loaded');
505
506 $Dispatcher =& new TestShellDispatcher();
507
508 $Dispatcher->shell = 'sample';
509 $Dispatcher->shellName = 'Sample';
510 $Dispatcher->shellClass = 'SampleShell';
511
512 $result = $Dispatcher->getShell();
513 $this->assertIsA($result, 'SampleShell');
514
515 $Dispatcher =& new TestShellDispatcher();
516
517 $Dispatcher->shell = 'example';
518 $Dispatcher->shellName = 'Example';
519 $Dispatcher->shellClass = 'ExampleShell';
520
521 $result = $Dispatcher->getShell('test_plugin');
522 $this->assertIsA($result, 'ExampleShell');
523 }
524
525 /**
526 * Verify correct dispatch of Shell subclasses with a main method
527 *
528 * @return void
529 * @access public
530 */
531 function testDispatchShellWithMain() {
532 Mock::generate('Shell', 'MockWithMainShell', array('main', '_secret'));
533
534 $Dispatcher =& new TestShellDispatcher();
535
536 $Shell = new MockWithMainShell();
537 $Shell->setReturnValue('main', true);
538 $Shell->expectOnce('initialize');
539 $Shell->expectOnce('loadTasks');
540 $Shell->expectOnce('startup');
541 $Shell->expectOnce('main');
542 $Dispatcher->TestShell =& $Shell;
543
544 $Dispatcher->args = array('mock_with_main');
545 $result = $Dispatcher->dispatch();
546 $this->assertTrue($result);
547 $this->assertEqual($Dispatcher->args, array());
548
549 $Shell = new MockWithMainShell();
550 $Shell->setReturnValue('main', true);
551 $Shell->expectOnce('startup');
552 $Shell->expectOnce('main');
553 $Dispatcher->TestShell =& $Shell;
554
555 $Dispatcher->args = array('mock_with_main', 'initdb');
556 $result = $Dispatcher->dispatch();
557 $this->assertTrue($result);
558 $this->assertEqual($Dispatcher->args, array('initdb'));
559
560 $Shell = new MockWithMainShell();
561 $Shell->setReturnValue('main', true);
562 $Shell->expectOnce('startup');
563 $Shell->expectOnce('help');
564 $Dispatcher->TestShell =& $Shell;
565
566 $Dispatcher->args = array('mock_with_main', 'help');
567 $result = $Dispatcher->dispatch();
568 $this->assertNull($result);
569 $this->assertEqual($Dispatcher->args, array());
570
571 $Shell = new MockWithMainShell();
572 $Shell->setReturnValue('main', true);
573 $Shell->expectNever('hr');
574 $Shell->expectOnce('startup');
575 $Shell->expectOnce('main');
576 $Dispatcher->TestShell =& $Shell;
577
578 $Dispatcher->args = array('mock_with_main', 'hr');
579 $result = $Dispatcher->dispatch();
580 $this->assertTrue($result);
581 $this->assertEqual($Dispatcher->args, array('hr'));
582
583 $Shell = new MockWithMainShell();
584 $Shell->setReturnValue('main', true);
585 $Shell->expectOnce('startup');
586 $Shell->expectOnce('main');
587 $Dispatcher->TestShell =& $Shell;
588
589 $Dispatcher->args = array('mock_with_main', 'dispatch');
590 $result = $Dispatcher->dispatch();
591 $this->assertTrue($result);
592 $this->assertEqual($Dispatcher->args, array('dispatch'));
593
594 $Shell = new MockWithMainShell();
595 $Shell->setReturnValue('main', true);
596 $Shell->expectOnce('startup');
597 $Shell->expectOnce('main');
598 $Dispatcher->TestShell =& $Shell;
599
600 $Dispatcher->args = array('mock_with_main', 'idontexist');
601 $result = $Dispatcher->dispatch();
602 $this->assertTrue($result);
603 $this->assertEqual($Dispatcher->args, array('idontexist'));
604
605 $Shell = new MockWithMainShell();
606 $Shell->expectNever('startup');
607 $Shell->expectNever('main');
608 $Shell->expectNever('_secret');
609 $Dispatcher->TestShell =& $Shell;
610
611 $Dispatcher->args = array('mock_with_main', '_secret');
612 $result = $Dispatcher->dispatch();
613 $this->assertFalse($result);
614 }
615
616 /**
617 * Verify correct dispatch of Shell subclasses without a main method
618 *
619 * @return void
620 * @access public
621 */
622 function testDispatchShellWithoutMain() {
623 Mock::generate('Shell', 'MockWithoutMainShell', array('initDb', '_secret'));
624
625 $Dispatcher =& new TestShellDispatcher();
626
627 $Shell = new MockWithoutMainShell();
628 $Shell->setReturnValue('initDb', true);
629 $Shell->expectOnce('initialize');
630 $Shell->expectOnce('loadTasks');
631 $Shell->expectNever('startup');
632 $Dispatcher->TestShell =& $Shell;
633
634 $Dispatcher->args = array('mock_without_main');
635 $result = $Dispatcher->dispatch();
636 $this->assertFalse($result);
637 $this->assertEqual($Dispatcher->args, array());
638
639 $Shell = new MockWithoutMainShell();
640 $Shell->setReturnValue('initDb', true);
641 $Shell->expectOnce('startup');
642 $Shell->expectOnce('initDb');
643 $Dispatcher->TestShell =& $Shell;
644
645 $Dispatcher->args = array('mock_without_main', 'initdb');
646 $result = $Dispatcher->dispatch();
647 $this->assertTrue($result);
648 $this->assertEqual($Dispatcher->args, array());
649
650 $Shell = new MockWithoutMainShell();
651 $Shell->setReturnValue('initDb', true);
652 $Shell->expectNever('startup');
653 $Shell->expectNever('hr');
654 $Dispatcher->TestShell =& $Shell;
655
656 $Dispatcher->args = array('mock_without_main', 'hr');
657 $result = $Dispatcher->dispatch();
658 $this->assertFalse($result);
659 $this->assertEqual($Dispatcher->args, array('hr'));
660
661 $Shell = new MockWithoutMainShell();
662 $Shell->setReturnValue('initDb', true);
663 $Shell->expectNever('startup');
664 $Dispatcher->TestShell =& $Shell;
665
666 $Dispatcher->args = array('mock_without_main', 'dispatch');
667 $result = $Dispatcher->dispatch();
668 $this->assertFalse($result);
669
670 $Shell = new MockWithoutMainShell();
671 $Shell->expectNever('startup');
672 $Dispatcher->TestShell =& $Shell;
673
674 $Dispatcher->args = array('mock_without_main', 'idontexist');
675 $result = $Dispatcher->dispatch();
676 $this->assertFalse($result);
677
678 $Shell = new MockWithoutMainShell();
679 $Shell->expectNever('startup');
680 $Shell->expectNever('_secret');
681 $Dispatcher->TestShell =& $Shell;
682
683 $Dispatcher->args = array('mock_without_main', '_secret');
684 $result = $Dispatcher->dispatch();
685 $this->assertFalse($result);
686 }
687
688 /**
689 * Verify correct dispatch of custom classes with a main method
690 *
691 * @return void
692 * @access public
693 */
694 function testDispatchNotAShellWithMain() {
695 Mock::generate('Object', 'MockWithMainNotAShell',
696 array('main', 'initialize', 'loadTasks', 'startup', '_secret'));
697
698 $Dispatcher =& new TestShellDispatcher();
699
700 $Shell = new MockWithMainNotAShell();
701 $Shell->setReturnValue('main', true);
702 $Shell->expectNever('initialize');
703 $Shell->expectNever('loadTasks');
704 $Shell->expectOnce('startup');
705 $Shell->expectOnce('main');
706 $Dispatcher->TestShell =& $Shell;
707
708 $Dispatcher->args = array('mock_with_main_not_a');
709 $result = $Dispatcher->dispatch();
710 $this->assertTrue($result);
711 $this->assertEqual($Dispatcher->args, array());
712
713 $Shell = new MockWithMainNotAShell();
714 $Shell->setReturnValue('main', true);
715 $Shell->expectOnce('startup');
716 $Shell->expectOnce('main');
717 $Dispatcher->TestShell =& $Shell;
718
719 $Dispatcher->args = array('mock_with_main_not_a', 'initdb');
720 $result = $Dispatcher->dispatch();
721 $this->assertTrue($result);
722 $this->assertEqual($Dispatcher->args, array('initdb'));
723
724 $Shell = new MockWithMainNotAShell();
725 $Shell->setReturnValue('main', true);
726 $Shell->expectOnce('startup');
727 $Shell->expectOnce('main');
728 $Dispatcher->TestShell =& $Shell;
729
730 $Dispatcher->args = array('mock_with_main_not_a', 'hr');
731 $result = $Dispatcher->dispatch();
732 $this->assertTrue($result);
733 $this->assertEqual($Dispatcher->args, array('hr'));
734
735 $Shell = new MockWithMainNotAShell();
736 $Shell->setReturnValue('main', true);
737 $Shell->expectOnce('startup');
738 $Shell->expectOnce('main');
739 $Dispatcher->TestShell =& $Shell;
740
741 $Dispatcher->args = array('mock_with_main_not_a', 'dispatch');
742 $result = $Dispatcher->dispatch();
743 $this->assertTrue($result);
744 $this->assertEqual($Dispatcher->args, array('dispatch'));
745
746 $Shell = new MockWithMainNotAShell();
747 $Shell->setReturnValue('main', true);
748 $Shell->expectOnce('startup');
749 $Shell->expectOnce('main');
750 $Dispatcher->TestShell =& $Shell;
751
752 $Dispatcher->args = array('mock_with_main_not_a', 'idontexist');
753 $result = $Dispatcher->dispatch();
754 $this->assertTrue($result);
755 $this->assertEqual($Dispatcher->args, array('idontexist'));
756
757 $Shell = new MockWithMainNotAShell();
758 $Shell->expectNever('startup');
759 $Shell->expectNever('main');
760 $Shell->expectNever('_secret');
761 $Dispatcher->TestShell =& $Shell;
762
763 $Dispatcher->args = array('mock_with_main_not_a', '_secret');
764 $result = $Dispatcher->dispatch();
765 $this->assertFalse($result);
766 }
767
768 /**
769 * Verify correct dispatch of custom classes without a main method
770 *
771 * @return void
772 * @access public
773 */
774 function testDispatchNotAShellWithoutMain() {
775 Mock::generate('Object', 'MockWithoutMainNotAShell',
776 array('initDb', 'initialize', 'loadTasks', 'startup', '_secret'));
777
778 $Dispatcher =& new TestShellDispatcher();
779
780 $Shell = new MockWithoutMainNotAShell();
781 $Shell->setReturnValue('initDb', true);
782 $Shell->expectNever('initialize');
783 $Shell->expectNever('loadTasks');
784 $Shell->expectNever('startup');
785 $Dispatcher->TestShell =& $Shell;
786
787 $Dispatcher->args = array('mock_without_main_not_a');
788 $result = $Dispatcher->dispatch();
789 $this->assertFalse($result);
790
791 $Shell = new MockWithoutMainNotAShell();
792 $Shell->setReturnValue('initDb', true);
793 $Shell->expectOnce('startup');
794 $Shell->expectOnce('initDb');
795 $Dispatcher->TestShell =& $Shell;
796
797 $Dispatcher->args = array('mock_without_main_not_a', 'initdb');
798 $result = $Dispatcher->dispatch();
799 $this->assertTrue($result);
800 $this->assertEqual($Dispatcher->args, array());
801
802 $Shell = new MockWithoutMainNotAShell();
803 $Shell->setReturnValue('initDb', true);
804 $Shell->expectNever('startup');
805 $Dispatcher->TestShell =& $Shell;
806
807 $Dispatcher->args = array('mock_without_main_not_a', 'hr');
808 $result = $Dispatcher->dispatch();
809 $this->assertFalse($result);
810
811 $Shell = new MockWithoutMainNotAShell();
812 $Shell->setReturnValue('initDb', true);
813 $Shell->expectNever('startup');
814 $Dispatcher->TestShell =& $Shell;
815
816 $Dispatcher->args = array('mock_without_main_not_a', 'dispatch');
817 $result = $Dispatcher->dispatch();
818 $this->assertFalse($result);
819
820 $Shell = new MockWithoutMainNotAShell();
821 $Shell->expectNever('startup');
822 $Dispatcher->TestShell =& $Shell;
823
824 $Dispatcher->args = array('mock_without_main_not_a', 'idontexist');
825 $result = $Dispatcher->dispatch();
826 $this->assertFalse($result);
827
828 $Shell = new MockWithoutMainNotAShell();
829 $Shell->expectNever('startup');
830 $Shell->expectNever('_secret');
831 $Dispatcher->TestShell =& $Shell;
832
833 $Dispatcher->args = array('mock_without_main_not_a', '_secret');
834 $result = $Dispatcher->dispatch();
835 $this->assertFalse($result);
836 }
837
838 /**
839 * Verify that a task is called instead of the shell if the first arg equals
840 * the name of the task
841 *
842 * @return void
843 * @access public
844 */
845 function testDispatchTask() {
846 Mock::generate('Shell', 'MockWeekShell', array('main'));
847 Mock::generate('Shell', 'MockOnSundayTask', array('execute'));
848
849 $Dispatcher =& new TestShellDispatcher();
850
851 $Shell = new MockWeekShell();
852 $Shell->expectOnce('initialize');
853 $Shell->expectOnce('loadTasks');
854 $Shell->expectNever('startup');
855 $Shell->expectNever('main');
856
857 $Task = new MockOnSundayTask();
858 $Task->setReturnValue('execute', true);
859 $Task->expectOnce('initialize');
860 $Task->expectOnce('loadTasks');
861 $Task->expectOnce('startup');
862 $Task->expectOnce('execute');
863
864 $Shell->MockOnSunday =& $Task;
865 $Shell->taskNames = array('MockOnSunday');
866 $Dispatcher->TestShell =& $Shell;
867
868 $Dispatcher->args = array('mock_week', 'mock_on_sunday');
869 $result = $Dispatcher->dispatch();
870 $this->assertTrue($result);
871 $this->assertEqual($Dispatcher->args, array());
872
873 $Shell = new MockWeekShell();
874 $Task = new MockOnSundayTask();
875 $Task->expectNever('execute');
876 $Task->expectOnce('help');
877
878 $Shell->MockOnSunday =& $Task;
879 $Shell->taskNames = array('MockOnSunday');
880 $Dispatcher->TestShell =& $Shell;
881
882 $Dispatcher->args = array('mock_week', 'mock_on_sunday', 'help');
883 $result = $Dispatcher->dispatch();
884 $this->assertTrue($result);
885 }
886
887 /**
888 * Verify shifting of arguments
889 *
890 * @return void
891 * @access public
892 */
893 function testShiftArgs() {
894 $Dispatcher =& new TestShellDispatcher();
895
896 $Dispatcher->args = array('a', 'b', 'c');
897 $this->assertEqual($Dispatcher->shiftArgs(), 'a');
898 $this->assertIdentical($Dispatcher->args, array('b', 'c'));
899
900 $Dispatcher->args = array('a' => 'b', 'c', 'd');
901 $this->assertEqual($Dispatcher->shiftArgs(), 'b');
902 $this->assertIdentical($Dispatcher->args, array('c', 'd'));
903
904 $Dispatcher->args = array('a', 'b' => 'c', 'd');
905 $this->assertEqual($Dispatcher->shiftArgs(), 'a');
906 $this->assertIdentical($Dispatcher->args, array('b' => 'c', 'd'));
907
908 $Dispatcher->args = array(0 => 'a', 2 => 'b', 30 => 'c');
909 $this->assertEqual($Dispatcher->shiftArgs(), 'a');
910 $this->assertIdentical($Dispatcher->args, array(0 => 'b', 1 => 'c'));
911
912 $Dispatcher->args = array();
913 $this->assertNull($Dispatcher->shiftArgs());
914 $this->assertIdentical($Dispatcher->args, array());
915 }
916
917 /**
918 * testHelpCommand method
919 *
920 * @return void
921 * @access public
922 */
923 function testHelpCommand() {
924 $Dispatcher =& new TestShellDispatcher();
925
926 $expected = "/example \[.*TestPlugin, TestPluginTwo.*\]/";
927 $this->assertPattern($expected, $Dispatcher->stdout);
928
929 $expected = "/welcome \[.*TestPluginTwo.*\]/";
930 $this->assertPattern($expected, $Dispatcher->stdout);
931
932 $expected = "/acl \[.*CORE.*\]/";
933 $this->assertPattern($expected, $Dispatcher->stdout);
934
935 $expected = "/api \[.*CORE.*\]/";
936 $this->assertPattern($expected, $Dispatcher->stdout);
937
938 $expected = "/bake \[.*CORE.*\]/";
939 $this->assertPattern($expected, $Dispatcher->stdout);
940
941 $expected = "/console \[.*CORE.*\]/";
942 $this->assertPattern($expected, $Dispatcher->stdout);
943
944 $expected = "/i18n \[.*CORE.*\]/";
945 $this->assertPattern($expected, $Dispatcher->stdout);
946
947 $expected = "/schema \[.*CORE.*\]/";
948 $this->assertPattern($expected, $Dispatcher->stdout);
949
950 $expected = "/testsuite \[.*CORE.*\]/";
951 $this->assertPattern($expected, $Dispatcher->stdout);
952
953 $expected = "/sample \[.*test_app.*\]/";
954 $this->assertPattern($expected, $Dispatcher->stdout);
955 }
956 }