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

76 lines
2.8 KiB
PHP
Raw Normal View History

2016-07-31 17:10:16 +03:00
<?php
2019-10-05 18:26:10 +03:00
2017-10-12 11:13:20 +03:00
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Action\Visit;
2016-07-31 17:10:16 +03:00
use Cake\Chronos\Chronos;
use Laminas\Diactoros\ServerRequestFactory;
use Pagerfanta\Adapter\ArrayAdapter;
use PHPUnit\Framework\MockObject\MockObject;
2017-03-24 22:34:18 +03:00
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ServerRequestInterface;
use Shlinkio\Shlink\Common\Paginator\Paginator;
2016-07-31 17:10:16 +03:00
use Shlinkio\Shlink\Common\Util\DateRange;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
use Shlinkio\Shlink\Core\Visit\Model\VisitsParams;
use Shlinkio\Shlink\Core\Visit\VisitsStatsHelperInterface;
use Shlinkio\Shlink\Rest\Action\Visit\ShortUrlVisitsAction;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
2016-07-31 17:10:16 +03:00
class ShortUrlVisitsActionTest extends TestCase
2016-07-31 17:10:16 +03:00
{
private ShortUrlVisitsAction $action;
2022-10-24 20:53:13 +03:00
private MockObject & VisitsStatsHelperInterface $visitsHelper;
2016-07-31 17:10:16 +03:00
protected function setUp(): void
2016-07-31 17:10:16 +03:00
{
$this->visitsHelper = $this->createMock(VisitsStatsHelperInterface::class);
$this->action = new ShortUrlVisitsAction($this->visitsHelper);
2016-07-31 17:10:16 +03:00
}
2019-02-17 22:28:34 +03:00
/** @test */
public function providingCorrectShortCodeReturnsVisits(): void
2016-07-31 17:10:16 +03:00
{
$shortCode = 'abc123';
$this->visitsHelper->expects($this->once())->method('visitsForShortUrl')->with(
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
$this->isInstanceOf(VisitsParams::class),
$this->isInstanceOf(ApiKey::class),
)->willReturn(new Paginator(new ArrayAdapter([])));
2016-07-31 17:10:16 +03:00
$response = $this->action->handle($this->requestWithApiKey()->withAttribute('shortCode', $shortCode));
2020-10-04 01:35:14 +03:00
self::assertEquals(200, $response->getStatusCode());
2016-07-31 17:10:16 +03:00
}
2019-02-17 22:28:34 +03:00
/** @test */
public function paramsAreReadFromQuery(): void
2016-07-31 17:10:16 +03:00
{
$shortCode = 'abc123';
$this->visitsHelper->expects($this->once())->method('visitsForShortUrl')->with(
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
new VisitsParams(
DateRange::until(Chronos::parse('2016-01-01 00:00:00')),
3,
10,
),
$this->isInstanceOf(ApiKey::class),
)->willReturn(new Paginator(new ArrayAdapter([])));
2016-07-31 17:10:16 +03:00
2018-03-26 20:02:41 +03:00
$response = $this->action->handle(
$this->requestWithApiKey()->withAttribute('shortCode', $shortCode)
->withQueryParams([
'endDate' => '2016-01-01 00:00:00',
'page' => '3',
'itemsPerPage' => '10',
]),
2016-07-31 17:10:16 +03:00
);
2020-10-04 01:35:14 +03:00
self::assertEquals(200, $response->getStatusCode());
2016-07-31 17:10:16 +03:00
}
private function requestWithApiKey(): ServerRequestInterface
{
return ServerRequestFactory::fromGlobals()->withAttribute(ApiKey::class, ApiKey::create());
}
2016-07-31 17:10:16 +03:00
}