Migrated OrphanVisitsActionTest to use PHPUnit mocks

This commit is contained in:
Alejandro Celaya 2022-10-23 22:25:54 +02:00
parent bb444a02fe
commit d4684fd01f

View File

@ -7,10 +7,8 @@ namespace ShlinkioTest\Shlink\Rest\Action\Visit;
use Laminas\Diactoros\Response\JsonResponse; use Laminas\Diactoros\Response\JsonResponse;
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 Shlinkio\Shlink\Common\Paginator\Paginator; use Shlinkio\Shlink\Common\Paginator\Paginator;
use Shlinkio\Shlink\Common\Rest\DataTransformerInterface; use Shlinkio\Shlink\Common\Rest\DataTransformerInterface;
use Shlinkio\Shlink\Core\Visit\Entity\Visit; use Shlinkio\Shlink\Core\Visit\Entity\Visit;
@ -23,18 +21,16 @@ use function count;
class OrphanVisitsActionTest extends TestCase class OrphanVisitsActionTest extends TestCase
{ {
use ProphecyTrait;
private OrphanVisitsAction $action; private OrphanVisitsAction $action;
private ObjectProphecy $visitsHelper; private MockObject $visitsHelper;
private ObjectProphecy $orphanVisitTransformer; private MockObject $orphanVisitTransformer;
protected function setUp(): void protected function setUp(): void
{ {
$this->visitsHelper = $this->prophesize(VisitsStatsHelperInterface::class); $this->visitsHelper = $this->createMock(VisitsStatsHelperInterface::class);
$this->orphanVisitTransformer = $this->prophesize(DataTransformerInterface::class); $this->orphanVisitTransformer = $this->createMock(DataTransformerInterface::class);
$this->action = new OrphanVisitsAction($this->visitsHelper->reveal(), $this->orphanVisitTransformer->reveal()); $this->action = new OrphanVisitsAction($this->visitsHelper, $this->orphanVisitTransformer);
} }
/** @test */ /** @test */
@ -42,11 +38,13 @@ class OrphanVisitsActionTest extends TestCase
{ {
$visitor = Visitor::emptyInstance(); $visitor = Visitor::emptyInstance();
$visits = [Visit::forInvalidShortUrl($visitor), Visit::forRegularNotFound($visitor)]; $visits = [Visit::forInvalidShortUrl($visitor), Visit::forRegularNotFound($visitor)];
$orphanVisits = $this->visitsHelper->orphanVisits(Argument::type(VisitsParams::class))->willReturn( $this->visitsHelper->expects($this->once())->method('orphanVisits')->with(
new Paginator(new ArrayAdapter($visits)), $this->isInstanceOf(VisitsParams::class),
); )->willReturn(new Paginator(new ArrayAdapter($visits)));
$visitsAmount = count($visits); $visitsAmount = count($visits);
$transform = $this->orphanVisitTransformer->transform(Argument::type(Visit::class))->willReturn([]); $this->orphanVisitTransformer->expects($this->exactly($visitsAmount))->method('transform')->with(
$this->isInstanceOf(Visit::class),
)->willReturn([]);
/** @var JsonResponse $response */ /** @var JsonResponse $response */
$response = $this->action->handle(ServerRequestFactory::fromGlobals()); $response = $this->action->handle(ServerRequestFactory::fromGlobals());
@ -54,7 +52,5 @@ class OrphanVisitsActionTest extends TestCase
self::assertCount($visitsAmount, $payload['visits']['data']); self::assertCount($visitsAmount, $payload['visits']['data']);
self::assertEquals(200, $response->getStatusCode()); self::assertEquals(200, $response->getStatusCode());
$orphanVisits->shouldHaveBeenCalledOnce();
$transform->shouldHaveBeenCalledTimes($visitsAmount);
} }
} }