Skip to content

Instantly share code, notes, and snippets.

@alfredbez
Last active December 6, 2022 13:06
Show Gist options
  • Save alfredbez/07b246046c7823a17557c844f21ae989 to your computer and use it in GitHub Desktop.
Save alfredbez/07b246046c7823a17557c844f21ae989 to your computer and use it in GitHub Desktop.
Testing stdout output with PHPUnit
<?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;
}
}
<?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