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