Merge pull request #887 from acelaya-forks/feature/track-url-creator

Feature/track url creator
This commit is contained in:
Alejandro Celaya 2020-11-07 10:40:43 +01:00 committed by GitHub
commit 8577d6bd99
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
30 changed files with 398 additions and 233 deletions

View file

@ -16,7 +16,7 @@
"ext-json": "*",
"ext-pdo": "*",
"akrabat/ip-address-middleware": "^1.0",
"cakephp/chronos": "^1.2",
"cakephp/chronos": "^2.0",
"cocur/slugify": "^4.0",
"doctrine/cache": "^1.9",
"doctrine/dbal": "^2.10",
@ -49,7 +49,7 @@
"predis/predis": "^1.1",
"pugx/shortid-php": "^0.5",
"ramsey/uuid": "^3.9",
"shlinkio/shlink-common": "^3.2.0",
"shlinkio/shlink-common": "^3.3.0",
"shlinkio/shlink-config": "^1.0",
"shlinkio/shlink-event-dispatcher": "^1.4",
"shlinkio/shlink-importer": "^2.0.1",
@ -109,7 +109,7 @@
],
"cs": "phpcs",
"cs:fix": "phpcbf",
"stan": "phpstan analyse module/*/src/ module/*/config config docker/config --level=6",
"stan": "phpstan analyse module/*/src/ module/*/config config docker/config data/migrations --level=6",
"test": [
"@test:unit",
"@test:db",

View file

@ -5,7 +5,7 @@ declare(strict_types=1);
return [
'not_found_redirects' => [
'invalid_short_url' => null, // Formerly url_shortener.not_found_short_url.redirect_to
'invalid_short_url' => null,
'regular_404' => null,
'base_url' => null,
],

View file

@ -24,10 +24,10 @@ class Version20171021093246 extends AbstractMigration
return;
}
$shortUrls->addColumn('valid_since', Types::DATETIME, [
$shortUrls->addColumn('valid_since', Types::DATETIME_MUTABLE, [
'notnull' => false,
]);
$shortUrls->addColumn('valid_until', Types::DATETIME, [
$shortUrls->addColumn('valid_until', Types::DATETIME_MUTABLE, [
'notnull' => false,
]);
}

View file

@ -0,0 +1,86 @@
<?php
declare(strict_types=1);
namespace ShlinkMigrations;
use Cake\Chronos\Chronos;
use Doctrine\DBAL\Driver\Result;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Types\Types;
use Doctrine\Migrations\AbstractMigration;
final class Version20201102113208 extends AbstractMigration
{
private const API_KEY_COLUMN = 'author_api_key_id';
public function up(Schema $schema): void
{
$shortUrls = $schema->getTable('short_urls');
$this->skipIf($shortUrls->hasColumn(self::API_KEY_COLUMN));
$shortUrls->addColumn(self::API_KEY_COLUMN, Types::BIGINT, [
'unsigned' => true,
'notnull' => false,
]);
$shortUrls->addForeignKeyConstraint('api_keys', [self::API_KEY_COLUMN], ['id'], [
'onDelete' => 'SET NULL',
'onUpdate' => 'RESTRICT',
], 'FK_' . self::API_KEY_COLUMN);
}
public function postUp(Schema $schema): void
{
// If there's only one API key and it's active, link all existing URLs with it
$qb = $this->connection->createQueryBuilder();
$qb->select('id')
->from('api_keys')
->where($qb->expr()->eq('enabled', ':enabled'))
->andWhere($qb->expr()->or(
$qb->expr()->isNull('expiration_date'),
$qb->expr()->gt('expiration_date', ':expiration'),
))
->setParameters([
'enabled' => true,
'expiration' => Chronos::now()->toDateTimeString(),
]);
/** @var Result $result */
$result = $qb->execute();
$id = $this->resolveOneApiKeyId($result);
if ($id === null) {
return;
}
$qb = $this->connection->createQueryBuilder();
$qb->update('short_urls')
->set(self::API_KEY_COLUMN, ':apiKeyId')
->setParameter('apiKeyId', $id)
->execute();
}
private function resolveOneApiKeyId(Result $result): ?string
{
$results = [];
while ($row = $result->fetchAssociative()) {
// As soon as we have to iterate more than once, then we cannot resolve a single API key
if (! empty($results)) {
return null;
}
$results[] = $row['id'] ?? null;
}
return $results[0] ?? null;
}
public function down(Schema $schema): void
{
$shortUrls = $schema->getTable('short_urls');
$this->skipIf(! $shortUrls->hasColumn(self::API_KEY_COLUMN));
$shortUrls->removeForeignKey('FK_' . self::API_KEY_COLUMN);
$shortUrls->dropColumn(self::API_KEY_COLUMN);
}
}

View file

@ -142,7 +142,7 @@ class GenerateShortUrlCommand extends Command
$doValidateUrl = $this->doValidateUrl($input);
try {
$shortUrl = $this->urlShortener->urlToShortCode($longUrl, $tags, ShortUrlMeta::fromRawData([
$shortUrl = $this->urlShortener->shorten($longUrl, $tags, ShortUrlMeta::fromRawData([
ShortUrlMetaInputFilter::VALID_SINCE => $input->getOption('validSince'),
ShortUrlMetaInputFilter::VALID_UNTIL => $input->getOption('validUntil'),
ShortUrlMetaInputFilter::CUSTOM_SLUG => $customSlug,

View file

@ -44,7 +44,7 @@ class GenerateShortUrlCommandTest extends TestCase
public function properShortCodeIsCreatedIfLongUrlIsCorrect(): void
{
$shortUrl = new ShortUrl('');
$urlToShortCode = $this->urlShortener->urlToShortCode(Argument::cetera())->willReturn($shortUrl);
$urlToShortCode = $this->urlShortener->shorten(Argument::cetera())->willReturn($shortUrl);
$this->commandTester->execute([
'longUrl' => 'http://domain.com/foo/bar',
@ -61,7 +61,7 @@ class GenerateShortUrlCommandTest extends TestCase
public function exceptionWhileParsingLongUrlOutputsError(): void
{
$url = 'http://domain.com/invalid';
$this->urlShortener->urlToShortCode(Argument::cetera())->willThrow(InvalidUrlException::fromUrl($url))
$this->urlShortener->shorten(Argument::cetera())->willThrow(InvalidUrlException::fromUrl($url))
->shouldBeCalledOnce();
$this->commandTester->execute(['longUrl' => $url]);
@ -74,7 +74,7 @@ class GenerateShortUrlCommandTest extends TestCase
/** @test */
public function providingNonUniqueSlugOutputsError(): void
{
$urlToShortCode = $this->urlShortener->urlToShortCode(Argument::cetera())->willThrow(
$urlToShortCode = $this->urlShortener->shorten(Argument::cetera())->willThrow(
NonUniqueSlugException::fromSlug('my-slug'),
);
@ -90,7 +90,7 @@ class GenerateShortUrlCommandTest extends TestCase
public function properlyProcessesProvidedTags(): void
{
$shortUrl = new ShortUrl('');
$urlToShortCode = $this->urlShortener->urlToShortCode(
$urlToShortCode = $this->urlShortener->shorten(
Argument::type('string'),
Argument::that(function (array $tags) {
Assert::assertEquals(['foo', 'bar', 'baz', 'boo', 'zar'], $tags);
@ -117,7 +117,7 @@ class GenerateShortUrlCommandTest extends TestCase
public function urlValidationHasExpectedValueBasedOnProvidedTags(array $options, ?bool $expectedValidateUrl): void
{
$shortUrl = new ShortUrl('');
$urlToShortCode = $this->urlShortener->urlToShortCode(
$urlToShortCode = $this->urlShortener->shorten(
Argument::type('string'),
Argument::type('array'),
Argument::that(function (ShortUrlMeta $meta) use ($expectedValidateUrl) {

View file

@ -7,7 +7,6 @@ namespace Shlinkio\Shlink\Core;
use Laminas\ServiceManager\AbstractFactory\ConfigAbstractFactory;
use Mezzio\Template\TemplateRendererInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Shlinkio\Shlink\Core\Domain\Resolver;
use Shlinkio\Shlink\Core\ErrorHandler;
use Shlinkio\Shlink\Core\Options\NotFoundRedirectOptions;
use Shlinkio\Shlink\Importer\ImportedLinksProcessorInterface;
@ -42,7 +41,7 @@ return [
Action\PixelAction::class => ConfigAbstractFactory::class,
Action\QrCodeAction::class => ConfigAbstractFactory::class,
Resolver\PersistenceDomainResolver::class => ConfigAbstractFactory::class,
ShortUrl\Resolver\PersistenceShortUrlRelationResolver::class => ConfigAbstractFactory::class,
Mercure\MercureUpdatesGenerator::class => ConfigAbstractFactory::class,
@ -66,7 +65,7 @@ return [
Service\UrlShortener::class => [
Util\UrlValidator::class,
'em',
Resolver\PersistenceDomainResolver::class,
ShortUrl\Resolver\PersistenceShortUrlRelationResolver::class,
Service\ShortUrl\ShortCodeHelper::class,
],
Service\VisitsTracker::class => [
@ -109,13 +108,13 @@ return [
'Logger_Shlink',
],
Resolver\PersistenceDomainResolver::class => ['em'],
ShortUrl\Resolver\PersistenceShortUrlRelationResolver::class => ['em'],
Mercure\MercureUpdatesGenerator::class => ['config.url_shortener.domain'],
Importer\ImportedLinksProcessor::class => [
'em',
Resolver\PersistenceDomainResolver::class,
ShortUrl\Resolver\PersistenceShortUrlRelationResolver::class,
Service\ShortUrl\ShortCodeHelper::class,
Util\DoctrineBatchHelper::class,
],

View file

@ -8,6 +8,7 @@ use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder;
use Doctrine\ORM\Mapping\ClassMetadata;
use Shlinkio\Shlink\Common\Doctrine\Type\ChronosDateTimeType;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
return static function (ClassMetadata $metadata, array $emConfig): void {
$builder = new ClassMetadataBuilder($metadata);
@ -78,5 +79,9 @@ return static function (ClassMetadata $metadata, array $emConfig): void {
->cascadePersist()
->build();
$builder->createManyToOne('authorApiKey', ApiKey::class)
->addJoinColumn('author_api_key_id', 'id', true, false, 'SET NULL')
->build();
$builder->addUniqueConstraint(['short_code', 'domain_id'], 'unique_short_code_plus_domain');
};

View file

@ -1,12 +0,0 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Domain\Resolver;
use Shlinkio\Shlink\Core\Entity\Domain;
interface DomainResolverInterface
{
public function resolveDomain(?string $domain): ?Domain;
}

View file

@ -1,15 +0,0 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Domain\Resolver;
use Shlinkio\Shlink\Core\Entity\Domain;
class SimpleDomainResolver implements DomainResolverInterface
{
public function resolveDomain(?string $domain): ?Domain
{
return $domain !== null ? new Domain($domain) : null;
}
}

View file

@ -9,13 +9,14 @@ use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Laminas\Diactoros\Uri;
use Shlinkio\Shlink\Common\Entity\AbstractEntity;
use Shlinkio\Shlink\Core\Domain\Resolver\DomainResolverInterface;
use Shlinkio\Shlink\Core\Domain\Resolver\SimpleDomainResolver;
use Shlinkio\Shlink\Core\Exception\ShortCodeCannotBeRegeneratedException;
use Shlinkio\Shlink\Core\Model\ShortUrlEdit;
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
use Shlinkio\Shlink\Core\ShortUrl\Resolver\ShortUrlRelationResolverInterface;
use Shlinkio\Shlink\Core\ShortUrl\Resolver\SimpleShortUrlRelationResolver;
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter;
use Shlinkio\Shlink\Importer\Model\ImportedShlinkUrl;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
use function count;
use function Shlinkio\Shlink\Core\generateRandomShortCode;
@ -37,13 +38,15 @@ class ShortUrl extends AbstractEntity
private int $shortCodeLength;
private ?string $importSource = null;
private ?string $importOriginalShortCode = null;
private ?ApiKey $authorApiKey = null;
public function __construct(
string $longUrl,
?ShortUrlMeta $meta = null,
?DomainResolverInterface $domainResolver = null
?ShortUrlRelationResolverInterface $relationResolver = null
) {
$meta = $meta ?? ShortUrlMeta::createEmpty();
$relationResolver = $relationResolver ?? new SimpleShortUrlRelationResolver();
$this->longUrl = $longUrl;
$this->dateCreated = Chronos::now();
@ -55,13 +58,14 @@ class ShortUrl extends AbstractEntity
$this->customSlugWasProvided = $meta->hasCustomSlug();
$this->shortCodeLength = $meta->getShortCodeLength();
$this->shortCode = $meta->getCustomSlug() ?? generateRandomShortCode($this->shortCodeLength);
$this->domain = ($domainResolver ?? new SimpleDomainResolver())->resolveDomain($meta->getDomain());
$this->domain = $relationResolver->resolveDomain($meta->getDomain());
$this->authorApiKey = $relationResolver->resolveApiKey($meta->getApiKey());
}
public static function fromImport(
ImportedShlinkUrl $url,
bool $importShortCode,
?DomainResolverInterface $domainResolver = null
?ShortUrlRelationResolverInterface $relationResolver = null
): self {
$meta = [
ShortUrlMetaInputFilter::DOMAIN => $url->domain(),
@ -71,7 +75,7 @@ class ShortUrl extends AbstractEntity
$meta[ShortUrlMetaInputFilter::CUSTOM_SLUG] = $url->shortCode();
}
$instance = new self($url->longUrl(), ShortUrlMeta::fromRawData($meta), $domainResolver);
$instance = new self($url->longUrl(), ShortUrlMeta::fromRawData($meta), $relationResolver);
$instance->importSource = $url->source();
$instance->importOriginalShortCode = $url->shortCode();
$instance->dateCreated = Chronos::instance($url->createdAt());

View file

@ -5,10 +5,10 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Importer;
use Doctrine\ORM\EntityManagerInterface;
use Shlinkio\Shlink\Core\Domain\Resolver\DomainResolverInterface;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Repository\ShortUrlRepositoryInterface;
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortCodeHelperInterface;
use Shlinkio\Shlink\Core\ShortUrl\Resolver\ShortUrlRelationResolverInterface;
use Shlinkio\Shlink\Core\Util\DoctrineBatchHelperInterface;
use Shlinkio\Shlink\Core\Util\TagManagerTrait;
use Shlinkio\Shlink\Importer\ImportedLinksProcessorInterface;
@ -22,18 +22,18 @@ class ImportedLinksProcessor implements ImportedLinksProcessorInterface
use TagManagerTrait;
private EntityManagerInterface $em;
private DomainResolverInterface $domainResolver;
private ShortUrlRelationResolverInterface $relationResolver;
private ShortCodeHelperInterface $shortCodeHelper;
private DoctrineBatchHelperInterface $batchHelper;
public function __construct(
EntityManagerInterface $em,
DomainResolverInterface $domainResolver,
ShortUrlRelationResolverInterface $relationResolver,
ShortCodeHelperInterface $shortCodeHelper,
DoctrineBatchHelperInterface $batchHelper
) {
$this->em = $em;
$this->domainResolver = $domainResolver;
$this->relationResolver = $relationResolver;
$this->shortCodeHelper = $shortCodeHelper;
$this->batchHelper = $batchHelper;
}
@ -58,7 +58,7 @@ class ImportedLinksProcessor implements ImportedLinksProcessorInterface
continue;
}
$shortUrl = ShortUrl::fromImport($url, $importShortCodes, $this->domainResolver);
$shortUrl = ShortUrl::fromImport($url, $importShortCodes, $this->relationResolver);
$shortUrl->setTags($this->tagNamesToEntities($this->em, $url->tags()));
if (! $this->handleShortCodeUniqueness($url, $shortUrl, $io, $importShortCodes)) {

View file

@ -24,6 +24,7 @@ final class ShortUrlMeta
private ?string $domain = null;
private int $shortCodeLength = 5;
private ?bool $validateUrl = null;
private ?string $apiKey = null;
// Enforce named constructors
private function __construct()
@ -66,6 +67,7 @@ final class ShortUrlMeta
$inputFilter,
ShortUrlMetaInputFilter::SHORT_CODE_LENGTH,
) ?? DEFAULT_SHORT_CODES_LENGTH;
$this->apiKey = $inputFilter->getValue(ShortUrlMetaInputFilter::API_KEY);
}
public function getValidSince(): ?Chronos
@ -132,4 +134,9 @@ final class ShortUrlMeta
{
return $this->validateUrl;
}
public function getApiKey(): ?string
{
return $this->apiKey;
}
}

View file

@ -5,13 +5,13 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Service;
use Doctrine\ORM\EntityManagerInterface;
use Shlinkio\Shlink\Core\Domain\Resolver\DomainResolverInterface;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
use Shlinkio\Shlink\Core\Repository\ShortUrlRepositoryInterface;
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortCodeHelperInterface;
use Shlinkio\Shlink\Core\ShortUrl\Resolver\ShortUrlRelationResolverInterface;
use Shlinkio\Shlink\Core\Util\TagManagerTrait;
use Shlinkio\Shlink\Core\Util\UrlValidatorInterface;
use Throwable;
@ -22,18 +22,18 @@ class UrlShortener implements UrlShortenerInterface
private EntityManagerInterface $em;
private UrlValidatorInterface $urlValidator;
private DomainResolverInterface $domainResolver;
private ShortUrlRelationResolverInterface $relationResolver;
private ShortCodeHelperInterface $shortCodeHelper;
public function __construct(
UrlValidatorInterface $urlValidator,
EntityManagerInterface $em,
DomainResolverInterface $domainResolver,
ShortUrlRelationResolverInterface $relationResolver,
ShortCodeHelperInterface $shortCodeHelper
) {
$this->urlValidator = $urlValidator;
$this->em = $em;
$this->domainResolver = $domainResolver;
$this->relationResolver = $relationResolver;
$this->shortCodeHelper = $shortCodeHelper;
}
@ -43,7 +43,7 @@ class UrlShortener implements UrlShortenerInterface
* @throws InvalidUrlException
* @throws Throwable
*/
public function urlToShortCode(string $url, array $tags, ShortUrlMeta $meta): ShortUrl
public function shorten(string $url, array $tags, ShortUrlMeta $meta): ShortUrl
{
// First, check if a short URL exists for all provided params
$existingShortUrl = $this->findExistingShortUrlIfExists($url, $tags, $meta);
@ -52,25 +52,16 @@ class UrlShortener implements UrlShortenerInterface
}
$this->urlValidator->validateUrl($url, $meta->doValidateUrl());
$this->em->beginTransaction();
$shortUrl = new ShortUrl($url, $meta, $this->domainResolver);
$shortUrl->setTags($this->tagNamesToEntities($this->em, $tags));
try {
return $this->em->transactional(function () use ($url, $tags, $meta) {
$shortUrl = new ShortUrl($url, $meta, $this->relationResolver);
$shortUrl->setTags($this->tagNamesToEntities($this->em, $tags));
$this->verifyShortCodeUniqueness($meta, $shortUrl);
$this->em->persist($shortUrl);
$this->em->flush();
$this->em->commit();
} catch (Throwable $e) {
if ($this->em->getConnection()->isTransactionActive()) {
$this->em->rollback();
$this->em->close();
}
throw $e;
}
return $shortUrl;
return $shortUrl;
});
}
private function findExistingShortUrlIfExists(string $url, array $tags, ShortUrlMeta $meta): ?ShortUrl

View file

@ -16,5 +16,5 @@ interface UrlShortenerInterface
* @throws NonUniqueSlugException
* @throws InvalidUrlException
*/
public function urlToShortCode(string $url, array $tags, ShortUrlMeta $meta): ShortUrl;
public function shorten(string $url, array $tags, ShortUrlMeta $meta): ShortUrl;
}

View file

@ -2,12 +2,13 @@
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Domain\Resolver;
namespace Shlinkio\Shlink\Core\ShortUrl\Resolver;
use Doctrine\ORM\EntityManagerInterface;
use Shlinkio\Shlink\Core\Entity\Domain;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
class PersistenceDomainResolver implements DomainResolverInterface
class PersistenceShortUrlRelationResolver implements ShortUrlRelationResolverInterface
{
private EntityManagerInterface $em;
@ -26,4 +27,15 @@ class PersistenceDomainResolver implements DomainResolverInterface
$existingDomain = $this->em->getRepository(Domain::class)->findOneBy(['authority' => $domain]);
return $existingDomain ?? new Domain($domain);
}
public function resolveApiKey(?string $key): ?ApiKey
{
if ($key === null) {
return null;
}
/** @var ApiKey|null $existingApiKey */
$existingApiKey = $this->em->getRepository(ApiKey::class)->findOneBy(['key' => $key]);
return $existingApiKey;
}
}

View file

@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\ShortUrl\Resolver;
use Shlinkio\Shlink\Core\Entity\Domain;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
interface ShortUrlRelationResolverInterface
{
public function resolveDomain(?string $domain): ?Domain;
public function resolveApiKey(?string $key): ?ApiKey;
}

View file

@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\ShortUrl\Resolver;
use Shlinkio\Shlink\Core\Entity\Domain;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
class SimpleShortUrlRelationResolver implements ShortUrlRelationResolverInterface
{
public function resolveDomain(?string $domain): ?Domain
{
return $domain !== null ? new Domain($domain) : null;
}
public function resolveApiKey(?string $key): ?ApiKey
{
return null;
}
}

View file

@ -28,6 +28,7 @@ class ShortUrlMetaInputFilter extends InputFilter
public const SHORT_CODE_LENGTH = 'shortCodeLength';
public const LONG_URL = 'longUrl';
public const VALIDATE_URL = 'validateUrl';
public const API_KEY = 'apiKey';
public function __construct(array $data)
{
@ -70,6 +71,8 @@ class ShortUrlMetaInputFilter extends InputFilter
$domain = $this->createInput(self::DOMAIN, false);
$domain->getValidatorChain()->attach(new Validation\HostAndPortValidator());
$this->add($domain);
$this->add($this->createInput(self::API_KEY, false));
}
private function createPositiveNumberInput(string $name, int $min = 1): Input

View file

@ -1,65 +0,0 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\Domain\Resolver;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ObjectRepository;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Domain\Resolver\PersistenceDomainResolver;
use Shlinkio\Shlink\Core\Entity\Domain;
class PersistenceDomainResolverTest extends TestCase
{
use ProphecyTrait;
private PersistenceDomainResolver $domainResolver;
private ObjectProphecy $em;
public function setUp(): void
{
$this->em = $this->prophesize(EntityManagerInterface::class);
$this->domainResolver = new PersistenceDomainResolver($this->em->reveal());
}
/** @test */
public function returnsEmptyWhenNoDomainIsProvided(): void
{
$getRepository = $this->em->getRepository(Domain::class);
self::assertNull($this->domainResolver->resolveDomain(null));
$getRepository->shouldNotHaveBeenCalled();
}
/**
* @test
* @dataProvider provideFoundDomains
*/
public function findsOrCreatesDomainWhenValueIsProvided(?Domain $foundDomain, string $authority): void
{
$repo = $this->prophesize(ObjectRepository::class);
$findDomain = $repo->findOneBy(['authority' => $authority])->willReturn($foundDomain);
$getRepository = $this->em->getRepository(Domain::class)->willReturn($repo->reveal());
$result = $this->domainResolver->resolveDomain($authority);
if ($foundDomain !== null) {
self::assertSame($result, $foundDomain);
}
self::assertInstanceOf(Domain::class, $result);
self::assertEquals($authority, $result->getAuthority());
$findDomain->shouldHaveBeenCalledOnce();
$getRepository->shouldHaveBeenCalledOnce();
}
public function provideFoundDomains(): iterable
{
$authority = 'doma.in';
yield 'without found domain' => [null, $authority];
yield 'with found domain' => [new Domain($authority), $authority];
}
}

View file

@ -1,41 +0,0 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\Domain\Resolver;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Core\Domain\Resolver\SimpleDomainResolver;
use Shlinkio\Shlink\Core\Entity\Domain;
class SimpleDomainResolverTest extends TestCase
{
private SimpleDomainResolver $domainResolver;
public function setUp(): void
{
$this->domainResolver = new SimpleDomainResolver();
}
/**
* @test
* @dataProvider provideDomains
*/
public function resolvesExpectedDomain(?string $domain): void
{
$result = $this->domainResolver->resolveDomain($domain);
if ($domain === null) {
self::assertNull($result);
} else {
self::assertInstanceOf(Domain::class, $result);
self::assertEquals($domain, $result->getAuthority());
}
}
public function provideDomains(): iterable
{
yield 'with empty domain' => [null];
yield 'with non-empty domain' => ['domain.com'];
}
}

View file

@ -10,11 +10,11 @@ use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Domain\Resolver\SimpleDomainResolver;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Importer\ImportedLinksProcessor;
use Shlinkio\Shlink\Core\Repository\ShortUrlRepositoryInterface;
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortCodeHelperInterface;
use Shlinkio\Shlink\Core\ShortUrl\Resolver\SimpleShortUrlRelationResolver;
use Shlinkio\Shlink\Core\Util\DoctrineBatchHelperInterface;
use Shlinkio\Shlink\Importer\Model\ImportedShlinkUrl;
use Symfony\Component\Console\Style\StyleInterface;
@ -46,7 +46,7 @@ class ImportedLinksProcessorTest extends TestCase
$this->processor = new ImportedLinksProcessor(
$this->em->reveal(),
new SimpleDomainResolver(),
new SimpleShortUrlRelationResolver(),
$this->shortCodeHelper->reveal(),
$batchHelper->reveal(),
);

View file

@ -6,14 +6,11 @@ namespace ShlinkioTest\Shlink\Core\Service;
use Cake\Chronos\Chronos;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\ORMException;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Domain\Resolver\SimpleDomainResolver;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Entity\Tag;
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
@ -21,6 +18,7 @@ use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
use Shlinkio\Shlink\Core\Repository\ShortUrlRepository;
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortCodeHelperInterface;
use Shlinkio\Shlink\Core\Service\UrlShortener;
use Shlinkio\Shlink\Core\ShortUrl\Resolver\SimpleShortUrlRelationResolver;
use Shlinkio\Shlink\Core\Util\UrlValidatorInterface;
class UrlShortenerTest extends TestCase
@ -41,17 +39,17 @@ class UrlShortenerTest extends TestCase
);
$this->em = $this->prophesize(EntityManagerInterface::class);
$conn = $this->prophesize(Connection::class);
$conn->isTransactionActive()->willReturn(false);
$this->em->getConnection()->willReturn($conn->reveal());
$this->em->flush()->willReturn(null);
$this->em->commit()->willReturn(null);
$this->em->beginTransaction()->willReturn(null);
$this->em->persist(Argument::any())->will(function ($arguments): void {
/** @var ShortUrl $shortUrl */
[$shortUrl] = $arguments;
$shortUrl->setId('10');
});
$this->em->transactional(Argument::type('callable'))->will(function (array $args) {
/** @var callable $callback */
[$callback] = $args;
return $callback();
});
$repo = $this->prophesize(ShortUrlRepository::class);
$repo->shortCodeIsInUse(Argument::cetera())->willReturn(false);
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
@ -62,7 +60,7 @@ class UrlShortenerTest extends TestCase
$this->urlShortener = new UrlShortener(
$this->urlValidator->reveal(),
$this->em->reveal(),
new SimpleDomainResolver(),
new SimpleShortUrlRelationResolver(),
$this->shortCodeHelper->reveal(),
);
}
@ -70,7 +68,7 @@ class UrlShortenerTest extends TestCase
/** @test */
public function urlIsProperlyShortened(): void
{
$shortUrl = $this->urlShortener->urlToShortCode(
$shortUrl = $this->urlShortener->shorten(
'http://foobar.com/12345/hello?foo=bar',
[],
ShortUrlMeta::createEmpty(),
@ -87,32 +85,13 @@ class UrlShortenerTest extends TestCase
$ensureUniqueness->shouldBeCalledOnce();
$this->expectException(NonUniqueSlugException::class);
$this->urlShortener->urlToShortCode(
$this->urlShortener->shorten(
'http://foobar.com/12345/hello?foo=bar',
[],
ShortUrlMeta::fromRawData(['customSlug' => 'custom-slug']),
);
}
/** @test */
public function transactionIsRolledBackAndExceptionRethrownWhenExceptionIsThrown(): void
{
$conn = $this->prophesize(Connection::class);
$conn->isTransactionActive()->willReturn(true);
$this->em->getConnection()->willReturn($conn->reveal());
$this->em->rollback()->shouldBeCalledOnce();
$this->em->close()->shouldBeCalledOnce();
$this->em->flush()->willThrow(new ORMException());
$this->expectException(ORMException::class);
$this->urlShortener->urlToShortCode(
'http://foobar.com/12345/hello?foo=bar',
[],
ShortUrlMeta::createEmpty(),
);
}
/**
* @test
* @dataProvider provideExistingShortUrls
@ -127,7 +106,7 @@ class UrlShortenerTest extends TestCase
$findExisting = $repo->findOneMatching(Argument::cetera())->willReturn($expected);
$getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
$result = $this->urlShortener->urlToShortCode($url, $tags, $meta);
$result = $this->urlShortener->shorten($url, $tags, $meta);
$findExisting->shouldHaveBeenCalledOnce();
$getRepo->shouldHaveBeenCalledOnce();

View file

@ -0,0 +1,100 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\ShortUrl\Resolver;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ObjectRepository;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Entity\Domain;
use Shlinkio\Shlink\Core\ShortUrl\Resolver\PersistenceShortUrlRelationResolver;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
class PersistenceShortUrlRelationResolverTest extends TestCase
{
use ProphecyTrait;
private PersistenceShortUrlRelationResolver $resolver;
private ObjectProphecy $em;
public function setUp(): void
{
$this->em = $this->prophesize(EntityManagerInterface::class);
$this->resolver = new PersistenceShortUrlRelationResolver($this->em->reveal());
}
/** @test */
public function returnsEmptyWhenNoDomainIsProvided(): void
{
$getRepository = $this->em->getRepository(Domain::class);
self::assertNull($this->resolver->resolveDomain(null));
$getRepository->shouldNotHaveBeenCalled();
}
/**
* @test
* @dataProvider provideFoundDomains
*/
public function findsOrCreatesDomainWhenValueIsProvided(?Domain $foundDomain, string $authority): void
{
$repo = $this->prophesize(ObjectRepository::class);
$findDomain = $repo->findOneBy(['authority' => $authority])->willReturn($foundDomain);
$getRepository = $this->em->getRepository(Domain::class)->willReturn($repo->reveal());
$result = $this->resolver->resolveDomain($authority);
if ($foundDomain !== null) {
self::assertSame($result, $foundDomain);
}
self::assertInstanceOf(Domain::class, $result);
self::assertEquals($authority, $result->getAuthority());
$findDomain->shouldHaveBeenCalledOnce();
$getRepository->shouldHaveBeenCalledOnce();
}
public function provideFoundDomains(): iterable
{
$authority = 'doma.in';
yield 'not found domain' => [null, $authority];
yield 'found domain' => [new Domain($authority), $authority];
}
/** @test */
public function returnsEmptyWhenNoApiKeyIsProvided(): void
{
$getRepository = $this->em->getRepository(ApiKey::class);
self::assertNull($this->resolver->resolveApiKey(null));
$getRepository->shouldNotHaveBeenCalled();
}
/**
* @test
* @dataProvider provideFoundApiKeys
*/
public function triesToFindApiKeyWhenValueIsProvided(?ApiKey $foundApiKey, string $key): void
{
$repo = $this->prophesize(ObjectRepository::class);
$find = $repo->findOneBy(['key' => $key])->willReturn($foundApiKey);
$getRepository = $this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
$result = $this->resolver->resolveApiKey($key);
self::assertSame($result, $foundApiKey);
$find->shouldHaveBeenCalledOnce();
$getRepository->shouldHaveBeenCalledOnce();
}
public function provideFoundApiKeys(): iterable
{
$key = 'abc123';
yield 'not found api key' => [null, $key];
yield 'found api key' => [new ApiKey(), $key];
}
}

View file

@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\ShortUrl\Resolver;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Core\Entity\Domain;
use Shlinkio\Shlink\Core\ShortUrl\Resolver\SimpleShortUrlRelationResolver;
class SimpleShortUrlRelationResolverTest extends TestCase
{
private SimpleShortUrlRelationResolver $resolver;
public function setUp(): void
{
$this->resolver = new SimpleShortUrlRelationResolver();
}
/**
* @test
* @dataProvider provideDomains
*/
public function resolvesExpectedDomain(?string $domain): void
{
$result = $this->resolver->resolveDomain($domain);
if ($domain === null) {
self::assertNull($result);
} else {
self::assertInstanceOf(Domain::class, $result);
self::assertEquals($domain, $result->getAuthority());
}
}
public function provideDomains(): iterable
{
yield 'empty domain' => [null];
yield 'non-empty domain' => ['domain.com'];
}
/**
* @test
* @dataProvider provideKeys
*/
public function alwaysReturnsNullForApiKeys(?string $key): void
{
self::assertNull($this->resolver->resolveApiKey($key));
}
public function provideKeys(): iterable
{
yield 'empty api key' => [null];
yield 'non-empty api key' => ['abc123'];
}
}

View file

@ -31,7 +31,7 @@ abstract class AbstractCreateShortUrlAction extends AbstractRestAction
$tags = $shortUrlData->getTags();
$shortUrlMeta = $shortUrlData->getMeta();
$shortUrl = $this->urlShortener->urlToShortCode($longUrl, $tags, $shortUrlMeta);
$shortUrl = $this->urlShortener->shorten($longUrl, $tags, $shortUrlMeta);
$transformer = new ShortUrlDataTransformer($this->domainConfig);
return new JsonResponse($transformer->transform($shortUrl));

View file

@ -8,6 +8,8 @@ use Psr\Http\Message\ServerRequestInterface as Request;
use Shlinkio\Shlink\Core\Exception\ValidationException;
use Shlinkio\Shlink\Core\Model\CreateShortUrlData;
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter;
use Shlinkio\Shlink\Rest\Authentication\Plugin\ApiKeyHeaderPlugin;
class CreateShortUrlAction extends AbstractCreateShortUrlAction
{
@ -19,14 +21,16 @@ class CreateShortUrlAction extends AbstractCreateShortUrlAction
*/
protected function buildShortUrlData(Request $request): CreateShortUrlData
{
$postData = (array) $request->getParsedBody();
if (! isset($postData['longUrl'])) {
$payload = (array) $request->getParsedBody();
if (! isset($payload['longUrl'])) {
throw ValidationException::fromArray([
'longUrl' => 'A URL was not provided',
]);
}
$meta = ShortUrlMeta::fromRawData($postData);
return new CreateShortUrlData($postData['longUrl'], (array) ($postData['tags'] ?? []), $meta);
$payload[ShortUrlMetaInputFilter::API_KEY] = $request->getHeaderLine(ApiKeyHeaderPlugin::HEADER_NAME);
$meta = ShortUrlMeta::fromRawData($payload);
return new CreateShortUrlData($payload['longUrl'], (array) ($payload['tags'] ?? []), $meta);
}
}

View file

@ -7,7 +7,9 @@ namespace Shlinkio\Shlink\Rest\Action\ShortUrl;
use Psr\Http\Message\ServerRequestInterface as Request;
use Shlinkio\Shlink\Core\Exception\ValidationException;
use Shlinkio\Shlink\Core\Model\CreateShortUrlData;
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter;
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
class SingleStepCreateShortUrlAction extends AbstractCreateShortUrlAction
@ -32,19 +34,23 @@ class SingleStepCreateShortUrlAction extends AbstractCreateShortUrlAction
protected function buildShortUrlData(Request $request): CreateShortUrlData
{
$query = $request->getQueryParams();
$apiKey = $query['apiKey'] ?? '';
$longUrl = $query['longUrl'] ?? null;
if (! $this->apiKeyService->check($query['apiKey'] ?? '')) {
if (! $this->apiKeyService->check($apiKey)) {
throw ValidationException::fromArray([
'apiKey' => 'No API key was provided or it is not valid',
]);
}
if (! isset($query['longUrl'])) {
if ($longUrl === null) {
throw ValidationException::fromArray([
'longUrl' => 'A URL was not provided',
]);
}
return new CreateShortUrlData($query['longUrl']);
return new CreateShortUrlData($longUrl, [], ShortUrlMeta::fromRawData([
ShortUrlMetaInputFilter::API_KEY => $apiKey,
]));
}
}

View file

@ -48,16 +48,20 @@ class CreateShortUrlActionTest extends TestCase
* @test
* @dataProvider provideRequestBodies
*/
public function properShortcodeConversionReturnsData(array $body, ShortUrlMeta $expectedMeta): void
public function properShortcodeConversionReturnsData(array $body, ShortUrlMeta $expectedMeta, ?string $apiKey): void
{
$shortUrl = new ShortUrl('');
$shorten = $this->urlShortener->urlToShortCode(
$shorten = $this->urlShortener->shorten(
Argument::type('string'),
Argument::type('array'),
$expectedMeta,
)->willReturn($shortUrl);
$request = ServerRequestFactory::fromGlobals()->withParsedBody($body);
if ($apiKey !== null) {
$request = $request->withHeader('X-Api-Key', $apiKey);
}
$response = $this->action->handle($request);
self::assertEquals(200, $response->getStatusCode());
@ -77,8 +81,14 @@ class CreateShortUrlActionTest extends TestCase
'domain' => 'my-domain.com',
];
yield [['longUrl' => 'http://www.domain.com/foo/bar'], ShortUrlMeta::createEmpty()];
yield [$fullMeta, ShortUrlMeta::fromRawData($fullMeta)];
yield 'no data' => [['longUrl' => 'http://www.domain.com/foo/bar'], ShortUrlMeta::createEmpty(), null];
yield 'all data' => [$fullMeta, ShortUrlMeta::fromRawData($fullMeta), null];
yield 'all data and API key' => (static function (array $meta): array {
$apiKey = 'abc123';
$meta['apiKey'] = $apiKey;
return [$meta, ShortUrlMeta::fromRawData($meta), $apiKey];
})($fullMeta);
}
/**
@ -88,7 +98,7 @@ class CreateShortUrlActionTest extends TestCase
public function anInvalidDomainReturnsError(string $domain): void
{
$shortUrl = new ShortUrl('');
$urlToShortCode = $this->urlShortener->urlToShortCode(Argument::cetera())->willReturn($shortUrl);
$urlToShortCode = $this->urlShortener->shorten(Argument::cetera())->willReturn($shortUrl);
$request = (new ServerRequest())->withParsedBody([
'longUrl' => 'http://www.domain.com/foo/bar',

View file

@ -72,13 +72,13 @@ class SingleStepCreateShortUrlActionTest extends TestCase
'longUrl' => 'http://foobar.com',
]);
$findApiKey = $this->apiKeyService->check('abc123')->willReturn(true);
$generateShortCode = $this->urlShortener->urlToShortCode(
$generateShortCode = $this->urlShortener->shorten(
Argument::that(function (string $argument): string {
Assert::assertEquals('http://foobar.com', $argument);
return $argument;
}),
[],
ShortUrlMeta::createEmpty(),
ShortUrlMeta::fromRawData(['apiKey' => 'abc123']),
)->willReturn(new ShortUrl(''));
$resp = $this->action->handle($request);