stein197/phpunit-extended 问题修复 & 功能扩展

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

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

stein197/phpunit-extended

Composer 安装命令:

composer require stein197/phpunit-extended

包简介

Extended PHPUnit assertions

README 文档

README

PHPUnit Extended

This package is a small extension to PHPUnit that extends its assertions. The library provides assertions for PSR-7 HTTP responses, XML/HTML documents and JSON structures. The extension uses the following libraries to make it work:

  • DOM: the new native PHP 8.4 DOM library to make assertions against XML/HTML DOM structures
  • PSR-7: HTTP message interfaces to make assertions against HTTP server response objects
  • JSONPath: JSONPath library to make assertions against JSON structures

Installation

$ composer require --dev stein197/phpunit-extended

Usage

Let's say you test a an HTTP response:

namespace Test;

use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Stein197\PHPUnit\ExtendedTextCase; // Trait that extends the basic PHPUnit assertion functionality and implements the ExtendedTestCaseInterface interface
use Stein197\PHPUnit\ExtendedTestCaseInterface;

// Your test class
final class ResponseTest extends TestCase implements ExtendedTestCaseInterface {

	use ExtendedTextCase; // Include the extended assertions

	#[Test]
	public function testResponse(): void {
		// HTTP response assertions
		$response = $this->request('/home'); // Anything that returns a PSR-7 response object
		$response = $this->createResponseAssertion($response); // Wrap the PSR-7 response in an assertion object
		$response->assertOk(); // Assert the status code is 200
		$response->assertHeaderEquals('Content-Type', 'text/html'); // Assert that there is a header 'Content-Type' with the value 'text/html'
		// ...

		// XML/HTML DOM assertions
		$document = $response->document(); // Return a DocumentAssert assertion object containing the response body
		$document->query('#main h1')->assertTextEquals('Hello, World!'); // Query elements by query selector an assert
		$document->xpath('//*[@id = "main"]//h1')->assertTextEquals('Hello, World!'); // The the same but using XPath
		// ...

		// JSON assertions
		$json = $response->createJsonAssertion(); // Return a JsonAssert containing the response body
		$json->assertExists('$.user'); // Assert that there is a given JSONPath
		// ...

		$this->createJsonAssertion('{...}'); // Wrap a JSON string
		$this->createHtmlAssertion(('<!DOCTYPE html>...'); // Wrap an HTML string
		$this->createXmlAssertion(('<?xml version="1.0" ?>...'); // Wrap an XML string
	}
}

IMPORTANT!

The extension uses instances of the PHPUnit\Framework\TestCase class. The methods cannot be called statically like the native PHPUnit's methods:

self::assertOk(); // Won't work

Testing

Run make test.

Documentation

The main methods of the trait are:

  • createResponseAssertion(ResponseInterface $response): ResponseAssert: Wraps an HTTP response object in an assertion one
  • createJsonAssertion(string $json): JsonAssert: Wraps a JSON string in an assertion object
  • createHtmlAssertion(string $html): DocumentAssert: Wraps an HTML string in an assertion object
  • createXmlAssertion(string $xml): DocumentAssert: Wraps an XML string in an assertion object
  • pass(): void: Marks test as passed

The whole documentation to every assertion method can be found in the phpdoc comments in the source code.

stein197/phpunit-extended 适用场景与选型建议

stein197/phpunit-extended 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 25 次下载、GitHub Stars 达 7, 最近一次更新时间为 2025 年 04 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 stein197/phpunit-extended 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-19