Migrated ShortUrlVisitsActionTest to use PHPUnit mocks

This commit is contained in:
Alejandro Celaya 2022-10-23 22:27:27 +02:00
parent d4684fd01f
commit d414496a3c

View file

@ -7,10 +7,8 @@ namespace ShlinkioTest\Shlink\Rest\Action\Visit;
use Cake\Chronos\Chronos;
use Laminas\Diactoros\ServerRequestFactory;
use Pagerfanta\Adapter\ArrayAdapter;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\ServerRequestInterface;
use Shlinkio\Shlink\Common\Paginator\Paginator;
use Shlinkio\Shlink\Common\Util\DateRange;
@ -22,27 +20,24 @@ use Shlinkio\Shlink\Rest\Entity\ApiKey;
class ShortUrlVisitsActionTest extends TestCase
{
use ProphecyTrait;
private ShortUrlVisitsAction $action;
private ObjectProphecy $visitsHelper;
private MockObject $visitsHelper;
protected function setUp(): void
{
$this->visitsHelper = $this->prophesize(VisitsStatsHelperInterface::class);
$this->action = new ShortUrlVisitsAction($this->visitsHelper->reveal());
$this->visitsHelper = $this->createMock(VisitsStatsHelperInterface::class);
$this->action = new ShortUrlVisitsAction($this->visitsHelper);
}
/** @test */
public function providingCorrectShortCodeReturnsVisits(): void
{
$shortCode = 'abc123';
$this->visitsHelper->visitsForShortUrl(
$this->visitsHelper->expects($this->once())->method('visitsForShortUrl')->with(
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
Argument::type(VisitsParams::class),
Argument::type(ApiKey::class),
)->willReturn(new Paginator(new ArrayAdapter([])))
->shouldBeCalledOnce();
$this->isInstanceOf(VisitsParams::class),
$this->isInstanceOf(ApiKey::class),
)->willReturn(new Paginator(new ArrayAdapter([])));
$response = $this->action->handle($this->requestWithApiKey()->withAttribute('shortCode', $shortCode));
self::assertEquals(200, $response->getStatusCode());
@ -52,13 +47,15 @@ class ShortUrlVisitsActionTest extends TestCase
public function paramsAreReadFromQuery(): void
{
$shortCode = 'abc123';
$this->visitsHelper->visitsForShortUrl(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode), new VisitsParams(
$this->visitsHelper->expects($this->once())->method('visitsForShortUrl')->with(
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
new VisitsParams(
DateRange::until(Chronos::parse('2016-01-01 00:00:00')),
3,
10,
), Argument::type(ApiKey::class))
->willReturn(new Paginator(new ArrayAdapter([])))
->shouldBeCalledOnce();
),
$this->isInstanceOf(ApiKey::class),
)->willReturn(new Paginator(new ArrayAdapter([])));
$response = $this->action->handle(
$this->requestWithApiKey()->withAttribute('shortCode', $shortCode)