承接 stein197/phpunit-extended 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

stein197/phpunit-extended

最新稳定版本:3.0.0

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.

统计信息

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

GitHub 信息

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

其他信息

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