Skip to content

Commit e52d27f

Browse files
committed
Initial commit
0 parents  commit e52d27f

17 files changed

Lines changed: 2264 additions & 0 deletions

.gitattributes

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.github
2+
.gitignore
3+
phpunit.xml
4+
tests

.github/workflows/ci.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: 'Unit Tests'
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
continuous-integration:
11+
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- uses: actions/checkout@v2
16+
- name: Composer
17+
uses: php-actions/composer@v6
18+
- name: Tests
19+
uses: php-actions/phpunit@v3
20+
with:
21+
version: 10.0

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor
2+
.phpunit.cache
3+
.phpunit.result.cache

LICENSE

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright 2023 Charles Sprayberry
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
4+
files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy,
5+
modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
6+
Software is furnished to do so, subject to the following conditions:
7+
8+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9+
10+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
11+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
12+
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
13+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
```

composer.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "cspray/stream-buffer-intercept",
3+
"description": "A test utility to help capture output sent to stream resources.",
4+
"license": ["MIT"],
5+
"require": {
6+
"php": "^8.1"
7+
},
8+
"require-dev": {
9+
"phpunit/phpunit": "^10.0"
10+
},
11+
"autoload": {
12+
"psr-4": {
13+
"Cspray\\StreamBufferIntercept\\": "src"
14+
}
15+
},
16+
"autoload-dev": {
17+
"psr-4": {
18+
"Cspray\\StreamBufferIntercept\\Test\\": "tests"
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)