PHP code example of cspray / stream-buffer-intercept
1. Go to this page and download the library: Download cspray/stream-buffer-intercept library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
cspray / stream-buffer-intercept example snippets
declare(strict_types=1);
namespace Cspray\StreamBufferDemo;
use Cspray\StreamBufferIntercept\Buffer;
use Cspray\StreamBufferIntercept\StreamFilter;
use PHPUnit\Framework\TestCase;
class MyLogger {
private $stdout;
private $stderr;
public function __construct($stdout, $stderr) {
$this->stdout = $stdout;
$this->stderr = $stderr;
}
public function log(string $message) : void {
fwrite($this->stdout, $message);
}
public function logError(string $message) : void {
fwrite($this->stderr, $message);
}
}
class MyLoggerTest extends TestCase {
private Buffer $stdout;
private Buffer $stderr;
private MyLogger $subject;
protected function setUp() : void{
StreamFilter::register();
$this->stdout = StreamFilter::intercept(STDOUT);
$this->stderr = StreamFilter::intercept(STDERR);
$this->subject = new MyLogger(STDOUT, STDERR);
}
protected function tearDown() : void{
$this->stdout->stopIntercepting();
$this->stderr->stopIntercepting();
}
public function testLogMessageSentToStdOutAndNotStdErr() : void {
$this->subject->log('My stdout output');
self::assertSame('My stdout output', $this->stdout->output());
self::assertSame('', $this->stderr->output());
}
public function testLogErrorMessageSentToStdErrAndNotStdOut() : void {
$this->subject->logError('My stderr output');
self::assertSame('My stderr output', $this->stderr->output());
self::assertSame('', $this->stdout->output());
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.