forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCITestStreamFilter.php
More file actions
107 lines (90 loc) · 2.41 KB
/
CITestStreamFilter.php
File metadata and controls
107 lines (90 loc) · 2.41 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
<?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\Filters;
use php_user_filter;
/**
* Used to capture output during unit testing, so that it can
* be used in assertions.
*/
class CITestStreamFilter extends php_user_filter
{
/**
* Buffer to capture stream content.
*
* @var string
*/
public static $buffer = '';
protected static bool $registered = false;
/**
* @var resource|null
*/
private static $err;
/**
* @var resource|null
*/
private static $out;
/**
* This method is called whenever data is read from or written to the
* attached stream (such as with fread() or fwrite()).
*
* @param resource $in
* @param resource $out
* @param int $consumed
* @param bool $closing
*
* @param-out int $consumed
*/
public function filter($in, $out, &$consumed, $closing): int
{
while ($bucket = stream_bucket_make_writeable($in)) {
static::$buffer .= $bucket->data;
$consumed += (int) $bucket->datalen;
}
return PSFS_PASS_ON;
}
public static function registration(): void
{
if (! static::$registered) {
static::$registered = stream_filter_register('CITestStreamFilter', self::class); // @codeCoverageIgnore
}
static::$buffer = '';
}
public static function addErrorFilter(): void
{
self::removeFilter(self::$err);
self::$err = stream_filter_append(STDERR, 'CITestStreamFilter');
}
public static function addOutputFilter(): void
{
self::removeFilter(self::$out);
self::$out = stream_filter_append(STDOUT, 'CITestStreamFilter');
}
public static function removeErrorFilter(): void
{
self::removeFilter(self::$err);
}
public static function removeOutputFilter(): void
{
self::removeFilter(self::$out);
}
/**
* @param resource|null $stream
*
* @param-out null $stream
*/
protected static function removeFilter(&$stream): void
{
if ($stream !== null) {
stream_filter_remove($stream);
$stream = null;
}
}
}