Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
SmetDenis committed Apr 11, 2022
1 parent b31a099 commit ee1b982
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/Helper.php
Expand Up @@ -37,6 +37,7 @@ class Helper
public const VERB_V = 'v';
public const VERB_VV = 'vv';
public const VERB_VVV = 'vvv';
public const VERB_E = 'e';

public const VERB_DEBUG = 'debug';
public const VERB_INFO = 'info';
Expand Down Expand Up @@ -242,12 +243,15 @@ public function _($messages, string $verboseLevel = self::VERB_DEFAULT): void
$this->_('<yellow>Warning:</yellow> ' . $messages, self::VERB_VV);
} elseif ($verboseLevel === self::VERB_INFO) {
$this->_('<blue>Info:</blue> ' . $messages, self::VERB_V);
} elseif ($verboseLevel === self::VERB_E) {
$this->outputHasErrors = true;
$this->errOutput->writeln($profilePrefix . $messages, $vNormal);
} elseif ($verboseLevel === self::VERB_ERROR) {
$this->outputHasErrors = true;
$this->errOutput->writeln($profilePrefix . '<red-bg>Error:</red-bg> ' . $messages, $vNormal);
} elseif ($verboseLevel === self::VERB_EXCEPTION) {
$this->outputHasErrors = true;
$this->errOutput->writeln($profilePrefix . '<red-bg>Exception:</red-bg> ' . $messages, $vNormal);
$this->errOutput->writeln($profilePrefix . '<red-bg>Muted Exception:</red-bg> ' . $messages, $vNormal);
} else {
throw new Exception("Undefined verbose level: \"{$verboseLevel}\"");
}
Expand Down
49 changes: 44 additions & 5 deletions tests/Cli/CliOutputTest.php
Expand Up @@ -26,20 +26,31 @@ class CliOutputTest extends PHPUnit
public function testNormal()
{
[$exitCode, $stdOut, $errOut] = Helper::executeReal('test:output');
isSame('Error: Message', $errOut);
isSame(implode(PHP_EOL, [
'Error: Message',
'Error (e)',
'Error: Error (error)',
'Muted Exception: Error (exception)',
]), $errOut);
isSame(0, $exitCode);

isSame(implode("\n", [
'Normal 1',
'Normal 2',
'Quiet -q',
'Legacy Output: Legacy message',
]), $stdOut);
}

public function testInfo()
{
[$exitCode, $stdOut, $errOut] = Helper::executeReal('test:output', ['v' => null]);
isSame('Error: Message', $errOut);
isSame(implode(PHP_EOL, [
'Error: Message',
'Error (e)',
'Error: Error (error)',
'Muted Exception: Error (exception)',
]), $errOut);
isSame(0, $exitCode);

isSame(implode("\n", [
Expand All @@ -48,6 +59,7 @@ public function testInfo()
'Info1 -v',
'Info: Info2 -v',
'Quiet -q',
'Legacy Output: Legacy message',
]), $stdOut);

isSame(
Expand All @@ -59,7 +71,12 @@ public function testInfo()
public function testVerbose()
{
[$exitCode, $stdOut, $errOut] = Helper::executeReal('test:output', ['-vv' => null]);
isSame('Error: Message', $errOut);
isSame(implode(PHP_EOL, [
'Error: Message',
'Error (e)',
'Error: Error (error)',
'Muted Exception: Error (exception)',
]), $errOut);
isSame(0, $exitCode);

isSame(implode("\n", [
Expand All @@ -73,13 +90,19 @@ public function testVerbose()
'Warning: Verbose2 -vv',

'Quiet -q',
'Legacy Output: Legacy message',
]), $stdOut);
}

public function testDebug()
{
[$exitCode, $stdOut, $errOut] = Helper::executeReal('test:output', ['-vvv' => null]);
isSame('Error: Message', $errOut);
isSame(implode(PHP_EOL, [
'Error: Message',
'Error (e)',
'Error: Error (error)',
'Muted Exception: Error (exception)',
]), $errOut);
isSame(0, $exitCode);

isSame(implode("\n", [
Expand All @@ -97,6 +120,7 @@ public function testDebug()
'Debug: Message #2 -vvv',

'Quiet -q',
'Legacy Output: Legacy message',
'Debug: Exit Code is "0"',
]), $stdOut);
}
Expand Down Expand Up @@ -130,7 +154,11 @@ public function testStdoutOnly()
'Normal 1',
'Normal 2',
'Error: Message',
'Error (e)',
'Error: Error (error)',
'Muted Exception: Error (exception)',
'Quiet -q',
'Legacy Output: Legacy message'
]), $stdOut);

// Redirect exception messsage
Expand All @@ -145,7 +173,11 @@ public function testStdoutOnly()
'Normal 1',
'Normal 2',
'Error: Message',
'Error (e)',
'Error: Error (error)',
'Muted Exception: Error (exception)',
'Quiet -q',
'Legacy Output: Legacy message',
]), $stdOut);

// No redirect exception messsage
Expand All @@ -160,20 +192,27 @@ public function testStdoutOnly()
'Normal 1',
'Normal 2',
'Quiet -q',
'Legacy Output: Legacy message',
]), $stdOut);
}

public function testStrict()
{
// Redirect common message
[$exitCode, $stdOut, $errOut] = Helper::executeReal('test:output', ['strict' => null]);
isSame('Error: Message', $errOut);
isSame(implode(PHP_EOL, [
'Error: Message',
'Error (e)',
'Error: Error (error)',
'Muted Exception: Error (exception)',
]), $errOut);
isSame(1, $exitCode);

isSame(implode("\n", [
'Normal 1',
'Normal 2',
'Quiet -q',
'Legacy Output: Legacy message',
]), $stdOut);
}

Expand Down
6 changes: 6 additions & 0 deletions tests/fake-app/Commands/TestOutput.php
Expand Up @@ -44,6 +44,8 @@ protected function configure(): void
*/
protected function executeAction(): int
{
echo "Legacy message";

$this->_(['Normal 1', 'Normal 2']);
$this->_('Message', Helper::VERB_ERROR);

Expand All @@ -59,6 +61,10 @@ protected function executeAction(): int
'Message #2 -vvv'
], Helper::VERB_DEBUG);

$this->_('Error (e)', Helper::VERB_E);
$this->_('Error (error)', Helper::VERB_ERROR);
$this->_('Error (exception)', Helper::VERB_EXCEPTION);

$this->_('Quiet -q', Helper::VERB_QUIET);

if ($exception = $this->getOptString('exception')) {
Expand Down

0 comments on commit ee1b982

Please sign in to comment.