Last active
December 6, 2022 13:06
-
-
Save alfredbez/07b246046c7823a17557c844f21ae989 to your computer and use it in GitHub Desktop.
Testing stdout output with PHPUnit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Intercept extends \php_user_filter | |
{ | |
public static $cache = ''; | |
public function filter($in, $out, &$consumed, $closing) | |
{ | |
while ($bucket = stream_bucket_make_writeable($in)) { | |
self::$cache .= $bucket->data; | |
$consumed += $bucket->datalen; | |
stream_bucket_append($out, $bucket); | |
} | |
return PSFS_PASS_ON; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Monolog\Handler\StreamHandler; | |
use Monolog\Logger; | |
use Intercept; | |
use PHPUnit\Framework\TestCase; | |
class SomeTest extends TestCase | |
{ | |
public function testWritesAlsoToStdout() | |
{ | |
stream_filter_register("intercept", Intercept::class); | |
$logger = new Logger('test'); | |
$handler = new StreamHandler('php://stderr', Logger::DEBUG); | |
$logger->pushHandler($handler); | |
$logger->warning('just open that stream bro'); | |
$stderr = $handler->getStream(); | |
stream_filter_append($stderr, "intercept"); | |
$logger->warning('This is written to stderr + to stream filter'); | |
self::assertNotEmpty(Intercept::$cache); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment