mirror of
https://github.com/shlinkio/shlink.git
synced 2025-03-27 12:01:49 +03:00
Created new service to resolve short URLs
This commit is contained in:
parent
f71bd84a20
commit
4ebd48b2b0
18 changed files with 193 additions and 89 deletions
|
@ -55,7 +55,7 @@ return [
|
||||||
GeolocationDbUpdater::class => [DbUpdater::class, Reader::class, 'Shlinkio\Shlink\LocalLockFactory'],
|
GeolocationDbUpdater::class => [DbUpdater::class, Reader::class, 'Shlinkio\Shlink\LocalLockFactory'],
|
||||||
|
|
||||||
Command\ShortUrl\GenerateShortUrlCommand::class => [Service\UrlShortener::class, 'config.url_shortener.domain'],
|
Command\ShortUrl\GenerateShortUrlCommand::class => [Service\UrlShortener::class, 'config.url_shortener.domain'],
|
||||||
Command\ShortUrl\ResolveUrlCommand::class => [Service\UrlShortener::class],
|
Command\ShortUrl\ResolveUrlCommand::class => [Service\ShortUrl\ShortUrlResolver::class],
|
||||||
Command\ShortUrl\ListShortUrlsCommand::class => [Service\ShortUrlService::class, 'config.url_shortener.domain'],
|
Command\ShortUrl\ListShortUrlsCommand::class => [Service\ShortUrlService::class, 'config.url_shortener.domain'],
|
||||||
Command\ShortUrl\GetVisitsCommand::class => [Service\VisitsTracker::class],
|
Command\ShortUrl\GetVisitsCommand::class => [Service\VisitsTracker::class],
|
||||||
Command\ShortUrl\DeleteShortUrlCommand::class => [Service\ShortUrl\DeleteShortUrlService::class],
|
Command\ShortUrl\DeleteShortUrlCommand::class => [Service\ShortUrl\DeleteShortUrlService::class],
|
||||||
|
|
|
@ -6,7 +6,7 @@ namespace Shlinkio\Shlink\CLI\Command\ShortUrl;
|
||||||
|
|
||||||
use Shlinkio\Shlink\CLI\Util\ExitCodes;
|
use Shlinkio\Shlink\CLI\Util\ExitCodes;
|
||||||
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
|
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
|
||||||
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
|
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortUrlResolverInterface;
|
||||||
use Symfony\Component\Console\Command\Command;
|
use Symfony\Component\Console\Command\Command;
|
||||||
use Symfony\Component\Console\Input\InputArgument;
|
use Symfony\Component\Console\Input\InputArgument;
|
||||||
use Symfony\Component\Console\Input\InputInterface;
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
|
@ -20,12 +20,12 @@ class ResolveUrlCommand extends Command
|
||||||
{
|
{
|
||||||
public const NAME = 'short-url:parse';
|
public const NAME = 'short-url:parse';
|
||||||
|
|
||||||
private UrlShortenerInterface $urlShortener;
|
private ShortUrlResolverInterface $urlResolver;
|
||||||
|
|
||||||
public function __construct(UrlShortenerInterface $urlShortener)
|
public function __construct(ShortUrlResolverInterface $urlResolver)
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
$this->urlShortener = $urlShortener;
|
$this->urlResolver = $urlResolver;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function configure(): void
|
protected function configure(): void
|
||||||
|
@ -58,7 +58,7 @@ class ResolveUrlCommand extends Command
|
||||||
$domain = $input->getOption('domain');
|
$domain = $input->getOption('domain');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$url = $this->urlShortener->shortCodeToUrl($shortCode, $domain);
|
$url = $this->urlResolver->shortCodeToShortUrl($shortCode, $domain);
|
||||||
$output->writeln(sprintf('Long URL: <info>%s</info>', $url->getLongUrl()));
|
$output->writeln(sprintf('Long URL: <info>%s</info>', $url->getLongUrl()));
|
||||||
return ExitCodes::EXIT_SUCCESS;
|
return ExitCodes::EXIT_SUCCESS;
|
||||||
} catch (ShortUrlNotFoundException $e) {
|
} catch (ShortUrlNotFoundException $e) {
|
||||||
|
|
|
@ -9,7 +9,7 @@ use Prophecy\Prophecy\ObjectProphecy;
|
||||||
use Shlinkio\Shlink\CLI\Command\ShortUrl\ResolveUrlCommand;
|
use Shlinkio\Shlink\CLI\Command\ShortUrl\ResolveUrlCommand;
|
||||||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||||
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
|
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
|
||||||
use Shlinkio\Shlink\Core\Service\UrlShortener;
|
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortUrlResolverInterface;
|
||||||
use Symfony\Component\Console\Application;
|
use Symfony\Component\Console\Application;
|
||||||
use Symfony\Component\Console\Tester\CommandTester;
|
use Symfony\Component\Console\Tester\CommandTester;
|
||||||
|
|
||||||
|
@ -20,12 +20,12 @@ use const PHP_EOL;
|
||||||
class ResolveUrlCommandTest extends TestCase
|
class ResolveUrlCommandTest extends TestCase
|
||||||
{
|
{
|
||||||
private CommandTester $commandTester;
|
private CommandTester $commandTester;
|
||||||
private ObjectProphecy $urlShortener;
|
private ObjectProphecy $urlResolver;
|
||||||
|
|
||||||
public function setUp(): void
|
public function setUp(): void
|
||||||
{
|
{
|
||||||
$this->urlShortener = $this->prophesize(UrlShortener::class);
|
$this->urlResolver = $this->prophesize(ShortUrlResolverInterface::class);
|
||||||
$command = new ResolveUrlCommand($this->urlShortener->reveal());
|
$command = new ResolveUrlCommand($this->urlResolver->reveal());
|
||||||
$app = new Application();
|
$app = new Application();
|
||||||
$app->add($command);
|
$app->add($command);
|
||||||
|
|
||||||
|
@ -38,8 +38,8 @@ class ResolveUrlCommandTest extends TestCase
|
||||||
$shortCode = 'abc123';
|
$shortCode = 'abc123';
|
||||||
$expectedUrl = 'http://domain.com/foo/bar';
|
$expectedUrl = 'http://domain.com/foo/bar';
|
||||||
$shortUrl = new ShortUrl($expectedUrl);
|
$shortUrl = new ShortUrl($expectedUrl);
|
||||||
$this->urlShortener->shortCodeToUrl($shortCode, null)->willReturn($shortUrl)
|
$this->urlResolver->shortCodeToShortUrl($shortCode, null)->willReturn($shortUrl)
|
||||||
->shouldBeCalledOnce();
|
->shouldBeCalledOnce();
|
||||||
|
|
||||||
$this->commandTester->execute(['shortCode' => $shortCode]);
|
$this->commandTester->execute(['shortCode' => $shortCode]);
|
||||||
$output = $this->commandTester->getDisplay();
|
$output = $this->commandTester->getDisplay();
|
||||||
|
@ -50,7 +50,7 @@ class ResolveUrlCommandTest extends TestCase
|
||||||
public function incorrectShortCodeOutputsErrorMessage(): void
|
public function incorrectShortCodeOutputsErrorMessage(): void
|
||||||
{
|
{
|
||||||
$shortCode = 'abc123';
|
$shortCode = 'abc123';
|
||||||
$this->urlShortener->shortCodeToUrl($shortCode, null)
|
$this->urlResolver->shortCodeToShortUrl($shortCode, null)
|
||||||
->willThrow(ShortUrlNotFoundException::fromNotFoundShortCode($shortCode))
|
->willThrow(ShortUrlNotFoundException::fromNotFoundShortCode($shortCode))
|
||||||
->shouldBeCalledOnce();
|
->shouldBeCalledOnce();
|
||||||
|
|
||||||
|
|
|
@ -30,6 +30,7 @@ return [
|
||||||
Service\VisitService::class => ConfigAbstractFactory::class,
|
Service\VisitService::class => ConfigAbstractFactory::class,
|
||||||
Service\Tag\TagService::class => ConfigAbstractFactory::class,
|
Service\Tag\TagService::class => ConfigAbstractFactory::class,
|
||||||
Service\ShortUrl\DeleteShortUrlService::class => ConfigAbstractFactory::class,
|
Service\ShortUrl\DeleteShortUrlService::class => ConfigAbstractFactory::class,
|
||||||
|
Service\ShortUrl\ShortUrlResolver::class => ConfigAbstractFactory::class,
|
||||||
|
|
||||||
Util\UrlValidator::class => ConfigAbstractFactory::class,
|
Util\UrlValidator::class => ConfigAbstractFactory::class,
|
||||||
|
|
||||||
|
@ -56,22 +57,27 @@ return [
|
||||||
Service\VisitService::class => ['em'],
|
Service\VisitService::class => ['em'],
|
||||||
Service\Tag\TagService::class => ['em'],
|
Service\Tag\TagService::class => ['em'],
|
||||||
Service\ShortUrl\DeleteShortUrlService::class => ['em', Options\DeleteShortUrlsOptions::class],
|
Service\ShortUrl\DeleteShortUrlService::class => ['em', Options\DeleteShortUrlsOptions::class],
|
||||||
|
Service\ShortUrl\ShortUrlResolver::class => ['em'],
|
||||||
|
|
||||||
Util\UrlValidator::class => ['httpClient'],
|
Util\UrlValidator::class => ['httpClient'],
|
||||||
|
|
||||||
Action\RedirectAction::class => [
|
Action\RedirectAction::class => [
|
||||||
Service\UrlShortener::class,
|
Service\ShortUrl\ShortUrlResolver::class,
|
||||||
Service\VisitsTracker::class,
|
Service\VisitsTracker::class,
|
||||||
Options\AppOptions::class,
|
Options\AppOptions::class,
|
||||||
'Logger_Shlink',
|
'Logger_Shlink',
|
||||||
],
|
],
|
||||||
Action\PixelAction::class => [
|
Action\PixelAction::class => [
|
||||||
Service\UrlShortener::class,
|
Service\ShortUrl\ShortUrlResolver::class,
|
||||||
Service\VisitsTracker::class,
|
Service\VisitsTracker::class,
|
||||||
Options\AppOptions::class,
|
Options\AppOptions::class,
|
||||||
'Logger_Shlink',
|
'Logger_Shlink',
|
||||||
],
|
],
|
||||||
Action\QrCodeAction::class => [RouterInterface::class, Service\UrlShortener::class, 'Logger_Shlink'],
|
Action\QrCodeAction::class => [
|
||||||
|
RouterInterface::class,
|
||||||
|
Service\ShortUrl\ShortUrlResolver::class,
|
||||||
|
'Logger_Shlink',
|
||||||
|
],
|
||||||
|
|
||||||
Middleware\QrCodeCacheMiddleware::class => [Cache::class],
|
Middleware\QrCodeCacheMiddleware::class => [Cache::class],
|
||||||
],
|
],
|
||||||
|
|
|
@ -15,7 +15,7 @@ use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||||
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
|
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
|
||||||
use Shlinkio\Shlink\Core\Model\Visitor;
|
use Shlinkio\Shlink\Core\Model\Visitor;
|
||||||
use Shlinkio\Shlink\Core\Options\AppOptions;
|
use Shlinkio\Shlink\Core\Options\AppOptions;
|
||||||
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
|
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortUrlResolverInterface;
|
||||||
use Shlinkio\Shlink\Core\Service\VisitsTrackerInterface;
|
use Shlinkio\Shlink\Core\Service\VisitsTrackerInterface;
|
||||||
|
|
||||||
use function array_key_exists;
|
use function array_key_exists;
|
||||||
|
@ -25,18 +25,18 @@ use function GuzzleHttp\Psr7\parse_query;
|
||||||
|
|
||||||
abstract class AbstractTrackingAction implements MiddlewareInterface
|
abstract class AbstractTrackingAction implements MiddlewareInterface
|
||||||
{
|
{
|
||||||
private UrlShortenerInterface $urlShortener;
|
private ShortUrlResolverInterface $urlResolver;
|
||||||
private VisitsTrackerInterface $visitTracker;
|
private VisitsTrackerInterface $visitTracker;
|
||||||
private AppOptions $appOptions;
|
private AppOptions $appOptions;
|
||||||
private LoggerInterface $logger;
|
private LoggerInterface $logger;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
UrlShortenerInterface $urlShortener,
|
ShortUrlResolverInterface $urlResolver,
|
||||||
VisitsTrackerInterface $visitTracker,
|
VisitsTrackerInterface $visitTracker,
|
||||||
AppOptions $appOptions,
|
AppOptions $appOptions,
|
||||||
?LoggerInterface $logger = null
|
?LoggerInterface $logger = null
|
||||||
) {
|
) {
|
||||||
$this->urlShortener = $urlShortener;
|
$this->urlResolver = $urlResolver;
|
||||||
$this->visitTracker = $visitTracker;
|
$this->visitTracker = $visitTracker;
|
||||||
$this->appOptions = $appOptions;
|
$this->appOptions = $appOptions;
|
||||||
$this->logger = $logger ?: new NullLogger();
|
$this->logger = $logger ?: new NullLogger();
|
||||||
|
@ -50,7 +50,7 @@ abstract class AbstractTrackingAction implements MiddlewareInterface
|
||||||
$disableTrackParam = $this->appOptions->getDisableTrackParam();
|
$disableTrackParam = $this->appOptions->getDisableTrackParam();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$url = $this->urlShortener->shortCodeToUrl($shortCode, $domain);
|
$url = $this->urlResolver->shortCodeToEnabledShortUrl($shortCode, $domain);
|
||||||
|
|
||||||
// Track visit to this short code
|
// Track visit to this short code
|
||||||
if ($disableTrackParam === null || ! array_key_exists($disableTrackParam, $query)) {
|
if ($disableTrackParam === null || ! array_key_exists($disableTrackParam, $query)) {
|
||||||
|
|
|
@ -5,7 +5,6 @@ declare(strict_types=1);
|
||||||
namespace Shlinkio\Shlink\Core\Action;
|
namespace Shlinkio\Shlink\Core\Action;
|
||||||
|
|
||||||
use Endroid\QrCode\QrCode;
|
use Endroid\QrCode\QrCode;
|
||||||
use Mezzio\Router\Exception\RuntimeException;
|
|
||||||
use Mezzio\Router\RouterInterface;
|
use Mezzio\Router\RouterInterface;
|
||||||
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,7 +14,7 @@ use Psr\Log\LoggerInterface;
|
||||||
use Psr\Log\NullLogger;
|
use Psr\Log\NullLogger;
|
||||||
use Shlinkio\Shlink\Common\Response\QrCodeResponse;
|
use Shlinkio\Shlink\Common\Response\QrCodeResponse;
|
||||||
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
|
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
|
||||||
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
|
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortUrlResolverInterface;
|
||||||
|
|
||||||
class QrCodeAction implements MiddlewareInterface
|
class QrCodeAction implements MiddlewareInterface
|
||||||
{
|
{
|
||||||
|
@ -24,27 +23,19 @@ class QrCodeAction implements MiddlewareInterface
|
||||||
private const MAX_SIZE = 1000;
|
private const MAX_SIZE = 1000;
|
||||||
|
|
||||||
private RouterInterface $router;
|
private RouterInterface $router;
|
||||||
private UrlShortenerInterface $urlShortener;
|
private ShortUrlResolverInterface $urlResolver;
|
||||||
private LoggerInterface $logger;
|
private LoggerInterface $logger;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
RouterInterface $router,
|
RouterInterface $router,
|
||||||
UrlShortenerInterface $urlShortener,
|
ShortUrlResolverInterface $urlResolver,
|
||||||
?LoggerInterface $logger = null
|
?LoggerInterface $logger = null
|
||||||
) {
|
) {
|
||||||
$this->router = $router;
|
$this->router = $router;
|
||||||
$this->urlShortener = $urlShortener;
|
$this->urlResolver = $urlResolver;
|
||||||
$this->logger = $logger ?: new NullLogger();
|
$this->logger = $logger ?: new NullLogger();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Process an incoming server request and return a response, optionally delegating
|
|
||||||
* to the next middleware component to create the response.
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @throws \InvalidArgumentException
|
|
||||||
* @throws RuntimeException
|
|
||||||
*/
|
|
||||||
public function process(Request $request, RequestHandlerInterface $handler): Response
|
public function process(Request $request, RequestHandlerInterface $handler): Response
|
||||||
{
|
{
|
||||||
// Make sure the short URL exists for this short code
|
// Make sure the short URL exists for this short code
|
||||||
|
@ -52,7 +43,7 @@ class QrCodeAction implements MiddlewareInterface
|
||||||
$domain = $request->getUri()->getAuthority();
|
$domain = $request->getUri()->getAuthority();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$this->urlShortener->shortCodeToUrl($shortCode, $domain);
|
$this->urlResolver->shortCodeToEnabledShortUrl($shortCode, $domain);
|
||||||
} catch (ShortUrlNotFoundException $e) {
|
} catch (ShortUrlNotFoundException $e) {
|
||||||
$this->logger->warning('An error occurred while creating QR code. {e}', ['e' => $e]);
|
$this->logger->warning('An error occurred while creating QR code. {e}', ['e' => $e]);
|
||||||
return $handler->handle($request);
|
return $handler->handle($request);
|
||||||
|
|
|
@ -149,9 +149,25 @@ class ShortUrl extends AbstractEntity
|
||||||
return $this->maxVisits;
|
return $this->maxVisits;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function maxVisitsReached(): bool
|
public function isEnabled(): bool
|
||||||
{
|
{
|
||||||
return $this->maxVisits !== null && $this->getVisitsCount() >= $this->maxVisits;
|
$maxVisitsReached = $this->maxVisits !== null && $this->getVisitsCount() >= $this->maxVisits;
|
||||||
|
if ($maxVisitsReached) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$now = Chronos::now();
|
||||||
|
$beforeValidSince = $this->validSince !== null && $this->validSince->gt($now);
|
||||||
|
if ($beforeValidSince) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$afterValidUntil = $this->validUntil !== null && $this->validUntil->lt($now);
|
||||||
|
if ($afterValidUntil) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function toString(array $domainConfig): string
|
public function toString(array $domainConfig): string
|
||||||
|
@ -186,12 +202,10 @@ class ShortUrl extends AbstractEntity
|
||||||
}
|
}
|
||||||
|
|
||||||
$shortUrlTags = invoke($this->getTags(), '__toString');
|
$shortUrlTags = invoke($this->getTags(), '__toString');
|
||||||
$hasAllTags = count($shortUrlTags) === count($tags) && array_reduce(
|
return count($shortUrlTags) === count($tags) && array_reduce(
|
||||||
$tags,
|
$tags,
|
||||||
fn (bool $hasAllTags, string $tag) => $hasAllTags && contains($shortUrlTags, $tag),
|
fn (bool $hasAllTags, string $tag) => $hasAllTags && contains($shortUrlTags, $tag),
|
||||||
true,
|
true,
|
||||||
);
|
);
|
||||||
|
|
||||||
return $hasAllTags;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -146,8 +146,6 @@ class ShortUrlRepository extends EntityRepository implements ShortUrlRepositoryI
|
||||||
FROM Shlinkio\Shlink\Core\Entity\ShortUrl AS s
|
FROM Shlinkio\Shlink\Core\Entity\ShortUrl AS s
|
||||||
LEFT JOIN s.domain AS d
|
LEFT JOIN s.domain AS d
|
||||||
WHERE s.shortCode = :shortCode
|
WHERE s.shortCode = :shortCode
|
||||||
AND (s.validSince <= :now OR s.validSince IS NULL)
|
|
||||||
AND (s.validUntil >= :now OR s.validUntil IS NULL)
|
|
||||||
AND (s.domain IS NULL OR d.authority = :domain)
|
AND (s.domain IS NULL OR d.authority = :domain)
|
||||||
ORDER BY s.domain {$ordering}
|
ORDER BY s.domain {$ordering}
|
||||||
DQL;
|
DQL;
|
||||||
|
@ -156,7 +154,6 @@ DQL;
|
||||||
$query->setMaxResults(1)
|
$query->setMaxResults(1)
|
||||||
->setParameters([
|
->setParameters([
|
||||||
'shortCode' => $shortCode,
|
'shortCode' => $shortCode,
|
||||||
'now' => Chronos::now(),
|
|
||||||
'domain' => $domain,
|
'domain' => $domain,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
@ -166,9 +163,7 @@ DQL;
|
||||||
// * The short URL matching the short code but without any domain, or
|
// * The short URL matching the short code but without any domain, or
|
||||||
// * No short URL at all
|
// * No short URL at all
|
||||||
|
|
||||||
/** @var ShortUrl|null $shortUrl */
|
return $query->getOneOrNullResult();
|
||||||
$shortUrl = $query->getOneOrNullResult();
|
|
||||||
return $shortUrl !== null && ! $shortUrl->maxVisitsReached() ? $shortUrl : null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function shortCodeIsInUse(string $slug, ?string $domain = null): bool
|
public function shortCodeIsInUse(string $slug, ?string $domain = null): bool
|
||||||
|
|
48
module/Core/src/Service/ShortUrl/ShortUrlResolver.php
Normal file
48
module/Core/src/Service/ShortUrl/ShortUrlResolver.php
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Shlinkio\Shlink\Core\Service\ShortUrl;
|
||||||
|
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||||
|
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
|
||||||
|
use Shlinkio\Shlink\Core\Repository\ShortUrlRepository;
|
||||||
|
|
||||||
|
class ShortUrlResolver implements ShortUrlResolverInterface
|
||||||
|
{
|
||||||
|
private EntityManagerInterface $em;
|
||||||
|
|
||||||
|
public function __construct(EntityManagerInterface $em)
|
||||||
|
{
|
||||||
|
$this->em = $em;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws ShortUrlNotFoundException
|
||||||
|
*/
|
||||||
|
public function shortCodeToShortUrl(string $shortCode, ?string $domain = null): ShortUrl
|
||||||
|
{
|
||||||
|
/** @var ShortUrlRepository $shortUrlRepo */
|
||||||
|
$shortUrlRepo = $this->em->getRepository(ShortUrl::class);
|
||||||
|
$shortUrl = $shortUrlRepo->findOneByShortCode($shortCode, $domain);
|
||||||
|
if ($shortUrl === null) {
|
||||||
|
throw ShortUrlNotFoundException::fromNotFoundShortCode($shortCode, $domain);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $shortUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws ShortUrlNotFoundException
|
||||||
|
*/
|
||||||
|
public function shortCodeToEnabledShortUrl(string $shortCode, ?string $domain = null): ShortUrl
|
||||||
|
{
|
||||||
|
$shortUrl = $this->shortCodeToShortUrl($shortCode, $domain);
|
||||||
|
if (! $shortUrl->isEnabled()) {
|
||||||
|
throw ShortUrlNotFoundException::fromNotFoundShortCode($shortCode, $domain);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $shortUrl;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Shlinkio\Shlink\Core\Service\ShortUrl;
|
||||||
|
|
||||||
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||||
|
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
|
||||||
|
|
||||||
|
interface ShortUrlResolverInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @throws ShortUrlNotFoundException
|
||||||
|
*/
|
||||||
|
public function shortCodeToShortUrl(string $shortCode, ?string $domain = null): ShortUrl;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws ShortUrlNotFoundException
|
||||||
|
*/
|
||||||
|
public function shortCodeToEnabledShortUrl(string $shortCode, ?string $domain = null): ShortUrl;
|
||||||
|
}
|
|
@ -13,22 +13,22 @@ use Shlinkio\Shlink\Common\Response\PixelResponse;
|
||||||
use Shlinkio\Shlink\Core\Action\PixelAction;
|
use Shlinkio\Shlink\Core\Action\PixelAction;
|
||||||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||||
use Shlinkio\Shlink\Core\Options\AppOptions;
|
use Shlinkio\Shlink\Core\Options\AppOptions;
|
||||||
use Shlinkio\Shlink\Core\Service\UrlShortener;
|
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortUrlResolverInterface;
|
||||||
use Shlinkio\Shlink\Core\Service\VisitsTracker;
|
use Shlinkio\Shlink\Core\Service\VisitsTracker;
|
||||||
|
|
||||||
class PixelActionTest extends TestCase
|
class PixelActionTest extends TestCase
|
||||||
{
|
{
|
||||||
private PixelAction $action;
|
private PixelAction $action;
|
||||||
private ObjectProphecy $urlShortener;
|
private ObjectProphecy $urlResolver;
|
||||||
private ObjectProphecy $visitTracker;
|
private ObjectProphecy $visitTracker;
|
||||||
|
|
||||||
public function setUp(): void
|
public function setUp(): void
|
||||||
{
|
{
|
||||||
$this->urlShortener = $this->prophesize(UrlShortener::class);
|
$this->urlResolver = $this->prophesize(ShortUrlResolverInterface::class);
|
||||||
$this->visitTracker = $this->prophesize(VisitsTracker::class);
|
$this->visitTracker = $this->prophesize(VisitsTracker::class);
|
||||||
|
|
||||||
$this->action = new PixelAction(
|
$this->action = new PixelAction(
|
||||||
$this->urlShortener->reveal(),
|
$this->urlResolver->reveal(),
|
||||||
$this->visitTracker->reveal(),
|
$this->visitTracker->reveal(),
|
||||||
new AppOptions(),
|
new AppOptions(),
|
||||||
);
|
);
|
||||||
|
@ -38,7 +38,7 @@ class PixelActionTest extends TestCase
|
||||||
public function imageIsReturned(): void
|
public function imageIsReturned(): void
|
||||||
{
|
{
|
||||||
$shortCode = 'abc123';
|
$shortCode = 'abc123';
|
||||||
$this->urlShortener->shortCodeToUrl($shortCode, '')->willReturn(
|
$this->urlResolver->shortCodeToEnabledShortUrl($shortCode, '')->willReturn(
|
||||||
new ShortUrl('http://domain.com/foo/bar'),
|
new ShortUrl('http://domain.com/foo/bar'),
|
||||||
)->shouldBeCalledOnce();
|
)->shouldBeCalledOnce();
|
||||||
$this->visitTracker->track(Argument::cetera())->shouldBeCalledOnce();
|
$this->visitTracker->track(Argument::cetera())->shouldBeCalledOnce();
|
||||||
|
|
|
@ -15,29 +15,29 @@ use Shlinkio\Shlink\Common\Response\QrCodeResponse;
|
||||||
use Shlinkio\Shlink\Core\Action\QrCodeAction;
|
use Shlinkio\Shlink\Core\Action\QrCodeAction;
|
||||||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||||
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
|
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
|
||||||
use Shlinkio\Shlink\Core\Service\UrlShortener;
|
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortUrlResolverInterface;
|
||||||
|
|
||||||
class QrCodeActionTest extends TestCase
|
class QrCodeActionTest extends TestCase
|
||||||
{
|
{
|
||||||
private QrCodeAction $action;
|
private QrCodeAction $action;
|
||||||
private ObjectProphecy $urlShortener;
|
private ObjectProphecy $urlResolver;
|
||||||
|
|
||||||
public function setUp(): void
|
public function setUp(): void
|
||||||
{
|
{
|
||||||
$router = $this->prophesize(RouterInterface::class);
|
$router = $this->prophesize(RouterInterface::class);
|
||||||
$router->generateUri(Argument::cetera())->willReturn('/foo/bar');
|
$router->generateUri(Argument::cetera())->willReturn('/foo/bar');
|
||||||
|
|
||||||
$this->urlShortener = $this->prophesize(UrlShortener::class);
|
$this->urlResolver = $this->prophesize(ShortUrlResolverInterface::class);
|
||||||
|
|
||||||
$this->action = new QrCodeAction($router->reveal(), $this->urlShortener->reveal());
|
$this->action = new QrCodeAction($router->reveal(), $this->urlResolver->reveal());
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function aNotFoundShortCodeWillDelegateIntoNextMiddleware(): void
|
public function aNotFoundShortCodeWillDelegateIntoNextMiddleware(): void
|
||||||
{
|
{
|
||||||
$shortCode = 'abc123';
|
$shortCode = 'abc123';
|
||||||
$this->urlShortener->shortCodeToUrl($shortCode, '')->willThrow(ShortUrlNotFoundException::class)
|
$this->urlResolver->shortCodeToEnabledShortUrl($shortCode, '')->willThrow(ShortUrlNotFoundException::class)
|
||||||
->shouldBeCalledOnce();
|
->shouldBeCalledOnce();
|
||||||
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
||||||
$process = $delegate->handle(Argument::any())->willReturn(new Response());
|
$process = $delegate->handle(Argument::any())->willReturn(new Response());
|
||||||
|
|
||||||
|
@ -50,8 +50,8 @@ class QrCodeActionTest extends TestCase
|
||||||
public function anInvalidShortCodeWillReturnNotFoundResponse(): void
|
public function anInvalidShortCodeWillReturnNotFoundResponse(): void
|
||||||
{
|
{
|
||||||
$shortCode = 'abc123';
|
$shortCode = 'abc123';
|
||||||
$this->urlShortener->shortCodeToUrl($shortCode, '')->willThrow(ShortUrlNotFoundException::class)
|
$this->urlResolver->shortCodeToEnabledShortUrl($shortCode, '')->willThrow(ShortUrlNotFoundException::class)
|
||||||
->shouldBeCalledOnce();
|
->shouldBeCalledOnce();
|
||||||
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
||||||
$process = $delegate->handle(Argument::any())->willReturn(new Response());
|
$process = $delegate->handle(Argument::any())->willReturn(new Response());
|
||||||
|
|
||||||
|
@ -64,8 +64,8 @@ class QrCodeActionTest extends TestCase
|
||||||
public function aCorrectRequestReturnsTheQrCodeResponse(): void
|
public function aCorrectRequestReturnsTheQrCodeResponse(): void
|
||||||
{
|
{
|
||||||
$shortCode = 'abc123';
|
$shortCode = 'abc123';
|
||||||
$this->urlShortener->shortCodeToUrl($shortCode, '')->willReturn(new ShortUrl(''))
|
$this->urlResolver->shortCodeToEnabledShortUrl($shortCode, '')->willReturn(new ShortUrl(''))
|
||||||
->shouldBeCalledOnce();
|
->shouldBeCalledOnce();
|
||||||
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
||||||
|
|
||||||
$resp = $this->action->process(
|
$resp = $this->action->process(
|
||||||
|
|
|
@ -14,24 +14,24 @@ use Shlinkio\Shlink\Core\Action\RedirectAction;
|
||||||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||||
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
|
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
|
||||||
use Shlinkio\Shlink\Core\Options;
|
use Shlinkio\Shlink\Core\Options;
|
||||||
use Shlinkio\Shlink\Core\Service\UrlShortener;
|
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortUrlResolverInterface;
|
||||||
use Shlinkio\Shlink\Core\Service\VisitsTracker;
|
use Shlinkio\Shlink\Core\Service\VisitsTrackerInterface;
|
||||||
|
|
||||||
use function array_key_exists;
|
use function array_key_exists;
|
||||||
|
|
||||||
class RedirectActionTest extends TestCase
|
class RedirectActionTest extends TestCase
|
||||||
{
|
{
|
||||||
private RedirectAction $action;
|
private RedirectAction $action;
|
||||||
private ObjectProphecy $urlShortener;
|
private ObjectProphecy $urlResolver;
|
||||||
private ObjectProphecy $visitTracker;
|
private ObjectProphecy $visitTracker;
|
||||||
|
|
||||||
public function setUp(): void
|
public function setUp(): void
|
||||||
{
|
{
|
||||||
$this->urlShortener = $this->prophesize(UrlShortener::class);
|
$this->urlResolver = $this->prophesize(ShortUrlResolverInterface::class);
|
||||||
$this->visitTracker = $this->prophesize(VisitsTracker::class);
|
$this->visitTracker = $this->prophesize(VisitsTrackerInterface::class);
|
||||||
|
|
||||||
$this->action = new RedirectAction(
|
$this->action = new RedirectAction(
|
||||||
$this->urlShortener->reveal(),
|
$this->urlResolver->reveal(),
|
||||||
$this->visitTracker->reveal(),
|
$this->visitTracker->reveal(),
|
||||||
new Options\AppOptions(['disableTrackParam' => 'foobar']),
|
new Options\AppOptions(['disableTrackParam' => 'foobar']),
|
||||||
);
|
);
|
||||||
|
@ -45,7 +45,7 @@ class RedirectActionTest extends TestCase
|
||||||
{
|
{
|
||||||
$shortCode = 'abc123';
|
$shortCode = 'abc123';
|
||||||
$shortUrl = new ShortUrl('http://domain.com/foo/bar?some=thing');
|
$shortUrl = new ShortUrl('http://domain.com/foo/bar?some=thing');
|
||||||
$shortCodeToUrl = $this->urlShortener->shortCodeToUrl($shortCode, '')->willReturn($shortUrl);
|
$shortCodeToUrl = $this->urlResolver->shortCodeToEnabledShortUrl($shortCode, '')->willReturn($shortUrl);
|
||||||
$track = $this->visitTracker->track(Argument::cetera())->will(function (): void {
|
$track = $this->visitTracker->track(Argument::cetera())->will(function (): void {
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -74,8 +74,8 @@ class RedirectActionTest extends TestCase
|
||||||
public function nextMiddlewareIsInvokedIfLongUrlIsNotFound(): void
|
public function nextMiddlewareIsInvokedIfLongUrlIsNotFound(): void
|
||||||
{
|
{
|
||||||
$shortCode = 'abc123';
|
$shortCode = 'abc123';
|
||||||
$this->urlShortener->shortCodeToUrl($shortCode, '')->willThrow(ShortUrlNotFoundException::class)
|
$this->urlResolver->shortCodeToEnabledShortUrl($shortCode, '')->willThrow(ShortUrlNotFoundException::class)
|
||||||
->shouldBeCalledOnce();
|
->shouldBeCalledOnce();
|
||||||
$this->visitTracker->track(Argument::cetera())->shouldNotBeCalled();
|
$this->visitTracker->track(Argument::cetera())->shouldNotBeCalled();
|
||||||
|
|
||||||
$handler = $this->prophesize(RequestHandlerInterface::class);
|
$handler = $this->prophesize(RequestHandlerInterface::class);
|
||||||
|
|
|
@ -57,7 +57,10 @@ return [
|
||||||
],
|
],
|
||||||
Action\ShortUrl\EditShortUrlAction::class => [Service\ShortUrlService::class, 'Logger_Shlink'],
|
Action\ShortUrl\EditShortUrlAction::class => [Service\ShortUrlService::class, 'Logger_Shlink'],
|
||||||
Action\ShortUrl\DeleteShortUrlAction::class => [Service\ShortUrl\DeleteShortUrlService::class, 'Logger_Shlink'],
|
Action\ShortUrl\DeleteShortUrlAction::class => [Service\ShortUrl\DeleteShortUrlService::class, 'Logger_Shlink'],
|
||||||
Action\ShortUrl\ResolveShortUrlAction::class => [Service\UrlShortener::class, 'config.url_shortener.domain'],
|
Action\ShortUrl\ResolveShortUrlAction::class => [
|
||||||
|
Service\ShortUrl\ShortUrlResolver::class,
|
||||||
|
'config.url_shortener.domain',
|
||||||
|
],
|
||||||
Action\Visit\GetVisitsAction::class => [Service\VisitsTracker::class, 'Logger_Shlink'],
|
Action\Visit\GetVisitsAction::class => [Service\VisitsTracker::class, 'Logger_Shlink'],
|
||||||
Action\ShortUrl\ListShortUrlsAction::class => [
|
Action\ShortUrl\ListShortUrlsAction::class => [
|
||||||
Service\ShortUrlService::class,
|
Service\ShortUrlService::class,
|
||||||
|
|
|
@ -4,12 +4,11 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Shlinkio\Shlink\Rest\Action\ShortUrl;
|
namespace Shlinkio\Shlink\Rest\Action\ShortUrl;
|
||||||
|
|
||||||
use InvalidArgumentException;
|
|
||||||
use Laminas\Diactoros\Response\JsonResponse;
|
use Laminas\Diactoros\Response\JsonResponse;
|
||||||
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;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
|
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortUrlResolverInterface;
|
||||||
use Shlinkio\Shlink\Core\Transformer\ShortUrlDataTransformer;
|
use Shlinkio\Shlink\Core\Transformer\ShortUrlDataTransformer;
|
||||||
use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
|
use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
|
||||||
|
|
||||||
|
@ -18,29 +17,26 @@ class ResolveShortUrlAction extends AbstractRestAction
|
||||||
protected const ROUTE_PATH = '/short-urls/{shortCode}';
|
protected const ROUTE_PATH = '/short-urls/{shortCode}';
|
||||||
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET];
|
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET];
|
||||||
|
|
||||||
private UrlShortenerInterface $urlShortener;
|
private ShortUrlResolverInterface $urlResolver;
|
||||||
private array $domainConfig;
|
private array $domainConfig;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
UrlShortenerInterface $urlShortener,
|
ShortUrlResolverInterface $urlResolver,
|
||||||
array $domainConfig,
|
array $domainConfig,
|
||||||
?LoggerInterface $logger = null
|
?LoggerInterface $logger = null
|
||||||
) {
|
) {
|
||||||
parent::__construct($logger);
|
parent::__construct($logger);
|
||||||
$this->urlShortener = $urlShortener;
|
$this->urlResolver = $urlResolver;
|
||||||
$this->domainConfig = $domainConfig;
|
$this->domainConfig = $domainConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws InvalidArgumentException
|
|
||||||
*/
|
|
||||||
public function handle(Request $request): Response
|
public function handle(Request $request): Response
|
||||||
{
|
{
|
||||||
$shortCode = $request->getAttribute('shortCode');
|
$shortCode = $request->getAttribute('shortCode');
|
||||||
$domain = $request->getQueryParams()['domain'] ?? null;
|
$domain = $request->getQueryParams()['domain'] ?? null;
|
||||||
$transformer = new ShortUrlDataTransformer($this->domainConfig);
|
$transformer = new ShortUrlDataTransformer($this->domainConfig);
|
||||||
|
|
||||||
$url = $this->urlShortener->shortCodeToUrl($shortCode, $domain);
|
$url = $this->urlResolver->shortCodeToShortUrl($shortCode, $domain);
|
||||||
return new JsonResponse($transformer->transform($url));
|
return new JsonResponse($transformer->transform($url));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,7 @@ class EditShortUrlActionTest extends ApiTestCase
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @test
|
* @test
|
||||||
* @dataProvider provideDisablingMeta
|
* @dataProvider provideMeta
|
||||||
*/
|
*/
|
||||||
public function metadataCanBeReset(array $meta): void
|
public function metadataCanBeReset(array $meta): void
|
||||||
{
|
{
|
||||||
|
@ -30,11 +30,9 @@ class EditShortUrlActionTest extends ApiTestCase
|
||||||
'maxVisits' => null,
|
'maxVisits' => null,
|
||||||
];
|
];
|
||||||
|
|
||||||
// Setting meta that disables the URL should not let it be visited
|
|
||||||
$editWithProvidedMeta = $this->callApiWithKey(self::METHOD_PATCH, $url, [RequestOptions::JSON => $meta]);
|
$editWithProvidedMeta = $this->callApiWithKey(self::METHOD_PATCH, $url, [RequestOptions::JSON => $meta]);
|
||||||
$metaAfterEditing = $this->findShortUrlMetaByShortCode($shortCode);
|
$metaAfterEditing = $this->findShortUrlMetaByShortCode($shortCode);
|
||||||
|
|
||||||
// Resetting all meta should allow the URL to be visitable again
|
|
||||||
$editWithResetMeta = $this->callApiWithKey(self::METHOD_PATCH, $url, [
|
$editWithResetMeta = $this->callApiWithKey(self::METHOD_PATCH, $url, [
|
||||||
RequestOptions::JSON => $resetMeta,
|
RequestOptions::JSON => $resetMeta,
|
||||||
]);
|
]);
|
||||||
|
@ -46,7 +44,7 @@ class EditShortUrlActionTest extends ApiTestCase
|
||||||
self::assertArraySubset($meta, $metaAfterEditing);
|
self::assertArraySubset($meta, $metaAfterEditing);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function provideDisablingMeta(): iterable
|
public function provideMeta(): iterable
|
||||||
{
|
{
|
||||||
$now = Chronos::now();
|
$now = Chronos::now();
|
||||||
|
|
||||||
|
|
|
@ -4,10 +4,42 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace ShlinkioApiTest\Shlink\Rest\Action;
|
namespace ShlinkioApiTest\Shlink\Rest\Action;
|
||||||
|
|
||||||
|
use Cake\Chronos\Chronos;
|
||||||
|
use GuzzleHttp\RequestOptions;
|
||||||
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
|
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
|
||||||
|
|
||||||
|
use function sprintf;
|
||||||
|
|
||||||
class ResolveShortUrlActionTest extends ApiTestCase
|
class ResolveShortUrlActionTest extends ApiTestCase
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @dataProvider provideDisabledMeta
|
||||||
|
*/
|
||||||
|
public function shortUrlIsProperlyResolvedEvenWhenNotEnabled(array $disabledMeta): void
|
||||||
|
{
|
||||||
|
$shortCode = 'abc123';
|
||||||
|
$url = sprintf('/short-urls/%s', $shortCode);
|
||||||
|
$this->callShortUrl($shortCode);
|
||||||
|
|
||||||
|
$editResp = $this->callApiWithKey(self::METHOD_PATCH, $url, [RequestOptions::JSON => $disabledMeta]);
|
||||||
|
$visitResp = $this->callShortUrl($shortCode);
|
||||||
|
$fetchResp = $this->callApiWithKey(self::METHOD_GET, $url);
|
||||||
|
|
||||||
|
$this->assertEquals(self::STATUS_NO_CONTENT, $editResp->getStatusCode());
|
||||||
|
$this->assertEquals(self::STATUS_NOT_FOUND, $visitResp->getStatusCode());
|
||||||
|
$this->assertEquals(self::STATUS_OK, $fetchResp->getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function provideDisabledMeta(): iterable
|
||||||
|
{
|
||||||
|
$now = Chronos::now();
|
||||||
|
|
||||||
|
yield 'future validSince' => [['validSince' => $now->addMonth()->toAtomString()]];
|
||||||
|
yield 'past validUntil' => [['validUntil' => $now->subMonth()->toAtomString()]];
|
||||||
|
yield 'maxVisits reached' => [['maxVisits' => 1]];
|
||||||
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function tryingToResolveInvalidUrlReturnsNotFoundError(): void
|
public function tryingToResolveInvalidUrlReturnsNotFoundError(): void
|
||||||
{
|
{
|
||||||
|
|
|
@ -8,7 +8,7 @@ use Laminas\Diactoros\ServerRequest;
|
||||||
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\Service\UrlShortener;
|
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortUrlResolverInterface;
|
||||||
use Shlinkio\Shlink\Rest\Action\ShortUrl\ResolveShortUrlAction;
|
use Shlinkio\Shlink\Rest\Action\ShortUrl\ResolveShortUrlAction;
|
||||||
|
|
||||||
use function strpos;
|
use function strpos;
|
||||||
|
@ -16,19 +16,19 @@ use function strpos;
|
||||||
class ResolveShortUrlActionTest extends TestCase
|
class ResolveShortUrlActionTest extends TestCase
|
||||||
{
|
{
|
||||||
private ResolveShortUrlAction $action;
|
private ResolveShortUrlAction $action;
|
||||||
private ObjectProphecy $urlShortener;
|
private ObjectProphecy $urlResolver;
|
||||||
|
|
||||||
public function setUp(): void
|
public function setUp(): void
|
||||||
{
|
{
|
||||||
$this->urlShortener = $this->prophesize(UrlShortener::class);
|
$this->urlResolver = $this->prophesize(ShortUrlResolverInterface::class);
|
||||||
$this->action = new ResolveShortUrlAction($this->urlShortener->reveal(), []);
|
$this->action = new ResolveShortUrlAction($this->urlResolver->reveal(), []);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function correctShortCodeReturnsSuccess(): void
|
public function correctShortCodeReturnsSuccess(): void
|
||||||
{
|
{
|
||||||
$shortCode = 'abc123';
|
$shortCode = 'abc123';
|
||||||
$this->urlShortener->shortCodeToUrl($shortCode, null)->willReturn(
|
$this->urlResolver->shortCodeToShortUrl($shortCode, null)->willReturn(
|
||||||
new ShortUrl('http://domain.com/foo/bar'),
|
new ShortUrl('http://domain.com/foo/bar'),
|
||||||
)->shouldBeCalledOnce();
|
)->shouldBeCalledOnce();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue