pock/src/Mock.php

158 lines
3.5 KiB
PHP
Raw Normal View History

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;
use Pock\Exception\PockNetworkException;
use Pock\Exception\PockRequestException;
use Pock\Factory\ReplyFactoryInterface;
2021-05-13 21:08:55 +03:00
use Pock\Matchers\RequestMatcherInterface;
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;
/** @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
/** @var int */
private $matchAt;
2021-05-13 21:08:55 +03:00
/**
* Mock constructor.
*
* @param \Pock\Matchers\RequestMatcherInterface $matcher
* @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
* @param int $matchAt
2021-05-13 21:08:55 +03:00
*/
2021-05-14 18:52:00 +03:00
public function __construct(
RequestMatcherInterface $matcher,
?ReplyFactoryInterface $replyFactory,
2021-05-14 18:52:00 +03:00
?ResponseInterface $response,
?Throwable $throwable,
int $maxHits,
int $matchAt
2021-05-14 18:52:00 +03:00
) {
2021-05-13 21:08:55 +03:00
$this->matcher = $matcher;
$this->replyFactory = $replyFactory;
2021-05-13 21:08:55 +03:00
$this->response = $response;
$this->throwable = $throwable;
$this->matchAt = $matchAt;
2021-05-14 18:52:00 +03:00
$this->maxHits = $maxHits;
$this->hits = 0;
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
{
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
{
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
*/
public function matches(RequestInterface $request): bool
2021-05-13 21:08:55 +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
{
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;
}
/**
* @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
*/
public function getThrowable(RequestInterface $request): ?Throwable
2021-05-13 21:08:55 +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;
}
}