cspray/stream-buffer-intercept 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

cspray/stream-buffer-intercept

最新稳定版本:0.2.1

Composer 安装命令:

composer require cspray/stream-buffer-intercept

包简介

A test utility to help capture output sent to stream resources.

README 文档

README

A PHP testing utility designed to capture stream output and facilitate writing unit tests for systems that require writing to streams.

Installation

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 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());
    }
}

统计信息

  • 总下载量: 458
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 4
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-03-16

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固