|
| 1 | +# Stream Buffer Intercept |
| 2 | + |
| 3 | +A PHP testing utility designed to capture stream output and facilitate writing unit tests for systems that require writing to streams. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```shell |
| 8 | +composer require --dev cspray/stream-buffer-intercept |
| 9 | +``` |
| 10 | + |
| 11 | +## Usage Guide |
| 12 | + |
| 13 | +There are scenarios where you may want to unit test some piece of code that writes to a stream. An example might be where you're writing tests to confirm log messages sent to `stdout` or `stderr`. The `Cspray\StreamBufferIntercept\StreamBuffer` class allows you to easily capture output sent to these streams, and others, to easily assert your expectations. |
| 14 | + |
| 15 | +Let's take a look at a quick code example. |
| 16 | + |
| 17 | +```php |
| 18 | +<?php declare(strict_types=1); |
| 19 | + |
| 20 | +namespace Cspray\StreamBufferDemo; |
| 21 | + |
| 22 | +use Cspray\StreamBufferIntercept\BufferIdentifier;use Cspray\StreamBufferIntercept\StreamBuffer; |
| 23 | +use PHPUnit\Framework\TestCase; |
| 24 | + |
| 25 | +class MyLogger { |
| 26 | + |
| 27 | + private $stdout; |
| 28 | + private $stderr; |
| 29 | + |
| 30 | + public function __construct($stdout, $stderr) { |
| 31 | + $this->stdout = $stdout; |
| 32 | + $this->stderr = $stderr; |
| 33 | + } |
| 34 | + |
| 35 | + public function log(string $message) : void { |
| 36 | + fwrite($this->stdout, $message); |
| 37 | + } |
| 38 | + |
| 39 | + public function logError(string $message) : void { |
| 40 | + fwrite($this->stderr, $message); |
| 41 | + } |
| 42 | + |
| 43 | +} |
| 44 | + |
| 45 | +class MyLoggerTest extends TestCase { |
| 46 | + |
| 47 | + private BufferIdentifier $stdout; |
| 48 | + |
| 49 | + private BufferIdentifier $stderr; |
| 50 | + |
| 51 | + private MyLogger $subject; |
| 52 | + |
| 53 | + protected function setUp() : void{ |
| 54 | + StreamBuffer::register(); |
| 55 | + $this->stdout = StreamBuffer::intercept(STDOUT); |
| 56 | + $this->stderr = StreamBuffer::intercept(STDERR); |
| 57 | + $this->subject = new MyLogger(STDOUT, STDERR); |
| 58 | + } |
| 59 | + |
| 60 | + protected function tearDown() : void{ |
| 61 | + StreamBuffer::stopIntercepting($this->stdout); |
| 62 | + StreamBuffer::stopIntercepting($this->stderr); |
| 63 | + } |
| 64 | + |
| 65 | + public function testLogMessageSentToStdOutAndNotStdErr() : void { |
| 66 | + $this->subject->log('My stdout output'); |
| 67 | + |
| 68 | + self::assertSame('My stdout output', StreamBuffer::output($this->stdout)); |
| 69 | + self::assertSame('', StreamBuffer::output($this->stderr)); |
| 70 | + } |
| 71 | + |
| 72 | + public function testLogErrorMessageSentToStdErrAndNotStdOut() : void { |
| 73 | + $this->subject->logError('My stderr output'); |
| 74 | + |
| 75 | + self::assertSame('My stderr output', StreamBuffer::output($this->stderr)); |
| 76 | + self::assertSame('', StreamBuffer::output($this->stdout)); |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
0 commit comments