shlink/module/Rest/test/Action/Visit/GlobalVisitsActionTest.php

41 lines
1.3 KiB
PHP
Raw Normal View History

2020-05-01 12:57:46 +03:00
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Action\Visit;
use Laminas\Diactoros\Response\JsonResponse;
use Laminas\Diactoros\ServerRequestFactory;
use PHPUnit\Framework\MockObject\MockObject;
2020-05-01 12:57:46 +03:00
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Core\Visit\Model\VisitsStats;
use Shlinkio\Shlink\Core\Visit\VisitsStatsHelperInterface;
use Shlinkio\Shlink\Rest\Action\Visit\GlobalVisitsAction;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
2020-05-01 12:57:46 +03:00
class GlobalVisitsActionTest extends TestCase
{
private GlobalVisitsAction $action;
2022-10-24 20:53:13 +03:00
private MockObject & VisitsStatsHelperInterface $helper;
2020-05-01 12:57:46 +03:00
protected function setUp(): void
2020-05-01 12:57:46 +03:00
{
$this->helper = $this->createMock(VisitsStatsHelperInterface::class);
$this->action = new GlobalVisitsAction($this->helper);
2020-05-01 12:57:46 +03:00
}
/** @test */
public function statsAreReturnedFromHelper(): void
{
$apiKey = ApiKey::create();
$stats = new VisitsStats(5, 3);
$this->helper->expects($this->once())->method('getVisitsStats')->with($apiKey)->willReturn($stats);
2020-05-01 12:57:46 +03:00
/** @var JsonResponse $resp */
$resp = $this->action->handle(ServerRequestFactory::fromGlobals()->withAttribute(ApiKey::class, $apiKey));
2020-05-01 12:57:46 +03:00
$payload = $resp->getPayload();
2020-10-04 01:35:14 +03:00
self::assertEquals($payload, ['visits' => $stats]);
2020-05-01 12:57:46 +03:00
}
}