diff --git a/cake/console/libs/shell.php b/cake/console/libs/shell.php index 01855bceb..a0a194d70 100644 --- a/cake/console/libs/shell.php +++ b/cake/console/libs/shell.php @@ -338,53 +338,59 @@ class Shell extends Object { } } /** - * Outputs to the stdout filehandle. + * Outputs a single or multiple messages to stdout. * - * @param string $string String to output. - * @param boolean $newline If true, the outputs gets an added newline. + * @param mixed $message A string or a an array of strings to output + * @param mixed $after Appended to message, if true a newline is used * @access public */ - function out($string, $newline = true) { - if (is_array($string)) { - $str = ''; - foreach ($string as $message) { - $str .= $message ."\n"; - } - $string = $str; + function out($message, $after = true) { + if (is_array($message)) { + $message = implode($this->nl(), $message); } - return $this->Dispatch->stdout($string, $newline); + $this->Dispatch->stdout($message . $this->nl($after), false); } /** - * Outputs to the stderr filehandle. + * Outputs a single or multiple error messages to stderr. * - * @param string $string Error text to output. - * @param boolean $newline If true, the outputs gets an added newline. + * @param mixed $message A string or a an array of strings to output + * @param mixed $after Appended to message, if true a newline is used * @access public */ - function err($string, $newline = true) { - if (is_array($string)) { - $str = ''; - foreach ($string as $message) { - $str .= $message ."\n"; - } - $string = $str; + function err($message, $after = true) { + if (is_array($message)) { + $message = implode($this->nl(), $message); } - return $this->Dispatch->stderr($string . ($newline ? "\n" : '')); + $this->Dispatch->stderr($message . $this->nl($after)); + } +/** + * Returns a single or multiple linefeeds sequences. + * + * @param mixed $format If true returns a linefeed sequence, if false null, + * if a string is given that is returned, + * if an integer is given it is used as a multiplier to return multiple linefeed sequences + * @access public + * @return string + */ + function nl($format = true) { + if (is_string($format)) { + return $format; + } + if (is_integer($format)) { + return str_repeat("\n", $format); + } + return $format ? "\n" : null; } /** * Outputs a series of minus characters to the standard output, acts as a visual separator. * - * @param boolean $newline If true, the outputs gets an added newline. + * @param mixed $surround If true, the outputs gets surrounded by newlines. * @access public */ - function hr($newline = false) { - if ($newline) { - $this->out("\n"); - } + function hr($surround = false) { + $this->out(null, $surround); $this->out('---------------------------------------------------------------'); - if ($newline) { - $this->out("\n"); - } + $this->out(null, $surround); } /** * Displays a formatted error message diff --git a/cake/tests/cases/console/libs/shell.test.php b/cake/tests/cases/console/libs/shell.test.php index 8d03541fc..6144842a4 100644 --- a/cake/tests/cases/console/libs/shell.test.php +++ b/cake/tests/cases/console/libs/shell.test.php @@ -48,6 +48,23 @@ Mock::generatePartial( * @subpackage cake.tests.cases.console.libs */ class TestShell extends Shell { +/** + * stopped property + * + * @var integer + * @access public + */ + var $stopped; +/** + * stop method + * + * @param integer $status + * @access protected + * @return void + */ + function _stop($status = 0) { + $this->stopped = $status; + } } /** * TestAppleTask class @@ -147,32 +164,6 @@ class ShellTest extends CakeTestCase { Configure::write('pluginPaths', $_back['pluginPaths']); Configure::write('modelPaths', $_back['modelPaths']); } -/** - * testOut method - * - * @return void - * @access public - */ - function testOut() { - $this->Shell->Dispatch->expectAt(0, 'stdout', array('Just a test', true)); - $this->Shell->out('Just a test'); - - $this->Shell->Dispatch->expectAt(1, 'stdout', array("Just\na\ntest\n", true)); - $this->Shell->out(array('Just', 'a', 'test')); - } -/** - * testErr method - * - * @return void - * @access public - */ - function testErr() { - $this->Shell->Dispatch->expectAt(0, 'stderr', array("Just a test\n")); - $this->Shell->err('Just a test'); - - $this->Shell->Dispatch->expectAt(1, 'stderr', array("Just\na\ntest\n\n")); - $this->Shell->err(array('Just', 'a', 'test')); - } /** * testIn method * @@ -210,6 +201,94 @@ class ShellTest extends CakeTestCase { $result = $this->Shell->in('Just a test?', 'y/n', 'n'); $this->assertEqual($result, 'n'); } +/** + * testOut method + * + * @return void + * @access public + */ + function testOut() { + $this->Shell->Dispatch->expectAt(0, 'stdout', array("Just a test\n", false)); + $this->Shell->out('Just a test'); + + $this->Shell->Dispatch->expectAt(1, 'stdout', array("Just\na\ntest\n", false)); + $this->Shell->out(array('Just', 'a', 'test')); + + $this->Shell->Dispatch->expectAt(2, 'stdout', array("Just\na\ntest\n\n", false)); + $this->Shell->out(array('Just', 'a', 'test'), 2); + } +/** + * testErr method + * + * @return void + * @access public + */ + function testErr() { + $this->Shell->Dispatch->expectAt(0, 'stderr', array("Just a test\n")); + $this->Shell->err('Just a test'); + + $this->Shell->Dispatch->expectAt(1, 'stderr', array("Just\na\ntest\n")); + $this->Shell->err(array('Just', 'a', 'test')); + + $this->Shell->Dispatch->expectAt(2, 'stderr', array("Just\na\ntest\n\n")); + $this->Shell->err(array('Just', 'a', 'test'), 2); + } +/** + * testNl + * + * @access public + * @return void + */ + function testNl() { + $this->assertEqual($this->Shell->nl(), "\n"); + $this->assertEqual($this->Shell->nl(true), "\n"); + $this->assertEqual($this->Shell->nl(false), ""); + $this->assertEqual($this->Shell->nl(2), "\n\n"); + $this->assertEqual($this->Shell->nl(1), "\n"); + $this->assertEqual($this->Shell->nl("custom\n"), "custom\n"); + } +/** + * testHr + * + * @access public + * @return void + */ + function testHr() { + $bar = '---------------------------------------------------------------'; + + $this->Shell->Dispatch->expectAt(0, 'stdout', array('', false)); + $this->Shell->Dispatch->expectAt(1, 'stdout', array($bar . "\n", false)); + $this->Shell->Dispatch->expectAt(2, 'stdout', array('', false)); + $this->Shell->hr(); + + $this->Shell->Dispatch->expectAt(3, 'stdout', array("\n", false)); + $this->Shell->Dispatch->expectAt(4, 'stdout', array($bar . "\n", false)); + $this->Shell->Dispatch->expectAt(5, 'stdout', array("\n", false)); + $this->Shell->hr(true); + + $this->Shell->Dispatch->expectAt(3, 'stdout', array("\n\n", false)); + $this->Shell->Dispatch->expectAt(4, 'stdout', array($bar . "\n", false)); + $this->Shell->Dispatch->expectAt(5, 'stdout', array("\n\n", false)); + $this->Shell->hr(2); + } +/** + * testError + * + * @access public + * @return void + */ + function testError() { + $this->Shell->Dispatch->expectAt(0, 'stderr', array("Error: Foo Not Found\n")); + $this->Shell->error('Foo Not Found'); + $this->assertIdentical($this->Shell->stopped, 1); + + $this->Shell->stopped = null; + + $this->Shell->Dispatch->expectAt(1, 'stderr', array("Error: Foo Not Found\n")); + $this->Shell->Dispatch->expectAt(2, 'stderr', array("Searched all...\n")); + $this->Shell->error('Foo Not Found', 'Searched all...'); + $this->assertIdentical($this->Shell->stopped, 1); + } /** * testLoadTasks method *