Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
15403 manish.sha 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
 * Test overwriting.
373
 *
374
 * @return void
375
 */
376
	public function testOverwrite() {
377
		$number = strlen('Some text I want to overwrite');
378
 
379
		$this->Shell->stdout->expects($this->at(0))
380
			->method('write')
381
			->with('Some <info>text</info> I want to overwrite', 0)
382
			->will($this->returnValue($number));
383
 
384
		$this->Shell->stdout->expects($this->at(1))
385
			->method('write')
386
			->with(str_repeat("\x08", $number), 0);
387
 
388
		$this->Shell->stdout->expects($this->at(2))
389
			->method('write')
390
			->with('Less text', 0)
391
			->will($this->returnValue(9));
392
 
393
		$this->Shell->stdout->expects($this->at(3))
394
			->method('write')
395
			->with(str_repeat(' ', $number - 9), 0);
396
 
397
		$this->Shell->out('Some <info>text</info> I want to overwrite', 0);
398
		$this->Shell->overwrite('Less text');
399
	}
400
 
401
/**
402
 * testErr method
403
 *
404
 * @return void
405
 */
406
	public function testErr() {
407
		$this->Shell->stderr->expects($this->at(0))
408
			->method('write')
409
			->with("Just a test", 1);
410
 
411
		$this->Shell->stderr->expects($this->at(1))
412
			->method('write')
413
			->with(array('Just', 'a', 'test'), 1);
414
 
415
		$this->Shell->stderr->expects($this->at(2))
416
			->method('write')
417
			->with(array('Just', 'a', 'test'), 2);
418
 
419
		$this->Shell->stderr->expects($this->at(3))
420
			->method('write')
421
			->with('', 1);
422
 
423
		$this->Shell->err('Just a test');
424
 
425
		$this->Shell->err(array('Just', 'a', 'test'));
426
 
427
		$this->Shell->err(array('Just', 'a', 'test'), 2);
428
 
429
		$this->Shell->err();
430
	}
431
 
432
/**
433
 * testNl
434
 *
435
 * @return void
436
 */
437
	public function testNl() {
438
		$newLine = "\n";
439
		if (DS === '\\') {
440
			$newLine = "\r\n";
441
		}
442
		$this->assertEquals($this->Shell->nl(), $newLine);
443
		$this->assertEquals($this->Shell->nl(true), $newLine);
444
		$this->assertEquals("", $this->Shell->nl(false));
445
		$this->assertEquals($this->Shell->nl(2), $newLine . $newLine);
446
		$this->assertEquals($this->Shell->nl(1), $newLine);
447
	}
448
 
449
/**
450
 * testHr
451
 *
452
 * @return void
453
 */
454
	public function testHr() {
455
		$bar = '---------------------------------------------------------------';
456
 
457
		$this->Shell->stdout->expects($this->at(0))->method('write')->with('', 0);
458
		$this->Shell->stdout->expects($this->at(1))->method('write')->with($bar, 1);
459
		$this->Shell->stdout->expects($this->at(2))->method('write')->with('', 0);
460
 
461
		$this->Shell->stdout->expects($this->at(3))->method('write')->with("", true);
462
		$this->Shell->stdout->expects($this->at(4))->method('write')->with($bar, 1);
463
		$this->Shell->stdout->expects($this->at(5))->method('write')->with("", true);
464
 
465
		$this->Shell->stdout->expects($this->at(6))->method('write')->with("", 2);
466
		$this->Shell->stdout->expects($this->at(7))->method('write')->with($bar, 1);
467
		$this->Shell->stdout->expects($this->at(8))->method('write')->with("", 2);
468
 
469
		$this->Shell->hr();
470
 
471
		$this->Shell->hr(true);
472
 
473
		$this->Shell->hr(2);
474
	}
475
 
476
/**
477
 * testError
478
 *
479
 * @return void
480
 */
481
	public function testError() {
482
		$this->Shell->stderr->expects($this->at(0))
483
			->method('write')
484
			->with("<error>Error:</error> Foo Not Found", 1);
485
 
486
		$this->Shell->stderr->expects($this->at(1))
487
			->method('write')
488
			->with("<error>Error:</error> Foo Not Found", 1);
489
 
490
		$this->Shell->stderr->expects($this->at(2))
491
			->method('write')
492
			->with("Searched all...", 1);
493
 
494
		$this->Shell->error('Foo Not Found');
495
		$this->assertSame($this->Shell->stopped, 1);
496
 
497
		$this->Shell->stopped = null;
498
 
499
		$this->Shell->error('Foo Not Found', 'Searched all...');
500
		$this->assertSame($this->Shell->stopped, 1);
501
	}
502
 
503
/**
504
 * testLoadTasks method
505
 *
506
 * @return void
507
 */
508
	public function testLoadTasks() {
509
		$this->assertTrue($this->Shell->loadTasks());
510
 
511
		$this->Shell->tasks = null;
512
		$this->assertTrue($this->Shell->loadTasks());
513
 
514
		$this->Shell->tasks = false;
515
		$this->assertTrue($this->Shell->loadTasks());
516
 
517
		$this->Shell->tasks = true;
518
		$this->assertTrue($this->Shell->loadTasks());
519
 
520
		$this->Shell->tasks = array();
521
		$this->assertTrue($this->Shell->loadTasks());
522
 
523
		$this->Shell->tasks = array('TestApple');
524
		$this->assertTrue($this->Shell->loadTasks());
525
		$this->assertInstanceOf('TestAppleTask', $this->Shell->TestApple);
526
 
527
		$this->Shell->tasks = 'TestBanana';
528
		$this->assertTrue($this->Shell->loadTasks());
529
		$this->assertInstanceOf('TestAppleTask', $this->Shell->TestApple);
530
		$this->assertInstanceOf('TestBananaTask', $this->Shell->TestBanana);
531
 
532
		unset($this->Shell->ShellTestApple, $this->Shell->TestBanana);
533
 
534
		$this->Shell->tasks = array('TestApple', 'TestBanana');
535
		$this->assertTrue($this->Shell->loadTasks());
536
		$this->assertInstanceOf('TestAppleTask', $this->Shell->TestApple);
537
		$this->assertInstanceOf('TestBananaTask', $this->Shell->TestBanana);
538
	}
539
 
540
/**
541
 * test that __get() makes args and params references
542
 *
543
 * @return void
544
 */
545
	public function testMagicGetArgAndParamReferences() {
546
		$this->Shell->tasks = array('TestApple');
547
		$this->Shell->args = array('one');
548
		$this->Shell->params = array('help' => false);
549
		$this->Shell->loadTasks();
550
		$result = $this->Shell->TestApple;
551
 
552
		$this->Shell->args = array('one', 'two');
553
 
554
		$this->assertSame($this->Shell->args, $result->args);
555
		$this->assertSame($this->Shell->params, $result->params);
556
	}
557
 
558
/**
559
 * testShortPath method
560
 *
561
 * @return void
562
 */
563
	public function testShortPath() {
564
		$path = $expected = DS . 'tmp' . DS . 'ab' . DS . 'cd';
565
		$this->assertEquals($expected, $this->Shell->shortPath($path));
566
 
567
		$path = $expected = DS . 'tmp' . DS . 'ab' . DS . 'cd' . DS;
568
		$this->assertEquals($expected, $this->Shell->shortPath($path));
569
 
570
		$path = $expected = DS . 'tmp' . DS . 'ab' . DS . 'index.php';
571
		$this->assertEquals($expected, $this->Shell->shortPath($path));
572
 
573
		$path = DS . 'tmp' . DS . 'ab' . DS . DS . 'cd';
574
		$expected = DS . 'tmp' . DS . 'ab' . DS . 'cd';
575
		$this->assertEquals($expected, $this->Shell->shortPath($path));
576
 
577
		$path = 'tmp' . DS . 'ab';
578
		$expected = 'tmp' . DS . 'ab';
579
		$this->assertEquals($expected, $this->Shell->shortPath($path));
580
 
581
		$path = 'tmp' . DS . 'ab';
582
		$expected = 'tmp' . DS . 'ab';
583
		$this->assertEquals($expected, $this->Shell->shortPath($path));
584
 
585
		$path = APP;
586
		$expected = DS . basename(APP) . DS;
587
		$this->assertEquals($expected, $this->Shell->shortPath($path));
588
 
589
		$path = APP . 'index.php';
590
		$expected = DS . basename(APP) . DS . 'index.php';
591
		$this->assertEquals($expected, $this->Shell->shortPath($path));
592
	}
593
 
594
/**
595
 * testCreateFile method
596
 *
597
 * @return void
598
 */
599
	public function testCreateFileNonInteractive() {
600
		$eol = PHP_EOL;
601
 
602
		$path = TMP . 'shell_test';
603
		$file = $path . DS . 'file1.php';
604
 
605
		new Folder($path, true);
606
 
607
		$this->Shell->interactive = false;
608
 
609
		$contents = "<?php{$eol}echo 'test';${eol}\$te = 'st';{$eol}";
610
		$result = $this->Shell->createFile($file, $contents);
611
		$this->assertTrue($result);
612
		$this->assertTrue(file_exists($file));
613
		$this->assertEquals(file_get_contents($file), $contents);
614
 
615
		$contents = "<?php\necho 'another test';\n\$te = 'st';\n";
616
		$result = $this->Shell->createFile($file, $contents);
617
		$this->assertTrue($result);
618
		$this->assertTrue(file_exists($file));
619
		$this->assertTextEquals(file_get_contents($file), $contents);
620
	}
621
 
622
/**
623
 * test createFile when the shell is interactive.
624
 *
625
 * @return void
626
 */
627
	public function testCreateFileInteractive() {
628
		$eol = PHP_EOL;
629
 
630
		$path = TMP . 'shell_test';
631
		$file = $path . DS . 'file1.php';
632
		new Folder($path, true);
633
 
634
		$this->Shell->interactive = true;
635
 
636
		$this->Shell->stdin->expects($this->at(0))
637
			->method('read')
638
			->will($this->returnValue('n'));
639
 
640
		$this->Shell->stdin->expects($this->at(1))
641
			->method('read')
642
			->will($this->returnValue('y'));
643
 
644
		$contents = "<?php{$eol}echo 'yet another test';{$eol}\$te = 'st';{$eol}";
645
		$result = $this->Shell->createFile($file, $contents);
646
		$this->assertTrue($result);
647
		$this->assertTrue(file_exists($file));
648
		$this->assertEquals(file_get_contents($file), $contents);
649
 
650
		// no overwrite
651
		$contents = 'new contents';
652
		$result = $this->Shell->createFile($file, $contents);
653
		$this->assertFalse($result);
654
		$this->assertTrue(file_exists($file));
655
		$this->assertNotEquals($contents, file_get_contents($file));
656
 
657
		// overwrite
658
		$contents = 'more new contents';
659
		$result = $this->Shell->createFile($file, $contents);
660
		$this->assertTrue($result);
661
		$this->assertTrue(file_exists($file));
662
		$this->assertEquals($contents, file_get_contents($file));
663
	}
664
 
665
/**
666
 * Test that you can't create files that aren't writable.
667
 *
668
 * @return void
669
 */
670
	public function testCreateFileNoPermissions() {
671
		$this->skipIf(DIRECTORY_SEPARATOR === '\\', 'Cant perform operations using permissions on windows.');
672
 
673
		$path = TMP . 'shell_test';
674
		$file = $path . DS . 'no_perms';
675
 
676
		if (!is_dir($path)) {
677
			mkdir($path);
678
		}
679
		chmod($path, 0444);
680
 
681
		$this->Shell->createFile($file, 'testing');
682
		$this->assertFalse(file_exists($file));
683
 
684
		chmod($path, 0744);
685
		rmdir($path);
686
	}
687
 
688
/**
689
 * test hasTask method
690
 *
691
 * @return void
692
 */
693
	public function testHasTask() {
694
		$this->Shell->tasks = array('Extract', 'DbConfig');
695
		$this->Shell->loadTasks();
696
 
697
		$this->assertTrue($this->Shell->hasTask('extract'));
698
		$this->assertTrue($this->Shell->hasTask('Extract'));
699
		$this->assertFalse($this->Shell->hasTask('random'));
700
 
701
		$this->assertTrue($this->Shell->hasTask('db_config'));
702
		$this->assertTrue($this->Shell->hasTask('DbConfig'));
703
	}
704
 
705
/**
706
 * test the hasMethod
707
 *
708
 * @return void
709
 */
710
	public function testHasMethod() {
711
		$this->assertTrue($this->Shell->hasMethod('do_something'));
712
		$this->assertFalse($this->Shell->hasMethod('hr'), 'hr is callable');
713
		$this->assertFalse($this->Shell->hasMethod('_secret'), '_secret is callable');
714
		$this->assertFalse($this->Shell->hasMethod('no_access'), 'no_access is callable');
715
	}
716
 
717
/**
718
 * test run command calling main.
719
 *
720
 * @return void
721
 */
722
	public function testRunCommandMain() {
723
		$Mock = $this->getMock('Shell', array('main', 'startup'), array(), '', false);
724
 
725
		$Mock->expects($this->once())->method('main')->will($this->returnValue(true));
726
		$result = $Mock->runCommand(null, array());
727
		$this->assertTrue($result);
728
	}
729
 
730
/**
731
 * test run command calling a legit method.
732
 *
733
 * @return void
734
 */
735
	public function testRunCommandWithMethod() {
736
		$Mock = $this->getMock('Shell', array('hit_me', 'startup'), array(), '', false);
737
 
738
		$Mock->expects($this->once())->method('hit_me')->will($this->returnValue(true));
739
		$result = $Mock->runCommand('hit_me', array());
740
		$this->assertTrue($result);
741
	}
742
 
743
/**
744
 * test run command causing exception on Shell method.
745
 *
746
 * @return void
747
 */
748
	public function testRunCommandBaseclassMethod() {
749
		$Mock = $this->getMock('Shell', array('startup', 'getOptionParser', 'out'), array(), '', false);
750
		$Parser = $this->getMock('ConsoleOptionParser', array(), array(), '', false);
751
 
752
		$Parser->expects($this->once())->method('help');
753
		$Mock->expects($this->once())->method('getOptionParser')
754
			->will($this->returnValue($Parser));
755
		$Mock->expects($this->never())->method('hr');
756
		$Mock->expects($this->once())->method('out');
757
 
758
		$Mock->runCommand('hr', array());
759
	}
760
 
761
/**
762
 * test run command causing exception on Shell method.
763
 *
764
 * @return void
765
 */
766
	public function testRunCommandMissingMethod() {
767
		$Mock = $this->getMock('Shell', array('startup', 'getOptionParser', 'out'), array(), '', false);
768
		$Parser = $this->getMock('ConsoleOptionParser', array(), array(), '', false);
769
 
770
		$Parser->expects($this->once())->method('help');
771
		$Mock->expects($this->never())->method('idontexist');
772
		$Mock->expects($this->once())->method('getOptionParser')
773
			->will($this->returnValue($Parser));
774
		$Mock->expects($this->once())->method('out');
775
 
776
		$result = $Mock->runCommand('idontexist', array());
777
		$this->assertFalse($result);
778
	}
779
 
780
/**
781
 * test that a --help causes help to show.
782
 *
783
 * @return void
784
 */
785
	public function testRunCommandTriggeringHelp() {
786
		$Parser = $this->getMock('ConsoleOptionParser', array(), array(), '', false);
787
		$Parser->expects($this->once())->method('parse')
788
			->with(array('--help'))
789
			->will($this->returnValue(array(array('help' => true), array())));
790
		$Parser->expects($this->once())->method('help');
791
 
792
		$Shell = $this->getMock('Shell', array('getOptionParser', 'out', 'startup', '_welcome'), array(), '', false);
793
		$Shell->expects($this->once())->method('getOptionParser')
794
			->will($this->returnValue($Parser));
795
		$Shell->expects($this->once())->method('out');
796
 
797
		$Shell->runCommand(null, array('--help'));
798
	}
799
 
800
/**
801
 * test that runCommand will call runCommand on the task.
802
 *
803
 * @return void
804
 */
805
	public function testRunCommandHittingTask() {
806
		$Shell = $this->getMock('Shell', array('hasTask', 'startup'), array(), '', false);
807
		$task = $this->getMock('Shell', array('execute', 'runCommand'), array(), '', false);
808
		$task->expects($this->any())
809
			->method('runCommand')
810
			->with('execute', array('one', 'value'));
811
 
812
		$Shell->expects($this->once())->method('startup');
813
		$Shell->expects($this->any())
814
			->method('hasTask')
815
			->will($this->returnValue(true));
816
 
817
		$Shell->RunCommand = $task;
818
 
819
		$Shell->runCommand('run_command', array('run_command', 'one', 'value'));
820
	}
821
 
822
/**
823
 * test wrapBlock wrapping text.
824
 *
825
 * @return void
826
 */
827
	public function testWrapText() {
828
		$text = 'This is the song that never ends. This is the song that never ends. This is the song that never ends.';
829
		$result = $this->Shell->wrapText($text, 33);
830
		$expected = <<<TEXT
831
This is the song that never ends.
832
This is the song that never ends.
833
This is the song that never ends.
834
TEXT;
835
		$this->assertTextEquals($expected, $result, 'Text not wrapped.');
836
 
837
		$result = $this->Shell->wrapText($text, array('indent' => '  ', 'width' => 33));
838
		$expected = <<<TEXT
839
  This is the song that never ends.
840
  This is the song that never ends.
841
  This is the song that never ends.
842
TEXT;
843
		$this->assertTextEquals($expected, $result, 'Text not wrapped.');
844
	}
845
 
846
/**
847
 * Testing camel cased naming of tasks
848
 *
849
 * @return void
850
 */
851
	public function testShellNaming() {
852
		$this->Shell->tasks = array('TestApple');
853
		$this->Shell->loadTasks();
854
		$expected = 'TestApple';
855
		$this->assertEquals($expected, $this->Shell->TestApple->name);
856
	}
857
 
858
/**
859
 * Test that option parsers are created with the correct name/command.
860
 *
861
 * @return void
862
 */
863
	public function testGetOptionParser() {
864
		$this->Shell->name = 'test';
865
		$this->Shell->plugin = 'plugin';
866
		$parser = $this->Shell->getOptionParser();
867
 
868
		$this->assertEquals('plugin.test', $parser->command());
869
	}
870
 
871
/**
872
 * Test file and console and logging
873
 *
874
 * @return void
875
 */
876
	public function testFileAndConsoleLogging() {
877
		// file logging
878
		$this->Shell->log_something();
879
		$this->assertTrue(file_exists(LOGS . 'error.log'));
880
 
881
		unlink(LOGS . 'error.log');
882
		$this->assertFalse(file_exists(LOGS . 'error.log'));
883
 
884
		// both file and console logging
885
		require_once CORE_TEST_CASES . DS . 'Log' . DS . 'Engine' . DS . 'ConsoleLogTest.php';
886
		$mock = $this->getMock('ConsoleLog', array('write'), array(
887
			array('types' => 'error'),
888
		));
889
		TestCakeLog::config('console', array(
890
			'engine' => 'Console',
891
			'stream' => 'php://stderr',
892
			));
893
		TestCakeLog::replace('console', $mock);
894
		$mock->expects($this->once())
895
			->method('write')
896
			->with('error', $this->Shell->testMessage);
897
		$this->Shell->log_something();
898
		$this->assertTrue(file_exists(LOGS . 'error.log'));
899
		$contents = file_get_contents(LOGS . 'error.log');
900
		$this->assertContains($this->Shell->testMessage, $contents);
901
	}
902
 
903
/**
904
 * Tests that _useLogger works properly
905
 *
906
 * @return void
907
 */
908
	public function testProtectedUseLogger() {
909
		CakeLog::drop('stdout');
910
		CakeLog::drop('stderr');
911
		$this->Shell->useLogger(true);
912
		$this->assertNotEmpty(CakeLog::stream('stdout'));
913
		$this->assertNotEmpty(CakeLog::stream('stderr'));
914
		$this->Shell->useLogger(false);
915
		$this->assertFalse(CakeLog::stream('stdout'));
916
		$this->assertFalse(CakeLog::stream('stderr'));
917
	}
918
 
919
/**
920
 * Test file and console and logging quiet output
921
 *
922
 * @return void
923
 */
924
	public function testQuietLog() {
925
		$output = $this->getMock('ConsoleOutput', array(), array(), '', false);
926
		$error = $this->getMock('ConsoleOutput', array(), array(), '', false);
927
		$in = $this->getMock('ConsoleInput', array(), array(), '', false);
928
		$this->Shell = $this->getMock('ShellTestShell', array('_useLogger'), array($output, $error, $in));
929
		$this->Shell->expects($this->once())->method('_useLogger')->with(false);
930
		$this->Shell->runCommand('foo', array('--quiet'));
931
	}
932
 
933
}