Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
Buffer
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
8 / 8
10
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 write
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 read
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 colors
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 interactive
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 width
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 output
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 errorOutput
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Celema\Console;
6
7use Override;
8
9/**
10 * A terminal in memory, made for tests.
11 *
12 * It captures the output and the error output separately and answers
13 * the prompts from `$input`, one line each. Colors are off, so
14 * assertions need no escape-code stripping; the other settings let a
15 * test pretend to be an interactive terminal of a given width.
16 *
17 *     $buffer = new Buffer("yes\n");
18 *     $command(io: new Io($buffer));
19 *     $this->assertSame('Done' . PHP_EOL, $buffer->output());
20 *
21 * @api
22 */
23final class Buffer implements Terminal
24{
25    private string $output = '';
26    private string $errorOutput = '';
27
28    public function __construct(
29        private string $input = '',
30        private readonly bool $interactive = false,
31        private readonly int $width = 80,
32        private readonly bool $colors = false,
33    ) {}
34
35    #[Override]
36    public function write(string $text, bool $error = false): void
37    {
38        if ($error) {
39            $this->errorOutput .= $text;
40        } else {
41            $this->output .= $text;
42        }
43    }
44
45    #[Override]
46    public function read(bool $hidden = false): ?string
47    {
48        if ($this->input === '') {
49            return null;
50        }
51
52        $lines = explode("\n", $this->input, limit: 2);
53        $this->input = $lines[1] ?? '';
54
55        return rtrim($lines[0], characters: "\r");
56    }
57
58    #[Override]
59    public function colors(bool $error = false): bool
60    {
61        return $this->colors;
62    }
63
64    #[Override]
65    public function interactive(): bool
66    {
67        return $this->interactive;
68    }
69
70    #[Override]
71    public function width(): int
72    {
73        return $this->width;
74    }
75
76    public function output(): string
77    {
78        return $this->output;
79    }
80
81    public function errorOutput(): string
82    {
83        return $this->errorOutput;
84    }
85}