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 Mock
|
|
|
|
* @package Pock
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Pock;
|
|
|
|
|
2021-05-20 20:56:45 +03:00
|
|
|
use Pock\Exception\PockNetworkException;
|
|
|
|
use Pock\Exception\PockRequestException;
|
2021-05-21 09:38:15 +03:00
|
|
|
use Pock\Factory\ReplyFactoryInterface;
|
2021-05-13 21:08:55 +03:00
|
|
|
use Pock\Matchers\RequestMatcherInterface;
|
2021-05-20 20:56:45 +03:00
|
|
|
use Psr\Http\Client\NetworkExceptionInterface;
|
|
|
|
use Psr\Http\Client\RequestExceptionInterface;
|
|
|
|
use Psr\Http\Message\RequestInterface;
|
2021-05-13 21:08:55 +03:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
use Throwable;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Mock
|
|
|
|
*
|
|
|
|
* @category Mock
|
|
|
|
* @package Pock
|
|
|
|
*/
|
|
|
|
class Mock implements MockInterface
|
|
|
|
{
|
|
|
|
/** @var \Pock\Matchers\RequestMatcherInterface */
|
|
|
|
private $matcher;
|
|
|
|
|
2021-05-21 09:38:15 +03:00
|
|
|
/** @var \Pock\Factory\ReplyFactoryInterface|null */
|
|
|
|
private $replyFactory;
|
|
|
|
|
2021-05-13 21:08:55 +03:00
|
|
|
/** @var \Psr\Http\Message\ResponseInterface|null */
|
|
|
|
private $response;
|
|
|
|
|
|
|
|
/** @var \Throwable|null */
|
|
|
|
private $throwable;
|
|
|
|
|
2021-05-14 18:52:00 +03:00
|
|
|
/** @var int */
|
|
|
|
private $hits;
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
private $maxHits;
|
2021-05-13 21:08:55 +03:00
|
|
|
|
2021-05-20 20:56:45 +03:00
|
|
|
/** @var int */
|
|
|
|
private $matchAt;
|
|
|
|
|
2021-05-13 21:08:55 +03:00
|
|
|
/**
|
|
|
|
* Mock constructor.
|
|
|
|
*
|
|
|
|
* @param \Pock\Matchers\RequestMatcherInterface $matcher
|
2021-05-21 09:38:15 +03:00
|
|
|
* @param \Pock\Factory\ReplyFactoryInterface|null $replyFactory
|
2021-05-13 21:08:55 +03:00
|
|
|
* @param \Psr\Http\Message\ResponseInterface|null $response
|
|
|
|
* @param \Throwable|null $throwable
|
2021-05-14 18:52:00 +03:00
|
|
|
* @param int $maxHits
|
2021-05-20 20:56:45 +03:00
|
|
|
* @param int $matchAt
|
2021-05-13 21:08:55 +03:00
|
|
|
*/
|
2021-05-14 18:52:00 +03:00
|
|
|
public function __construct(
|
|
|
|
RequestMatcherInterface $matcher,
|
2021-05-21 09:38:15 +03:00
|
|
|
?ReplyFactoryInterface $replyFactory,
|
2021-05-14 18:52:00 +03:00
|
|
|
?ResponseInterface $response,
|
|
|
|
?Throwable $throwable,
|
2021-05-20 20:56:45 +03:00
|
|
|
int $maxHits,
|
|
|
|
int $matchAt
|
2021-05-14 18:52:00 +03:00
|
|
|
) {
|
2021-05-13 21:08:55 +03:00
|
|
|
$this->matcher = $matcher;
|
2021-05-21 09:38:15 +03:00
|
|
|
$this->replyFactory = $replyFactory;
|
2021-05-13 21:08:55 +03:00
|
|
|
$this->response = $response;
|
|
|
|
$this->throwable = $throwable;
|
2021-05-20 20:56:45 +03:00
|
|
|
$this->matchAt = $matchAt;
|
2021-05-14 18:52:00 +03:00
|
|
|
$this->maxHits = $maxHits;
|
|
|
|
$this->hits = 0;
|
2021-05-20 20:56:45 +03:00
|
|
|
|
|
|
|
if ($this->maxHits < ($matchAt + 1) && -1 !== $this->maxHits) {
|
|
|
|
$this->maxHits = $matchAt + 1;
|
|
|
|
}
|
2021-05-13 21:08:55 +03:00
|
|
|
}
|
|
|
|
|
2021-05-14 18:52:00 +03:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function registerHit(): MockInterface
|
2021-05-13 21:08:55 +03:00
|
|
|
{
|
2021-05-20 20:56:45 +03:00
|
|
|
if (-1 !== $this->maxHits) {
|
|
|
|
++$this->hits;
|
|
|
|
}
|
|
|
|
|
2021-05-13 21:08:55 +03:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-05-14 18:52:00 +03:00
|
|
|
* @inheritDoc
|
2021-05-13 21:08:55 +03:00
|
|
|
*/
|
2021-05-14 18:52:00 +03:00
|
|
|
public function available(): bool
|
2021-05-13 21:08:55 +03:00
|
|
|
{
|
2021-05-20 20:56:45 +03:00
|
|
|
return -1 === $this->maxHits || $this->hits < $this->maxHits;
|
2021-05-13 21:08:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-05-14 18:52:00 +03:00
|
|
|
* @inheritDoc
|
2021-05-13 21:08:55 +03:00
|
|
|
*/
|
2021-05-20 20:56:45 +03:00
|
|
|
public function matches(RequestInterface $request): bool
|
2021-05-13 21:08:55 +03:00
|
|
|
{
|
2021-05-20 20:56:45 +03:00
|
|
|
if ($this->matcher->matches($request)) {
|
|
|
|
if ($this->matchAt <= 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->matchAt === $this->hits) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->registerHit();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2021-05-13 21:08:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-05-14 18:52:00 +03:00
|
|
|
* @inheritDoc
|
2021-05-13 21:08:55 +03:00
|
|
|
*/
|
|
|
|
public function getResponse(): ?ResponseInterface
|
|
|
|
{
|
2021-05-15 18:57:28 +03:00
|
|
|
if (
|
|
|
|
null !== $this->response &&
|
|
|
|
null !== $this->response->getBody() &&
|
|
|
|
$this->response->getBody()->isSeekable()
|
|
|
|
) {
|
|
|
|
$this->response->getBody()->seek(0);
|
|
|
|
}
|
|
|
|
|
2021-05-13 21:08:55 +03:00
|
|
|
return $this->response;
|
|
|
|
}
|
|
|
|
|
2021-05-21 09:38:15 +03:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getReplyFactory(): ?ReplyFactoryInterface
|
|
|
|
{
|
|
|
|
return $this->replyFactory;
|
|
|
|
}
|
|
|
|
|
2021-05-13 21:08:55 +03:00
|
|
|
/**
|
2021-05-14 18:52:00 +03:00
|
|
|
* @inheritDoc
|
2021-05-13 21:08:55 +03:00
|
|
|
*/
|
2021-05-20 20:56:45 +03:00
|
|
|
public function getThrowable(RequestInterface $request): ?Throwable
|
2021-05-13 21:08:55 +03:00
|
|
|
{
|
2021-05-20 20:56:45 +03:00
|
|
|
if ($this->throwable instanceof PockRequestException || $this->throwable instanceof PockNetworkException) {
|
|
|
|
return $this->throwable->setRequest($request);
|
|
|
|
}
|
|
|
|
|
2021-05-13 21:08:55 +03:00
|
|
|
return $this->throwable;
|
|
|
|
}
|
|
|
|
}
|