mirror of
https://github.com/Neur0toxine/pock.git
synced 2024-11-28 15:56:09 +03:00
uri and scheme matcher, ability to add custom matcher
This commit is contained in:
parent
fea8179292
commit
539f96a76d
29
src/Enum/RequestMethod.php
Normal file
29
src/Enum/RequestMethod.php
Normal 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';
|
||||
}
|
22
src/Enum/RequestScheme.php
Normal file
22
src/Enum/RequestScheme.php
Normal 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';
|
||||
}
|
@ -37,6 +37,6 @@ class HostMatcher implements RequestMatcherInterface
|
||||
*/
|
||||
public function matches(RequestInterface $request): bool
|
||||
{
|
||||
return $request->getUri()->getHost() === $this->host;
|
||||
return strtolower($request->getUri()->getHost()) === strtolower($this->host);
|
||||
}
|
||||
}
|
||||
|
43
src/Matchers/SchemeMatcher.php
Normal file
43
src/Matchers/SchemeMatcher.php
Normal 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);
|
||||
}
|
||||
}
|
42
src/Matchers/UriMatcher.php
Normal file
42
src/Matchers/UriMatcher.php
Normal 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);
|
||||
}
|
||||
}
|
@ -9,9 +9,13 @@
|
||||
|
||||
namespace Pock;
|
||||
|
||||
use Pock\Enum\RequestScheme;
|
||||
use Pock\Matchers\AnyRequestMatcher;
|
||||
use Pock\Matchers\HostMatcher;
|
||||
use Pock\Matchers\MultipleMatcher;
|
||||
use Pock\Matchers\RequestMatcherInterface;
|
||||
use Pock\Matchers\SchemeMatcher;
|
||||
use Pock\Matchers\UriMatcher;
|
||||
use Psr\Http\Client\ClientInterface;
|
||||
|
||||
/**
|
||||
@ -45,6 +49,18 @@ class PockBuilder
|
||||
$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.
|
||||
*
|
||||
@ -53,9 +69,33 @@ class PockBuilder
|
||||
* @return $this
|
||||
*/
|
||||
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->matcher->addMatcher(new HostMatcher($host));
|
||||
$this->matcher->addMatcher($matcher);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -88,6 +128,9 @@ class PockBuilder
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Pock\Client
|
||||
*/
|
||||
public function getClient(): Client
|
||||
{
|
||||
return new Client($this->mocks, $this->fallbackClient);
|
||||
|
Loading…
Reference in New Issue
Block a user