Updated EditShortUrlAction so that it returns the parsed short URL instead of an empty response

This commit is contained in:
Alejandro Celaya 2021-01-31 13:21:23 +01:00
parent 85bc5ce595
commit c61e1e1c0e
6 changed files with 43 additions and 14 deletions

View file

@ -150,8 +150,33 @@
}
],
"responses": {
"204": {
"description": "The short code has been properly updated."
"200": {
"description": "The short URL has been properly updated.",
"content": {
"application/json": {
"schema": {
"$ref": "../definitions/ShortUrl.json"
}
}
},
"examples": {
"application/json": {
"shortCode": "12Kb3",
"shortUrl": "https://doma.in/12Kb3",
"longUrl": "https://shlink.io",
"dateCreated": "2016-05-01T20:34:16+02:00",
"visitsCount": 1029,
"tags": [
"shlink"
],
"meta": {
"validSince": "2017-01-21T00:00:00+02:00",
"validUntil": null,
"maxVisits": 100
},
"domain": null
}
}
},
"400": {
"description": "Provided meta arguments are invalid.",

View file

@ -59,7 +59,7 @@ return [
Service\UrlShortener::class,
'config.url_shortener.domain',
],
Action\ShortUrl\EditShortUrlAction::class => [Service\ShortUrlService::class],
Action\ShortUrl\EditShortUrlAction::class => [Service\ShortUrlService::class, 'config.url_shortener.domain'],
Action\ShortUrl\DeleteShortUrlAction::class => [Service\ShortUrl\DeleteShortUrlService::class],
Action\ShortUrl\ResolveShortUrlAction::class => [
Service\ShortUrl\ShortUrlResolver::class,

View file

@ -4,12 +4,13 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Action\ShortUrl;
use Laminas\Diactoros\Response\EmptyResponse;
use Laminas\Diactoros\Response\JsonResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Shlinkio\Shlink\Core\Model\ShortUrlEdit;
use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier;
use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface;
use Shlinkio\Shlink\Core\Transformer\ShortUrlDataTransformer;
use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
use Shlinkio\Shlink\Rest\Middleware\AuthenticationMiddleware;
@ -19,10 +20,12 @@ class EditShortUrlAction extends AbstractRestAction
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_PATCH, self::METHOD_PUT];
private ShortUrlServiceInterface $shortUrlService;
private ShortUrlDataTransformer $transformer;
public function __construct(ShortUrlServiceInterface $shortUrlService)
public function __construct(ShortUrlServiceInterface $shortUrlService, array $domainConfig)
{
$this->shortUrlService = $shortUrlService;
$this->transformer = new ShortUrlDataTransformer($domainConfig);
}
public function handle(ServerRequestInterface $request): ResponseInterface
@ -31,7 +34,8 @@ class EditShortUrlAction extends AbstractRestAction
$identifier = ShortUrlIdentifier::fromApiRequest($request);
$apiKey = AuthenticationMiddleware::apiKeyFromRequest($request);
$this->shortUrlService->updateShortUrl($identifier, $shortUrlEdit, $apiKey);
return new EmptyResponse();
$shortUrl = $this->shortUrlService->updateShortUrl($identifier, $shortUrlEdit, $apiKey);
return new JsonResponse($this->transformer->transform($shortUrl));
}
}

View file

@ -41,8 +41,8 @@ class EditShortUrlTest extends ApiTestCase
]);
$metaAfterResetting = $this->findShortUrlMetaByShortCode($shortCode);
self::assertEquals(self::STATUS_NO_CONTENT, $editWithProvidedMeta->getStatusCode());
self::assertEquals(self::STATUS_NO_CONTENT, $editWithResetMeta->getStatusCode());
self::assertEquals(self::STATUS_OK, $editWithProvidedMeta->getStatusCode());
self::assertEquals(self::STATUS_OK, $editWithResetMeta->getStatusCode());
self::assertEquals($resetMeta, $metaAfterResetting);
self::assertArraySubset($meta, $metaAfterEditing);
}
@ -93,7 +93,7 @@ class EditShortUrlTest extends ApiTestCase
public function provideLongUrls(): iterable
{
yield 'valid URL' => ['https://shlink.io', self::STATUS_NO_CONTENT, null];
yield 'valid URL' => ['https://shlink.io', self::STATUS_OK, null];
yield 'invalid URL' => ['htt:foo', self::STATUS_BAD_REQUEST, 'INVALID_URL'];
}
@ -155,7 +155,7 @@ class EditShortUrlTest extends ApiTestCase
]]);
$editedShortUrl = $this->getJsonResponsePayload($this->callApiWithKey(self::METHOD_GET, (string) $url));
self::assertEquals(self::STATUS_NO_CONTENT, $editResp->getStatusCode());
self::assertEquals(self::STATUS_OK, $editResp->getStatusCode());
self::assertEquals($domain, $editedShortUrl['domain']);
self::assertEquals($expectedUrl, $editedShortUrl['longUrl']);
self::assertEquals(100, $editedShortUrl['meta']['maxVisits'] ?? null);

View file

@ -29,7 +29,7 @@ class ResolveShortUrlTest extends ApiTestCase
$visitResp = $this->callShortUrl($shortCode);
$fetchResp = $this->callApiWithKey(self::METHOD_GET, $url);
self::assertEquals(self::STATUS_NO_CONTENT, $editResp->getStatusCode());
self::assertEquals(self::STATUS_OK, $editResp->getStatusCode());
self::assertEquals(self::STATUS_NOT_FOUND, $visitResp->getStatusCode());
self::assertEquals(self::STATUS_OK, $fetchResp->getStatusCode());
}

View file

@ -25,7 +25,7 @@ class EditShortUrlActionTest extends TestCase
public function setUp(): void
{
$this->shortUrlService = $this->prophesize(ShortUrlServiceInterface::class);
$this->action = new EditShortUrlAction($this->shortUrlService->reveal());
$this->action = new EditShortUrlAction($this->shortUrlService->reveal(), []);
}
/** @test */
@ -54,7 +54,7 @@ class EditShortUrlActionTest extends TestCase
$resp = $this->action->handle($request);
self::assertEquals(204, $resp->getStatusCode());
self::assertEquals(200, $resp->getStatusCode());
$updateMeta->shouldHaveBeenCalled();
}
}