2017 © Pedro Peláez
 

library phpunit-psr7-assert

PHPUnit assertions for testing PSR7-compliant applications

image

helmich/phpunit-psr7-assert

PHPUnit assertions for testing PSR7-compliant applications

  • Thursday, April 5, 2018
  • by helmich
  • Repository
  • 1 Watchers
  • 10 Stars
  • 35,860 Installations
  • PHP
  • 4 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 15 Versions
  • 15 % Grown

The README.md

PSR-7 assertions for PHPUnit

Unit tests Code Climate Test Coverage, (*1)

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., (*2)

Martin Helmich kontakt@martin-helmich.de This library is MIT-licensed., (*3)

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:, (*4)

PSR-7 assertion version PHPUnit 4 PHPUnit 5 PHPUnit 6 PHPUnit 7 PHPUnit 8 PHPUnit 9 PHPUnit 10
v1 (branch v1), unsupported :white_check_mark: :white_check_mark: :no_entry_sign: :no_entry_sign: :no_entry_sign: :no_entry_sign: :no_entry_sign:
v2 (branch v2), unsupported :no_entry_sign: :no_entry_sign: :white_check_mark: :no_entry_sign: :no_entry_sign: :no_entry_sign: :no_entry_sign:
v3 (branch v3), unsupported :no_entry_sign: :no_entry_sign: :no_entry_sign: :white_check_mark: :no_entry_sign: :no_entry_sign: :no_entry_sign:
v4 (branch master) :no_entry_sign: :no_entry_sign: :no_entry_sign: :no_entry_sign: :white_check_mark: :white_check_mark: :white_check_mark:

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., (*5)

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:, (*6)

<?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., (*7)

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):, (*8)

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, (*9)

$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:, (*10)

  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., (*11)

    Example:, (*12)

    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., (*13)

Example:, (*14)

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., (*15)

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

This actually asserts several facts:, (*16)

  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., (*17)

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

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

  • 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., (*19)

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

For the most common checks, some shorthand assertions are available:, (*20)

  • 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)., (*21)

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., (*22)

The $uriOrRequest value may be, (*23)

  • 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

The Versions

05/04 2018

v2.x-dev

2.9999999.9999999.9999999-dev

PHPUnit assertions for testing PSR7-compliant applications

  Sources   Download

MIT

The Requires

 

The Development Requires

05/04 2018

v2.1.0

2.1.0.0

PHPUnit assertions for testing PSR7-compliant applications

  Sources   Download

MIT

The Requires

 

The Development Requires

05/04 2018

dev-backport/11

dev-backport/11

PHPUnit assertions for testing PSR7-compliant applications

  Sources   Download

MIT

The Requires

 

The Development Requires

04/04 2018

dev-master

9999999-dev

PHPUnit assertions for testing PSR7-compliant applications

  Sources   Download

MIT

The Requires

 

The Development Requires

04/04 2018

v3.1.0

3.1.0.0

PHPUnit assertions for testing PSR7-compliant applications

  Sources   Download

MIT

The Requires

 

The Development Requires

04/04 2018

dev-feature/uri-assertions

dev-feature/uri-assertions

PHPUnit assertions for testing PSR7-compliant applications

  Sources   Download

MIT

The Requires

 

The Development Requires

09/02 2018

v3.0.0

3.0.0.0

PHPUnit assertions for testing PSR7-compliant applications

  Sources   Download

MIT

The Requires

 

The Development Requires

09/02 2018

v2.0.1

2.0.1.0

PHPUnit assertions for testing PSR7-compliant applications

  Sources   Download

MIT

The Requires

 

The Development Requires

10/02 2017

v2.0.0

2.0.0.0

PHPUnit assertions for testing PSR7-compliant applications

  Sources   Download

MIT

The Requires

 

The Development Requires

10/02 2017

v1.x-dev

1.9999999.9999999.9999999-dev

PHPUnit assertions for testing PSR7-compliant applications

  Sources   Download

MIT

The Requires

 

The Development Requires

10/02 2017

v1.1.3

1.1.3.0

PHPUnit assertions for testing PSR7-compliant applications

  Sources   Download

MIT

The Requires

 

The Development Requires

04/02 2016

v1.1.2

1.1.2.0

PHPUnit assertions for testing PSR7-compliant applications

  Sources   Download

MIT

The Requires

 

The Development Requires

28/01 2016

v1.1.1

1.1.1.0

PHPUnit assertions for testing PSR7-compliant applications

  Sources   Download

MIT

The Requires

 

The Development Requires

27/01 2016

v1.1.0

1.1.0.0

PHPUnit assertions for testing PSR7-compliant applications

  Sources   Download

MIT

The Requires

 

The Development Requires

14/12 2015

v1.0.0

1.0.0.0

PHPUnit assertions for testing PSR7-compliant applications

  Sources   Download

MIT

The Requires

 

The Development Requires