Merge pull request #213 from acelaya/feature/rename-rest-actions

Feature/rename rest actions
This commit is contained in:
Alejandro Celaya 2018-09-20 21:03:43 +02:00 committed by GitHub
commit 47e2322e33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
34 changed files with 263 additions and 149 deletions

View File

@ -1,12 +1,8 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
use Shlinkio\Shlink\Common\Middleware\LocaleMiddleware; namespace Shlinkio\Shlink;
use Shlinkio\Shlink\Core\Response\NotFoundHandler;
use Shlinkio\Shlink\Rest\Middleware\BodyParserMiddleware;
use Shlinkio\Shlink\Rest\Middleware\CheckAuthenticationMiddleware;
use Shlinkio\Shlink\Rest\Middleware\CrossDomainMiddleware;
use Shlinkio\Shlink\Rest\Middleware\PathVersionMiddleware;
use Zend\Expressive; use Zend\Expressive;
use Zend\Stratigility\Middleware\ErrorHandler; use Zend\Stratigility\Middleware\ErrorHandler;
@ -17,14 +13,15 @@ return [
'middleware' => [ 'middleware' => [
ErrorHandler::class, ErrorHandler::class,
Expressive\Helper\ContentLengthMiddleware::class, Expressive\Helper\ContentLengthMiddleware::class,
LocaleMiddleware::class, Common\Middleware\LocaleMiddleware::class,
], ],
'priority' => 11, 'priority' => 12,
], ],
'pre-routing-rest' => [ 'pre-routing-rest' => [
'path' => '/rest', 'path' => '/rest',
'middleware' => [ 'middleware' => [
PathVersionMiddleware::class, Rest\Middleware\PathVersionMiddleware::class,
Rest\Middleware\ShortUrl\ShortCodePathMiddleware::class,
], ],
'priority' => 11, 'priority' => 11,
], ],
@ -39,10 +36,10 @@ return [
'rest' => [ 'rest' => [
'path' => '/rest', 'path' => '/rest',
'middleware' => [ 'middleware' => [
CrossDomainMiddleware::class, Rest\Middleware\CrossDomainMiddleware::class,
Expressive\Router\Middleware\ImplicitOptionsMiddleware::class, Expressive\Router\Middleware\ImplicitOptionsMiddleware::class,
BodyParserMiddleware::class, Rest\Middleware\BodyParserMiddleware::class,
CheckAuthenticationMiddleware::class, Rest\Middleware\CheckAuthenticationMiddleware::class,
], ],
'priority' => 5, 'priority' => 5,
], ],
@ -50,7 +47,7 @@ return [
'post-routing' => [ 'post-routing' => [
'middleware' => [ 'middleware' => [
Expressive\Router\Middleware\DispatchMiddleware::class, Expressive\Router\Middleware\DispatchMiddleware::class,
NotFoundHandler::class, Core\Response\NotFoundHandler::class,
], ],
'priority' => 1, 'priority' => 1,
], ],

View File

@ -1,5 +1,6 @@
{ {
"post": { "post": {
"operationId": "authenticate",
"tags": [ "tags": [
"Authentication" "Authentication"
], ],

View File

@ -1,7 +1,8 @@
{ {
"get": { "get": {
"operationId": "listShortUrls",
"tags": [ "tags": [
"ShortCodes" "Short URLs"
], ],
"summary": "List short URLs", "summary": "List short URLs",
"description": "Returns the list of short codes", "description": "Returns the list of short codes",
@ -142,8 +143,9 @@
}, },
"post": { "post": {
"operationId": "createShortUrl",
"tags": [ "tags": [
"ShortCodes" "Short URLs"
], ],
"summary": "Create short URL", "summary": "Create short URL",
"description": "Creates a new short code", "description": "Creates a new short code",

View File

@ -1,7 +1,8 @@
{ {
"get": { "get": {
"operationId": "shortenUrl",
"tags": [ "tags": [
"ShortCodes" "Short URLs"
], ],
"summary": "Create a short URL", "summary": "Create a short URL",
"description": "Creates a short URL in a single API call. Useful for third party integrations", "description": "Creates a short URL in a single API call. Useful for third party integrations",

View File

@ -1,7 +1,8 @@
{ {
"get": { "get": {
"operationId": "getShortUrl",
"tags": [ "tags": [
"ShortCodes" "Short URLs"
], ],
"summary": "Parse short code", "summary": "Parse short code",
"description": "Get the long URL behind a short code.", "description": "Get the long URL behind a short code.",
@ -78,8 +79,9 @@
}, },
"put": { "put": {
"operationId": "editShortUrl",
"tags": [ "tags": [
"ShortCodes" "Short URLs"
], ],
"summary": "Edit short code", "summary": "Edit short code",
"description": "Update certain meta arguments from an existing short URL.", "description": "Update certain meta arguments from an existing short URL.",
@ -162,8 +164,9 @@
}, },
"delete": { "delete": {
"operationId": "deleteShortUrl",
"tags": [ "tags": [
"ShortCodes" "Short URLs"
], ],
"summary": "Delete short code", "summary": "Delete short code",
"description": "Deletes the short URL for provided short code.", "description": "Deletes the short URL for provided short code.",

View File

@ -1,8 +1,8 @@
{ {
"put": { "put": {
"operationId": "editShortUrlTags",
"tags": [ "tags": [
"ShortCodes", "Short URLs"
"Tags"
], ],
"summary": "Edit tags on short URL", "summary": "Edit tags on short URL",
"description": "Edit the tags on provided short code.", "description": "Edit the tags on provided short code.",
@ -10,7 +10,7 @@
{ {
"name": "shortCode", "name": "shortCode",
"in": "path", "in": "path",
"description": "The shortCode in which we want to edit tags.", "description": "The short code for the short URL in which we want to edit tags.",
"required": true, "required": true,
"schema": { "schema": {
"type": "string" "type": "string"

View File

@ -1,7 +1,7 @@
{ {
"get": { "get": {
"operationId": "getShortUrlVisits",
"tags": [ "tags": [
"ShortCodes",
"Visits" "Visits"
], ],
"summary": "List visits for short URL", "summary": "List visits for short URL",
@ -10,7 +10,7 @@
{ {
"name": "shortCode", "name": "shortCode",
"in": "path", "in": "path",
"description": "The shortCode from which we want to get the visits.", "description": "The short code for the short URL from which we want to get the visits.",
"required": true, "required": true,
"schema": { "schema": {
"type": "string" "type": "string"

View File

@ -1,5 +1,6 @@
{ {
"get": { "get": {
"operationId": "listTags",
"tags": [ "tags": [
"Tags" "Tags"
], ],
@ -60,6 +61,7 @@
}, },
"post": { "post": {
"operationId": "createTags",
"tags": [ "tags": [
"Tags" "Tags"
], ],
@ -143,6 +145,7 @@
}, },
"put": { "put": {
"operationId": "renameTag",
"tags": [ "tags": [
"Tags" "Tags"
], ],
@ -216,6 +219,7 @@
}, },
"delete": { "delete": {
"operationId": "deleteTags",
"tags": [ "tags": [
"Tags" "Tags"
], ],

View File

@ -32,21 +32,40 @@
} }
}, },
"tags": [
{
"name": "Authentication",
"description": "Authentication-related endpoints"
},
{
"name": "Short URLs",
"description": "Operations that can be performed on short URLs"
},
{
"name": "Tags",
"description": "Let you handle the list of available tags"
},
{
"name": "Visits",
"description": "Operations to manage visits on short URLs"
}
],
"paths": { "paths": {
"/v1/authenticate": { "/v1/authenticate": {
"$ref": "paths/v1_authenticate.json" "$ref": "paths/v1_authenticate.json"
}, },
"/v1/short-codes": { "/v1/short-urls": {
"$ref": "paths/v1_short-codes.json" "$ref": "paths/v1_short-codes.json"
}, },
"/v1/short-codes/shorten": { "/v1/short-urls/shorten": {
"$ref": "paths/v1_short-codes_shorten.json" "$ref": "paths/v1_short-codes_shorten.json"
}, },
"/v1/short-codes/{shortCode}": { "/v1/short-urls/{shortCode}": {
"$ref": "paths/v1_short-codes_{shortCode}.json" "$ref": "paths/v1_short-codes_{shortCode}.json"
}, },
"/v1/short-codes/{shortCode}/tags": { "/v1/short-urls/{shortCode}/tags": {
"$ref": "paths/v1_short-codes_{shortCode}_tags.json" "$ref": "paths/v1_short-codes_{shortCode}_tags.json"
}, },
@ -54,7 +73,7 @@
"$ref": "paths/v1_tags.json" "$ref": "paths/v1_tags.json"
}, },
"/v1/short-codes/{shortCode}/visits": { "/v1/short-urls/{shortCode}/visits": {
"$ref": "paths/v1_short-codes_{shortCode}_visits.json" "$ref": "paths/v1_short-codes_{shortCode}_visits.json"
} }
} }

View File

@ -5,7 +5,7 @@ namespace Shlinkio\Shlink\Core\Model;
use Psr\Http\Message\UriInterface; use Psr\Http\Message\UriInterface;
final class CreateShortCodeData final class CreateShortUrlData
{ {
/** /**
* @var UriInterface * @var UriInterface

View File

@ -8,7 +8,7 @@ return [
'auth' => [ 'auth' => [
'routes_whitelist' => [ 'routes_whitelist' => [
Action\AuthenticateAction::class, Action\AuthenticateAction::class,
Action\ShortCode\SingleStepCreateShortCodeAction::class, Action\ShortUrl\SingleStepCreateShortUrlAction::class,
], ],
], ],

View File

@ -1,12 +1,11 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace Shlinkio\Shlink\Rest;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Shlinkio\Shlink\Core\Options\AppOptions; use Shlinkio\Shlink\Core\Options\AppOptions;
use Shlinkio\Shlink\Core\Service; use Shlinkio\Shlink\Core\Service;
use Shlinkio\Shlink\Rest\Action;
use Shlinkio\Shlink\Rest\Authentication\JWTService;
use Shlinkio\Shlink\Rest\Middleware;
use Shlinkio\Shlink\Rest\Service\ApiKeyService; use Shlinkio\Shlink\Rest\Service\ApiKeyService;
use Zend\I18n\Translator\Translator; use Zend\I18n\Translator\Translator;
use Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory; use Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory;
@ -16,18 +15,18 @@ return [
'dependencies' => [ 'dependencies' => [
'factories' => [ 'factories' => [
JWTService::class => ConfigAbstractFactory::class, Authentication\JWTService::class => ConfigAbstractFactory::class,
ApiKeyService::class => ConfigAbstractFactory::class, ApiKeyService::class => ConfigAbstractFactory::class,
Action\AuthenticateAction::class => ConfigAbstractFactory::class, Action\AuthenticateAction::class => ConfigAbstractFactory::class,
Action\ShortCode\CreateShortCodeAction::class => ConfigAbstractFactory::class, Action\ShortUrl\CreateShortUrlAction::class => ConfigAbstractFactory::class,
Action\ShortCode\SingleStepCreateShortCodeAction::class => ConfigAbstractFactory::class, Action\ShortUrl\SingleStepCreateShortUrlAction::class => ConfigAbstractFactory::class,
Action\ShortCode\EditShortCodeAction::class => ConfigAbstractFactory::class, Action\ShortUrl\EditShortUrlAction::class => ConfigAbstractFactory::class,
Action\ShortCode\DeleteShortCodeAction::class => ConfigAbstractFactory::class, Action\ShortUrl\DeleteShortUrlAction::class => ConfigAbstractFactory::class,
Action\ShortCode\ResolveUrlAction::class => ConfigAbstractFactory::class, Action\ShortUrl\ResolveShortUrlAction::class => ConfigAbstractFactory::class,
Action\Visit\GetVisitsAction::class => ConfigAbstractFactory::class, Action\Visit\GetVisitsAction::class => ConfigAbstractFactory::class,
Action\ShortCode\ListShortCodesAction::class => ConfigAbstractFactory::class, Action\ShortUrl\ListShortUrlsAction::class => ConfigAbstractFactory::class,
Action\ShortCode\EditShortCodeTagsAction::class => ConfigAbstractFactory::class, Action\ShortUrl\EditShortUrlTagsAction::class => ConfigAbstractFactory::class,
Action\Tag\ListTagsAction::class => ConfigAbstractFactory::class, Action\Tag\ListTagsAction::class => ConfigAbstractFactory::class,
Action\Tag\DeleteTagsAction::class => ConfigAbstractFactory::class, Action\Tag\DeleteTagsAction::class => ConfigAbstractFactory::class,
Action\Tag\CreateTagsAction::class => ConfigAbstractFactory::class, Action\Tag\CreateTagsAction::class => ConfigAbstractFactory::class,
@ -37,47 +36,53 @@ return [
Middleware\CrossDomainMiddleware::class => InvokableFactory::class, Middleware\CrossDomainMiddleware::class => InvokableFactory::class,
Middleware\PathVersionMiddleware::class => InvokableFactory::class, Middleware\PathVersionMiddleware::class => InvokableFactory::class,
Middleware\CheckAuthenticationMiddleware::class => ConfigAbstractFactory::class, Middleware\CheckAuthenticationMiddleware::class => ConfigAbstractFactory::class,
Middleware\ShortCode\CreateShortCodeContentNegotiationMiddleware::class => InvokableFactory::class, Middleware\ShortUrl\CreateShortUrlContentNegotiationMiddleware::class => InvokableFactory::class,
Middleware\ShortUrl\ShortCodePathMiddleware::class => InvokableFactory::class,
], ],
], ],
ConfigAbstractFactory::class => [ ConfigAbstractFactory::class => [
JWTService::class => [AppOptions::class], Authentication\JWTService::class => [AppOptions::class],
ApiKeyService::class => ['em'], ApiKeyService::class => ['em'],
Action\AuthenticateAction::class => [ApiKeyService::class, JWTService::class, 'translator', 'Logger_Shlink'], Action\AuthenticateAction::class => [
Action\ShortCode\CreateShortCodeAction::class => [ ApiKeyService::class,
Authentication\JWTService::class,
'translator',
'Logger_Shlink',
],
Action\ShortUrl\CreateShortUrlAction::class => [
Service\UrlShortener::class, Service\UrlShortener::class,
'translator', 'translator',
'config.url_shortener.domain', 'config.url_shortener.domain',
'Logger_Shlink', 'Logger_Shlink',
], ],
Action\ShortCode\SingleStepCreateShortCodeAction::class => [ Action\ShortUrl\SingleStepCreateShortUrlAction::class => [
Service\UrlShortener::class, Service\UrlShortener::class,
'translator', 'translator',
ApiKeyService::class, ApiKeyService::class,
'config.url_shortener.domain', 'config.url_shortener.domain',
'Logger_Shlink', 'Logger_Shlink',
], ],
Action\ShortCode\EditShortCodeAction::class => [Service\ShortUrlService::class, 'translator', 'Logger_Shlink'], Action\ShortUrl\EditShortUrlAction::class => [Service\ShortUrlService::class, 'translator', 'Logger_Shlink'],
Action\ShortCode\DeleteShortCodeAction::class => [ Action\ShortUrl\DeleteShortUrlAction::class => [
Service\ShortUrl\DeleteShortUrlService::class, Service\ShortUrl\DeleteShortUrlService::class,
'translator', 'translator',
'Logger_Shlink', 'Logger_Shlink',
], ],
Action\ShortCode\ResolveUrlAction::class => [ Action\ShortUrl\ResolveShortUrlAction::class => [
Service\UrlShortener::class, Service\UrlShortener::class,
'translator', 'translator',
'config.url_shortener.domain', 'config.url_shortener.domain',
], ],
Action\Visit\GetVisitsAction::class => [Service\VisitsTracker::class, 'translator', 'Logger_Shlink'], Action\Visit\GetVisitsAction::class => [Service\VisitsTracker::class, 'translator', 'Logger_Shlink'],
Action\ShortCode\ListShortCodesAction::class => [ Action\ShortUrl\ListShortUrlsAction::class => [
Service\ShortUrlService::class, Service\ShortUrlService::class,
'translator', 'translator',
'config.url_shortener.domain', 'config.url_shortener.domain',
'Logger_Shlink', 'Logger_Shlink',
], ],
Action\ShortCode\EditShortCodeTagsAction::class => [ Action\ShortUrl\EditShortUrlTagsAction::class => [
Service\ShortUrlService::class, Service\ShortUrlService::class,
'translator', 'translator',
'Logger_Shlink', 'Logger_Shlink',
@ -88,7 +93,7 @@ return [
Action\Tag\UpdateTagAction::class => [Service\Tag\TagService::class, Translator::class, LoggerInterface::class], Action\Tag\UpdateTagAction::class => [Service\Tag\TagService::class, Translator::class, LoggerInterface::class],
Middleware\CheckAuthenticationMiddleware::class => [ Middleware\CheckAuthenticationMiddleware::class => [
JWTService::class, Authentication\JWTService::class,
'translator', 'translator',
'config.auth.routes_whitelist', 'config.auth.routes_whitelist',
'Logger_Shlink', 'Logger_Shlink',

View File

@ -11,17 +11,17 @@ return [
Action\AuthenticateAction::getRouteDef(), Action\AuthenticateAction::getRouteDef(),
// Short codes // Short codes
Action\ShortCode\CreateShortCodeAction::getRouteDef([ Action\ShortUrl\CreateShortUrlAction::getRouteDef([
Middleware\ShortCode\CreateShortCodeContentNegotiationMiddleware::class, Middleware\ShortUrl\CreateShortUrlContentNegotiationMiddleware::class,
]), ]),
Action\ShortCode\SingleStepCreateShortCodeAction::getRouteDef([ Action\ShortUrl\SingleStepCreateShortUrlAction::getRouteDef([
Middleware\ShortCode\CreateShortCodeContentNegotiationMiddleware::class, Middleware\ShortUrl\CreateShortUrlContentNegotiationMiddleware::class,
]), ]),
Action\ShortCode\EditShortCodeAction::getRouteDef(), Action\ShortUrl\EditShortUrlAction::getRouteDef(),
Action\ShortCode\DeleteShortCodeAction::getRouteDef(), Action\ShortUrl\DeleteShortUrlAction::getRouteDef(),
Action\ShortCode\ResolveUrlAction::getRouteDef(), Action\ShortUrl\ResolveShortUrlAction::getRouteDef(),
Action\ShortCode\ListShortCodesAction::getRouteDef(), Action\ShortUrl\ListShortUrlsAction::getRouteDef(),
Action\ShortCode\EditShortCodeTagsAction::getRouteDef(), Action\ShortUrl\EditShortUrlTagsAction::getRouteDef(),
// Visits // Visits
Action\Visit\GetVisitsAction::getRouteDef(), Action\Visit\GetVisitsAction::getRouteDef(),

View File

@ -1,7 +1,7 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Action\ShortCode; namespace Shlinkio\Shlink\Rest\Action\ShortUrl;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
@ -9,7 +9,7 @@ use Psr\Log\LoggerInterface;
use Shlinkio\Shlink\Core\Exception\InvalidArgumentException; use Shlinkio\Shlink\Core\Exception\InvalidArgumentException;
use Shlinkio\Shlink\Core\Exception\InvalidUrlException; use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException; use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
use Shlinkio\Shlink\Core\Model\CreateShortCodeData; use Shlinkio\Shlink\Core\Model\CreateShortUrlData;
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface; use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
use Shlinkio\Shlink\Core\Transformer\ShortUrlDataTransformer; use Shlinkio\Shlink\Core\Transformer\ShortUrlDataTransformer;
use Shlinkio\Shlink\Rest\Action\AbstractRestAction; use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
@ -17,7 +17,7 @@ use Shlinkio\Shlink\Rest\Util\RestUtils;
use Zend\Diactoros\Response\JsonResponse; use Zend\Diactoros\Response\JsonResponse;
use Zend\I18n\Translator\TranslatorInterface; use Zend\I18n\Translator\TranslatorInterface;
abstract class AbstractCreateShortCodeAction extends AbstractRestAction abstract class AbstractCreateShortUrlAction extends AbstractRestAction
{ {
/** /**
* @var UrlShortenerInterface * @var UrlShortenerInterface
@ -52,10 +52,10 @@ abstract class AbstractCreateShortCodeAction extends AbstractRestAction
public function handle(Request $request): Response public function handle(Request $request): Response
{ {
try { try {
$shortCodeData = $this->buildUrlToShortCodeData($request); $shortUrlData = $this->buildShortUrlData($request);
$shortCodeMeta = $shortCodeData->getMeta(); $shortUrlMeta = $shortUrlData->getMeta();
$longUrl = $shortCodeData->getLongUrl(); $longUrl = $shortUrlData->getLongUrl();
$customSlug = $shortCodeMeta->getCustomSlug(); $customSlug = $shortUrlMeta->getCustomSlug();
} catch (InvalidArgumentException $e) { } catch (InvalidArgumentException $e) {
$this->logger->warning('Provided data is invalid.' . PHP_EOL . $e); $this->logger->warning('Provided data is invalid.' . PHP_EOL . $e);
return new JsonResponse([ return new JsonResponse([
@ -67,11 +67,11 @@ abstract class AbstractCreateShortCodeAction extends AbstractRestAction
try { try {
$shortUrl = $this->urlShortener->urlToShortCode( $shortUrl = $this->urlShortener->urlToShortCode(
$longUrl, $longUrl,
$shortCodeData->getTags(), $shortUrlData->getTags(),
$shortCodeMeta->getValidSince(), $shortUrlMeta->getValidSince(),
$shortCodeMeta->getValidUntil(), $shortUrlMeta->getValidUntil(),
$customSlug, $customSlug,
$shortCodeMeta->getMaxVisits() $shortUrlMeta->getMaxVisits()
); );
$transformer = new ShortUrlDataTransformer($this->domainConfig); $transformer = new ShortUrlDataTransformer($this->domainConfig);
@ -95,7 +95,7 @@ abstract class AbstractCreateShortCodeAction extends AbstractRestAction
), ),
], self::STATUS_BAD_REQUEST); ], self::STATUS_BAD_REQUEST);
} catch (\Throwable $e) { } catch (\Throwable $e) {
$this->logger->error('Unexpected error creating shortcode.' . PHP_EOL . $e); $this->logger->error('Unexpected error creating short url.' . PHP_EOL . $e);
return new JsonResponse([ return new JsonResponse([
'error' => RestUtils::UNKNOWN_ERROR, 'error' => RestUtils::UNKNOWN_ERROR,
'message' => $this->translator->translate('Unexpected error occurred'), 'message' => $this->translator->translate('Unexpected error occurred'),
@ -105,8 +105,8 @@ abstract class AbstractCreateShortCodeAction extends AbstractRestAction
/** /**
* @param Request $request * @param Request $request
* @return CreateShortCodeData * @return CreateShortUrlData
* @throws InvalidArgumentException * @throws InvalidArgumentException
*/ */
abstract protected function buildUrlToShortCodeData(Request $request): CreateShortCodeData; abstract protected function buildShortUrlData(Request $request): CreateShortUrlData;
} }

View File

@ -1,33 +1,34 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Action\ShortCode; namespace Shlinkio\Shlink\Rest\Action\ShortUrl;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Shlinkio\Shlink\Core\Exception\InvalidArgumentException; use Shlinkio\Shlink\Core\Exception\InvalidArgumentException;
use Shlinkio\Shlink\Core\Model\CreateShortCodeData; use Shlinkio\Shlink\Core\Model\CreateShortUrlData;
use Shlinkio\Shlink\Core\Model\ShortUrlMeta; use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
use Shlinkio\Shlink\Rest\Action\ShortUrl\AbstractCreateShortUrlAction;
use Zend\Diactoros\Uri; use Zend\Diactoros\Uri;
class CreateShortCodeAction extends AbstractCreateShortCodeAction class CreateShortUrlAction extends AbstractCreateShortUrlAction
{ {
protected const ROUTE_PATH = '/short-codes'; protected const ROUTE_PATH = '/short-urls';
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_POST]; protected const ROUTE_ALLOWED_METHODS = [self::METHOD_POST];
/** /**
* @param Request $request * @param Request $request
* @return CreateShortCodeData * @return CreateShortUrlData
* @throws InvalidArgumentException * @throws InvalidArgumentException
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
protected function buildUrlToShortCodeData(Request $request): CreateShortCodeData protected function buildShortUrlData(Request $request): CreateShortUrlData
{ {
$postData = (array) $request->getParsedBody(); $postData = (array) $request->getParsedBody();
if (! isset($postData['longUrl'])) { if (! isset($postData['longUrl'])) {
throw new InvalidArgumentException($this->translator->translate('A URL was not provided')); throw new InvalidArgumentException($this->translator->translate('A URL was not provided'));
} }
return new CreateShortCodeData( return new CreateShortUrlData(
new Uri($postData['longUrl']), new Uri($postData['longUrl']),
(array) ($postData['tags'] ?? []), (array) ($postData['tags'] ?? []),
ShortUrlMeta::createFromParams( ShortUrlMeta::createFromParams(

View File

@ -1,7 +1,7 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Action\ShortCode; namespace Shlinkio\Shlink\Rest\Action\ShortUrl;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
@ -14,9 +14,9 @@ use Zend\Diactoros\Response\EmptyResponse;
use Zend\Diactoros\Response\JsonResponse; use Zend\Diactoros\Response\JsonResponse;
use Zend\I18n\Translator\TranslatorInterface; use Zend\I18n\Translator\TranslatorInterface;
class DeleteShortCodeAction extends AbstractRestAction class DeleteShortUrlAction extends AbstractRestAction
{ {
protected const ROUTE_PATH = '/short-codes/{shortCode}'; protected const ROUTE_PATH = '/short-urls/{shortCode}';
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_DELETE]; protected const ROUTE_ALLOWED_METHODS = [self::METHOD_DELETE];
/** /**

View File

@ -1,7 +1,7 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Action\ShortCode; namespace Shlinkio\Shlink\Rest\Action\ShortUrl;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
@ -15,9 +15,9 @@ use Zend\Diactoros\Response\EmptyResponse;
use Zend\Diactoros\Response\JsonResponse; use Zend\Diactoros\Response\JsonResponse;
use Zend\I18n\Translator\TranslatorInterface; use Zend\I18n\Translator\TranslatorInterface;
class EditShortCodeAction extends AbstractRestAction class EditShortUrlAction extends AbstractRestAction
{ {
protected const ROUTE_PATH = '/short-codes/{shortCode}'; protected const ROUTE_PATH = '/short-urls/{shortCode}';
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_PUT]; protected const ROUTE_ALLOWED_METHODS = [self::METHOD_PUT];
/** /**

View File

@ -1,7 +1,7 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Action\ShortCode; namespace Shlinkio\Shlink\Rest\Action\ShortUrl;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
@ -13,9 +13,9 @@ use Shlinkio\Shlink\Rest\Util\RestUtils;
use Zend\Diactoros\Response\JsonResponse; use Zend\Diactoros\Response\JsonResponse;
use Zend\I18n\Translator\TranslatorInterface; use Zend\I18n\Translator\TranslatorInterface;
class EditShortCodeTagsAction extends AbstractRestAction class EditShortUrlTagsAction extends AbstractRestAction
{ {
protected const ROUTE_PATH = '/short-codes/{shortCode}/tags'; protected const ROUTE_PATH = '/short-urls/{shortCode}/tags';
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_PUT]; protected const ROUTE_ALLOWED_METHODS = [self::METHOD_PUT];
/** /**

View File

@ -1,7 +1,7 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Action\ShortCode; namespace Shlinkio\Shlink\Rest\Action\ShortUrl;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
@ -14,11 +14,11 @@ use Shlinkio\Shlink\Rest\Util\RestUtils;
use Zend\Diactoros\Response\JsonResponse; use Zend\Diactoros\Response\JsonResponse;
use Zend\I18n\Translator\TranslatorInterface; use Zend\I18n\Translator\TranslatorInterface;
class ListShortCodesAction extends AbstractRestAction class ListShortUrlsAction extends AbstractRestAction
{ {
use PaginatorUtilsTrait; use PaginatorUtilsTrait;
protected const ROUTE_PATH = '/short-codes'; protected const ROUTE_PATH = '/short-urls';
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET]; protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET];
/** /**

View File

@ -1,7 +1,7 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Action\ShortCode; namespace Shlinkio\Shlink\Rest\Action\ShortUrl;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
@ -15,9 +15,9 @@ use Shlinkio\Shlink\Rest\Util\RestUtils;
use Zend\Diactoros\Response\JsonResponse; use Zend\Diactoros\Response\JsonResponse;
use Zend\I18n\Translator\TranslatorInterface; use Zend\I18n\Translator\TranslatorInterface;
class ResolveUrlAction extends AbstractRestAction class ResolveShortUrlAction extends AbstractRestAction
{ {
protected const ROUTE_PATH = '/short-codes/{shortCode}'; protected const ROUTE_PATH = '/short-urls/{shortCode}';
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET]; protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET];
/** /**

View File

@ -1,20 +1,20 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Action\ShortCode; namespace Shlinkio\Shlink\Rest\Action\ShortUrl;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Shlinkio\Shlink\Core\Exception\InvalidArgumentException; use Shlinkio\Shlink\Core\Exception\InvalidArgumentException;
use Shlinkio\Shlink\Core\Model\CreateShortCodeData; use Shlinkio\Shlink\Core\Model\CreateShortUrlData;
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface; use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface; use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
use Zend\Diactoros\Uri; use Zend\Diactoros\Uri;
use Zend\I18n\Translator\TranslatorInterface; use Zend\I18n\Translator\TranslatorInterface;
class SingleStepCreateShortCodeAction extends AbstractCreateShortCodeAction class SingleStepCreateShortUrlAction extends AbstractCreateShortUrlAction
{ {
protected const ROUTE_PATH = '/short-codes/shorten'; protected const ROUTE_PATH = '/short-urls/shorten';
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET]; protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET];
/** /**
@ -35,11 +35,11 @@ class SingleStepCreateShortCodeAction extends AbstractCreateShortCodeAction
/** /**
* @param Request $request * @param Request $request
* @return CreateShortCodeData * @return CreateShortUrlData
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
* @throws InvalidArgumentException * @throws InvalidArgumentException
*/ */
protected function buildUrlToShortCodeData(Request $request): CreateShortCodeData protected function buildShortUrlData(Request $request): CreateShortUrlData
{ {
$query = $request->getQueryParams(); $query = $request->getQueryParams();
@ -55,6 +55,6 @@ class SingleStepCreateShortCodeAction extends AbstractCreateShortCodeAction
throw new InvalidArgumentException($this->translator->translate('A URL was not provided')); throw new InvalidArgumentException($this->translator->translate('A URL was not provided'));
} }
return new CreateShortCodeData(new Uri($query['longUrl'])); return new CreateShortUrlData(new Uri($query['longUrl']));
} }
} }

View File

@ -16,7 +16,7 @@ use Zend\I18n\Translator\TranslatorInterface;
class GetVisitsAction extends AbstractRestAction class GetVisitsAction extends AbstractRestAction
{ {
protected const ROUTE_PATH = '/short-codes/{shortCode}/visits'; protected const ROUTE_PATH = '/short-urls/{shortCode}/visits';
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET]; protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET];
/** /**

View File

@ -1,7 +1,7 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Middleware\ShortCode; namespace Shlinkio\Shlink\Rest\Middleware\ShortUrl;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
@ -10,7 +10,7 @@ use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response; use Zend\Diactoros\Response;
use Zend\Diactoros\Response\JsonResponse; use Zend\Diactoros\Response\JsonResponse;
class CreateShortCodeContentNegotiationMiddleware implements MiddlewareInterface class CreateShortUrlContentNegotiationMiddleware implements MiddlewareInterface
{ {
private const PLAIN_TEXT = 'text'; private const PLAIN_TEXT = 'text';
private const JSON = 'json'; private const JSON = 'json';

View File

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Middleware\ShortUrl;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
class ShortCodePathMiddleware implements MiddlewareInterface
{
private const OLD_PATH_PREFIX = '/short-codes';
private const NEW_PATH_PREFIX = '/short-urls';
/**
* Process an incoming server request and return a response, optionally delegating
* response creation to a handler.
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$uri = $request->getUri();
$path = $uri->getPath();
// If the path starts with the old prefix, replace it by the new one
return $handler->handle(
$request->withUri($uri->withPath(\str_replace(self::OLD_PATH_PREFIX, self::NEW_PATH_PREFIX, $path)))
);
}
}

View File

@ -10,6 +10,7 @@ use Shlinkio\Shlink\Rest\Exception as Rest;
class RestUtils class RestUtils
{ {
public const INVALID_SHORTCODE_ERROR = 'INVALID_SHORTCODE'; public const INVALID_SHORTCODE_ERROR = 'INVALID_SHORTCODE';
// FIXME Should be INVALID_SHORT_URL_DELETION
public const INVALID_SHORTCODE_DELETION_ERROR = 'INVALID_SHORTCODE_DELETION'; public const INVALID_SHORTCODE_DELETION_ERROR = 'INVALID_SHORTCODE_DELETION';
public const INVALID_URL_ERROR = 'INVALID_URL'; public const INVALID_URL_ERROR = 'INVALID_URL';
public const INVALID_ARGUMENT_ERROR = 'INVALID_ARGUMENT'; public const INVALID_ARGUMENT_ERROR = 'INVALID_ARGUMENT';

View File

@ -1,7 +1,7 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Action\ShortCode; namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Argument; use Prophecy\Argument;
@ -10,16 +10,16 @@ use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Exception\InvalidUrlException; use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException; use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
use Shlinkio\Shlink\Core\Service\UrlShortener; use Shlinkio\Shlink\Core\Service\UrlShortener;
use Shlinkio\Shlink\Rest\Action\ShortCode\CreateShortCodeAction; use Shlinkio\Shlink\Rest\Action\ShortUrl\CreateShortUrlAction;
use Shlinkio\Shlink\Rest\Util\RestUtils; use Shlinkio\Shlink\Rest\Util\RestUtils;
use Zend\Diactoros\ServerRequestFactory; use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\Uri; use Zend\Diactoros\Uri;
use Zend\I18n\Translator\Translator; use Zend\I18n\Translator\Translator;
class CreateShortCodeActionTest extends TestCase class CreateShortUrlActionTest extends TestCase
{ {
/** /**
* @var CreateShortCodeAction * @var CreateShortUrlAction
*/ */
protected $action; protected $action;
/** /**
@ -30,7 +30,7 @@ class CreateShortCodeActionTest extends TestCase
public function setUp() public function setUp()
{ {
$this->urlShortener = $this->prophesize(UrlShortener::class); $this->urlShortener = $this->prophesize(UrlShortener::class);
$this->action = new CreateShortCodeAction($this->urlShortener->reveal(), Translator::factory([]), [ $this->action = new CreateShortUrlAction($this->urlShortener->reveal(), Translator::factory([]), [
'schema' => 'http', 'schema' => 'http',
'hostname' => 'foo.com', 'hostname' => 'foo.com',
]); ]);

View File

@ -1,23 +1,23 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Action\ShortCode; namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Argument; use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy; use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Exception; use Shlinkio\Shlink\Core\Exception;
use Shlinkio\Shlink\Core\Service\ShortUrl\DeleteShortUrlServiceInterface; use Shlinkio\Shlink\Core\Service\ShortUrl\DeleteShortUrlServiceInterface;
use Shlinkio\Shlink\Rest\Action\ShortCode\DeleteShortCodeAction; use Shlinkio\Shlink\Rest\Action\ShortUrl\DeleteShortUrlAction;
use Shlinkio\Shlink\Rest\Util\RestUtils; use Shlinkio\Shlink\Rest\Util\RestUtils;
use Zend\Diactoros\Response\JsonResponse; use Zend\Diactoros\Response\JsonResponse;
use Zend\Diactoros\ServerRequestFactory; use Zend\Diactoros\ServerRequestFactory;
use Zend\I18n\Translator\Translator; use Zend\I18n\Translator\Translator;
class DeleteShortCodeActionTest extends TestCase class DeleteShortUrlActionTest extends TestCase
{ {
/** /**
* @var DeleteShortCodeAction * @var DeleteShortUrlAction
*/ */
private $action; private $action;
/** /**
@ -28,7 +28,7 @@ class DeleteShortCodeActionTest extends TestCase
public function setUp() public function setUp()
{ {
$this->service = $this->prophesize(DeleteShortUrlServiceInterface::class); $this->service = $this->prophesize(DeleteShortUrlServiceInterface::class);
$this->action = new DeleteShortCodeAction($this->service->reveal(), Translator::factory([])); $this->action = new DeleteShortUrlAction($this->service->reveal(), Translator::factory([]));
} }
/** /**

View File

@ -1,7 +1,7 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Action\ShortCode; namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Argument; use Prophecy\Argument;
@ -9,16 +9,16 @@ use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException; use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface; use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface;
use Shlinkio\Shlink\Rest\Action\ShortCode\EditShortCodeAction; use Shlinkio\Shlink\Rest\Action\ShortUrl\EditShortUrlAction;
use Shlinkio\Shlink\Rest\Util\RestUtils; use Shlinkio\Shlink\Rest\Util\RestUtils;
use Zend\Diactoros\Response\JsonResponse; use Zend\Diactoros\Response\JsonResponse;
use Zend\Diactoros\ServerRequestFactory; use Zend\Diactoros\ServerRequestFactory;
use Zend\I18n\Translator\Translator; use Zend\I18n\Translator\Translator;
class EditShortCodeActionTest extends TestCase class EditShortUrlActionTest extends TestCase
{ {
/** /**
* @var EditShortCodeAction * @var EditShortUrlAction
*/ */
private $action; private $action;
/** /**
@ -29,7 +29,7 @@ class EditShortCodeActionTest extends TestCase
public function setUp() public function setUp()
{ {
$this->shortUrlService = $this->prophesize(ShortUrlServiceInterface::class); $this->shortUrlService = $this->prophesize(ShortUrlServiceInterface::class);
$this->action = new EditShortCodeAction($this->shortUrlService->reveal(), Translator::factory([])); $this->action = new EditShortUrlAction($this->shortUrlService->reveal(), Translator::factory([]));
} }
/** /**

View File

@ -1,21 +1,21 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Action\ShortCode; namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy; use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException; use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
use Shlinkio\Shlink\Core\Service\ShortUrlService; use Shlinkio\Shlink\Core\Service\ShortUrlService;
use Shlinkio\Shlink\Rest\Action\ShortCode\EditShortCodeTagsAction; use Shlinkio\Shlink\Rest\Action\ShortUrl\EditShortUrlTagsAction;
use Zend\Diactoros\ServerRequestFactory; use Zend\Diactoros\ServerRequestFactory;
use Zend\I18n\Translator\Translator; use Zend\I18n\Translator\Translator;
class EditShortCodeTagsActionTest extends TestCase class EditShortUrlTagsActionTest extends TestCase
{ {
/** /**
* @var EditShortCodeTagsAction * @var EditShortUrlTagsAction
*/ */
protected $action; protected $action;
/** /**
@ -26,7 +26,7 @@ class EditShortCodeTagsActionTest extends TestCase
public function setUp() public function setUp()
{ {
$this->shortUrlService = $this->prophesize(ShortUrlService::class); $this->shortUrlService = $this->prophesize(ShortUrlService::class);
$this->action = new EditShortCodeTagsAction($this->shortUrlService->reveal(), Translator::factory([])); $this->action = new EditShortUrlTagsAction($this->shortUrlService->reveal(), Translator::factory([]));
} }
/** /**

View File

@ -1,21 +1,21 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Action\ShortCode; namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy; use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Service\ShortUrlService; use Shlinkio\Shlink\Core\Service\ShortUrlService;
use Shlinkio\Shlink\Rest\Action\ShortCode\ListShortCodesAction; use Shlinkio\Shlink\Rest\Action\ShortUrl\ListShortUrlsAction;
use Zend\Diactoros\ServerRequestFactory; use Zend\Diactoros\ServerRequestFactory;
use Zend\I18n\Translator\Translator; use Zend\I18n\Translator\Translator;
use Zend\Paginator\Adapter\ArrayAdapter; use Zend\Paginator\Adapter\ArrayAdapter;
use Zend\Paginator\Paginator; use Zend\Paginator\Paginator;
class ListShortCodesActionTest extends TestCase class ListShortUrlsActionTest extends TestCase
{ {
/** /**
* @var ListShortCodesAction * @var ListShortUrlsAction
*/ */
protected $action; protected $action;
/** /**
@ -26,7 +26,7 @@ class ListShortCodesActionTest extends TestCase
public function setUp() public function setUp()
{ {
$this->service = $this->prophesize(ShortUrlService::class); $this->service = $this->prophesize(ShortUrlService::class);
$this->action = new ListShortCodesAction($this->service->reveal(), Translator::factory([]), [ $this->action = new ListShortUrlsAction($this->service->reveal(), Translator::factory([]), [
'hostname' => 'doma.in', 'hostname' => 'doma.in',
'schema' => 'https', 'schema' => 'https',
]); ]);

View File

@ -1,7 +1,7 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Action\ShortCode; namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy; use Prophecy\Prophecy\ObjectProphecy;
@ -9,15 +9,15 @@ use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException; use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException; use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
use Shlinkio\Shlink\Core\Service\UrlShortener; use Shlinkio\Shlink\Core\Service\UrlShortener;
use Shlinkio\Shlink\Rest\Action\ShortCode\ResolveUrlAction; use Shlinkio\Shlink\Rest\Action\ShortUrl\ResolveShortUrlAction;
use Shlinkio\Shlink\Rest\Util\RestUtils; use Shlinkio\Shlink\Rest\Util\RestUtils;
use Zend\Diactoros\ServerRequestFactory; use Zend\Diactoros\ServerRequestFactory;
use Zend\I18n\Translator\Translator; use Zend\I18n\Translator\Translator;
class ResolveUrlActionTest extends TestCase class ResolveShortUrlActionTest extends TestCase
{ {
/** /**
* @var ResolveUrlAction * @var ResolveShortUrlAction
*/ */
protected $action; protected $action;
/** /**
@ -28,7 +28,7 @@ class ResolveUrlActionTest extends TestCase
public function setUp() public function setUp()
{ {
$this->urlShortener = $this->prophesize(UrlShortener::class); $this->urlShortener = $this->prophesize(UrlShortener::class);
$this->action = new ResolveUrlAction($this->urlShortener->reveal(), Translator::factory([]), []); $this->action = new ResolveShortUrlAction($this->urlShortener->reveal(), Translator::factory([]), []);
} }
/** /**

View File

@ -1,7 +1,7 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Action\ShortCode; namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl;
use PHPUnit\Framework\Assert; use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
@ -10,17 +10,17 @@ use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\UriInterface; use Psr\Http\Message\UriInterface;
use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface; use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
use Shlinkio\Shlink\Rest\Action\ShortCode\SingleStepCreateShortCodeAction; use Shlinkio\Shlink\Rest\Action\ShortUrl\SingleStepCreateShortUrlAction;
use Shlinkio\Shlink\Rest\Entity\ApiKey; use Shlinkio\Shlink\Rest\Entity\ApiKey;
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface; use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
use Zend\Diactoros\Response\JsonResponse; use Zend\Diactoros\Response\JsonResponse;
use Zend\Diactoros\ServerRequestFactory; use Zend\Diactoros\ServerRequestFactory;
use Zend\I18n\Translator\Translator; use Zend\I18n\Translator\Translator;
class SingleStepCreateShortCodeActionTest extends TestCase class SingleStepCreateShortUrlActionTest extends TestCase
{ {
/** /**
* @var SingleStepCreateShortCodeAction * @var SingleStepCreateShortUrlAction
*/ */
private $action; private $action;
/** /**
@ -37,7 +37,7 @@ class SingleStepCreateShortCodeActionTest extends TestCase
$this->urlShortener = $this->prophesize(UrlShortenerInterface::class); $this->urlShortener = $this->prophesize(UrlShortenerInterface::class);
$this->apiKeyService = $this->prophesize(ApiKeyServiceInterface::class); $this->apiKeyService = $this->prophesize(ApiKeyServiceInterface::class);
$this->action = new SingleStepCreateShortCodeAction( $this->action = new SingleStepCreateShortUrlAction(
$this->urlShortener->reveal(), $this->urlShortener->reveal(),
Translator::factory([]), Translator::factory([]),
$this->apiKeyService->reveal(), $this->apiKeyService->reveal(),

View File

@ -1,21 +1,21 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Middleware\ShortCode; namespace ShlinkioTest\Shlink\Rest\Middleware\ShortUrl;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Argument; use Prophecy\Argument;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface; use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Rest\Middleware\ShortCode\CreateShortCodeContentNegotiationMiddleware; use Shlinkio\Shlink\Rest\Middleware\ShortUrl\CreateShortUrlContentNegotiationMiddleware;
use Zend\Diactoros\Response; use Zend\Diactoros\Response;
use Zend\Diactoros\Response\JsonResponse; use Zend\Diactoros\Response\JsonResponse;
use Zend\Diactoros\ServerRequestFactory; use Zend\Diactoros\ServerRequestFactory;
class CreateShortCodeContentNegotiationMiddlewareTest extends TestCase class CreateShortUrlContentNegotiationMiddlewareTest extends TestCase
{ {
/** /**
* @var CreateShortCodeContentNegotiationMiddleware * @var CreateShortUrlContentNegotiationMiddleware
*/ */
private $middleware; private $middleware;
/** /**
@ -25,7 +25,7 @@ class CreateShortCodeContentNegotiationMiddlewareTest extends TestCase
public function setUp() public function setUp()
{ {
$this->middleware = new CreateShortCodeContentNegotiationMiddleware(); $this->middleware = new CreateShortUrlContentNegotiationMiddleware();
$this->requestHandler = $this->prophesize(RequestHandlerInterface::class); $this->requestHandler = $this->prophesize(RequestHandlerInterface::class);
} }

View File

@ -0,0 +1,50 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Middleware\ShortUrl;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UriInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Rest\Middleware\ShortUrl\ShortCodePathMiddleware;
use Zend\Diactoros\Response;
use Zend\Diactoros\Uri;
class ShortCodePathMiddlewareTest extends TestCase
{
private $middleware;
private $requestHandler;
public function setUp()
{
$this->middleware = new ShortCodePathMiddleware();
$this->requestHandler = $this->prophesize(RequestHandlerInterface::class);
$this->requestHandler->handle(Argument::type(ServerRequestInterface::class))->willReturn(new Response());
}
/**
* @test
*/
public function properlyReplacesTheOldPathByTheNewOne()
{
$uri = new Uri('/short-codes/foo');
$request = $this->prophesize(ServerRequestInterface::class);
$request->getUri()->willReturn($uri);
$withUri = $request->withUri(Argument::that(function (UriInterface $uri) {
$path = $uri->getPath();
Assert::assertContains('/short-urls', $path);
Assert::assertNotContains('/short-codes', $path);
return $uri;
}))->willReturn($request->reveal());
$this->middleware->process($request->reveal(), $this->requestHandler->reveal());
$withUri->shouldHaveBeenCalledTimes(1);
}
}