cspray/assert-throws 问题修复 & 功能扩展

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

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

cspray/assert-throws

Composer 安装命令:

composer require cspray/assert-throws

包简介

Assertions for PHPUnit when testing code that throws complex exceptions.

README 文档

README

Provides an alternative way to test code that throws exceptions using PHPUnit 10 or 11. The primary benefit being access to the thrown Exception for further inspection. This might prove useful when you need to check for details of an exception that aren't available to be expected in PHPUnit. The below example acts as a Quick Start, for more details check out the Detailed Guide below.

<?php declare(strict_types=1);

use Cspray\AssertThrows\ThrowableAssert;
use PHPUnit\Framework\TestCase;

final class MyTest extends TestCase {
    public function testExceptionPreviousInstanceOf() : void {
        $throwable = ThrowableAssert::assertThrows(
            static fn() => throw new RuntimeException(previous: new BadMethodCallException())
        );
        
        self::assertInstanceOf(BadMethodCallException::class, $throwable->getPrevious());
    }
}

Installation

Composer is the only supported method to install this package.

composer require --dev cspray/assert-throws

Detailed Guide

PHPUnit provides a reasonable way to test for expected exceptions out-of-the-box. For most situations, the PHPUnit provided methods should be sufficient. However, there are scenarios where you might need to test exception throwing code in a way not readily available in PHPUnit. Examples of this include code where you want to test the previous exception or if the exception includes domain specific information. It is in these situations that this library is most appropriate.

All the assertions provided by this library takes a callable that is expected to throw an exception. If an exception is not thrown a PHPUnit\Framework\ExpectationFailedException will be thrown resulting in a test failure. Otherwise, the thrown exception will be returned for additional assertions.

The following static methods are available on the Cspray\AssertThrows\ThrowableAssert class:

<?php

use \Cspray\AssertThrows\ThrowableAssert;

$throwable = ThrowableAssert::assertThrows(static fn() => throw new RuntimeException());

$throwable = ThrowableAssert::assertThrowsExceptionType(
    static fn() => throw new BadMethodCallException(),
    BadMethodCallException::class
);

$throwable = ThrowableAssert::assertThrowsExceptionTypeWithMessage(
    static fn() => throw new RuntimeException('My exception message'),
    RuntimeException::class,
    'My exception message'
);

In addition to the ThrowableAssert class with static methods there are global functions available:

<?php

use function Cspray\AssertThrows\assertThrows;
use function Cspray\AssertThrows\assertThrowsExceptionType;
use function Cspray\AssertThrows\assertThrowsExceptionTypeWithMessage;

$throwable = assertThrows(static fn() => throw new RuntimeException());

$throwable = assertThrowsExceptionType(
    static fn() => throw new BadMethodCallException(),
    BadMethodCallException::class
);

$throwable = assertThrowsExceptionTypeWithMessage(
    static fn() => throw new RuntimeException('My exception message'),
    RuntimeException::class,
    'My exception message'
);

As well as a trait to use in your PHPUnit\Framework\TestCase implementations:

<?php

use Cspray\AssertThrows\ThrowableAssertTestCaseMethods;
use PHPUnit\Framework\TestCase;

class MyTestCase extends TestCase {
    use ThrowableAssertTestCaseMethods;
    
    public function testAssertThrows() : void {
        $throwable = self::assertThrows(static fn() => new RuntimeException());
    }
    
    public function testAssertThrowsExceptionType() : void {
        $throwable = self::assertThrowsExceptionType(
            static fn() => throw new BadMethodCallException(),
            BadMethodCallException::class
        );
    }
    
    public function testAssertThrowsExceptionTypeWithMessage() : void {
        $throwable = self::assertThrowsExceptionTypeWithMessage(
            static fn() => throw new RuntimeException('My exception message'),
            RuntimeException::class,
            'My exception message'
        );
    }
}

Which method you use is a personal preference. Ultimately, all examples utilize the static methods available in the ThrowableAssert class.

cspray/assert-throws 适用场景与选型建议

cspray/assert-throws 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 224 次下载、GitHub Stars 达 7, 最近一次更新时间为 2024 年 05 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 cspray/assert-throws 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 cspray/assert-throws 我们能提供哪些服务?
定制开发 / 二次开发

基于 cspray/assert-throws 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 224
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 20
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-05-27