-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from kbond/console-output
Always use `ConsoleOutput` for tests
- Loading branch information
Showing
7 changed files
with
160 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace Zenstruck\Console\Test; | ||
|
||
use Symfony\Component\Console\Input\StringInput; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @author Kevin Bond <kevinbond@gmail.com> | ||
*/ | ||
final class TestInput extends StringInput | ||
{ | ||
public function __construct(string $input, array $inputs) | ||
{ | ||
parent::__construct($input); | ||
|
||
$this->setInteractive(false); | ||
|
||
if ($inputs) { | ||
$stream = \fopen('php://memory', 'r+', false); | ||
|
||
foreach ($inputs as $value) { | ||
\fwrite($stream, $value.\PHP_EOL); | ||
} | ||
|
||
\rewind($stream); | ||
|
||
$this->setStream($stream); | ||
$this->setInteractive(true); | ||
} | ||
|
||
if (true === $this->hasParameterOption(['--no-interaction', '-n'], true)) { | ||
$this->setInteractive(false); | ||
} | ||
} | ||
|
||
public function isDecorated(): bool | ||
{ | ||
return true === $this->hasParameterOption(['--ansi'], true); | ||
} | ||
|
||
public function getVerbosity(): int | ||
{ | ||
if (true === $this->hasParameterOption(['--quiet', '-q'], true)) { | ||
return OutputInterface::VERBOSITY_QUIET; | ||
} | ||
|
||
if ($this->hasParameterOption('-vvv', true) || $this->hasParameterOption('--verbose=3', true) || 3 === $this->getParameterOption('--verbose', false, true)) { | ||
return OutputInterface::VERBOSITY_DEBUG; | ||
} | ||
|
||
if ($this->hasParameterOption('-vv', true) || $this->hasParameterOption('--verbose=2', true) || 2 === $this->getParameterOption('--verbose', false, true)) { | ||
return OutputInterface::VERBOSITY_VERY_VERBOSE; | ||
} | ||
|
||
if ($this->hasParameterOption('-v', true) || $this->hasParameterOption('--verbose=1', true) || $this->hasParameterOption('--verbose', true) || $this->getParameterOption('--verbose', false, true)) { | ||
return OutputInterface::VERBOSITY_VERBOSE; | ||
} | ||
|
||
return OutputInterface::VERBOSITY_NORMAL; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace Zenstruck\Console\Test; | ||
|
||
use Symfony\Component\Console\Output\ConsoleOutputInterface; | ||
use Symfony\Component\Console\Output\ConsoleSectionOutput; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Output\StreamOutput; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @author Kevin Bond <kevinbond@gmail.com> | ||
*/ | ||
final class TestOutput extends StreamOutput implements ConsoleOutputInterface | ||
{ | ||
private ?OutputInterface $error = null; | ||
private array $sections = []; | ||
|
||
public function __construct(bool $splitStreams, TestInput $input) | ||
{ | ||
parent::__construct( | ||
\fopen('php://memory', 'w', false), | ||
$input->getVerbosity(), | ||
$input->isDecorated() | ||
); | ||
|
||
if ($splitStreams) { | ||
$this->error = new StreamOutput(\fopen('php://memory', 'w', false)); | ||
$this->error->setFormatter($this->getFormatter()); | ||
$this->error->setVerbosity($this->getVerbosity()); | ||
$this->error->setDecorated($this->isDecorated()); | ||
} | ||
} | ||
|
||
public function getErrorOutput(): OutputInterface | ||
{ | ||
return $this->error ?? $this; | ||
} | ||
|
||
public function setErrorOutput(OutputInterface $error): void | ||
{ | ||
$this->error = $error; | ||
} | ||
|
||
public function section(): ConsoleSectionOutput | ||
{ | ||
return new ConsoleSectionOutput($this->getStream(), $this->sections, $this->getVerbosity(), $this->isDecorated(), $this->getFormatter()); | ||
} | ||
|
||
public function getDisplay(): string | ||
{ | ||
\rewind($this->getStream()); | ||
|
||
return \stream_get_contents($this->getStream()); | ||
} | ||
|
||
public function getErrorDisplay(): string | ||
{ | ||
if (!$this->error instanceof StreamOutput) { | ||
return ''; | ||
} | ||
|
||
\rewind($this->error->getStream()); | ||
|
||
return \stream_get_contents($this->error->getStream()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters