Migrated NonOrphanVisitsPaginatorAdapterTest to use PHPUnit mocks

This commit is contained in:
Alejandro Celaya 2022-10-23 21:55:06 +02:00
parent d3af51f684
commit 7442905873

View File

@ -4,9 +4,8 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\Visit\Paginator\Adapter; namespace ShlinkioTest\Shlink\Core\Visit\Paginator\Adapter;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Visit\Entity\Visit; use Shlinkio\Shlink\Core\Visit\Entity\Visit;
use Shlinkio\Shlink\Core\Visit\Model\Visitor; use Shlinkio\Shlink\Core\Visit\Model\Visitor;
use Shlinkio\Shlink\Core\Visit\Model\VisitsParams; use Shlinkio\Shlink\Core\Visit\Model\VisitsParams;
@ -18,34 +17,31 @@ use Shlinkio\Shlink\Rest\Entity\ApiKey;
class NonOrphanVisitsPaginatorAdapterTest extends TestCase class NonOrphanVisitsPaginatorAdapterTest extends TestCase
{ {
use ProphecyTrait;
private NonOrphanVisitsPaginatorAdapter $adapter; private NonOrphanVisitsPaginatorAdapter $adapter;
private ObjectProphecy $repo; private MockObject $repo;
private VisitsParams $params; private VisitsParams $params;
private ApiKey $apiKey; private ApiKey $apiKey;
protected function setUp(): void protected function setUp(): void
{ {
$this->repo = $this->prophesize(VisitRepositoryInterface::class); $this->repo = $this->createMock(VisitRepositoryInterface::class);
$this->params = VisitsParams::fromRawData([]); $this->params = VisitsParams::fromRawData([]);
$this->apiKey = ApiKey::create(); $this->apiKey = ApiKey::create();
$this->adapter = new NonOrphanVisitsPaginatorAdapter($this->repo->reveal(), $this->params, $this->apiKey); $this->adapter = new NonOrphanVisitsPaginatorAdapter($this->repo, $this->params, $this->apiKey);
} }
/** @test */ /** @test */
public function countDelegatesToRepository(): void public function countDelegatesToRepository(): void
{ {
$expectedCount = 5; $expectedCount = 5;
$repoCount = $this->repo->countNonOrphanVisits( $this->repo->expects($this->once())->method('countNonOrphanVisits')->with(
new VisitsCountFiltering($this->params->dateRange, $this->params->excludeBots, $this->apiKey), new VisitsCountFiltering($this->params->dateRange, $this->params->excludeBots, $this->apiKey),
)->willReturn($expectedCount); )->willReturn($expectedCount);
$result = $this->adapter->getNbResults(); $result = $this->adapter->getNbResults();
self::assertEquals($expectedCount, $result); self::assertEquals($expectedCount, $result);
$repoCount->shouldHaveBeenCalledOnce();
} }
/** /**
@ -56,7 +52,7 @@ class NonOrphanVisitsPaginatorAdapterTest extends TestCase
{ {
$visitor = Visitor::emptyInstance(); $visitor = Visitor::emptyInstance();
$list = [Visit::forRegularNotFound($visitor), Visit::forInvalidShortUrl($visitor)]; $list = [Visit::forRegularNotFound($visitor), Visit::forInvalidShortUrl($visitor)];
$repoFind = $this->repo->findNonOrphanVisits(new VisitsListFiltering( $this->repo->expects($this->once())->method('findNonOrphanVisits')->with(new VisitsListFiltering(
$this->params->dateRange, $this->params->dateRange,
$this->params->excludeBots, $this->params->excludeBots,
$this->apiKey, $this->apiKey,
@ -67,7 +63,6 @@ class NonOrphanVisitsPaginatorAdapterTest extends TestCase
$result = $this->adapter->getSlice($offset, $limit); $result = $this->adapter->getSlice($offset, $limit);
self::assertEquals($list, $result); self::assertEquals($list, $result);
$repoFind->shouldHaveBeenCalledOnce();
} }
public function provideLimitAndOffset(): iterable public function provideLimitAndOffset(): iterable