定制 helmich/phpunit-psr7-assert 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

helmich/phpunit-psr7-assert

Composer 安装命令:

composer require helmich/phpunit-psr7-assert

包简介

PHPUnit assertions for testing PSR7-compliant applications

README 文档

README

Unit tests Maintainability Code Coverage

This library adds several new assertions to PHPUnit that allow you to easily and concisely test HTTP request and response messages that implement the PSR-7 interfaces.

Author and copyright

Martin Helmich kontakt@martin-helmich.de This library is MIT-licensed.

Installation

$ composer require helmich/phpunit-psr7-assert

Compatibility

There are several release branches of this library, each of these being compatible with different releases of PHPUnit and PHP. The following table should give an easy overview:

PSR-7 assertion version PHPUnit 4 PHPUnit 5 PHPUnit 6 PHPUnit 7 PHPUnit 8 PHPUnit 9 PHPUnit 10 PHPUnit 11 PHPUnit 12
v1 (branch v1), unsupported 🚫 🚫 🚫 🚫 🚫 🚫 🚫
v2 (branch v2), unsupported 🚫 🚫 🚫 🚫 🚫 🚫 🚫 🚫
v3 (branch v3), unsupported 🚫 🚫 🚫 🚫 🚫 🚫 🚫 🚫
v4 (branch master) 🚫 🚫 🚫 🚫

When you are using composer require and have already declared a dependency to phpunit/phpunit in your composer.json file, Composer should pick latest compatible version automatically.

Usage

Using the Psr7Assertions trait

Simply use the trait Helmich\Psr7Assert\Psr7Assertions in any of your test cases. This trait offers a set of new assert* functions that you can use in your test cases:

<?php
use Helmich\Psr7Assert\Psr7Assertions;
use PHPUnit\Framework\Testcase;

class MyTestCase extends Testcase
{
  use Psr7Assertions;

  public function testRequestMatchesExpectations()
  {
    $request = /* build some instance of Psr\Http\Message\MessageInterface */;

    $this->assertRequestHasUri($request, '/foo');
    $this->assertRequestIsPost($request);
    $this->assertMessageHasHeaders($request, [
      'content-type' => 'application/json',
      'expect'       => '202-accepted'
    ]);
    $this->assertMessageBodyMatchesJson($request, [
      '$.username' => 'mhelmich'
    ]);
  }
}

Most assertions take a $message argument which is expected to be an instance of the Psr\Http\Message\MessageInterface class -- this means that many assertions work both with request and response messages. These interfaces are defined by the psr/http-message package.

Using the functional interface

This package also offers a functional interface that can be used in a more fluent way than the assertions offered by the Psr7Assertions trait. Simply include the file src/Functions.php for your test cases (preferably, using Composer's autoload-dev setting):

public function testRequestMatchesExpectations()
{
  $request = /* build some instance of Psr\Http\Message\MessageInterface */;

  assertThat($request, logicalAnd(
    hasUri('/foo'),
    isPost(),
    hasHeaders([
      'content-type' => 'application/json',
      'expect'       => '202-accepted'
    ]),
    bodyMatchesJson(['$.username' => 'mhelmich'])
  ));
}

Assertion reference

assertRequestHasUri($request, $uri) / hasUri($uri)

Asserts that the request URI of request $request is equal to $uri

$this->assertRequestHasUri($request, '/foo'));
assertThat($request, hasUri('/foo'));
assertMessageHasHeader($message, $header[, $constraint]) / hasHeader($name[, $constraint])

Asserts that the header named $header is present in the HTTP message. The exact behaviour of this assertion is dependent on the $constraint parameter:

  1. If no constraint is given, the assertion will match when the header is present and not empty.

  2. If a primitive value is given as $constraint, the assertion will match when the header is present and is equal to the specified value

  3. If $constraint is an instance of the PHPUnit\Framework\Constraint\Constraint class, the assertion will match when the constraint evaluates to TRUE.

    Example:

    assertThat($request, hasHeader('content-length', greaterThan(1000)));
assertMessageHasHeaders($message, $constraints) / hasHeaders($constraints)

Same as assertMessageHasHeader, just with multiple headers. $constraints is a key-value array using header names as keys and constraints (see above) as values.

Example:

assertThat($request, hasHeaders([
  'content-type'   => 'application/json',
  'content-length' => greaterThan(1000)
]));
assertMessageBodyMatches($message, $constraint) / bodyMatches($constraint)

Asserts that the message body matches the constraint $constraint. If $constraint is a primitive value, the assertion will pass when the message body is equal to the constraint. If $constraint is an instance of the PHPUnit\Framework\Constraint\Constraint class, the constraint will be evaluated as-is.

assertMessageBodyMatchesJson($message, $jsonConstraints) / bodyMatchesJson($jsonConstraints)

This actually asserts several facts:

  1. The message must have a content-type header that is equal to application/json
  2. The message body must be a valid JSON string (that means decodeable by json_decode)
  3. The encoded JSON object must match all constraints specified in the $jsonConstraints array. For this, the helmich/phpunit-json-assert package will be used.
assertMessageBodyMatchesForm($message, $formConstraints) / bodyMatchesForm($formConstraints)

This asserts that the message body contains application/x-www-form-urlencoded-encoded content, with individual variables matching the $formConstraints array.

assertRequestHasMethod($request, $method) / hasMethod($method)

Asserts that the request has the method $method. For the most common request method, shorthand assertions are available:

  • assertRequestIsGet($request) / isGet()
  • assertRequestIsPost($request) / isPost()
  • assertRequestIsPut($request) / isPut()
  • assertRequestIsDelete($request) / isDelete()

assertResponseHasStatus($response, $status) / hasStatus($status)

Asserts that the response status matches a given constraint. If $status is a scalar value, this assertion will check for equality.

assertThat($response, hasStatus(200));
assertThat($response, hasStatus(logicalAnd(greaterThanOrEqual(200), lessThan(400))));

For the most common checks, some shorthand assertions are available:

  • assertResponseIsSuccess($response) / isSuccess() -- Status codes 200 to 299
  • assertResponseIsRedirect($response) / isRedirect() -- Status codes 300 to 399
  • assertResponseIsClientError($response) / isClientError() -- Status codes 400 to 499
  • assertResponseIsServerError($response) / isServerError() -- Status codes 500 to 599

assertStringIsAbsoluteUri($uri) / isAbsoluteUri()

Assert that the string $uri contains a valid absolute URL (scheme and hostname are required).

assertHasQueryParameter($uriOrRequest, $name[, $value]) / hasQueryParameter($name[, $value])

Asserts that an URI contains a query parameter matching the given constraints. $name and $value may both be string values as well as instances of the PHPUnit\Framework\Constraint\Constraint interface.

The $uriOrRequest value may be

  • a string, which will be interpreted as URI
  • an instance of the Psr\Http\Message\UriInterface interface
  • an instance of the Psr\Http\Message\RequestInterface interface

helmich/phpunit-psr7-assert 适用场景与选型建议

helmich/phpunit-psr7-assert 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.34M 次下载、GitHub Stars 达 27, 最近一次更新时间为 2015 年 12 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 1.34M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 27
  • 点击次数: 21
  • 依赖项目数: 11
  • 推荐数: 0

GitHub 信息

  • Stars: 27
  • Watchers: 2
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-12-14