2021-05-13 21:08:55 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PHP 7.3
|
|
|
|
*
|
|
|
|
* @category PockBuilder
|
|
|
|
* @package Pock
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Pock;
|
|
|
|
|
2021-05-13 22:05:07 +03:00
|
|
|
use Pock\Enum\RequestScheme;
|
2021-05-13 21:08:55 +03:00
|
|
|
use Pock\Matchers\AnyRequestMatcher;
|
|
|
|
use Pock\Matchers\HostMatcher;
|
|
|
|
use Pock\Matchers\MultipleMatcher;
|
2021-05-13 22:05:07 +03:00
|
|
|
use Pock\Matchers\RequestMatcherInterface;
|
|
|
|
use Pock\Matchers\SchemeMatcher;
|
|
|
|
use Pock\Matchers\UriMatcher;
|
2021-05-13 21:51:13 +03:00
|
|
|
use Psr\Http\Client\ClientInterface;
|
2021-05-13 21:08:55 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class PockBuilder
|
|
|
|
*
|
|
|
|
* @category PockBuilder
|
|
|
|
* @package Pock
|
|
|
|
*/
|
|
|
|
class PockBuilder
|
|
|
|
{
|
|
|
|
/** @var \Pock\Matchers\MultipleMatcher */
|
|
|
|
private $matcher;
|
|
|
|
|
|
|
|
/** @var \Psr\Http\Message\ResponseInterface|null */
|
|
|
|
private $response;
|
|
|
|
|
|
|
|
/** @var \Throwable|null */
|
|
|
|
private $throwable;
|
|
|
|
|
2021-05-14 18:52:00 +03:00
|
|
|
/** @var int */
|
|
|
|
private $maxHits;
|
|
|
|
|
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-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
|
|
|
*/
|
|
|
|
public function matchScheme(string $scheme = RequestScheme::HTTP): PockBuilder
|
|
|
|
{
|
|
|
|
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-13 21:51:13 +03:00
|
|
|
public function matchHost(string $host): PockBuilder
|
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
|
|
|
|
*
|
|
|
|
* @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
|
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
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function repeat(int $hits): PockBuilder
|
|
|
|
{
|
|
|
|
if ($hits > 0) {
|
|
|
|
$this->maxHits = $hits;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2021-05-13 21:08:55 +03:00
|
|
|
/**
|
|
|
|
* Resets the builder.
|
|
|
|
*
|
|
|
|
* @return \Pock\PockBuilder
|
|
|
|
*/
|
|
|
|
public function reset(): PockBuilder
|
|
|
|
{
|
|
|
|
$this->matcher = new MultipleMatcher();
|
|
|
|
$this->response = null;
|
|
|
|
$this->throwable = null;
|
2021-05-14 18:52:00 +03:00
|
|
|
$this->maxHits = 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
|
|
|
|
*
|
|
|
|
* @return \Pock\PockBuilder
|
|
|
|
*/
|
|
|
|
public function setFallbackClient(?ClientInterface $fallbackClient = null): PockBuilder
|
|
|
|
{
|
|
|
|
$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-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
|
|
|
|
{
|
|
|
|
if (null !== $this->response || null !== $this->throwable) {
|
|
|
|
if (0 === count($this->matcher)) {
|
|
|
|
$this->matcher->addMatcher(new AnyRequestMatcher());
|
|
|
|
}
|
|
|
|
|
2021-05-14 18:52:00 +03:00
|
|
|
$this->mocks[] = new Mock($this->matcher, $this->response, $this->throwable, $this->maxHits);
|
|
|
|
$this->matcher = new MultipleMatcher();
|
2021-05-13 21:08:55 +03:00
|
|
|
$this->response = null;
|
|
|
|
$this->throwable = null;
|
2021-05-14 18:52:00 +03:00
|
|
|
$this->maxHits = 1;
|
2021-05-13 21:08:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|