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