forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMockInputOutput.php
More file actions
140 lines (113 loc) · 3.36 KB
/
MockInputOutput.php
File metadata and controls
140 lines (113 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Test\Mock;
use CodeIgniter\CLI\InputOutput;
use CodeIgniter\Exceptions\InvalidArgumentException;
use CodeIgniter\Exceptions\LogicException;
use CodeIgniter\Test\Filters\CITestStreamFilter;
use CodeIgniter\Test\PhpStreamWrapper;
final class MockInputOutput extends InputOutput
{
/**
* String to be entered by the user.
*
* @var list<string>
*/
private array $inputs = [];
/**
* Output lines.
*
* @var list<string>
*/
private array $outputs = [];
/**
* Sets user inputs.
*
* @param list<string> $inputs
*/
public function setInputs(array $inputs): void
{
$this->inputs = $inputs;
}
/**
* Gets the item from the output array.
*
* @param int|null $index The output array index. If null, returns all output
* string. If negative int, returns the last $index-th
* item.
*/
public function getOutput(?int $index = null): string
{
if ($index === null) {
return implode('', $this->outputs);
}
if (array_key_exists($index, $this->outputs)) {
return $this->outputs[$index];
}
if ($index < 0) {
$i = count($this->outputs) + $index;
if (array_key_exists($i, $this->outputs)) {
return $this->outputs[$i];
}
}
throw new InvalidArgumentException(
'No such index in output: ' . $index . ', the last index is: '
. (count($this->outputs) - 1),
);
}
/**
* Returns the outputs array.
*
* @return list<string>
*/
public function getOutputs(): array
{
return $this->outputs;
}
private function addStreamFilters(): void
{
CITestStreamFilter::registration();
CITestStreamFilter::addOutputFilter();
CITestStreamFilter::addErrorFilter();
}
private function removeStreamFilters(): void
{
CITestStreamFilter::removeOutputFilter();
CITestStreamFilter::removeErrorFilter();
}
public function input(?string $prefix = null): string
{
if ($this->inputs === []) {
throw new LogicException(
'No input data. Specifiy input data with `MockInputOutput::setInputs()`.',
);
}
$input = array_shift($this->inputs);
$this->addStreamFilters();
PhpStreamWrapper::register();
PhpStreamWrapper::setContent($input);
$userInput = parent::input($prefix);
$this->outputs[] = CITestStreamFilter::$buffer . $input . PHP_EOL;
PhpStreamWrapper::restore();
$this->removeStreamFilters();
if ($input !== $userInput) {
throw new LogicException($input . '!==' . $userInput);
}
return $input;
}
public function fwrite($handle, string $string): void
{
$this->addStreamFilters();
parent::fwrite($handle, $string);
$this->outputs[] = CITestStreamFilter::$buffer;
$this->removeStreamFilters();
}
}