Added hability to override URL validation from the CLI

This commit is contained in:
Alejandro Celaya 2020-09-24 21:54:03 +02:00
parent cdd87f5962
commit 405369824b
2 changed files with 60 additions and 0 deletions

View file

@ -22,6 +22,7 @@ use function Functional\curry;
use function Functional\flatten; use function Functional\flatten;
use function Functional\unique; use function Functional\unique;
use function sprintf; use function sprintf;
use function strpos;
class GenerateShortUrlCommand extends Command class GenerateShortUrlCommand extends Command
{ {
@ -94,6 +95,18 @@ class GenerateShortUrlCommand extends Command
'l', 'l',
InputOption::VALUE_REQUIRED, InputOption::VALUE_REQUIRED,
'The length for generated short code (it will be ignored if --customSlug was provided).', 'The length for generated short code (it will be ignored if --customSlug was provided).',
)
->addOption(
'validate-url',
null,
InputOption::VALUE_NONE,
'Forces the long URL to be validated, regardless what is globally configured.',
)
->addOption(
'no-validate-url',
null,
InputOption::VALUE_NONE,
'Forces the long URL to not be validated, regardless what is globally configured.',
); );
} }
@ -125,6 +138,7 @@ class GenerateShortUrlCommand extends Command
$customSlug = $input->getOption('customSlug'); $customSlug = $input->getOption('customSlug');
$maxVisits = $input->getOption('maxVisits'); $maxVisits = $input->getOption('maxVisits');
$shortCodeLength = $input->getOption('shortCodeLength') ?? $this->defaultShortCodeLength; $shortCodeLength = $input->getOption('shortCodeLength') ?? $this->defaultShortCodeLength;
$doValidateUrl = $this->doValidateUrl($input);
try { try {
$shortUrl = $this->urlShortener->urlToShortCode($longUrl, $tags, ShortUrlMeta::fromRawData([ $shortUrl = $this->urlShortener->urlToShortCode($longUrl, $tags, ShortUrlMeta::fromRawData([
@ -135,6 +149,7 @@ class GenerateShortUrlCommand extends Command
ShortUrlMetaInputFilter::FIND_IF_EXISTS => $input->getOption('findIfExists'), ShortUrlMetaInputFilter::FIND_IF_EXISTS => $input->getOption('findIfExists'),
ShortUrlMetaInputFilter::DOMAIN => $input->getOption('domain'), ShortUrlMetaInputFilter::DOMAIN => $input->getOption('domain'),
ShortUrlMetaInputFilter::SHORT_CODE_LENGTH => $shortCodeLength, ShortUrlMetaInputFilter::SHORT_CODE_LENGTH => $shortCodeLength,
ShortUrlMetaInputFilter::VALIDATE_URL => $doValidateUrl,
])); ]));
$io->writeln([ $io->writeln([
@ -147,4 +162,18 @@ class GenerateShortUrlCommand extends Command
return ExitCodes::EXIT_FAILURE; return ExitCodes::EXIT_FAILURE;
} }
} }
private function doValidateUrl(InputInterface $input): ?bool
{
$rawInput = (string) $input;
if (strpos($rawInput, '--no-validate-url') !== false) {
return false;
}
if (strpos($rawInput, '--validate-url') !== false) {
return true;
}
return null;
}
} }

View file

@ -13,6 +13,7 @@ use Shlinkio\Shlink\CLI\Util\ExitCodes;
use Shlinkio\Shlink\Core\Entity\ShortUrl; 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\Model\ShortUrlMeta;
use Shlinkio\Shlink\Core\Service\UrlShortener; use Shlinkio\Shlink\Core\Service\UrlShortener;
use Symfony\Component\Console\Application; use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester; use Symfony\Component\Console\Tester\CommandTester;
@ -105,4 +106,34 @@ class GenerateShortUrlCommandTest extends TestCase
$this->assertStringContainsString($shortUrl->toString(self::DOMAIN_CONFIG), $output); $this->assertStringContainsString($shortUrl->toString(self::DOMAIN_CONFIG), $output);
$urlToShortCode->shouldHaveBeenCalledOnce(); $urlToShortCode->shouldHaveBeenCalledOnce();
} }
/**
* @test
* @dataProvider provideFlags
*/
public function urlValidationHasExpectedValueBasedOnProvidedTags(array $options, ?bool $expectedValidateUrl): void
{
$shortUrl = new ShortUrl('');
$urlToShortCode = $this->urlShortener->urlToShortCode(
Argument::type('string'),
Argument::type('array'),
Argument::that(function (ShortUrlMeta $meta) use ($expectedValidateUrl) {
Assert::assertEquals($expectedValidateUrl, $meta->doValidateUrl());
return $meta;
}),
)->willReturn($shortUrl);
$options['longUrl'] = 'http://domain.com/foo/bar';
$this->commandTester->execute($options);
$urlToShortCode->shouldHaveBeenCalledOnce();
}
public function provideFlags(): iterable
{
yield 'no flags' => [[], null];
yield 'no-validate-url only' => [['--no-validate-url' => true], false];
yield 'validate-url' => [['--validate-url' => true], true];
yield 'both flags' => [['--validate-url' => true, '--no-validate-url' => true], false];
}
} }