• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

codeigniter4 / CodeIgniter4 / 12704370970

10 Jan 2025 06:18AM UTC coverage: 84.454%. Remained the same
12704370970

Pull #9395

github

web-flow
Merge f916c0a32 into 708fb6d70
Pull Request #9395: chore: add more trailing commas in more places

337 of 397 new or added lines in 117 files covered. (84.89%)

1 existing line in 1 file now uncovered.

20464 of 24231 relevant lines covered (84.45%)

189.67 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

60.53
/system/Test/Mock/MockInputOutput.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter\Test\Mock;
15

16
use CodeIgniter\CLI\InputOutput;
17
use CodeIgniter\Test\Filters\CITestStreamFilter;
18
use CodeIgniter\Test\PhpStreamWrapper;
19
use InvalidArgumentException;
20
use LogicException;
21

22
final class MockInputOutput extends InputOutput
23
{
24
    /**
25
     * String to be entered by the user.
26
     *
27
     * @var list<string>
28
     */
29
    private array $inputs = [];
30

31
    /**
32
     * Output lines.
33
     *
34
     * @var         array<int, string>
35
     * @phpstan-var list<string>
36
     */
37
    private array $outputs = [];
38

39
    /**
40
     * Sets user inputs.
41
     *
42
     * @param         array<int, string> $inputs
43
     * @phpstan-param list<string>       $inputs
44
     */
45
    public function setInputs(array $inputs): void
46
    {
47
        $this->inputs = $inputs;
1✔
48
    }
49

50
    /**
51
     * Gets the item from the output array.
52
     *
53
     * @param int|null $index The output array index. If null, returns all output
54
     *                        string. If negative int, returns the last $index-th
55
     *                        item.
56
     */
57
    public function getOutput(?int $index = null): string
58
    {
59
        if ($index === null) {
2✔
60
            return implode('', $this->outputs);
2✔
61
        }
62

63
        if (array_key_exists($index, $this->outputs)) {
×
64
            return $this->outputs[$index];
×
65
        }
66

67
        if ($index < 0) {
×
68
            $i = count($this->outputs) + $index;
×
69

70
            if (array_key_exists($i, $this->outputs)) {
×
71
                return $this->outputs[$i];
×
72
            }
73
        }
74

75
        throw new InvalidArgumentException(
×
76
            'No such index in output: ' . $index . ', the last index is: '
×
NEW
77
            . (count($this->outputs) - 1)
×
78
        );
×
79
    }
80

81
    /**
82
     * Returns the outputs array.
83
     */
84
    public function getOutputs(): array
85
    {
86
        return $this->outputs;
×
87
    }
88

89
    private function addStreamFilters(): void
90
    {
91
        CITestStreamFilter::registration();
2✔
92
        CITestStreamFilter::addOutputFilter();
2✔
93
        CITestStreamFilter::addErrorFilter();
2✔
94
    }
95

96
    private function removeStreamFilters(): void
97
    {
98
        CITestStreamFilter::removeOutputFilter();
2✔
99
        CITestStreamFilter::removeErrorFilter();
2✔
100
    }
101

102
    public function input(?string $prefix = null): string
103
    {
104
        if ($this->inputs === []) {
1✔
105
            throw new LogicException(
×
NEW
106
                'No input data. Specifiy input data with `MockInputOutput::setInputs()`.'
×
107
            );
×
108
        }
109

110
        $input = array_shift($this->inputs);
1✔
111

112
        $this->addStreamFilters();
1✔
113

114
        PhpStreamWrapper::register();
1✔
115
        PhpStreamWrapper::setContent($input);
1✔
116

117
        $userInput       = parent::input($prefix);
1✔
118
        $this->outputs[] = CITestStreamFilter::$buffer . $input . PHP_EOL;
1✔
119

120
        PhpStreamWrapper::restore();
1✔
121

122
        $this->removeStreamFilters();
1✔
123

124
        if ($input !== $userInput) {
1✔
125
            throw new LogicException($input . '!==' . $userInput);
×
126
        }
127

128
        return $input;
1✔
129
    }
130

131
    public function fwrite($handle, string $string): void
132
    {
133
        $this->addStreamFilters();
2✔
134

135
        parent::fwrite($handle, $string);
2✔
136
        $this->outputs[] = CITestStreamFilter::$buffer;
2✔
137

138
        $this->removeStreamFilters();
2✔
139
    }
140
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc