Skip to content

Commit

Permalink
feat: allow mocking output interface (#20)
Browse files Browse the repository at this point in the history
- required for old symfony versions
  • Loading branch information
imdhemy authored Mar 13, 2024
1 parent b8f6c71 commit 1ec95d7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected function configure(): void
public function run(InputInterface $input, OutputInterface $output): int
{
$this->input = $input;
$this->output = new SymfonyStyle($input, $output);
$this->output = StyleFactory::create($input, $output);

return parent::run($input, $output);
}
Expand Down Expand Up @@ -86,7 +86,7 @@ protected function arguments(): array
/**
* Writes a message to the output and adds a newline at the end.
*/
protected function line(string $message, string $style = null, string|int $verbosity = 'normal'): void
protected function line(string $message, ?string $style = null, string|int $verbosity = 'normal'): void
{
$styled = $style ? "<$style>$message</$style>" : $message;

Expand Down Expand Up @@ -168,12 +168,12 @@ protected function caution(string|array $message): void
$this->output->caution($message);
}

protected function ask(string $question, string $default = null, callable $validator = null): mixed
protected function ask(string $question, ?string $default = null, ?callable $validator = null): mixed
{
return $this->output->ask($question, $default, $validator);
}

protected function askHidden(string $question, callable $validator = null): mixed
protected function askHidden(string $question, ?callable $validator = null): mixed
{
return $this->output->askHidden($question, $validator);
}
Expand Down
21 changes: 21 additions & 0 deletions src/LegacySymfonyStyle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Symblaze\Console;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
* Old versions of Symfony do not add return data types to the output interface.
* This class is a workaround to allow mocking the output interface in tests.
*/
final class LegacySymfonyStyle extends SymfonyStyle
{
public function __construct(InputInterface $input)
{
parent::__construct($input, new NullOutput());
}
}
19 changes: 19 additions & 0 deletions src/StyleFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Symblaze\Console;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

final class StyleFactory
{
public static function create(InputInterface $input, OutputInterface $output): SymfonyStyle
{
return is_null($output->getFormatter()) ?
new LegacySymfonyStyle($input) :
new SymfonyStyle($input, $output);
}
}

0 comments on commit 1ec95d7

Please sign in to comment.