uri and scheme matcher, ability to add custom matcher

This commit is contained in:
Pavel 2021-05-13 22:05:07 +03:00
parent fea8179292
commit 539f96a76d
6 changed files with 181 additions and 2 deletions

View File

@ -0,0 +1,29 @@
<?php
/**
* PHP 7.3
*
* @category RequestMethod
* @package Pock\Enum
*/
namespace Pock\Enum;
/**
* Class RequestMethod
*
* @category RequestMethod
* @package Pock\Enum
*/
final class RequestMethod
{
public const GET = 'GET';
public const HEAD = 'HEAD';
public const POST = 'POST';
public const PUT = 'PUT';
public const PATCH = 'PATCH';
public const DELETE = 'DELETE';
public const CONNECT = 'CONNECT';
public const OPTIONS = 'OPTIONS';
public const TRACE = 'TRACE';
}

View File

@ -0,0 +1,22 @@
<?php
/**
* PHP 7.3
*
* @category RequestScheme
* @package Pock\Enum
*/
namespace Pock\Enum;
/**
* Class RequestScheme
*
* @category RequestScheme
* @package Pock\Enum
*/
final class RequestScheme
{
public const HTTP = 'http';
public const HTTPS = 'https';
}

View File

@ -37,6 +37,6 @@ class HostMatcher implements RequestMatcherInterface
*/ */
public function matches(RequestInterface $request): bool public function matches(RequestInterface $request): bool
{ {
return $request->getUri()->getHost() === $this->host; return strtolower($request->getUri()->getHost()) === strtolower($this->host);
} }
} }

View File

@ -0,0 +1,43 @@
<?php
/**
* PHP 7.3
*
* @category SchemeMatcher
* @package Pock\Matchers
*/
namespace Pock\Matchers;
use Pock\Enum\RequestScheme;
use Psr\Http\Message\RequestInterface;
/**
* Class SchemeMatcher
*
* @category SchemeMatcher
* @package Pock\Matchers
*/
class SchemeMatcher implements RequestMatcherInterface
{
/** @var string */
private $scheme;
/**
* SchemeMatcher constructor.
*
* @param string $scheme
*/
public function __construct(string $scheme = RequestScheme::HTTP)
{
$this->scheme = $scheme;
}
/**
* @inheritDoc
*/
public function matches(RequestInterface $request): bool
{
return strtolower($request->getUri()->getScheme()) === strtolower($this->scheme);
}
}

View File

@ -0,0 +1,42 @@
<?php
/**
* PHP 7.3
*
* @category UriMatcher
* @package Pock\Matchers
*/
namespace Pock\Matchers;
use Psr\Http\Message\RequestInterface;
/**
* Class UriMatcher
*
* @category UriMatcher
* @package Pock\Matchers
*/
class UriMatcher implements RequestMatcherInterface
{
/** @var \Psr\Http\Message\UriInterface|string */
private $uri;
/**
* UriMatcher constructor.
*
* @param \Psr\Http\Message\UriInterface|string $uri
*/
public function __construct($uri)
{
$this->uri = $uri;
}
/**
* @inheritDoc
*/
public function matches(RequestInterface $request): bool
{
return strtolower((string) $request->getUri()) === strtolower((string) $this->uri);
}
}

View File

@ -9,9 +9,13 @@
namespace Pock; namespace Pock;
use Pock\Enum\RequestScheme;
use Pock\Matchers\AnyRequestMatcher; use Pock\Matchers\AnyRequestMatcher;
use Pock\Matchers\HostMatcher; use Pock\Matchers\HostMatcher;
use Pock\Matchers\MultipleMatcher; use Pock\Matchers\MultipleMatcher;
use Pock\Matchers\RequestMatcherInterface;
use Pock\Matchers\SchemeMatcher;
use Pock\Matchers\UriMatcher;
use Psr\Http\Client\ClientInterface; use Psr\Http\Client\ClientInterface;
/** /**
@ -45,6 +49,18 @@ class PockBuilder
$this->reset(); $this->reset();
} }
/**
* Match request by its scheme.
*
* @param string $scheme
*
* @return $this
*/
public function matchScheme(string $scheme = RequestScheme::HTTP): PockBuilder
{
return $this->addMatcher(new SchemeMatcher($scheme));
}
/** /**
* Matches request by hostname. * Matches request by hostname.
* *
@ -53,9 +69,33 @@ class PockBuilder
* @return $this * @return $this
*/ */
public function matchHost(string $host): PockBuilder public function matchHost(string $host): PockBuilder
{
return $this->addMatcher(new HostMatcher($host));
}
/**
* Matches request by the whole URI.
*
* @param \Psr\Http\Message\UriInterface|string $uri
*
* @return \Pock\PockBuilder
*/
public function matchUri($uri): PockBuilder
{
return $this->addMatcher(new UriMatcher($uri));
}
/**
* Add custom matcher to the mock.
*
* @param \Pock\Matchers\RequestMatcherInterface $matcher
*
* @return \Pock\PockBuilder
*/
public function addMatcher(RequestMatcherInterface $matcher): PockBuilder
{ {
$this->closePrevious(); $this->closePrevious();
$this->matcher->addMatcher(new HostMatcher($host)); $this->matcher->addMatcher($matcher);
return $this; return $this;
} }
@ -88,6 +128,9 @@ class PockBuilder
return $this; return $this;
} }
/**
* @return \Pock\Client
*/
public function getClient(): Client public function getClient(): Client
{ {
return new Client($this->mocks, $this->fallbackClient); return new Client($this->mocks, $this->fallbackClient);