Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12345 anikendra 1
<?php
2
/**
3
 * ShellTest file
4
 *
5
 * Test Case for Shell
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
17
 * @since         CakePHP v 1.2.0.7726
18
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
19
 */
20
 
21
App::uses('ShellDispatcher', 'Console');
22
App::uses('Shell', 'Console');
23
App::uses('Folder', 'Utility');
24
 
25
/**
26
 * ShellTestShell class
27
 *
28
 * @package       Cake.Test.Case.Console.Command
29
 */
30
class ShellTestShell extends Shell {
31
 
32
/**
33
 * name property
34
 *
35
 * @var name
36
 */
37
	public $name = 'ShellTestShell';
38
 
39
/**
40
 * stopped property
41
 *
42
 * @var int
43
 */
44
	public $stopped;
45
 
46
/**
47
 * testMessage property
48
 *
49
 * @var string
50
 */
51
	public $testMessage = 'all your base are belong to us';
52
 
53
/**
54
 * stop method
55
 *
56
 * @param int $status
57
 * @return void
58
 */
59
	protected function _stop($status = 0) {
60
		$this->stopped = $status;
61
	}
62
 
63
	protected function _secret() {
64
	}
65
 
66
	//@codingStandardsIgnoreStart
67
	public function do_something() {
68
	}
69
 
70
	protected function no_access() {
71
	}
72
 
73
	public function log_something() {
74
		$this->log($this->testMessage);
75
	}
76
	//@codingStandardsIgnoreEnd
77
 
78
	public function mergeVars($properties, $class, $normalize = true) {
79
		return $this->_mergeVars($properties, $class, $normalize);
80
	}
81
 
82
	public function useLogger($enable = true) {
83
		$this->_useLogger($enable);
84
	}
85
 
86
}
87
 
88
/**
89
 * Class for testing merging vars
90
 *
91
 * @package       Cake.Test.Case.Console.Command
92
 */
93
class TestMergeShell extends Shell {
94
 
95
	public $tasks = array('DbConfig', 'Fixture');
96
 
97
	public $uses = array('Comment');
98
 
99
}
100
 
101
/**
102
 * TestAppleTask class
103
 *
104
 * @package       Cake.Test.Case.Console.Command
105
 */
106
class TestAppleTask extends Shell {
107
}
108
 
109
/**
110
 * TestBananaTask class
111
 *
112
 * @package       Cake.Test.Case.Console.Command
113
 */
114
class TestBananaTask extends Shell {
115
}
116
 
117
/**
118
 * ShellTest class
119
 *
120
 * @package       Cake.Test.Case.Console.Command
121
 */
122
class ShellTest extends CakeTestCase {
123
 
124
/**
125
 * Fixtures used in this test case
126
 *
127
 * @var array
128
 */
129
	public $fixtures = array(
130
		'core.post', 'core.comment', 'core.article', 'core.user',
131
		'core.tag', 'core.articles_tag', 'core.attachment'
132
	);
133
 
134
/**
135
 * setUp method
136
 *
137
 * @return void
138
 */
139
	public function setUp() {
140
		parent::setUp();
141
 
142
		$output = $this->getMock('ConsoleOutput', array(), array(), '', false);
143
		$error = $this->getMock('ConsoleOutput', array(), array(), '', false);
144
		$in = $this->getMock('ConsoleInput', array(), array(), '', false);
145
		$this->Shell = new ShellTestShell($output, $error, $in);
146
 
147
		if (is_dir(TMP . 'shell_test')) {
148
			$Folder = new Folder(TMP . 'shell_test');
149
			$Folder->delete();
150
		}
151
	}
152
 
153
/**
154
 * testConstruct method
155
 *
156
 * @return void
157
 */
158
	public function testConstruct() {
159
		$this->assertEquals('ShellTestShell', $this->Shell->name);
160
		$this->assertInstanceOf('ConsoleInput', $this->Shell->stdin);
161
		$this->assertInstanceOf('ConsoleOutput', $this->Shell->stdout);
162
		$this->assertInstanceOf('ConsoleOutput', $this->Shell->stderr);
163
	}
164
 
165
/**
166
 * test merging vars
167
 *
168
 * @return void
169
 */
170
	public function testMergeVars() {
171
		$this->Shell->tasks = array('DbConfig' => array('one', 'two'));
172
		$this->Shell->uses = array('Posts');
173
		$this->Shell->mergeVars(array('tasks'), 'TestMergeShell');
174
		$this->Shell->mergeVars(array('uses'), 'TestMergeShell', false);
175
 
176
		$expected = array('DbConfig' => null, 'Fixture' => null, 'DbConfig' => array('one', 'two'));
177
		$this->assertEquals($expected, $this->Shell->tasks);
178
 
179
		$expected = array('Fixture' => null, 'DbConfig' => array('one', 'two'));
180
		$this->assertEquals($expected, Hash::normalize($this->Shell->tasks), 'Normalized results are wrong.');
181
		$this->assertEquals(array('Comment', 'Posts'), $this->Shell->uses, 'Merged models are wrong.');
182
	}
183
 
184
/**
185
 * testInitialize method
186
 *
187
 * @return void
188
 */
189
	public function testInitialize() {
190
		App::build(array(
191
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
192
			'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS)
193
		), App::RESET);
194
 
195
		CakePlugin::load('TestPlugin');
196
		$this->Shell->tasks = array('DbConfig' => array('one', 'two'));
197
		$this->Shell->uses = array('TestPlugin.TestPluginPost');
198
		$this->Shell->initialize();
199
 
200
		$this->assertTrue(isset($this->Shell->TestPluginPost));
201
		$this->assertInstanceOf('TestPluginPost', $this->Shell->TestPluginPost);
202
		$this->assertEquals('TestPluginPost', $this->Shell->modelClass);
203
		CakePlugin::unload('TestPlugin');
204
 
205
		$this->Shell->uses = array('Comment');
206
		$this->Shell->initialize();
207
		$this->assertTrue(isset($this->Shell->Comment));
208
		$this->assertInstanceOf('Comment', $this->Shell->Comment);
209
		$this->assertEquals('Comment', $this->Shell->modelClass);
210
		$this->assertInstanceOf('DbConfigTask', $this->Shell->DbConfig);
211
 
212
		App::build();
213
	}
214
 
215
/**
216
 * testLoadModel method
217
 *
218
 * @return void
219
 */
220
	public function testLoadModel() {
221
		App::build(array(
222
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
223
			'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS)
224
		), App::RESET);
225
 
226
		$Shell = new TestMergeShell();
227
		$this->assertEquals('Comment', $Shell->Comment->alias);
228
		$this->assertInstanceOf('Comment', $Shell->Comment);
229
		$this->assertEquals('Comment', $Shell->modelClass);
230
 
231
		CakePlugin::load('TestPlugin');
232
		$this->Shell->loadModel('TestPlugin.TestPluginPost');
233
		$this->assertTrue(isset($this->Shell->TestPluginPost));
234
		$this->assertInstanceOf('TestPluginPost', $this->Shell->TestPluginPost);
235
		$this->assertEquals('TestPluginPost', $this->Shell->modelClass);
236
		CakePlugin::unload('TestPlugin');
237
 
238
		App::build();
239
	}
240
 
241
/**
242
 * testIn method
243
 *
244
 * @return void
245
 */
246
	public function testIn() {
247
		$this->Shell->stdin->expects($this->at(0))
248
			->method('read')
249
			->will($this->returnValue('n'));
250
 
251
		$this->Shell->stdin->expects($this->at(1))
252
			->method('read')
253
			->will($this->returnValue('Y'));
254
 
255
		$this->Shell->stdin->expects($this->at(2))
256
			->method('read')
257
			->will($this->returnValue('y'));
258
 
259
		$this->Shell->stdin->expects($this->at(3))
260
			->method('read')
261
			->will($this->returnValue('y'));
262
 
263
		$this->Shell->stdin->expects($this->at(4))
264
			->method('read')
265
			->will($this->returnValue('y'));
266
 
267
		$this->Shell->stdin->expects($this->at(5))
268
			->method('read')
269
			->will($this->returnValue('0'));
270
 
271
		$result = $this->Shell->in('Just a test?', array('y', 'n'), 'n');
272
		$this->assertEquals('n', $result);
273
 
274
		$result = $this->Shell->in('Just a test?', array('y', 'n'), 'n');
275
		$this->assertEquals('Y', $result);
276
 
277
		$result = $this->Shell->in('Just a test?', 'y,n', 'n');
278
		$this->assertEquals('y', $result);
279
 
280
		$result = $this->Shell->in('Just a test?', 'y/n', 'n');
281
		$this->assertEquals('y', $result);
282
 
283
		$result = $this->Shell->in('Just a test?', 'y', 'y');
284
		$this->assertEquals('y', $result);
285
 
286
		$result = $this->Shell->in('Just a test?', array(0, 1, 2), '0');
287
		$this->assertEquals('0', $result);
288
	}
289
 
290
/**
291
 * Test in() when not interactive.
292
 *
293
 * @return void
294
 */
295
	public function testInNonInteractive() {
296
		$this->Shell->interactive = false;
297
 
298
		$result = $this->Shell->in('Just a test?', 'y/n', 'n');
299
		$this->assertEquals('n', $result);
300
	}
301
 
302
/**
303
 * testOut method
304
 *
305
 * @return void
306
 */
307
	public function testOut() {
308
		$this->Shell->stdout->expects($this->at(0))
309
			->method('write')
310
			->with("Just a test", 1);
311
 
312
		$this->Shell->stdout->expects($this->at(1))
313
			->method('write')
314
			->with(array('Just', 'a', 'test'), 1);
315
 
316
		$this->Shell->stdout->expects($this->at(2))
317
			->method('write')
318
			->with(array('Just', 'a', 'test'), 2);
319
 
320
		$this->Shell->stdout->expects($this->at(3))
321
			->method('write')
322
			->with('', 1);
323
 
324
		$this->Shell->out('Just a test');
325
 
326
		$this->Shell->out(array('Just', 'a', 'test'));
327
 
328
		$this->Shell->out(array('Just', 'a', 'test'), 2);
329
 
330
		$this->Shell->out();
331
	}
332
 
333
/**
334
 * test that verbose and quiet output levels work
335
 *
336
 * @return void
337
 */
338
	public function testVerboseOutput() {
339
		$this->Shell->stdout->expects($this->at(0))->method('write')
340
			->with('Verbose', 1);
341
		$this->Shell->stdout->expects($this->at(1))->method('write')
342
			->with('Normal', 1);
343
		$this->Shell->stdout->expects($this->at(2))->method('write')
344
			->with('Quiet', 1);
345
 
346
		$this->Shell->params['verbose'] = true;
347
		$this->Shell->params['quiet'] = false;
348
 
349
		$this->Shell->out('Verbose', 1, Shell::VERBOSE);
350
		$this->Shell->out('Normal', 1, Shell::NORMAL);
351
		$this->Shell->out('Quiet', 1, Shell::QUIET);
352
	}
353
 
354
/**
355
 * test that verbose and quiet output levels work
356
 *
357
 * @return void
358
 */
359
	public function testQuietOutput() {
360
		$this->Shell->stdout->expects($this->once())->method('write')
361
			->with('Quiet', 1);
362
 
363
		$this->Shell->params['verbose'] = false;
364
		$this->Shell->params['quiet'] = true;
365
 
366
		$this->Shell->out('Verbose', 1, Shell::VERBOSE);
367
		$this->Shell->out('Normal', 1, Shell::NORMAL);
368
		$this->Shell->out('Quiet', 1, Shell::QUIET);
369
	}
370
 
371
/**
372
 * testErr method
373
 *
374
 * @return void
375
 */
376
	public function testErr() {
377
		$this->Shell->stderr->expects($this->at(0))
378
			->method('write')
379
			->with("Just a test", 1);
380
 
381
		$this->Shell->stderr->expects($this->at(1))
382
			->method('write')
383
			->with(array('Just', 'a', 'test'), 1);
384
 
385
		$this->Shell->stderr->expects($this->at(2))
386
			->method('write')
387
			->with(array('Just', 'a', 'test'), 2);
388
 
389
		$this->Shell->stderr->expects($this->at(3))
390
			->method('write')
391
			->with('', 1);
392
 
393
		$this->Shell->err('Just a test');
394
 
395
		$this->Shell->err(array('Just', 'a', 'test'));
396
 
397
		$this->Shell->err(array('Just', 'a', 'test'), 2);
398
 
399
		$this->Shell->err();
400
	}
401
 
402
/**
403
 * testNl
404
 *
405
 * @return void
406
 */
407
	public function testNl() {
408
		$newLine = "\n";
409
		if (DS === '\\') {
410
			$newLine = "\r\n";
411
		}
412
		$this->assertEquals($this->Shell->nl(), $newLine);
413
		$this->assertEquals($this->Shell->nl(true), $newLine);
414
		$this->assertEquals("", $this->Shell->nl(false));
415
		$this->assertEquals($this->Shell->nl(2), $newLine . $newLine);
416
		$this->assertEquals($this->Shell->nl(1), $newLine);
417
	}
418
 
419
/**
420
 * testHr
421
 *
422
 * @return void
423
 */
424
	public function testHr() {
425
		$bar = '---------------------------------------------------------------';
426
 
427
		$this->Shell->stdout->expects($this->at(0))->method('write')->with('', 0);
428
		$this->Shell->stdout->expects($this->at(1))->method('write')->with($bar, 1);
429
		$this->Shell->stdout->expects($this->at(2))->method('write')->with('', 0);
430
 
431
		$this->Shell->stdout->expects($this->at(3))->method('write')->with("", true);
432
		$this->Shell->stdout->expects($this->at(4))->method('write')->with($bar, 1);
433
		$this->Shell->stdout->expects($this->at(5))->method('write')->with("", true);
434
 
435
		$this->Shell->stdout->expects($this->at(6))->method('write')->with("", 2);
436
		$this->Shell->stdout->expects($this->at(7))->method('write')->with($bar, 1);
437
		$this->Shell->stdout->expects($this->at(8))->method('write')->with("", 2);
438
 
439
		$this->Shell->hr();
440
 
441
		$this->Shell->hr(true);
442
 
443
		$this->Shell->hr(2);
444
	}
445
 
446
/**
447
 * testError
448
 *
449
 * @return void
450
 */
451
	public function testError() {
452
		$this->Shell->stderr->expects($this->at(0))
453
			->method('write')
454
			->with("<error>Error:</error> Foo Not Found", 1);
455
 
456
		$this->Shell->stderr->expects($this->at(1))
457
			->method('write')
458
			->with("<error>Error:</error> Foo Not Found", 1);
459
 
460
		$this->Shell->stderr->expects($this->at(2))
461
			->method('write')
462
			->with("Searched all...", 1);
463
 
464
		$this->Shell->error('Foo Not Found');
465
		$this->assertSame($this->Shell->stopped, 1);
466
 
467
		$this->Shell->stopped = null;
468
 
469
		$this->Shell->error('Foo Not Found', 'Searched all...');
470
		$this->assertSame($this->Shell->stopped, 1);
471
	}
472
 
473
/**
474
 * testLoadTasks method
475
 *
476
 * @return void
477
 */
478
	public function testLoadTasks() {
479
		$this->assertTrue($this->Shell->loadTasks());
480
 
481
		$this->Shell->tasks = null;
482
		$this->assertTrue($this->Shell->loadTasks());
483
 
484
		$this->Shell->tasks = false;
485
		$this->assertTrue($this->Shell->loadTasks());
486
 
487
		$this->Shell->tasks = true;
488
		$this->assertTrue($this->Shell->loadTasks());
489
 
490
		$this->Shell->tasks = array();
491
		$this->assertTrue($this->Shell->loadTasks());
492
 
493
		$this->Shell->tasks = array('TestApple');
494
		$this->assertTrue($this->Shell->loadTasks());
495
		$this->assertInstanceOf('TestAppleTask', $this->Shell->TestApple);
496
 
497
		$this->Shell->tasks = 'TestBanana';
498
		$this->assertTrue($this->Shell->loadTasks());
499
		$this->assertInstanceOf('TestAppleTask', $this->Shell->TestApple);
500
		$this->assertInstanceOf('TestBananaTask', $this->Shell->TestBanana);
501
 
502
		unset($this->Shell->ShellTestApple, $this->Shell->TestBanana);
503
 
504
		$this->Shell->tasks = array('TestApple', 'TestBanana');
505
		$this->assertTrue($this->Shell->loadTasks());
506
		$this->assertInstanceOf('TestAppleTask', $this->Shell->TestApple);
507
		$this->assertInstanceOf('TestBananaTask', $this->Shell->TestBanana);
508
	}
509
 
510
/**
511
 * test that __get() makes args and params references
512
 *
513
 * @return void
514
 */
515
	public function testMagicGetArgAndParamReferences() {
516
		$this->Shell->tasks = array('TestApple');
517
		$this->Shell->args = array('one');
518
		$this->Shell->params = array('help' => false);
519
		$this->Shell->loadTasks();
520
		$result = $this->Shell->TestApple;
521
 
522
		$this->Shell->args = array('one', 'two');
523
 
524
		$this->assertSame($this->Shell->args, $result->args);
525
		$this->assertSame($this->Shell->params, $result->params);
526
	}
527
 
528
/**
529
 * testShortPath method
530
 *
531
 * @return void
532
 */
533
	public function testShortPath() {
534
		$path = $expected = DS . 'tmp' . DS . 'ab' . DS . 'cd';
535
		$this->assertEquals($expected, $this->Shell->shortPath($path));
536
 
537
		$path = $expected = DS . 'tmp' . DS . 'ab' . DS . 'cd' . DS;
538
		$this->assertEquals($expected, $this->Shell->shortPath($path));
539
 
540
		$path = $expected = DS . 'tmp' . DS . 'ab' . DS . 'index.php';
541
		$this->assertEquals($expected, $this->Shell->shortPath($path));
542
 
543
		$path = DS . 'tmp' . DS . 'ab' . DS . DS . 'cd';
544
		$expected = DS . 'tmp' . DS . 'ab' . DS . 'cd';
545
		$this->assertEquals($expected, $this->Shell->shortPath($path));
546
 
547
		$path = 'tmp' . DS . 'ab';
548
		$expected = 'tmp' . DS . 'ab';
549
		$this->assertEquals($expected, $this->Shell->shortPath($path));
550
 
551
		$path = 'tmp' . DS . 'ab';
552
		$expected = 'tmp' . DS . 'ab';
553
		$this->assertEquals($expected, $this->Shell->shortPath($path));
554
 
555
		$path = APP;
556
		$expected = DS . basename(APP) . DS;
557
		$this->assertEquals($expected, $this->Shell->shortPath($path));
558
 
559
		$path = APP . 'index.php';
560
		$expected = DS . basename(APP) . DS . 'index.php';
561
		$this->assertEquals($expected, $this->Shell->shortPath($path));
562
	}
563
 
564
/**
565
 * testCreateFile method
566
 *
567
 * @return void
568
 */
569
	public function testCreateFileNonInteractive() {
570
		$eol = PHP_EOL;
571
 
572
		$path = TMP . 'shell_test';
573
		$file = $path . DS . 'file1.php';
574
 
575
		new Folder($path, true);
576
 
577
		$this->Shell->interactive = false;
578
 
579
		$contents = "<?php{$eol}echo 'test';${eol}\$te = 'st';{$eol}";
580
		$result = $this->Shell->createFile($file, $contents);
581
		$this->assertTrue($result);
582
		$this->assertTrue(file_exists($file));
583
		$this->assertEquals(file_get_contents($file), $contents);
584
 
585
		$contents = "<?php\necho 'another test';\n\$te = 'st';\n";
586
		$result = $this->Shell->createFile($file, $contents);
587
		$this->assertTrue($result);
588
		$this->assertTrue(file_exists($file));
589
		$this->assertTextEquals(file_get_contents($file), $contents);
590
	}
591
 
592
/**
593
 * test createFile when the shell is interactive.
594
 *
595
 * @return void
596
 */
597
	public function testCreateFileInteractive() {
598
		$eol = PHP_EOL;
599
 
600
		$path = TMP . 'shell_test';
601
		$file = $path . DS . 'file1.php';
602
		new Folder($path, true);
603
 
604
		$this->Shell->interactive = true;
605
 
606
		$this->Shell->stdin->expects($this->at(0))
607
			->method('read')
608
			->will($this->returnValue('n'));
609
 
610
		$this->Shell->stdin->expects($this->at(1))
611
			->method('read')
612
			->will($this->returnValue('y'));
613
 
614
		$contents = "<?php{$eol}echo 'yet another test';{$eol}\$te = 'st';{$eol}";
615
		$result = $this->Shell->createFile($file, $contents);
616
		$this->assertTrue($result);
617
		$this->assertTrue(file_exists($file));
618
		$this->assertEquals(file_get_contents($file), $contents);
619
 
620
		// no overwrite
621
		$contents = 'new contents';
622
		$result = $this->Shell->createFile($file, $contents);
623
		$this->assertFalse($result);
624
		$this->assertTrue(file_exists($file));
625
		$this->assertNotEquals($contents, file_get_contents($file));
626
 
627
		// overwrite
628
		$contents = 'more new contents';
629
		$result = $this->Shell->createFile($file, $contents);
630
		$this->assertTrue($result);
631
		$this->assertTrue(file_exists($file));
632
		$this->assertEquals($contents, file_get_contents($file));
633
	}
634
 
635
/**
636
 * Test that you can't create files that aren't writable.
637
 *
638
 * @return void
639
 */
640
	public function testCreateFileNoPermissions() {
641
		$this->skipIf(DIRECTORY_SEPARATOR === '\\', 'Cant perform operations using permissions on windows.');
642
 
643
		$path = TMP . 'shell_test';
644
		$file = $path . DS . 'no_perms';
645
 
646
		if (!is_dir($path)) {
647
			mkdir($path);
648
		}
649
		chmod($path, 0444);
650
 
651
		$this->Shell->createFile($file, 'testing');
652
		$this->assertFalse(file_exists($file));
653
 
654
		chmod($path, 0744);
655
		rmdir($path);
656
	}
657
 
658
/**
659
 * test hasTask method
660
 *
661
 * @return void
662
 */
663
	public function testHasTask() {
664
		$this->Shell->tasks = array('Extract', 'DbConfig');
665
		$this->Shell->loadTasks();
666
 
667
		$this->assertTrue($this->Shell->hasTask('extract'));
668
		$this->assertTrue($this->Shell->hasTask('Extract'));
669
		$this->assertFalse($this->Shell->hasTask('random'));
670
 
671
		$this->assertTrue($this->Shell->hasTask('db_config'));
672
		$this->assertTrue($this->Shell->hasTask('DbConfig'));
673
	}
674
 
675
/**
676
 * test the hasMethod
677
 *
678
 * @return void
679
 */
680
	public function testHasMethod() {
681
		$this->assertTrue($this->Shell->hasMethod('do_something'));
682
		$this->assertFalse($this->Shell->hasMethod('hr'), 'hr is callable');
683
		$this->assertFalse($this->Shell->hasMethod('_secret'), '_secret is callable');
684
		$this->assertFalse($this->Shell->hasMethod('no_access'), 'no_access is callable');
685
	}
686
 
687
/**
688
 * test run command calling main.
689
 *
690
 * @return void
691
 */
692
	public function testRunCommandMain() {
693
		$Mock = $this->getMock('Shell', array('main', 'startup'), array(), '', false);
694
 
695
		$Mock->expects($this->once())->method('main')->will($this->returnValue(true));
696
		$result = $Mock->runCommand(null, array());
697
		$this->assertTrue($result);
698
	}
699
 
700
/**
701
 * test run command calling a legit method.
702
 *
703
 * @return void
704
 */
705
	public function testRunCommandWithMethod() {
706
		$Mock = $this->getMock('Shell', array('hit_me', 'startup'), array(), '', false);
707
 
708
		$Mock->expects($this->once())->method('hit_me')->will($this->returnValue(true));
709
		$result = $Mock->runCommand('hit_me', array());
710
		$this->assertTrue($result);
711
	}
712
 
713
/**
714
 * test run command causing exception on Shell method.
715
 *
716
 * @return void
717
 */
718
	public function testRunCommandBaseclassMethod() {
719
		$Mock = $this->getMock('Shell', array('startup', 'getOptionParser', 'out'), array(), '', false);
720
		$Parser = $this->getMock('ConsoleOptionParser', array(), array(), '', false);
721
 
722
		$Parser->expects($this->once())->method('help');
723
		$Mock->expects($this->once())->method('getOptionParser')
724
			->will($this->returnValue($Parser));
725
		$Mock->expects($this->never())->method('hr');
726
		$Mock->expects($this->once())->method('out');
727
 
728
		$Mock->runCommand('hr', array());
729
	}
730
 
731
/**
732
 * test run command causing exception on Shell method.
733
 *
734
 * @return void
735
 */
736
	public function testRunCommandMissingMethod() {
737
		$Mock = $this->getMock('Shell', array('startup', 'getOptionParser', 'out'), array(), '', false);
738
		$Parser = $this->getMock('ConsoleOptionParser', array(), array(), '', false);
739
 
740
		$Parser->expects($this->once())->method('help');
741
		$Mock->expects($this->never())->method('idontexist');
742
		$Mock->expects($this->once())->method('getOptionParser')
743
			->will($this->returnValue($Parser));
744
		$Mock->expects($this->once())->method('out');
745
 
746
		$result = $Mock->runCommand('idontexist', array());
747
		$this->assertFalse($result);
748
	}
749
 
750
/**
751
 * test that a --help causes help to show.
752
 *
753
 * @return void
754
 */
755
	public function testRunCommandTriggeringHelp() {
756
		$Parser = $this->getMock('ConsoleOptionParser', array(), array(), '', false);
757
		$Parser->expects($this->once())->method('parse')
758
			->with(array('--help'))
759
			->will($this->returnValue(array(array('help' => true), array())));
760
		$Parser->expects($this->once())->method('help');
761
 
762
		$Shell = $this->getMock('Shell', array('getOptionParser', 'out', 'startup', '_welcome'), array(), '', false);
763
		$Shell->expects($this->once())->method('getOptionParser')
764
			->will($this->returnValue($Parser));
765
		$Shell->expects($this->once())->method('out');
766
 
767
		$Shell->runCommand(null, array('--help'));
768
	}
769
 
770
/**
771
 * test that runCommand will call runCommand on the task.
772
 *
773
 * @return void
774
 */
775
	public function testRunCommandHittingTask() {
776
		$Shell = $this->getMock('Shell', array('hasTask', 'startup'), array(), '', false);
777
		$task = $this->getMock('Shell', array('execute', 'runCommand'), array(), '', false);
778
		$task->expects($this->any())
779
			->method('runCommand')
780
			->with('execute', array('one', 'value'));
781
 
782
		$Shell->expects($this->once())->method('startup');
783
		$Shell->expects($this->any())
784
			->method('hasTask')
785
			->will($this->returnValue(true));
786
 
787
		$Shell->RunCommand = $task;
788
 
789
		$Shell->runCommand('run_command', array('run_command', 'one', 'value'));
790
	}
791
 
792
/**
793
 * test wrapBlock wrapping text.
794
 *
795
 * @return void
796
 */
797
	public function testWrapText() {
798
		$text = 'This is the song that never ends. This is the song that never ends. This is the song that never ends.';
799
		$result = $this->Shell->wrapText($text, 33);
800
		$expected = <<<TEXT
801
This is the song that never ends.
802
This is the song that never ends.
803
This is the song that never ends.
804
TEXT;
805
		$this->assertTextEquals($expected, $result, 'Text not wrapped.');
806
 
807
		$result = $this->Shell->wrapText($text, array('indent' => '  ', 'width' => 33));
808
		$expected = <<<TEXT
809
  This is the song that never ends.
810
  This is the song that never ends.
811
  This is the song that never ends.
812
TEXT;
813
		$this->assertTextEquals($expected, $result, 'Text not wrapped.');
814
	}
815
 
816
/**
817
 * Testing camel cased naming of tasks
818
 *
819
 * @return void
820
 */
821
	public function testShellNaming() {
822
		$this->Shell->tasks = array('TestApple');
823
		$this->Shell->loadTasks();
824
		$expected = 'TestApple';
825
		$this->assertEquals($expected, $this->Shell->TestApple->name);
826
	}
827
 
828
/**
829
 * Test that option parsers are created with the correct name/command.
830
 *
831
 * @return void
832
 */
833
	public function testGetOptionParser() {
834
		$this->Shell->name = 'test';
835
		$this->Shell->plugin = 'plugin';
836
		$parser = $this->Shell->getOptionParser();
837
 
838
		$this->assertEquals('plugin.test', $parser->command());
839
	}
840
 
841
/**
842
 * Test file and console and logging
843
 *
844
 * @return void
845
 */
846
	public function testFileAndConsoleLogging() {
847
		// file logging
848
		$this->Shell->log_something();
849
		$this->assertTrue(file_exists(LOGS . 'error.log'));
850
 
851
		unlink(LOGS . 'error.log');
852
		$this->assertFalse(file_exists(LOGS . 'error.log'));
853
 
854
		// both file and console logging
855
		require_once CORE_TEST_CASES . DS . 'Log' . DS . 'Engine' . DS . 'ConsoleLogTest.php';
856
		$mock = $this->getMock('ConsoleLog', array('write'), array(
857
			array('types' => 'error'),
858
		));
859
		TestCakeLog::config('console', array(
860
			'engine' => 'Console',
861
			'stream' => 'php://stderr',
862
			));
863
		TestCakeLog::replace('console', $mock);
864
		$mock->expects($this->once())
865
			->method('write')
866
			->with('error', $this->Shell->testMessage);
867
		$this->Shell->log_something();
868
		$this->assertTrue(file_exists(LOGS . 'error.log'));
869
		$contents = file_get_contents(LOGS . 'error.log');
870
		$this->assertContains($this->Shell->testMessage, $contents);
871
	}
872
 
873
/**
874
 * Tests that _useLogger works properly
875
 *
876
 * @return void
877
 */
878
	public function testProtectedUseLogger() {
879
		CakeLog::drop('stdout');
880
		CakeLog::drop('stderr');
881
		$this->Shell->useLogger(true);
882
		$this->assertNotEmpty(CakeLog::stream('stdout'));
883
		$this->assertNotEmpty(CakeLog::stream('stderr'));
884
		$this->Shell->useLogger(false);
885
		$this->assertFalse(CakeLog::stream('stdout'));
886
		$this->assertFalse(CakeLog::stream('stderr'));
887
	}
888
 
889
/**
890
 * Test file and console and logging quiet output
891
 *
892
 * @return void
893
 */
894
	public function testQuietLog() {
895
		$output = $this->getMock('ConsoleOutput', array(), array(), '', false);
896
		$error = $this->getMock('ConsoleOutput', array(), array(), '', false);
897
		$in = $this->getMock('ConsoleInput', array(), array(), '', false);
898
		$this->Shell = $this->getMock('ShellTestShell', array('_useLogger'), array($output, $error, $in));
899
		$this->Shell->expects($this->once())->method('_useLogger')->with(false);
900
		$this->Shell->runCommand('foo', array('--quiet'));
901
	}
902
 
903
}