Migrated GlobalVisitsActionTest to use PHPUnit mocks

This commit is contained in:
Alejandro Celaya 2022-10-23 22:23:08 +02:00
parent f493baaf2b
commit e980a8d121

View File

@ -6,9 +6,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 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\Model\VisitsStats; use Shlinkio\Shlink\Core\Visit\Model\VisitsStats;
use Shlinkio\Shlink\Core\Visit\VisitsStatsHelperInterface; use Shlinkio\Shlink\Core\Visit\VisitsStatsHelperInterface;
use Shlinkio\Shlink\Rest\Action\Visit\GlobalVisitsAction; use Shlinkio\Shlink\Rest\Action\Visit\GlobalVisitsAction;
@ -16,15 +15,13 @@ use Shlinkio\Shlink\Rest\Entity\ApiKey;
class GlobalVisitsActionTest extends TestCase class GlobalVisitsActionTest extends TestCase
{ {
use ProphecyTrait;
private GlobalVisitsAction $action; private GlobalVisitsAction $action;
private ObjectProphecy $helper; private MockObject $helper;
protected function setUp(): void protected function setUp(): void
{ {
$this->helper = $this->prophesize(VisitsStatsHelperInterface::class); $this->helper = $this->createMock(VisitsStatsHelperInterface::class);
$this->action = new GlobalVisitsAction($this->helper->reveal()); $this->action = new GlobalVisitsAction($this->helper);
} }
/** @test */ /** @test */
@ -32,13 +29,12 @@ class GlobalVisitsActionTest extends TestCase
{ {
$apiKey = ApiKey::create(); $apiKey = ApiKey::create();
$stats = new VisitsStats(5, 3); $stats = new VisitsStats(5, 3);
$getStats = $this->helper->getVisitsStats($apiKey)->willReturn($stats); $this->helper->expects($this->once())->method('getVisitsStats')->with($apiKey)->willReturn($stats);
/** @var JsonResponse $resp */ /** @var JsonResponse $resp */
$resp = $this->action->handle(ServerRequestFactory::fromGlobals()->withAttribute(ApiKey::class, $apiKey)); $resp = $this->action->handle(ServerRequestFactory::fromGlobals()->withAttribute(ApiKey::class, $apiKey));
$payload = $resp->getPayload(); $payload = $resp->getPayload();
self::assertEquals($payload, ['visits' => $stats]); self::assertEquals($payload, ['visits' => $stats]);
$getStats->shouldHaveBeenCalledOnce();
} }
} }