mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Ensured default domain is stripped when creating short URLs from CLI
This commit is contained in:
parent
f8a48c16f0
commit
cbd4b4849f
@ -79,6 +79,7 @@ return [
|
|||||||
Service\UrlShortener::class,
|
Service\UrlShortener::class,
|
||||||
ShortUrlStringifier::class,
|
ShortUrlStringifier::class,
|
||||||
'config.url_shortener.default_short_codes_length',
|
'config.url_shortener.default_short_codes_length',
|
||||||
|
'config.url_shortener.domain.hostname',
|
||||||
],
|
],
|
||||||
Command\ShortUrl\ResolveUrlCommand::class => [Service\ShortUrl\ShortUrlResolver::class],
|
Command\ShortUrl\ResolveUrlCommand::class => [Service\ShortUrl\ShortUrlResolver::class],
|
||||||
Command\ShortUrl\ListShortUrlsCommand::class => [
|
Command\ShortUrl\ListShortUrlsCommand::class => [
|
||||||
|
@ -30,10 +30,13 @@ class CreateShortUrlCommand extends BaseCommand
|
|||||||
{
|
{
|
||||||
public const NAME = 'short-url:create';
|
public const NAME = 'short-url:create';
|
||||||
|
|
||||||
|
private ?SymfonyStyle $io;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private UrlShortenerInterface $urlShortener,
|
private UrlShortenerInterface $urlShortener,
|
||||||
private ShortUrlStringifierInterface $stringifier,
|
private ShortUrlStringifierInterface $stringifier,
|
||||||
private int $defaultShortCodeLength,
|
private int $defaultShortCodeLength,
|
||||||
|
private string $defaultDomain,
|
||||||
) {
|
) {
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
}
|
}
|
||||||
@ -123,21 +126,33 @@ class CreateShortUrlCommand extends BaseCommand
|
|||||||
|
|
||||||
protected function interact(InputInterface $input, OutputInterface $output): void
|
protected function interact(InputInterface $input, OutputInterface $output): void
|
||||||
{
|
{
|
||||||
$io = new SymfonyStyle($input, $output);
|
$this->verifyLongUrlArgument($input, $output);
|
||||||
|
$this->verifyDomainArgument($input);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function verifyLongUrlArgument(InputInterface $input, OutputInterface $output): void
|
||||||
|
{
|
||||||
$longUrl = $input->getArgument('longUrl');
|
$longUrl = $input->getArgument('longUrl');
|
||||||
if (! empty($longUrl)) {
|
if (! empty($longUrl)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$io = $this->getIO($input, $output);
|
||||||
$longUrl = $io->ask('Which URL do you want to shorten?');
|
$longUrl = $io->ask('Which URL do you want to shorten?');
|
||||||
if (! empty($longUrl)) {
|
if (! empty($longUrl)) {
|
||||||
$input->setArgument('longUrl', $longUrl);
|
$input->setArgument('longUrl', $longUrl);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function verifyDomainArgument(InputInterface $input): void
|
||||||
|
{
|
||||||
|
$domain = $input->getOption('domain');
|
||||||
|
$input->setOption('domain', $domain === $this->defaultDomain ? null : $domain);
|
||||||
|
}
|
||||||
|
|
||||||
protected function execute(InputInterface $input, OutputInterface $output): ?int
|
protected function execute(InputInterface $input, OutputInterface $output): ?int
|
||||||
{
|
{
|
||||||
$io = new SymfonyStyle($input, $output);
|
$io = $this->getIO($input, $output);
|
||||||
$longUrl = $input->getArgument('longUrl');
|
$longUrl = $input->getArgument('longUrl');
|
||||||
if (empty($longUrl)) {
|
if (empty($longUrl)) {
|
||||||
$io->error('A URL was not provided!');
|
$io->error('A URL was not provided!');
|
||||||
@ -197,4 +212,9 @@ class CreateShortUrlCommand extends BaseCommand
|
|||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getIO(InputInterface $input, OutputInterface $output): SymfonyStyle
|
||||||
|
{
|
||||||
|
return $this->io ?? ($this->io = new SymfonyStyle($input, $output));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,8 @@ class CreateShortUrlCommandTest extends TestCase
|
|||||||
{
|
{
|
||||||
use CliTestUtilsTrait;
|
use CliTestUtilsTrait;
|
||||||
|
|
||||||
|
private const DEFAULT_DOMAIN = 'default.com';
|
||||||
|
|
||||||
private CommandTester $commandTester;
|
private CommandTester $commandTester;
|
||||||
private ObjectProphecy $urlShortener;
|
private ObjectProphecy $urlShortener;
|
||||||
private ObjectProphecy $stringifier;
|
private ObjectProphecy $stringifier;
|
||||||
@ -33,7 +35,12 @@ class CreateShortUrlCommandTest extends TestCase
|
|||||||
$this->stringifier = $this->prophesize(ShortUrlStringifierInterface::class);
|
$this->stringifier = $this->prophesize(ShortUrlStringifierInterface::class);
|
||||||
$this->stringifier->stringify(Argument::type(ShortUrl::class))->willReturn('');
|
$this->stringifier->stringify(Argument::type(ShortUrl::class))->willReturn('');
|
||||||
|
|
||||||
$command = new CreateShortUrlCommand($this->urlShortener->reveal(), $this->stringifier->reveal(), 5);
|
$command = new CreateShortUrlCommand(
|
||||||
|
$this->urlShortener->reveal(),
|
||||||
|
$this->stringifier->reveal(),
|
||||||
|
5,
|
||||||
|
self::DEFAULT_DOMAIN,
|
||||||
|
);
|
||||||
$this->commandTester = $this->testerForCommand($command);
|
$this->commandTester = $this->testerForCommand($command);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,6 +117,34 @@ class CreateShortUrlCommandTest extends TestCase
|
|||||||
$stringify->shouldHaveBeenCalledOnce();
|
$stringify->shouldHaveBeenCalledOnce();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @dataProvider provideDomains
|
||||||
|
*/
|
||||||
|
public function properlyProcessesProvidedDomain(array $input, ?string $expectedDomain): void
|
||||||
|
{
|
||||||
|
$shorten = $this->urlShortener->shorten(
|
||||||
|
Argument::that(function (ShortUrlMeta $meta) use ($expectedDomain) {
|
||||||
|
Assert::assertEquals($expectedDomain, $meta->getDomain());
|
||||||
|
return true;
|
||||||
|
}),
|
||||||
|
)->willReturn(ShortUrl::createEmpty());
|
||||||
|
|
||||||
|
$input['longUrl'] = 'http://domain.com/foo/bar';
|
||||||
|
$this->commandTester->execute($input);
|
||||||
|
|
||||||
|
self::assertEquals(ExitCodes::EXIT_SUCCESS, $this->commandTester->getStatusCode());
|
||||||
|
$shorten->shouldHaveBeenCalledOnce();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function provideDomains(): iterable
|
||||||
|
{
|
||||||
|
yield 'no domain' => [[], null];
|
||||||
|
yield 'non-default domain foo' => [['--domain' => 'foo.com'], 'foo.com'];
|
||||||
|
yield 'non-default domain bar' => [['-d' => 'bar.com'], 'bar.com'];
|
||||||
|
yield 'default domain' => [['--domain' => self::DEFAULT_DOMAIN], null];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @test
|
* @test
|
||||||
* @dataProvider provideFlags
|
* @dataProvider provideFlags
|
||||||
|
Loading…
Reference in New Issue
Block a user