Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cspray/stream-buffer-intercept
A PHP testing utility for writing unit tests that assert expectations on stream output
https://github.com/cspray/stream-buffer-intercept
php testing
Last synced: about 1 month ago
JSON representation
A PHP testing utility for writing unit tests that assert expectations on stream output
- Host: GitHub
- URL: https://github.com/cspray/stream-buffer-intercept
- Owner: cspray
- License: mit
- Created: 2023-03-16T13:28:28.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-06-15T11:58:46.000Z (6 months ago)
- Last Synced: 2024-08-16T05:11:16.581Z (4 months ago)
- Topics: php, testing
- Language: PHP
- Homepage:
- Size: 22.5 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Stream Buffer Intercept
A PHP testing utility designed to capture stream output and facilitate writing unit tests for systems that require writing to streams.
## Installation
```shell
composer require --dev cspray/stream-buffer-intercept
```## Usage Guide
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.
Let's take a look at a quick code example.
```php
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());
}
}
```