2019-01-30 20:28:07 +03:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioApiTest\Shlink\Rest\Action;
|
|
|
|
|
|
|
|
use Cake\Chronos\Chronos;
|
|
|
|
use GuzzleHttp\RequestOptions;
|
|
|
|
use ShlinkioTest\Shlink\Common\ApiTest\ApiTestCase;
|
|
|
|
|
|
|
|
class CreateShortUrlActionTest extends ApiTestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2019-02-03 13:01:38 +03:00
|
|
|
public function createsNewShortUrlWhenOnlyLongUrlIsProvided(): void
|
2019-01-30 20:28:07 +03:00
|
|
|
{
|
|
|
|
$expectedKeys = ['shortCode', 'shortUrl', 'longUrl', 'dateCreated', 'visitsCount', 'tags'];
|
|
|
|
[$statusCode, $payload] = $this->createShortUrl();
|
|
|
|
|
|
|
|
$this->assertEquals(self::STATUS_OK, $statusCode);
|
|
|
|
foreach ($expectedKeys as $key) {
|
|
|
|
$this->assertArrayHasKey($key, $payload);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2019-02-03 13:01:38 +03:00
|
|
|
public function createsNewShortUrlWithCustomSlug(): void
|
2019-01-30 20:28:07 +03:00
|
|
|
{
|
|
|
|
[$statusCode, $payload] = $this->createShortUrl(['customSlug' => 'my cool slug']);
|
|
|
|
|
|
|
|
$this->assertEquals(self::STATUS_OK, $statusCode);
|
|
|
|
$this->assertEquals('my-cool-slug', $payload['shortCode']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2019-02-03 13:01:38 +03:00
|
|
|
public function createsNewShortUrlWithTags(): void
|
2019-01-30 20:28:07 +03:00
|
|
|
{
|
|
|
|
[$statusCode, $payload] = $this->createShortUrl(['tags' => ['foo', 'bar', 'baz']]);
|
|
|
|
|
|
|
|
$this->assertEquals(self::STATUS_OK, $statusCode);
|
|
|
|
$this->assertEquals(['foo', 'bar', 'baz'], $payload['tags']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideMaxVisits
|
|
|
|
*/
|
2019-02-03 13:01:38 +03:00
|
|
|
public function createsNewShortUrlWithVisitsLimit(int $maxVisits): void
|
2019-01-30 20:28:07 +03:00
|
|
|
{
|
|
|
|
[$statusCode, ['shortCode' => $shortCode]] = $this->createShortUrl(['maxVisits' => $maxVisits]);
|
|
|
|
|
|
|
|
$this->assertEquals(self::STATUS_OK, $statusCode);
|
|
|
|
|
|
|
|
// Last request to the short URL will return a 404, and the rest, a 302
|
|
|
|
for ($i = 0; $i < $maxVisits; $i++) {
|
|
|
|
$this->assertEquals(self::STATUS_FOUND, $this->callShortUrl($shortCode)->getStatusCode());
|
|
|
|
}
|
|
|
|
$lastResp = $this->callShortUrl($shortCode);
|
|
|
|
$this->assertEquals(self::STATUS_NOT_FOUND, $lastResp->getStatusCode());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideMaxVisits(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[1],
|
|
|
|
[5],
|
|
|
|
[3],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2019-02-03 13:01:38 +03:00
|
|
|
public function createsShortUrlWithValidSince(): void
|
2019-01-30 20:28:07 +03:00
|
|
|
{
|
|
|
|
[$statusCode, ['shortCode' => $shortCode]] = $this->createShortUrl([
|
|
|
|
'validSince' => Chronos::now()->addDay()->toAtomString(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(self::STATUS_OK, $statusCode);
|
|
|
|
|
|
|
|
// Request to the short URL will return a 404 since ist' not valid yet
|
|
|
|
$lastResp = $this->callShortUrl($shortCode);
|
|
|
|
$this->assertEquals(self::STATUS_NOT_FOUND, $lastResp->getStatusCode());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2019-02-03 13:01:38 +03:00
|
|
|
public function createsShortUrlWithValidUntil(): void
|
2019-01-30 20:28:07 +03:00
|
|
|
{
|
|
|
|
[$statusCode, ['shortCode' => $shortCode]] = $this->createShortUrl([
|
|
|
|
'validUntil' => Chronos::now()->subDay()->toAtomString(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(self::STATUS_OK, $statusCode);
|
|
|
|
|
|
|
|
// Request to the short URL will return a 404 since it's no longer valid
|
|
|
|
$lastResp = $this->callShortUrl($shortCode);
|
|
|
|
$this->assertEquals(self::STATUS_NOT_FOUND, $lastResp->getStatusCode());
|
|
|
|
}
|
|
|
|
|
2019-02-03 13:01:38 +03:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideMatchingBodies
|
|
|
|
*/
|
|
|
|
public function returnsAnExistingShortUrlWhenRequested(array $body): void
|
|
|
|
{
|
|
|
|
|
|
|
|
[$firstStatusCode, ['shortCode' => $firstShortCode]] = $this->createShortUrl($body);
|
|
|
|
|
|
|
|
$body['findIfExists'] = true;
|
|
|
|
[$secondStatusCode, ['shortCode' => $secondShortCode]] = $this->createShortUrl($body);
|
|
|
|
|
|
|
|
$this->assertEquals(self::STATUS_OK, $firstStatusCode);
|
|
|
|
$this->assertEquals(self::STATUS_OK, $secondStatusCode);
|
|
|
|
$this->assertEquals($firstShortCode, $secondShortCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideMatchingBodies(): array
|
|
|
|
{
|
|
|
|
$longUrl = 'https://www.alejandrocelaya.com';
|
|
|
|
|
|
|
|
return [
|
|
|
|
'only long URL' => [['longUrl' => $longUrl]],
|
|
|
|
'long URL and tags' => [['longUrl' => $longUrl, 'tags' => ['boo', 'far']]],
|
|
|
|
'long URL custom slug' => [['longUrl' => $longUrl, 'customSlug' => 'my cool slug']],
|
|
|
|
'several params' => [[
|
|
|
|
'longUrl' => $longUrl,
|
|
|
|
'tags' => ['boo', 'far'],
|
|
|
|
'validSince' => Chronos::now()->toAtomString(),
|
|
|
|
'maxVisits' => 7,
|
|
|
|
]],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function returnsErrorWhenRequestingReturnExistingButCustomSlugIsInUse(): void
|
|
|
|
{
|
|
|
|
$longUrl = 'https://www.alejandrocelaya.com';
|
|
|
|
|
|
|
|
[$firstStatusCode] = $this->createShortUrl(['longUrl' => $longUrl]);
|
|
|
|
[$secondStatusCode] = $this->createShortUrl([
|
|
|
|
'longUrl' => $longUrl,
|
|
|
|
'customSlug' => 'custom',
|
|
|
|
'findIfExists' => true,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(self::STATUS_OK, $firstStatusCode);
|
|
|
|
$this->assertEquals(self::STATUS_BAD_REQUEST, $secondStatusCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function createsNewShortUrlIfRequestedToFindButThereIsNoMatch(): void
|
|
|
|
{
|
|
|
|
[$firstStatusCode, ['shortCode' => $firstShortCode]] = $this->createShortUrl([
|
|
|
|
'longUrl' => 'https://www.alejandrocelaya.com',
|
|
|
|
]);
|
|
|
|
[$secondStatusCode, ['shortCode' => $secondShortCode]] = $this->createShortUrl([
|
|
|
|
'longUrl' => 'https://www.alejandrocelaya.com/projects',
|
|
|
|
'findIfExists' => true,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(self::STATUS_OK, $firstStatusCode);
|
|
|
|
$this->assertEquals(self::STATUS_OK, $secondStatusCode);
|
|
|
|
$this->assertNotEquals($firstShortCode, $secondShortCode);
|
|
|
|
}
|
|
|
|
|
2019-01-30 20:28:07 +03:00
|
|
|
/**
|
|
|
|
* @return array {
|
|
|
|
* @var int $statusCode
|
|
|
|
* @var array $payload
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
private function createShortUrl(array $body = []): array
|
|
|
|
{
|
2019-02-03 13:01:38 +03:00
|
|
|
if (! isset($body['longUrl'])) {
|
|
|
|
$body['longUrl'] = 'https://app.shlink.io';
|
|
|
|
}
|
2019-01-30 20:28:07 +03:00
|
|
|
$resp = $this->callApiWithKey(self::METHOD_POST, '/short-urls', [RequestOptions::JSON => $body]);
|
|
|
|
$payload = $this->getJsonResponsePayload($resp);
|
|
|
|
|
|
|
|
return [$resp->getStatusCode(), $payload];
|
|
|
|
}
|
|
|
|
}
|