Skip to content

phanxipang/request-matcher

Repository files navigation

Fansipan PSR-7 Request Matcher

Latest Version on Packagist Github Actions Codecov Total Downloads Software License

PSR-7 request matcher equivalent of Symfony's RequestMatcher.

Installation

You may use Composer to install this package:

composer require fansipan/request-matcher

Usage

To create a matcher instance with your assertions, you can use the following example to match the request host:

use Fansipan\RequestMatcher\HostRequestMatcher;
use Psr\Http\Message\RequestInterface;

$matcher = new HostRequestMatcher('localhost');

// Matches http://localhost

/** @var RequestInterface $request */
$matcher->matches($request);

Customer Request Matcher

You can also create a matcher using a callback. For instance:

use Fansipan\RequestMatcher\CallbackRequestMatcher;
use Psr\Http\Message\RequestInterface;

$matcher = new CallbackRequestMatcher(static fn (RequestInterface $request) => $request->getUri()->getScheme() === 'https' && $request->getUri()->getHost() === 'my.app');

Chain Request Matcher

The example above can be grouped by using ChainRequestMatcher

use Fansipan\RequestMatcher\CallbackRequestMatcher;
use Fansipan\RequestMatcher\HostRequestMatcher;
use Fansipan\RequestMatcher\SchemeRequestMatcher;
use Psr\Http\Message\RequestInterface;

$matcher = new ChainRequestMatcher([
    new SchemeRequestMatcher('https'),
    new HostRequestMatcher('my.app'),
]);

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.