Updated API tests so that fixture short URLs are created with matching short codes and different domains

This commit is contained in:
Alejandro Celaya 2020-02-02 09:51:17 +01:00
parent 75cd9774b7
commit aa80c2bb82
4 changed files with 66 additions and 9 deletions

View file

@ -17,7 +17,7 @@ interface VisitsTrackerInterface
/**
* Tracks a new visit to provided short code from provided visitor
*/
public function track(ShortUrl $shortUrl, Visitor $visitor): void; // FIXME
public function track(ShortUrl $shortUrl, Visitor $visitor): void;
/**
* Returns the visits on certain short code
@ -25,5 +25,5 @@ interface VisitsTrackerInterface
* @return Visit[]|Paginator
* @throws ShortUrlNotFoundException
*/
public function info(ShortUrlIdentifier $identifier, VisitsParams $params): Paginator; // FIXME
public function info(ShortUrlIdentifier $identifier, VisitsParams $params): Paginator;
}

View file

@ -7,9 +7,10 @@ namespace ShlinkioApiTest\Shlink\Rest\Action;
use Cake\Chronos\Chronos;
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
use GuzzleHttp\RequestOptions;
use Laminas\Diactoros\Uri;
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
use function Functional\first;
use function GuzzleHttp\Psr7\build_query;
use function sprintf;
class EditShortUrlActionTest extends ApiTestCase
@ -61,10 +62,9 @@ class EditShortUrlActionTest extends ApiTestCase
private function findShortUrlMetaByShortCode(string $shortCode): ?array
{
// FIXME Call GET /short-urls/{shortCode} once issue https://github.com/shlinkio/shlink/issues/628 is fixed
$allShortUrls = $this->getJsonResponsePayload($this->callApiWithKey(self::METHOD_GET, '/short-urls'));
$list = $allShortUrls['shortUrls']['data'] ?? [];
$matchingShortUrl = first($list, fn (array $shortUrl) => $shortUrl['shortCode'] ?? '' === $shortCode);
$matchingShortUrl = $this->getJsonResponsePayload(
$this->callApiWithKey(self::METHOD_GET, '/short-urls/' . $shortCode),
);
return $matchingShortUrl['meta'] ?? null;
}
@ -101,4 +101,37 @@ class EditShortUrlActionTest extends ApiTestCase
$this->assertEquals($expectedDetail, $payload['detail']);
$this->assertEquals('Invalid data', $payload['title']);
}
/**
* @test
* @dataProvider provideDomains
*/
public function metadataIsEditedOnProperShortUrlBasedOnDomain(?string $domain, string $expectedUrl): void
{
$shortCode = 'ghi789';
$url = new Uri(sprintf('/short-urls/%s', $shortCode));
if ($domain !== null) {
$url = $url->withQuery(build_query(['domain' => $domain]));
}
$editResp = $this->callApiWithKey(self::METHOD_PATCH, (string) $url, [RequestOptions::JSON => [
'maxVisits' => 100,
]]);
$editedShortUrl = $this->getJsonResponsePayload($this->callApiWithKey(self::METHOD_GET, (string) $url));
$this->assertEquals(self::STATUS_NO_CONTENT, $editResp->getStatusCode());
$this->assertEquals($domain, $editedShortUrl['domain']);
$this->assertEquals($expectedUrl, $editedShortUrl['longUrl']);
$this->assertEquals(100, $editedShortUrl['meta']['maxVisits'] ?? null);
}
public function provideDomains(): iterable
{
yield 'domain' => [null, 'https://shlink.io/documentation/'];
yield 'no domain' => [
'example.com',
'https://blog.alejandrocelaya.com/2019/04/27/considerations-to-properly-use-open-source-software-projects/',
];
}
}

View file

@ -26,6 +26,20 @@ class ListShortUrlsTest extends ApiTestCase
],
'domain' => null,
];
private const SHORT_URL_DOCS = [
'shortCode' => 'ghi789',
'shortUrl' => 'http://doma.in/ghi789',
'longUrl' => 'https://shlink.io/documentation/',
'dateCreated' => '2018-05-01T00:00:00+00:00',
'visitsCount' => 0,
'tags' => [],
'meta' => [
'validSince' => null,
'validUntil' => null,
'maxVisits' => null,
],
'domain' => null,
];
private const SHORT_URL_CUSTOM_SLUG_AND_DOMAIN = [
'shortCode' => 'custom-with-domain',
'shortUrl' => 'http://some-domain.com/custom-with-domain',
@ -109,6 +123,7 @@ class ListShortUrlsTest extends ApiTestCase
{
yield [[], [
self::SHORT_URL_SHLINK,
self::SHORT_URL_DOCS,
self::SHORT_URL_CUSTOM_SLUG_AND_DOMAIN,
self::SHORT_URL_META,
self::SHORT_URL_CUSTOM_SLUG,
@ -119,9 +134,11 @@ class ListShortUrlsTest extends ApiTestCase
self::SHORT_URL_CUSTOM_SLUG,
self::SHORT_URL_CUSTOM_SLUG_AND_DOMAIN,
self::SHORT_URL_META,
self::SHORT_URL_DOCS,
self::SHORT_URL_CUSTOM_DOMAIN,
]];
yield [['orderBy' => ['shortCode' => 'DESC']], [
self::SHORT_URL_DOCS,
self::SHORT_URL_CUSTOM_DOMAIN,
self::SHORT_URL_META,
self::SHORT_URL_CUSTOM_SLUG_AND_DOMAIN,
@ -135,6 +152,7 @@ class ListShortUrlsTest extends ApiTestCase
]];
yield [['endDate' => Chronos::parse('2018-12-01')->toAtomString()], [
self::SHORT_URL_SHLINK,
self::SHORT_URL_DOCS,
self::SHORT_URL_CUSTOM_SLUG_AND_DOMAIN,
]];
yield [['tags' => ['foo']], [

View file

@ -37,11 +37,17 @@ class ShortUrlsFixture extends AbstractFixture
), '2019-01-01 00:00:20');
$manager->persist($customShortUrl);
$withDomainShortUrl = $this->setShortUrlDate(new ShortUrl(
$ghiShortUrl = $this->setShortUrlDate(
new ShortUrl('https://shlink.io/documentation/', ShortUrlMeta::fromRawData(['customSlug' => 'ghi789'])),
'2018-05-01',
);
$manager->persist($ghiShortUrl);
$withDomainDuplicatingShortCode = $this->setShortUrlDate(new ShortUrl(
'https://blog.alejandrocelaya.com/2019/04/27/considerations-to-properly-use-open-source-software-projects/',
ShortUrlMeta::fromRawData(['domain' => 'example.com', 'customSlug' => 'ghi789']),
), '2019-01-01 00:00:30');
$manager->persist($withDomainShortUrl);
$manager->persist($withDomainDuplicatingShortCode);
$withDomainAndSlugShortUrl = $this->setShortUrlDate(new ShortUrl(
'https://google.com',