-
Notifications
You must be signed in to change notification settings - Fork 0
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 #2 from WonderNetwork/feature/fingers-crossed-handler
`FindersCrossedHandler`
- Loading branch information
Showing
4 changed files
with
194 additions
and
0 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 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,42 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace WonderNetwork\SlimKernel\Cli; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Throwable; | ||
|
||
final class FingersCrossedHandler { | ||
private InputInterface $input; | ||
private OutputInterface $output; | ||
|
||
public static function of(InputInterface $input, OutputInterface $output): self { | ||
return new self($input, $output); | ||
} | ||
|
||
private function __construct(InputInterface $input, OutputInterface $output) { | ||
$this->input = $input; | ||
$this->output = $output; | ||
} | ||
|
||
/** | ||
* @param callable(InputParams $input, FingersCrossedOutput $output):int $closure | ||
* @return int | ||
* @throws Throwable | ||
*/ | ||
public function run(callable $closure): int { | ||
$output = new FingersCrossedOutput($this->output); | ||
try { | ||
$result = $closure(InputParams::ofInput($this->input), $output); | ||
if (Command::SUCCESS !== $result) { | ||
$output->flush(); | ||
} | ||
} catch (Throwable $e) { | ||
$output->flush(); | ||
throw $e; | ||
} | ||
return $result; | ||
} | ||
} |
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,33 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace WonderNetwork\SlimKernel\Cli; | ||
|
||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
final class FingersCrossedOutput { | ||
private OutputInterface $output; | ||
/** @var string[] */ | ||
private array $messages = []; | ||
|
||
public function __construct(OutputInterface $output) { | ||
$this->output = $output; | ||
} | ||
|
||
public function writeln(string $message): void { | ||
if ($this->isBuffering()) { | ||
$this->messages[] = $message; | ||
return; | ||
} | ||
$this->flush(); | ||
$this->output->writeln($message); | ||
} | ||
|
||
public function flush(): void { | ||
$this->output->writeln(array_slice($this->messages, 0)); | ||
} | ||
|
||
public function isBuffering(): bool { | ||
return false === $this->output->isVerbose(); | ||
} | ||
} |
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,92 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace WonderNetwork\SlimKernel\Cli; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use RuntimeException; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\ArrayInput; | ||
use Symfony\Component\Console\Output\BufferedOutput; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Throwable; | ||
|
||
class FingersCrossedHandlerTest extends TestCase { | ||
private BufferedOutput $spy; | ||
private FingersCrossedHandler $sut; | ||
|
||
protected function setUp(): void { | ||
$this->spy = new BufferedOutput(); | ||
$this->sut = FingersCrossedHandler::of(new ArrayInput([]), $this->spy); | ||
} | ||
|
||
public function testHidesNormalOutputOnSuccess(): void { | ||
$result = $this->sut->run( | ||
function (InputParams $input, FingersCrossedOutput $output) { | ||
$output->writeln("Hello world"); | ||
return Command::SUCCESS; | ||
}, | ||
); | ||
self::assertSame(0, $result); | ||
self::assertSame("", $this->spy->fetch()); | ||
} | ||
|
||
public function testDisplaysAllOutputOnFailure(): void { | ||
$result = $this->sut->run( | ||
function (InputParams $input, FingersCrossedOutput $output) { | ||
$output->writeln("Hello world"); | ||
return Command::FAILURE; | ||
}, | ||
); | ||
self::assertSame(1, $result); | ||
self::assertSame("Hello world\n", $this->spy->fetch()); | ||
} | ||
|
||
public function testCorrectlyFormatsAllOutput(): void { | ||
$this->sut->run( | ||
function (InputParams $input, FingersCrossedOutput $output) { | ||
$output->writeln("Alpha"); | ||
$output->writeln("Bravo"); | ||
return Command::FAILURE; | ||
}, | ||
); | ||
self::assertSame("Alpha\nBravo\n", $this->spy->fetch()); | ||
} | ||
|
||
public function testDisplaysAllOutputWhenThrows(): void { | ||
$e = null; | ||
try { | ||
$this->sut->run( | ||
function (InputParams $input, FingersCrossedOutput $output) { | ||
$output->writeln("Hello world"); | ||
throw new RuntimeException(); | ||
}, | ||
); | ||
} catch (Throwable $e) {} | ||
self::assertSame("Hello world\n", $this->spy->fetch()); | ||
self::assertInstanceOf(Throwable::class, $e); | ||
} | ||
|
||
public function testDisplaysAllOutputWhenVerbose(): void { | ||
$this->spy->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); | ||
$this->sut->run( | ||
function (InputParams $input, FingersCrossedOutput $output) { | ||
$output->writeln("Hello world"); | ||
return Command::SUCCESS; | ||
}, | ||
); | ||
self::assertSame("Hello world\n", $this->spy->fetch()); | ||
} | ||
|
||
public function testOutputIsInOrderWhenVerbositiIsIncreased(): void { | ||
$this->sut->run( | ||
function (InputParams $input, FingersCrossedOutput $output) { | ||
$output->writeln("Alpha"); | ||
$this->spy->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); | ||
$output->writeln("Bravo"); | ||
return Command::SUCCESS; | ||
}, | ||
); | ||
self::assertSame("Alpha\nBravo\n", $this->spy->fetch()); | ||
} | ||
} |