]> git.mxchange.org Git - friendica.git/blob - tests/Util/Intercept.php
Refactoring Installation
[friendica.git] / tests / Util / Intercept.php
1 <?php
2
3 namespace Friendica\Test\Util;
4
5 use php_user_filter;
6
7 /**
8  * Output Interceptor for STDOUT to prevent outputing to the console
9  * Instead the $cache variable will get filled with the output
10  *
11  * @package Friendica\Test\Util
12  */
13 class Intercept extends php_user_filter
14 {
15         /**
16          * @var string The cache which holds the current output of STDOUT
17          */
18         public static $cache = '';
19
20         public function filter($in, $out, &$consumed, $closing)
21         {
22                 while ($bucket = stream_bucket_make_writeable($in)) {
23                         self::$cache .= $bucket->data;
24                         $consumed += $bucket->datalen;
25                         stream_bucket_append($out, $bucket);
26                 }
27                 return PSFS_FEED_ME;
28         }
29
30         /**
31          * Registers the interceptor and prevents therefore the output to STDOUT
32          */
33         public static function setUp() {
34                 stream_filter_register("intercept", Intercept::class);
35                 stream_filter_append(STDOUT, "intercept");
36                 stream_filter_append(STDERR, "intercept");
37         }
38
39         /**
40          * Resets the cache
41          */
42         public static function reset() {
43                 self::$cache = '';
44         }
45 }