{"id":21194437,"url":"https://github.com/cspray/stream-buffer-intercept","last_synced_at":"2025-07-10T03:33:42.834Z","repository":{"id":143112869,"uuid":"614864191","full_name":"cspray/stream-buffer-intercept","owner":"cspray","description":"A PHP testing utility for writing unit tests that assert expectations on stream output","archived":false,"fork":false,"pushed_at":"2024-06-15T11:58:46.000Z","size":23,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-08-16T05:11:16.581Z","etag":null,"topics":["php","testing"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cspray.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-16T13:28:28.000Z","updated_at":"2024-06-15T11:58:31.000Z","dependencies_parsed_at":"2023-07-20T00:16:27.514Z","dependency_job_id":null,"html_url":"https://github.com/cspray/stream-buffer-intercept","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cspray%2Fstream-buffer-intercept","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cspray%2Fstream-buffer-intercept/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cspray%2Fstream-buffer-intercept/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cspray%2Fstream-buffer-intercept/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cspray","download_url":"https://codeload.github.com/cspray/stream-buffer-intercept/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225616382,"owners_count":17497158,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["php","testing"],"created_at":"2024-11-20T19:22:11.103Z","updated_at":"2024-11-20T19:22:11.907Z","avatar_url":"https://github.com/cspray.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Stream Buffer Intercept\n\nA PHP testing utility designed to capture stream output and facilitate writing unit tests for systems that require writing to streams.\n\n## Installation\n\n```shell\ncomposer require --dev cspray/stream-buffer-intercept\n```\n\n## Usage Guide\n\nThere 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.\n\nLet's take a look at a quick code example.\n\n```php\n\u003c?php declare(strict_types=1);\n\nnamespace Cspray\\StreamBufferDemo;\n\nuse Cspray\\StreamBufferIntercept\\Buffer;\nuse Cspray\\StreamBufferIntercept\\StreamFilter;\nuse PHPUnit\\Framework\\TestCase;\n\nclass MyLogger {\n\n    private $stdout;\n    private $stderr;\n\n    public function __construct($stdout, $stderr) {\n        $this-\u003estdout = $stdout;\n        $this-\u003estderr = $stderr;\n    }\n    \n    public function log(string $message) : void {\n        fwrite($this-\u003estdout, $message);\n    }\n    \n    public function logError(string $message) : void {\n        fwrite($this-\u003estderr, $message);\n    }\n\n}\n\nclass MyLoggerTest extends TestCase {\n\n    private Buffer $stdout;\n    \n    private Buffer $stderr;\n    \n    private MyLogger $subject;\n    \n    protected function setUp() : void{\n        StreamFilter::register();\n        $this-\u003estdout = StreamFilter::intercept(STDOUT);\n        $this-\u003estderr = StreamFilter::intercept(STDERR);\n        $this-\u003esubject = new MyLogger(STDOUT, STDERR);\n    }\n    \n    protected function tearDown() : void{\n        $this-\u003estdout-\u003estopIntercepting();\n        $this-\u003estderr-\u003estopIntercepting();\n    }\n    \n    public function testLogMessageSentToStdOutAndNotStdErr() : void {\n        $this-\u003esubject-\u003elog('My stdout output'); \n        \n        self::assertSame('My stdout output', $this-\u003estdout-\u003eoutput());\n        self::assertSame('', $this-\u003estderr-\u003eoutput());\n    }\n\n    public function testLogErrorMessageSentToStdErrAndNotStdOut() : void {\n        $this-\u003esubject-\u003elogError('My stderr output'); \n        \n        self::assertSame('My stderr output', $this-\u003estderr-\u003eoutput());\n        self::assertSame('', $this-\u003estdout-\u003eoutput());\n    }\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcspray%2Fstream-buffer-intercept","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcspray%2Fstream-buffer-intercept","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcspray%2Fstream-buffer-intercept/lists"}