2019-11-20 13:58:16 -06:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioApiTest\Shlink\Rest\Action;
|
|
|
|
|
2020-02-02 03:46:38 -06:00
|
|
|
use Laminas\Diactoros\Uri;
|
2019-11-20 13:58:16 -06:00
|
|
|
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
|
|
|
|
|
2020-02-02 03:46:38 -06:00
|
|
|
use function GuzzleHttp\Psr7\build_query;
|
|
|
|
use function sprintf;
|
|
|
|
|
2019-11-20 13:58:16 -06:00
|
|
|
class GetVisitsActionTest extends ApiTestCase
|
|
|
|
{
|
|
|
|
/** @test */
|
|
|
|
public function tryingToGetVisitsForInvalidUrlReturnsNotFoundError(): void
|
|
|
|
{
|
2019-11-27 13:48:35 -06:00
|
|
|
$expectedDetail = 'No URL found with short code "invalid"';
|
|
|
|
|
2019-11-20 13:58:16 -06:00
|
|
|
$resp = $this->callApiWithKey(self::METHOD_GET, '/short-urls/invalid/visits');
|
2019-11-27 13:48:35 -06:00
|
|
|
$payload = $this->getJsonResponsePayload($resp);
|
2019-11-20 13:58:16 -06:00
|
|
|
|
|
|
|
$this->assertEquals(self::STATUS_NOT_FOUND, $resp->getStatusCode());
|
2019-11-27 13:48:35 -06:00
|
|
|
$this->assertEquals(self::STATUS_NOT_FOUND, $payload['status']);
|
|
|
|
$this->assertEquals('INVALID_SHORTCODE', $payload['type']);
|
|
|
|
$this->assertEquals($expectedDetail, $payload['detail']);
|
|
|
|
$this->assertEquals('Short URL not found', $payload['title']);
|
|
|
|
$this->assertEquals('invalid', $payload['shortCode']);
|
2019-11-20 13:58:16 -06:00
|
|
|
}
|
2020-02-02 03:46:38 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideDomains
|
|
|
|
*/
|
|
|
|
public function properVisitsAreReturnedWhenDomainIsProvided(?string $domain, int $expectedAmountOfVisits): void
|
|
|
|
{
|
|
|
|
$shortCode = 'ghi789';
|
|
|
|
$url = new Uri(sprintf('/short-urls/%s/visits', $shortCode));
|
|
|
|
|
|
|
|
if ($domain !== null) {
|
|
|
|
$url = $url->withQuery(build_query(['domain' => $domain]));
|
|
|
|
}
|
|
|
|
|
|
|
|
$resp = $this->callApiWithKey(self::METHOD_GET, (string) $url);
|
|
|
|
$payload = $this->getJsonResponsePayload($resp);
|
|
|
|
|
|
|
|
$this->assertEquals($expectedAmountOfVisits, $payload['visits']['pagination']['totalItems'] ?? -1);
|
|
|
|
$this->assertCount($expectedAmountOfVisits, $payload['visits']['data'] ?? []);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideDomains(): iterable
|
|
|
|
{
|
|
|
|
yield 'domain' => ['example.com', 0];
|
|
|
|
yield 'no domain' => [null, 2];
|
|
|
|
}
|
2019-11-20 13:58:16 -06:00
|
|
|
}
|