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