2020-05-01 04:57:46 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Rest\Action\Visit;
|
|
|
|
|
|
|
|
use Laminas\Diactoros\Response\JsonResponse;
|
|
|
|
use Laminas\Diactoros\ServerRequestFactory;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2020-11-02 04:50:19 -06:00
|
|
|
use Prophecy\PhpUnit\ProphecyTrait;
|
2020-05-01 04:57:46 -05:00
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
|
|
|
use Shlinkio\Shlink\Core\Visit\Model\VisitsStats;
|
|
|
|
use Shlinkio\Shlink\Core\Visit\VisitsStatsHelperInterface;
|
|
|
|
use Shlinkio\Shlink\Rest\Action\Visit\GlobalVisitsAction;
|
2021-01-04 04:27:55 -06:00
|
|
|
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
2020-05-01 04:57:46 -05:00
|
|
|
|
|
|
|
class GlobalVisitsActionTest extends TestCase
|
|
|
|
{
|
2020-11-02 04:50:19 -06:00
|
|
|
use ProphecyTrait;
|
|
|
|
|
2020-05-01 04:57:46 -05:00
|
|
|
private GlobalVisitsAction $action;
|
|
|
|
private ObjectProphecy $helper;
|
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
$this->helper = $this->prophesize(VisitsStatsHelperInterface::class);
|
|
|
|
$this->action = new GlobalVisitsAction($this->helper->reveal());
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function statsAreReturnedFromHelper(): void
|
|
|
|
{
|
2021-01-04 04:27:55 -06:00
|
|
|
$apiKey = new ApiKey();
|
2020-05-01 04:57:46 -05:00
|
|
|
$stats = new VisitsStats(5);
|
2021-01-04 04:27:55 -06:00
|
|
|
$getStats = $this->helper->getVisitsStats($apiKey)->willReturn($stats);
|
2020-05-01 04:57:46 -05:00
|
|
|
|
|
|
|
/** @var JsonResponse $resp */
|
2021-01-04 04:27:55 -06:00
|
|
|
$resp = $this->action->handle(ServerRequestFactory::fromGlobals()->withAttribute(ApiKey::class, $apiKey));
|
2020-05-01 04:57:46 -05:00
|
|
|
$payload = $resp->getPayload();
|
|
|
|
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertEquals($payload, ['visits' => $stats]);
|
2020-05-01 04:57:46 -05:00
|
|
|
$getStats->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
|
|
|
}
|