From cbd4b4849fb52473155cab0ca5f68be1e621323b Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 9 Dec 2021 10:24:58 +0100 Subject: [PATCH] Ensured default domain is stripped when creating short URLs from CLI --- module/CLI/config/dependencies.config.php | 1 + .../ShortUrl/CreateShortUrlCommand.php | 24 +++++++++++- .../ShortUrl/CreateShortUrlCommandTest.php | 37 ++++++++++++++++++- 3 files changed, 59 insertions(+), 3 deletions(-) diff --git a/module/CLI/config/dependencies.config.php b/module/CLI/config/dependencies.config.php index cde351ee..41d415dc 100644 --- a/module/CLI/config/dependencies.config.php +++ b/module/CLI/config/dependencies.config.php @@ -79,6 +79,7 @@ return [ Service\UrlShortener::class, ShortUrlStringifier::class, 'config.url_shortener.default_short_codes_length', + 'config.url_shortener.domain.hostname', ], Command\ShortUrl\ResolveUrlCommand::class => [Service\ShortUrl\ShortUrlResolver::class], Command\ShortUrl\ListShortUrlsCommand::class => [ diff --git a/module/CLI/src/Command/ShortUrl/CreateShortUrlCommand.php b/module/CLI/src/Command/ShortUrl/CreateShortUrlCommand.php index 26673c6c..62b50456 100644 --- a/module/CLI/src/Command/ShortUrl/CreateShortUrlCommand.php +++ b/module/CLI/src/Command/ShortUrl/CreateShortUrlCommand.php @@ -30,10 +30,13 @@ class CreateShortUrlCommand extends BaseCommand { public const NAME = 'short-url:create'; + private ?SymfonyStyle $io; + public function __construct( private UrlShortenerInterface $urlShortener, private ShortUrlStringifierInterface $stringifier, private int $defaultShortCodeLength, + private string $defaultDomain, ) { parent::__construct(); } @@ -123,21 +126,33 @@ class CreateShortUrlCommand extends BaseCommand 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'); if (! empty($longUrl)) { return; } + $io = $this->getIO($input, $output); $longUrl = $io->ask('Which URL do you want to shorten?'); if (! empty($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 { - $io = new SymfonyStyle($input, $output); + $io = $this->getIO($input, $output); $longUrl = $input->getArgument('longUrl'); if (empty($longUrl)) { $io->error('A URL was not provided!'); @@ -197,4 +212,9 @@ class CreateShortUrlCommand extends BaseCommand return null; } + + private function getIO(InputInterface $input, OutputInterface $output): SymfonyStyle + { + return $this->io ?? ($this->io = new SymfonyStyle($input, $output)); + } } diff --git a/module/CLI/test/Command/ShortUrl/CreateShortUrlCommandTest.php b/module/CLI/test/Command/ShortUrl/CreateShortUrlCommandTest.php index 14d75f76..08389d61 100644 --- a/module/CLI/test/Command/ShortUrl/CreateShortUrlCommandTest.php +++ b/module/CLI/test/Command/ShortUrl/CreateShortUrlCommandTest.php @@ -23,6 +23,8 @@ class CreateShortUrlCommandTest extends TestCase { use CliTestUtilsTrait; + private const DEFAULT_DOMAIN = 'default.com'; + private CommandTester $commandTester; private ObjectProphecy $urlShortener; private ObjectProphecy $stringifier; @@ -33,7 +35,12 @@ class CreateShortUrlCommandTest extends TestCase $this->stringifier = $this->prophesize(ShortUrlStringifierInterface::class); $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); } @@ -110,6 +117,34 @@ class CreateShortUrlCommandTest extends TestCase $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 * @dataProvider provideFlags