2021-05-13 21:08:55 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2021-05-14 18:57:35 +03:00
|
|
|
* PHP 7.2
|
2021-05-13 21:08:55 +03:00
|
|
|
*
|
|
|
|
* @category PockBuilder
|
|
|
|
* @package Pock
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Pock;
|
|
|
|
|
2021-05-15 18:57:28 +03:00
|
|
|
use Diff\ArrayComparer\StrictArrayComparer;
|
2021-05-21 18:05:43 +03:00
|
|
|
use DOMDocument;
|
2021-05-15 13:14:21 +03:00
|
|
|
use Pock\Enum\RequestMethod;
|
2021-05-13 22:05:07 +03:00
|
|
|
use Pock\Enum\RequestScheme;
|
2021-05-20 20:56:45 +03:00
|
|
|
use Pock\Exception\PockClientException;
|
|
|
|
use Pock\Exception\PockNetworkException;
|
|
|
|
use Pock\Exception\PockRequestException;
|
2021-05-21 18:05:43 +03:00
|
|
|
use Pock\Exception\XmlException;
|
2021-05-21 09:38:15 +03:00
|
|
|
use Pock\Factory\CallbackReplyFactory;
|
|
|
|
use Pock\Factory\ReplyFactoryInterface;
|
2021-05-13 21:08:55 +03:00
|
|
|
use Pock\Matchers\AnyRequestMatcher;
|
2021-05-15 18:57:28 +03:00
|
|
|
use Pock\Matchers\BodyMatcher;
|
|
|
|
use Pock\Matchers\CallbackRequestMatcher;
|
|
|
|
use Pock\Matchers\ExactHeaderMatcher;
|
|
|
|
use Pock\Matchers\ExactHeadersMatcher;
|
|
|
|
use Pock\Matchers\ExactQueryMatcher;
|
|
|
|
use Pock\Matchers\HeaderLineMatcher;
|
|
|
|
use Pock\Matchers\HeaderLineRegexpMatcher;
|
|
|
|
use Pock\Matchers\HeaderMatcher;
|
|
|
|
use Pock\Matchers\HeadersMatcher;
|
2021-05-13 21:08:55 +03:00
|
|
|
use Pock\Matchers\HostMatcher;
|
2021-05-15 18:57:28 +03:00
|
|
|
use Pock\Matchers\JsonBodyMatcher;
|
2021-05-15 13:14:21 +03:00
|
|
|
use Pock\Matchers\MethodMatcher;
|
2021-05-13 21:08:55 +03:00
|
|
|
use Pock\Matchers\MultipleMatcher;
|
2021-05-15 18:57:28 +03:00
|
|
|
use Pock\Matchers\PathMatcher;
|
|
|
|
use Pock\Matchers\QueryMatcher;
|
2021-05-13 22:05:07 +03:00
|
|
|
use Pock\Matchers\RequestMatcherInterface;
|
|
|
|
use Pock\Matchers\SchemeMatcher;
|
|
|
|
use Pock\Matchers\UriMatcher;
|
2021-05-21 18:05:43 +03:00
|
|
|
use Pock\Matchers\XmlBodyMatcher;
|
2021-05-15 18:57:28 +03:00
|
|
|
use Pock\Traits\JsonDecoderTrait;
|
|
|
|
use Pock\Traits\JsonSerializerAwareTrait;
|
2021-05-15 20:28:41 +03:00
|
|
|
use Pock\Traits\XmlSerializerAwareTrait;
|
2021-05-13 21:51:13 +03:00
|
|
|
use Psr\Http\Client\ClientInterface;
|
2021-05-15 18:57:28 +03:00
|
|
|
use Throwable;
|
2021-05-13 21:08:55 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class PockBuilder
|
|
|
|
*
|
|
|
|
* @category PockBuilder
|
|
|
|
* @package Pock
|
2021-05-15 18:57:28 +03:00
|
|
|
*
|
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
|
2021-05-20 20:56:45 +03:00
|
|
|
* @SuppressWarnings(PHPMD.TooManyMethods)
|
2021-05-13 21:08:55 +03:00
|
|
|
*/
|
|
|
|
class PockBuilder
|
|
|
|
{
|
2021-05-15 18:57:28 +03:00
|
|
|
use JsonDecoderTrait;
|
|
|
|
use JsonSerializerAwareTrait;
|
2021-05-15 20:28:41 +03:00
|
|
|
use XmlSerializerAwareTrait;
|
2021-05-15 18:57:28 +03:00
|
|
|
|
2021-05-13 21:08:55 +03:00
|
|
|
/** @var \Pock\Matchers\MultipleMatcher */
|
|
|
|
private $matcher;
|
|
|
|
|
2021-05-14 20:06:58 +03:00
|
|
|
/** @var \Pock\PockResponseBuilder|null */
|
|
|
|
private $responseBuilder;
|
2021-05-13 21:08:55 +03:00
|
|
|
|
2021-05-21 09:38:15 +03:00
|
|
|
/** @var ReplyFactoryInterface|null */
|
|
|
|
private $replyFactory;
|
|
|
|
|
2021-05-13 21:08:55 +03:00
|
|
|
/** @var \Throwable|null */
|
|
|
|
private $throwable;
|
|
|
|
|
2021-05-14 18:52:00 +03:00
|
|
|
/** @var int */
|
|
|
|
private $maxHits;
|
|
|
|
|
2021-05-20 20:56:45 +03:00
|
|
|
/** @var int */
|
|
|
|
private $matchAt;
|
|
|
|
|
2021-05-13 21:08:55 +03:00
|
|
|
/** @var \Pock\MockInterface[] */
|
|
|
|
private $mocks;
|
|
|
|
|
2021-05-13 21:51:13 +03:00
|
|
|
/** @var \Psr\Http\Client\ClientInterface|null */
|
|
|
|
private $fallbackClient;
|
|
|
|
|
2021-05-13 21:08:55 +03:00
|
|
|
/**
|
|
|
|
* PockBuilder constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->reset();
|
|
|
|
}
|
|
|
|
|
2021-05-15 13:14:21 +03:00
|
|
|
/**
|
|
|
|
* Match request by its method.
|
|
|
|
*
|
|
|
|
* @param string $method
|
|
|
|
*
|
2021-05-15 18:57:28 +03:00
|
|
|
* @return self
|
2021-05-15 13:14:21 +03:00
|
|
|
*/
|
|
|
|
public function matchMethod(string $method): self
|
|
|
|
{
|
|
|
|
return $this->addMatcher(new MethodMatcher($method));
|
|
|
|
}
|
|
|
|
|
2021-05-13 22:05:07 +03:00
|
|
|
/**
|
|
|
|
* Match request by its scheme.
|
|
|
|
*
|
|
|
|
* @param string $scheme
|
|
|
|
*
|
2021-05-14 18:52:00 +03:00
|
|
|
* @return self
|
2021-05-13 22:05:07 +03:00
|
|
|
*/
|
2021-05-15 13:14:21 +03:00
|
|
|
public function matchScheme(string $scheme): self
|
2021-05-13 22:05:07 +03:00
|
|
|
{
|
|
|
|
return $this->addMatcher(new SchemeMatcher($scheme));
|
|
|
|
}
|
|
|
|
|
2021-05-13 21:08:55 +03:00
|
|
|
/**
|
|
|
|
* Matches request by hostname.
|
|
|
|
*
|
|
|
|
* @param string $host
|
|
|
|
*
|
2021-05-14 18:52:00 +03:00
|
|
|
* @return self
|
2021-05-13 21:08:55 +03:00
|
|
|
*/
|
2021-05-15 13:14:21 +03:00
|
|
|
public function matchHost(string $host): self
|
2021-05-13 22:05:07 +03:00
|
|
|
{
|
|
|
|
return $this->addMatcher(new HostMatcher($host));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Matches request by the whole URI.
|
|
|
|
*
|
|
|
|
* @param \Psr\Http\Message\UriInterface|string $uri
|
|
|
|
*
|
2021-05-15 13:14:21 +03:00
|
|
|
* @return self
|
2021-05-13 22:05:07 +03:00
|
|
|
*/
|
2021-05-15 13:14:21 +03:00
|
|
|
public function matchUri($uri): self
|
2021-05-13 22:05:07 +03:00
|
|
|
{
|
|
|
|
return $this->addMatcher(new UriMatcher($uri));
|
|
|
|
}
|
|
|
|
|
2021-05-15 18:57:28 +03:00
|
|
|
/**
|
|
|
|
* Matches request by header value or several values. Header can have other values which are not specified here.
|
|
|
|
* @see PockBuilder::matchExactHeader() if you want to match exact header values.
|
|
|
|
*
|
|
|
|
* @param string $header
|
|
|
|
* @param string|string[] $value
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function matchHeader(string $header, $value): self
|
|
|
|
{
|
|
|
|
return $this->addMatcher(new HeaderMatcher($header, $value));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Matches request by headers values or several values. Headers can have other values which are not specified here.
|
|
|
|
* @see PockBuilder::matchExactHeaders() if you want to match exact headers collection.
|
|
|
|
*
|
|
|
|
* @param array<string, string|string[]> $headers
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function matchHeaders(array $headers): self
|
|
|
|
{
|
|
|
|
return $this->addMatcher(new HeadersMatcher($headers));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Matches request by the exact header pattern or values.
|
|
|
|
*
|
|
|
|
* @param string $header
|
|
|
|
* @param string|string[] $value
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function matchExactHeader(string $header, $value): self
|
|
|
|
{
|
|
|
|
return $this->addMatcher(new ExactHeaderMatcher($header, $value));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Matches request by headers values or several values.
|
|
|
|
* Note: only host header will be dropped. Any other headers will not be excluded and can result in the problems
|
|
|
|
* with the exact matching.
|
|
|
|
*
|
|
|
|
* @param array<string, string|string[]> $headers
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function matchExactHeaders(array $headers): self
|
|
|
|
{
|
|
|
|
return $this->addMatcher(new ExactHeadersMatcher($headers));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Matches request by the unparsed header line.
|
|
|
|
*
|
|
|
|
* @param string $header
|
|
|
|
* @param string $value
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function matchHeaderLine(string $header, string $value): self
|
|
|
|
{
|
|
|
|
return $this->addMatcher(new HeaderLineMatcher($header, $value));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Matches request by the unparsed header line using provided regular expression.
|
|
|
|
*
|
|
|
|
* @param string $header
|
|
|
|
* @param string $pattern
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function matchHeaderLineRegexp(string $header, string $pattern): self
|
|
|
|
{
|
|
|
|
return $this->addMatcher(new HeaderLineRegexpMatcher($header, $pattern));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Match request by its path. Path with and without slash at the start will be treated as the same path.
|
|
|
|
* It's not the same for the path with slash at the end of it.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function matchPath(string $path): self
|
|
|
|
{
|
|
|
|
return $this->addMatcher(new PathMatcher($path));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Match request by its query. Request can contain other query variables.
|
|
|
|
* @see PockBuilder::matchExactQuery() if you want to match an entire query string.
|
|
|
|
*
|
|
|
|
* @param array<string, mixed> $query
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function matchQuery(array $query): self
|
|
|
|
{
|
|
|
|
return $this->addMatcher(new QueryMatcher($query));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Match request by its query. Additional query parameters aren't allowed.
|
|
|
|
*
|
|
|
|
* @param array<string, mixed> $query
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function matchExactQuery(array $query): self
|
|
|
|
{
|
|
|
|
return $this->addMatcher(new ExactQueryMatcher($query));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Match entire request body.
|
|
|
|
*
|
|
|
|
* @param \Psr\Http\Message\StreamInterface|resource|string $data
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function matchBody($data): self
|
|
|
|
{
|
|
|
|
return $this->addMatcher(new BodyMatcher($data));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Match JSON request body.
|
|
|
|
*
|
|
|
|
* @param mixed $data
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
* @throws \Pock\Exception\JsonException
|
|
|
|
*/
|
|
|
|
public function matchJsonBody($data): self
|
|
|
|
{
|
|
|
|
return $this->addMatcher(new JsonBodyMatcher(
|
|
|
|
self::jsonDecode(
|
|
|
|
self::serializeJson($data) ?? '',
|
|
|
|
true
|
|
|
|
)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2021-05-15 20:28:41 +03:00
|
|
|
/**
|
2021-05-21 18:05:43 +03:00
|
|
|
* Match XML request body using raw XML data.
|
2021-05-15 20:28:41 +03:00
|
|
|
*
|
2021-05-21 18:05:43 +03:00
|
|
|
* **Note:** this method will fallback to the string comparison if ext-xsl is not available.
|
|
|
|
* It also doesn't serializer values with available XML serializer.
|
|
|
|
* Use PockBuilder::matchSerializedXmlBody if you want to execute available serializer.
|
2021-05-15 20:28:41 +03:00
|
|
|
*
|
2021-05-21 18:05:43 +03:00
|
|
|
* @see \Pock\PockBuilder::matchSerializedXmlBody()
|
2021-05-15 20:28:41 +03:00
|
|
|
*
|
2021-05-21 18:05:43 +03:00
|
|
|
* @param DOMDocument|\Psr\Http\Message\StreamInterface|resource|string $data
|
2021-05-15 20:28:41 +03:00
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function matchXmlBody($data): self
|
|
|
|
{
|
2021-05-21 18:05:43 +03:00
|
|
|
return $this->addMatcher(new XmlBodyMatcher($data));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Match XML request body.
|
|
|
|
*
|
|
|
|
* This method will try to use available XML serializer before matching.
|
|
|
|
*
|
|
|
|
* @phpstan-ignore-next-line
|
|
|
|
* @param string|array|object $data
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
* @throws \Pock\Exception\XmlException
|
|
|
|
*/
|
|
|
|
public function matchSerializedXmlBody($data): self
|
|
|
|
{
|
|
|
|
return $this->matchXmlBody(self::serializeXml($data) ?? '');
|
2021-05-15 20:28:41 +03:00
|
|
|
}
|
|
|
|
|
2021-05-15 18:57:28 +03:00
|
|
|
/**
|
|
|
|
* Match request using provided callback. Callback should receive RequestInterface and return boolean.
|
|
|
|
* If returned value is true then request is matched.
|
|
|
|
*
|
|
|
|
* @param callable $callback
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function matchCallback(callable $callback): self
|
|
|
|
{
|
|
|
|
return $this->addMatcher(new CallbackRequestMatcher($callback));
|
|
|
|
}
|
|
|
|
|
2021-05-13 22:05:07 +03:00
|
|
|
/**
|
|
|
|
* Add custom matcher to the mock.
|
|
|
|
*
|
|
|
|
* @param \Pock\Matchers\RequestMatcherInterface $matcher
|
|
|
|
*
|
2021-05-15 13:14:21 +03:00
|
|
|
* @return self
|
2021-05-13 22:05:07 +03:00
|
|
|
*/
|
2021-05-15 13:14:21 +03:00
|
|
|
public function addMatcher(RequestMatcherInterface $matcher): self
|
2021-05-13 21:08:55 +03:00
|
|
|
{
|
|
|
|
$this->closePrevious();
|
2021-05-13 22:05:07 +03:00
|
|
|
$this->matcher->addMatcher($matcher);
|
2021-05-13 21:08:55 +03:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2021-05-14 18:52:00 +03:00
|
|
|
/**
|
|
|
|
* Repeat this mock provided amount of times.
|
|
|
|
* For example, if you pass 2 as an argument mock will be able to handle two identical requests.
|
|
|
|
*
|
|
|
|
* @param int $hits
|
|
|
|
*
|
2021-05-15 18:57:28 +03:00
|
|
|
* @return self
|
2021-05-14 18:52:00 +03:00
|
|
|
*/
|
2021-05-15 13:14:21 +03:00
|
|
|
public function repeat(int $hits): self
|
2021-05-14 18:52:00 +03:00
|
|
|
{
|
2021-05-20 20:56:45 +03:00
|
|
|
$this->closePrevious();
|
|
|
|
|
2021-05-14 18:52:00 +03:00
|
|
|
if ($hits > 0) {
|
|
|
|
$this->maxHits = $hits;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2021-05-20 20:56:45 +03:00
|
|
|
/**
|
|
|
|
* Always execute this mock if matched. Mock with this call will not be expired ever.
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function always(): self
|
|
|
|
{
|
|
|
|
$this->closePrevious();
|
|
|
|
$this->maxHits = -1;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Match request only at Nth hit. Previous matches will not be executed.
|
|
|
|
*
|
|
|
|
* **Note:** There IS a catch if you use this with the equal mocks. The test Client will not register hit
|
|
|
|
* for the second mock and the second mock will be executed at N+1 time.
|
|
|
|
*
|
|
|
|
* For example, if you try to send 5 requests with this mocks and log response codes:
|
|
|
|
* ```php
|
|
|
|
* $builder = new PockBuilder();
|
|
|
|
*
|
|
|
|
* $builder->matchHost('example.com')->at(2)->reply(200);
|
|
|
|
* $builder->matchHost('example.com')->at(4)->reply(201);
|
|
|
|
* $builder->always()->reply(400);
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* You will get this: 400, 400, 200, 400, 400, 201
|
|
|
|
* Instead of this: 400, 400, 200, 400, 201, 400
|
|
|
|
*
|
|
|
|
* @param int $hit
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function at(int $hit): self
|
|
|
|
{
|
|
|
|
$this->closePrevious();
|
|
|
|
$this->matchAt = $hit - 1;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2021-05-15 18:57:28 +03:00
|
|
|
/**
|
|
|
|
* Throw an exception when request is being sent.
|
|
|
|
*
|
|
|
|
* @param \Throwable $throwable
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function throwException(Throwable $throwable): self
|
|
|
|
{
|
|
|
|
$this->throwable = $throwable;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2021-05-20 20:56:45 +03:00
|
|
|
/**
|
|
|
|
* Throw an ClientExceptionInterface instance with specified message
|
|
|
|
*
|
|
|
|
* @param string $message
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function throwClientException(string $message = 'Pock ClientException'): self
|
|
|
|
{
|
|
|
|
return $this->throwException(new PockClientException($message));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throw an NetworkExceptionInterface instance with specified message
|
|
|
|
*
|
|
|
|
* @param string $message
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function throwNetworkException(string $message = 'Pock NetworkException'): self
|
|
|
|
{
|
|
|
|
return $this->throwException(new PockNetworkException($message));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throw an RequestExceptionInterface instance with specified message
|
|
|
|
*
|
|
|
|
* @param string $message
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function throwRequestException(string $message = 'Pock RequestException'): self
|
|
|
|
{
|
|
|
|
return $this->throwException(new PockRequestException($message));
|
|
|
|
}
|
|
|
|
|
2021-05-14 20:06:58 +03:00
|
|
|
/**
|
|
|
|
* @param int $statusCode
|
|
|
|
*
|
|
|
|
* @return \Pock\PockResponseBuilder
|
|
|
|
*/
|
|
|
|
public function reply(int $statusCode = 200): PockResponseBuilder
|
|
|
|
{
|
|
|
|
if (null === $this->responseBuilder) {
|
|
|
|
$this->responseBuilder = new PockResponseBuilder($statusCode);
|
|
|
|
|
|
|
|
return $this->responseBuilder;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->responseBuilder->withStatusCode($statusCode);
|
|
|
|
}
|
|
|
|
|
2021-05-21 09:38:15 +03:00
|
|
|
/**
|
|
|
|
* Construct the response during request execution using provided ReplytFactoryInterface implementation.
|
|
|
|
*
|
|
|
|
* @param \Pock\Factory\ReplyFactoryInterface $factory
|
|
|
|
* @see ReplyFactoryInterface
|
|
|
|
*/
|
|
|
|
public function replyWithFactory(ReplyFactoryInterface $factory): void
|
|
|
|
{
|
|
|
|
$this->replyFactory = $factory;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct the response during request execution using provided callback.
|
|
|
|
*
|
|
|
|
* Callback should receive the same parameters as in the `ReplyFactoryInterface::createReply` method.
|
|
|
|
*
|
|
|
|
* @see ReplyFactoryInterface::createReply()
|
|
|
|
*
|
|
|
|
* @param callable $callback
|
|
|
|
*/
|
|
|
|
public function replyWithCallback(callable $callback): void
|
|
|
|
{
|
|
|
|
$this->replyWithFactory(new CallbackReplyFactory($callback));
|
|
|
|
}
|
|
|
|
|
2021-05-13 21:08:55 +03:00
|
|
|
/**
|
|
|
|
* Resets the builder.
|
|
|
|
*
|
2021-05-15 13:14:21 +03:00
|
|
|
* @return self
|
2021-05-13 21:08:55 +03:00
|
|
|
*/
|
2021-05-15 13:14:21 +03:00
|
|
|
public function reset(): self
|
2021-05-13 21:08:55 +03:00
|
|
|
{
|
|
|
|
$this->matcher = new MultipleMatcher();
|
2021-05-21 09:38:15 +03:00
|
|
|
$this->replyFactory = null;
|
2021-05-14 20:06:58 +03:00
|
|
|
$this->responseBuilder = null;
|
2021-05-13 21:08:55 +03:00
|
|
|
$this->throwable = null;
|
2021-05-14 18:52:00 +03:00
|
|
|
$this->maxHits = 1;
|
2021-05-20 20:56:45 +03:00
|
|
|
$this->matchAt = -1;
|
2021-05-13 21:08:55 +03:00
|
|
|
$this->mocks = [];
|
2021-05-13 21:51:13 +03:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets fallback Client. It will be used if no request can be matched.
|
|
|
|
*
|
|
|
|
* @param \Psr\Http\Client\ClientInterface|null $fallbackClient
|
|
|
|
*
|
2021-05-15 13:14:21 +03:00
|
|
|
* @return self
|
2021-05-13 21:51:13 +03:00
|
|
|
*/
|
2021-05-15 13:14:21 +03:00
|
|
|
public function setFallbackClient(?ClientInterface $fallbackClient = null): self
|
2021-05-13 21:51:13 +03:00
|
|
|
{
|
|
|
|
$this->fallbackClient = $fallbackClient;
|
|
|
|
return $this;
|
2021-05-13 21:08:55 +03:00
|
|
|
}
|
|
|
|
|
2021-05-13 22:05:07 +03:00
|
|
|
/**
|
|
|
|
* @return \Pock\Client
|
|
|
|
*/
|
2021-05-13 21:08:55 +03:00
|
|
|
public function getClient(): Client
|
|
|
|
{
|
2021-05-15 13:14:21 +03:00
|
|
|
$this->closePrevious();
|
2021-05-13 21:51:13 +03:00
|
|
|
return new Client($this->mocks, $this->fallbackClient);
|
2021-05-13 21:08:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private function closePrevious(): void
|
|
|
|
{
|
2021-05-21 09:38:15 +03:00
|
|
|
if (null !== $this->responseBuilder || null !== $this->replyFactory || null !== $this->throwable) {
|
2021-05-13 21:08:55 +03:00
|
|
|
if (0 === count($this->matcher)) {
|
|
|
|
$this->matcher->addMatcher(new AnyRequestMatcher());
|
|
|
|
}
|
|
|
|
|
2021-05-14 20:06:58 +03:00
|
|
|
$response = null;
|
|
|
|
|
|
|
|
if (null !== $this->responseBuilder) {
|
|
|
|
$response = $this->responseBuilder->getResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->mocks[] = new Mock(
|
|
|
|
$this->matcher,
|
2021-05-21 09:38:15 +03:00
|
|
|
$this->replyFactory,
|
2021-05-14 20:06:58 +03:00
|
|
|
$response,
|
|
|
|
$this->throwable,
|
2021-05-20 20:56:45 +03:00
|
|
|
$this->maxHits,
|
|
|
|
$this->matchAt
|
2021-05-14 20:06:58 +03:00
|
|
|
);
|
2021-05-14 18:52:00 +03:00
|
|
|
$this->matcher = new MultipleMatcher();
|
2021-05-21 09:38:15 +03:00
|
|
|
$this->replyFactory = null;
|
2021-05-14 20:06:58 +03:00
|
|
|
$this->responseBuilder = null;
|
2021-05-13 21:08:55 +03:00
|
|
|
$this->throwable = null;
|
2021-05-14 18:52:00 +03:00
|
|
|
$this->maxHits = 1;
|
2021-05-20 20:56:45 +03:00
|
|
|
$this->matchAt = -1;
|
2021-05-13 21:08:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|