From 539f96a76d946718d275633486d1279e116d75fe Mon Sep 17 00:00:00 2001 From: Neur0toxine Date: Thu, 13 May 2021 22:05:07 +0300 Subject: [PATCH] uri and scheme matcher, ability to add custom matcher --- src/Enum/RequestMethod.php | 29 ++++++++++++++++++++++ src/Enum/RequestScheme.php | 22 +++++++++++++++++ src/Matchers/HostMatcher.php | 2 +- src/Matchers/SchemeMatcher.php | 43 ++++++++++++++++++++++++++++++++ src/Matchers/UriMatcher.php | 42 +++++++++++++++++++++++++++++++ src/PockBuilder.php | 45 +++++++++++++++++++++++++++++++++- 6 files changed, 181 insertions(+), 2 deletions(-) create mode 100644 src/Enum/RequestMethod.php create mode 100644 src/Enum/RequestScheme.php create mode 100644 src/Matchers/SchemeMatcher.php create mode 100644 src/Matchers/UriMatcher.php diff --git a/src/Enum/RequestMethod.php b/src/Enum/RequestMethod.php new file mode 100644 index 0000000..06289a5 --- /dev/null +++ b/src/Enum/RequestMethod.php @@ -0,0 +1,29 @@ +getUri()->getHost() === $this->host; + return strtolower($request->getUri()->getHost()) === strtolower($this->host); } } diff --git a/src/Matchers/SchemeMatcher.php b/src/Matchers/SchemeMatcher.php new file mode 100644 index 0000000..399227c --- /dev/null +++ b/src/Matchers/SchemeMatcher.php @@ -0,0 +1,43 @@ +scheme = $scheme; + } + + /** + * @inheritDoc + */ + public function matches(RequestInterface $request): bool + { + return strtolower($request->getUri()->getScheme()) === strtolower($this->scheme); + } +} diff --git a/src/Matchers/UriMatcher.php b/src/Matchers/UriMatcher.php new file mode 100644 index 0000000..758dabc --- /dev/null +++ b/src/Matchers/UriMatcher.php @@ -0,0 +1,42 @@ +uri = $uri; + } + + /** + * @inheritDoc + */ + public function matches(RequestInterface $request): bool + { + return strtolower((string) $request->getUri()) === strtolower((string) $this->uri); + } +} diff --git a/src/PockBuilder.php b/src/PockBuilder.php index a28ee4a..aaf1f95 100644 --- a/src/PockBuilder.php +++ b/src/PockBuilder.php @@ -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);