Migrated VisitsForTagPaginatorAdapterTest to use PHPUnit mocks

This commit is contained in:
Alejandro Celaya 2022-10-23 21:59:18 +02:00
parent a4373aee91
commit 257134cd80
2 changed files with 12 additions and 31 deletions

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\Common\Util\DateRange; use Shlinkio\Shlink\Common\Util\DateRange;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier; use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
use Shlinkio\Shlink\Core\Visit\Model\VisitsParams; use Shlinkio\Shlink\Core\Visit\Model\VisitsParams;
@ -18,13 +17,11 @@ use Shlinkio\Shlink\Rest\Entity\ApiKey;
class ShortUrlVisitsPaginatorAdapterTest extends TestCase class ShortUrlVisitsPaginatorAdapterTest extends TestCase
{ {
use ProphecyTrait; private MockObject $repo;
private ObjectProphecy $repo;
protected function setUp(): void protected function setUp(): void
{ {
$this->repo = $this->prophesize(VisitRepositoryInterface::class); $this->repo = $this->createMock(VisitRepositoryInterface::class);
} }
/** @test */ /** @test */
@ -34,7 +31,7 @@ class ShortUrlVisitsPaginatorAdapterTest extends TestCase
$limit = 1; $limit = 1;
$offset = 5; $offset = 5;
$adapter = $this->createAdapter(null); $adapter = $this->createAdapter(null);
$findVisits = $this->repo->findVisitsByShortCode( $this->repo->expects($this->exactly($count))->method('findVisitsByShortCode')->with(
ShortUrlIdentifier::fromShortCodeAndDomain(''), ShortUrlIdentifier::fromShortCodeAndDomain(''),
new VisitsListFiltering(DateRange::allTime(), false, null, $limit, $offset), new VisitsListFiltering(DateRange::allTime(), false, null, $limit, $offset),
)->willReturn([]); )->willReturn([]);
@ -42,8 +39,6 @@ class ShortUrlVisitsPaginatorAdapterTest extends TestCase
for ($i = 0; $i < $count; $i++) { for ($i = 0; $i < $count; $i++) {
$adapter->getSlice($offset, $limit); $adapter->getSlice($offset, $limit);
} }
$findVisits->shouldHaveBeenCalledTimes($count);
} }
/** @test */ /** @test */
@ -52,7 +47,7 @@ class ShortUrlVisitsPaginatorAdapterTest extends TestCase
$count = 3; $count = 3;
$apiKey = ApiKey::create(); $apiKey = ApiKey::create();
$adapter = $this->createAdapter($apiKey); $adapter = $this->createAdapter($apiKey);
$countVisits = $this->repo->countVisitsByShortCode( $this->repo->expects($this->once())->method('countVisitsByShortCode')->with(
ShortUrlIdentifier::fromShortCodeAndDomain(''), ShortUrlIdentifier::fromShortCodeAndDomain(''),
new VisitsCountFiltering(DateRange::allTime(), false, $apiKey), new VisitsCountFiltering(DateRange::allTime(), false, $apiKey),
)->willReturn(3); )->willReturn(3);
@ -60,14 +55,12 @@ class ShortUrlVisitsPaginatorAdapterTest extends TestCase
for ($i = 0; $i < $count; $i++) { for ($i = 0; $i < $count; $i++) {
$adapter->getNbResults(); $adapter->getNbResults();
} }
$countVisits->shouldHaveBeenCalledOnce();
} }
private function createAdapter(?ApiKey $apiKey): ShortUrlVisitsPaginatorAdapter private function createAdapter(?ApiKey $apiKey): ShortUrlVisitsPaginatorAdapter
{ {
return new ShortUrlVisitsPaginatorAdapter( return new ShortUrlVisitsPaginatorAdapter(
$this->repo->reveal(), $this->repo,
ShortUrlIdentifier::fromShortCodeAndDomain(''), ShortUrlIdentifier::fromShortCodeAndDomain(''),
VisitsParams::fromRawData([]), VisitsParams::fromRawData([]),
$apiKey, $apiKey,

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\Common\Util\DateRange; use Shlinkio\Shlink\Common\Util\DateRange;
use Shlinkio\Shlink\Core\Visit\Model\VisitsParams; use Shlinkio\Shlink\Core\Visit\Model\VisitsParams;
use Shlinkio\Shlink\Core\Visit\Paginator\Adapter\TagVisitsPaginatorAdapter; use Shlinkio\Shlink\Core\Visit\Paginator\Adapter\TagVisitsPaginatorAdapter;
@ -17,13 +16,11 @@ use Shlinkio\Shlink\Rest\Entity\ApiKey;
class VisitsForTagPaginatorAdapterTest extends TestCase class VisitsForTagPaginatorAdapterTest extends TestCase
{ {
use ProphecyTrait; private MockObject $repo;
private ObjectProphecy $repo;
protected function setUp(): void protected function setUp(): void
{ {
$this->repo = $this->prophesize(VisitRepositoryInterface::class); $this->repo = $this->createMock(VisitRepositoryInterface::class);
} }
/** @test */ /** @test */
@ -33,7 +30,7 @@ class VisitsForTagPaginatorAdapterTest extends TestCase
$limit = 1; $limit = 1;
$offset = 5; $offset = 5;
$adapter = $this->createAdapter(null); $adapter = $this->createAdapter(null);
$findVisits = $this->repo->findVisitsByTag( $this->repo->expects($this->exactly($count))->method('findVisitsByTag')->with(
'foo', 'foo',
new VisitsListFiltering(DateRange::allTime(), false, null, $limit, $offset), new VisitsListFiltering(DateRange::allTime(), false, null, $limit, $offset),
)->willReturn([]); )->willReturn([]);
@ -41,8 +38,6 @@ class VisitsForTagPaginatorAdapterTest extends TestCase
for ($i = 0; $i < $count; $i++) { for ($i = 0; $i < $count; $i++) {
$adapter->getSlice($offset, $limit); $adapter->getSlice($offset, $limit);
} }
$findVisits->shouldHaveBeenCalledTimes($count);
} }
/** @test */ /** @test */
@ -51,7 +46,7 @@ class VisitsForTagPaginatorAdapterTest extends TestCase
$count = 3; $count = 3;
$apiKey = ApiKey::create(); $apiKey = ApiKey::create();
$adapter = $this->createAdapter($apiKey); $adapter = $this->createAdapter($apiKey);
$countVisits = $this->repo->countVisitsByTag( $this->repo->expects($this->once())->method('countVisitsByTag')->with(
'foo', 'foo',
new VisitsCountFiltering(DateRange::allTime(), false, $apiKey), new VisitsCountFiltering(DateRange::allTime(), false, $apiKey),
)->willReturn(3); )->willReturn(3);
@ -59,17 +54,10 @@ class VisitsForTagPaginatorAdapterTest extends TestCase
for ($i = 0; $i < $count; $i++) { for ($i = 0; $i < $count; $i++) {
$adapter->getNbResults(); $adapter->getNbResults();
} }
$countVisits->shouldHaveBeenCalledOnce();
} }
private function createAdapter(?ApiKey $apiKey): TagVisitsPaginatorAdapter private function createAdapter(?ApiKey $apiKey): TagVisitsPaginatorAdapter
{ {
return new TagVisitsPaginatorAdapter( return new TagVisitsPaginatorAdapter($this->repo, 'foo', VisitsParams::fromRawData([]), $apiKey);
$this->repo->reveal(),
'foo',
VisitsParams::fromRawData([]),
$apiKey,
);
} }
} }