1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Blitz PHP framework. |
5
|
|
|
* |
6
|
|
|
* (c) 2022 Dimitri Sitchet Tomkeu <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace BlitzPHP\Spec; |
13
|
|
|
|
14
|
|
|
use BlitzPHP\Facades\Fs; |
15
|
|
|
use php_user_filter; |
16
|
|
|
use ReturnTypeWillChange; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Pour tester la sortie de la console. |
20
|
|
|
*/ |
21
|
|
|
class CliOutputHelper |
22
|
|
|
{ |
23
|
|
|
protected static $ou = TEMP_PATH . 'output.test'; |
24
|
|
|
|
25
|
|
|
public static function setUpBeforeClass(): void |
26
|
|
|
{ |
27
|
|
|
if (! is_dir($dirname = pathinfo(static::$ou, PATHINFO_DIRNAME))) { |
|
|
|
|
28
|
2 |
|
mkdir($dirname, 0o777, true); |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
// Thanks: https://stackoverflow.com/a/39785995 |
32
|
22 |
|
stream_filter_register('intercept', StreamInterceptor::class); |
33
|
22 |
|
stream_filter_append(\STDOUT, 'intercept'); |
34
|
22 |
|
stream_filter_append(\STDERR, 'intercept'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public static function setUp(): void |
38
|
|
|
{ |
39
|
22 |
|
ob_start(); |
40
|
22 |
|
StreamInterceptor::$buffer = ''; |
41
|
22 |
|
file_put_contents(static::$ou, '', LOCK_EX); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public static function tearDown(): void |
45
|
|
|
{ |
46
|
22 |
|
ob_end_clean(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public static function tearDownAfterClass(): void |
50
|
|
|
{ |
51
|
|
|
// Make sure we clean up after ourselves: |
52
|
|
|
if (is_dir($dirname = pathinfo(static::$ou, PATHINFO_DIRNAME))) { |
|
|
|
|
53
|
22 |
|
Fs::cleanDirectory($dirname); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public static function buffer() |
58
|
|
|
{ |
59
|
22 |
|
return StreamInterceptor::$buffer ?: file_get_contents(static::$ou); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
class StreamInterceptor extends php_user_filter |
64
|
|
|
{ |
65
|
|
|
public static $buffer = ''; |
66
|
|
|
|
67
|
|
|
#[ReturnTypeWillChange] |
68
|
|
|
public function filter($in, $out, &$consumed, $closing): int |
69
|
|
|
{ |
70
|
|
|
while ($bucket = stream_bucket_make_writeable($in)) { |
71
|
22 |
|
static::$buffer .= $bucket->data; |
72
|
|
|
} |
73
|
|
|
|
74
|
22 |
|
return PSFS_PASS_ON; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|