Skip to content

Commit

Permalink
Add helper methods for ConsoleIo
Browse files Browse the repository at this point in the history
These methods will allow Commands to have the same output interfaces as
shells have today. One change is that `warn` has been renamed to
`warning`. This removes a short form that to remember, and makes output
consistent with logging which also uses `warning`.

Refs #11137
  • Loading branch information
markstory committed Oct 11, 2017
1 parent 65ba3ad commit 3b5f420
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 0 deletions.
86 changes: 86 additions & 0 deletions src/Console/ConsoleIo.php
Expand Up @@ -174,6 +174,92 @@ public function out($message = '', $newlines = 1, $level = ConsoleIo::NORMAL)
return true;
}

/**
* Convenience method for out() that wraps message between <info /> tag
*
* @param string|array|null $message A string or an array of strings to output
* @param int $newlines Number of newlines to append
* @param int $level The message's output level, see above.
* @return int|bool The number of bytes returned from writing to stdout.
* @see https://book.cakephp.org/3.0/en/console-and-shells.html#Shell::out
*/
public function info($message = null, $newlines = 1, $level = Shell::NORMAL)
{
$messageType = 'info';
$message = $this->wrapMessageWithType($messageType, $message);

return $this->out($message, $newlines, $level);
}

/**
* Convenience method for err() that wraps message between <warning /> tag
*
* @param string|array|null $message A string or an array of strings to output
* @param int $newlines Number of newlines to append
* @return int|bool The number of bytes returned from writing to stderr.
* @see https://book.cakephp.org/3.0/en/console-and-shells.html#Shell::err
*/
public function warning($message = null, $newlines = 1)
{
$messageType = 'warning';
$message = $this->wrapMessageWithType($messageType, $message);

return $this->err($message, $newlines);
}

/**
* Convenience method for err() that wraps message between <error /> tag
*
* @param string|array|null $message A string or an array of strings to output
* @param int $newlines Number of newlines to append
* @return int|bool The number of bytes returned from writing to stderr.
* @see https://book.cakephp.org/3.0/en/console-and-shells.html#Shell::err
*/
public function error($message = null, $newlines = 1)
{
$messageType = 'error';
$message = $this->wrapMessageWithType($messageType, $message);

return $this->err($message, $newlines);
}

/**
* Convenience method for out() that wraps message between <success /> tag
*
* @param string|array|null $message A string or an array of strings to output
* @param int $newlines Number of newlines to append
* @param int $level The message's output level, see above.
* @return int|bool The number of bytes returned from writing to stdout.
* @see https://book.cakephp.org/3.0/en/console-and-shells.html#Shell::out
*/
public function success($message = null, $newlines = 1, $level = Shell::NORMAL)
{
$messageType = 'success';
$message = $this->wrapMessageWithType($messageType, $message);

return $this->out($message, $newlines, $level);
}

/**
* Wraps a message with a given message type, e.g. <warning>
*
* @param string $messageType The message type, e.g. "warning".
* @param string|array $message The message to wrap.
* @return array|string The message wrapped with the given message type.
*/
protected function wrapMessageWithType($messageType, $message)
{
if (is_array($message)) {
foreach ($message as $k => $v) {
$message[$k] = "<{$messageType}>{$v}</{$messageType}>";
}
} else {
$message = "<{$messageType}>{$message}</{$messageType}>";
}

return $message;
}

/**
* Overwrite some already output text.
*
Expand Down
60 changes: 60 additions & 0 deletions tests/TestCase/Console/ConsoleIoTest.php
Expand Up @@ -497,4 +497,64 @@ public function testHelper()
$this->assertInstanceOf('Cake\Console\Helper', $helper);
$helper->output(['well', 'ish']);
}

/**
* Provider for output helpers
*
* @return array
*/
public function outHelperProvider()
{
return [['info'], ['success']];
}

/**
* Provider for err helpers
*
* @return array
*/
public function errHelperProvider()
{
return [['warning'], ['error']];
}

/**
* test out helper methods
*
* @dataProvider outHelperProvider
* @return void
*/
public function testOutHelpers($method)
{
$this->out->expects($this->at(0))
->method('write')
->with("<{$method}>Just a test</{$method}>", 1);

$this->out->expects($this->at(1))
->method('write')
->with(["<{$method}>Just</{$method}>", "<{$method}>a test</{$method}>"], 1);

$this->io->{$method}('Just a test');
$this->io->{$method}(['Just', 'a test']);
}

/**
* test err helper methods
*
* @dataProvider errHelperProvider
* @return void
*/
public function testErrHelpers($method)
{
$this->err->expects($this->at(0))
->method('write')
->with("<{$method}>Just a test</{$method}>", 1);

$this->err->expects($this->at(1))
->method('write')
->with(["<{$method}>Just</{$method}>", "<{$method}>a test</{$method}>"], 1);

$this->io->{$method}('Just a test');
$this->io->{$method}(['Just', 'a test']);
}
}

0 comments on commit 3b5f420

Please sign in to comment.