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\Matchers\RequestMatcherInterface;
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
use Throwable;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Mock
|
|
|
|
*
|
|
|
|
* @category Mock
|
|
|
|
* @package Pock
|
|
|
|
*/
|
|
|
|
class Mock implements MockInterface
|
|
|
|
{
|
|
|
|
/** @var \Pock\Matchers\RequestMatcherInterface */
|
|
|
|
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 $hits;
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
private $maxHits;
|
2021-05-13 21:08:55 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Mock constructor.
|
|
|
|
*
|
|
|
|
* @param \Pock\Matchers\RequestMatcherInterface $matcher
|
|
|
|
* @param \Psr\Http\Message\ResponseInterface|null $response
|
|
|
|
* @param \Throwable|null $throwable
|
2021-05-14 18:52:00 +03:00
|
|
|
* @param int $maxHits
|
2021-05-13 21:08:55 +03:00
|
|
|
*/
|
2021-05-14 18:52:00 +03:00
|
|
|
public function __construct(
|
|
|
|
RequestMatcherInterface $matcher,
|
|
|
|
?ResponseInterface $response,
|
|
|
|
?Throwable $throwable,
|
|
|
|
int $maxHits
|
|
|
|
) {
|
2021-05-13 21:08:55 +03:00
|
|
|
$this->matcher = $matcher;
|
|
|
|
$this->response = $response;
|
|
|
|
$this->throwable = $throwable;
|
2021-05-14 18:52:00 +03:00
|
|
|
$this->maxHits = $maxHits;
|
|
|
|
$this->hits = 0;
|
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-14 18:52:00 +03:00
|
|
|
++$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-14 18:52:00 +03:00
|
|
|
return $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 getMatcher(): RequestMatcherInterface
|
|
|
|
{
|
|
|
|
return $this->matcher;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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-14 18:52:00 +03:00
|
|
|
* @inheritDoc
|
2021-05-13 21:08:55 +03:00
|
|
|
*/
|
|
|
|
public function getThrowable(): ?Throwable
|
|
|
|
{
|
|
|
|
return $this->throwable;
|
|
|
|
}
|
|
|
|
}
|