Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
100.00% |
14 / 14 |
|
100.00% |
8 / 8 |
|
100.00% |
7 / 7 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| BufferedIo | |
100.00% |
14 / 14 |
|
100.00% |
8 / 8 |
|
100.00% |
7 / 7 |
|
100.00% |
6 / 6 |
7 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
5 / 5 |
|
100.00% |
3 / 3 |
|
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| stdin | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| output | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| errorOutput | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| contents | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasColorSupport | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Celema\Console; |
| 6 | |
| 7 | use Override; |
| 8 | |
| 9 | /** |
| 10 | * An Io that captures everything in memory instead of writing to the |
| 11 | * terminal. |
| 12 | * |
| 13 | * Made for command tests: pass it to the command (or the Runner) and assert |
| 14 | * on `output()` and `errorOutput()`. Colors are always disabled, so |
| 15 | * assertions need no escape-code stripping. The `$input` string feeds |
| 16 | * `ask()` and `confirm()`, one line per prompt. |
| 17 | * |
| 18 | * @api |
| 19 | */ |
| 20 | final class BufferedIo extends Io |
| 21 | { |
| 22 | private mixed $inputBuffer = null; |
| 23 | |
| 24 | public function __construct(string $input = '') |
| 25 | { |
| 26 | parent::__construct('php://memory', 'php://memory'); |
| 27 | |
| 28 | if ($input !== '') { |
| 29 | $stream = $this->stdin(); |
| 30 | fwrite($stream, $input); |
| 31 | rewind($stream); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | #[Override] |
| 36 | protected function stdin(): mixed |
| 37 | { |
| 38 | // The parent opens its input target read-only, which php://memory |
| 39 | // honors, so the buffer needs its own read-write stream. |
| 40 | return $this->inputBuffer ??= fopen('php://memory', mode: 'w+'); |
| 41 | } |
| 42 | |
| 43 | public function output(): string |
| 44 | { |
| 45 | return $this->contents($this->stdout()); |
| 46 | } |
| 47 | |
| 48 | public function errorOutput(): string |
| 49 | { |
| 50 | return $this->contents($this->stderr()); |
| 51 | } |
| 52 | |
| 53 | private function contents(mixed $stream): string |
| 54 | { |
| 55 | $position = (int) ftell($stream); |
| 56 | rewind($stream); |
| 57 | $contents = (string) stream_get_contents($stream); |
| 58 | fseek($stream, $position); |
| 59 | |
| 60 | return $contents; |
| 61 | } |
| 62 | |
| 63 | #[Override] |
| 64 | protected function hasColorSupport(mixed $stream): bool |
| 65 | { |
| 66 | return false; |
| 67 | } |
| 68 | } |