From 2ffaabe594a33810e3f980ff23410237fa2657f4 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Wed, 2 Oct 2019 20:22:42 +0200 Subject: [PATCH] Added option to define domain to GenerateShortUrlCommand --- .../ShortUrl/GenerateShortUrlCommand.php | 9 +++++- .../ShortUrl/GenerateShortUrlCommandTest.php | 32 +++++++++++++++---- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/module/CLI/src/Command/ShortUrl/GenerateShortUrlCommand.php b/module/CLI/src/Command/ShortUrl/GenerateShortUrlCommand.php index 6b2c2eeb..c13e205e 100644 --- a/module/CLI/src/Command/ShortUrl/GenerateShortUrlCommand.php +++ b/module/CLI/src/Command/ShortUrl/GenerateShortUrlCommand.php @@ -84,6 +84,12 @@ class GenerateShortUrlCommand extends Command 'f', InputOption::VALUE_NONE, 'This will force existing matching URL to be returned if found, instead of creating a new one.' + ) + ->addOption( + 'domain', + 'd', + InputOption::VALUE_REQUIRED, + 'The domain to which this short URL will be attached.' ); } @@ -124,7 +130,8 @@ class GenerateShortUrlCommand extends Command $this->getOptionalDate($input, 'validUntil'), $customSlug, $maxVisits !== null ? (int) $maxVisits : null, - $input->getOption('findIfExists') + $input->getOption('findIfExists'), + $input->getOption('domain') ) ); diff --git a/module/CLI/test/Command/ShortUrl/GenerateShortUrlCommandTest.php b/module/CLI/test/Command/ShortUrl/GenerateShortUrlCommandTest.php index 064bfe78..6cda9528 100644 --- a/module/CLI/test/Command/ShortUrl/GenerateShortUrlCommandTest.php +++ b/module/CLI/test/Command/ShortUrl/GenerateShortUrlCommandTest.php @@ -9,8 +9,10 @@ use Prophecy\Argument; use Prophecy\Prophecy\ObjectProphecy; use Psr\Http\Message\UriInterface; use Shlinkio\Shlink\CLI\Command\ShortUrl\GenerateShortUrlCommand; +use Shlinkio\Shlink\CLI\Util\ExitCodes; use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Exception\InvalidUrlException; +use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException; use Shlinkio\Shlink\Core\Service\UrlShortener; use Symfony\Component\Console\Application; use Symfony\Component\Console\Tester\CommandTester; @@ -35,7 +37,7 @@ class GenerateShortUrlCommandTest extends TestCase } /** @test */ - public function properShortCodeIsCreatedIfLongUrlIsCorrect() + public function properShortCodeIsCreatedIfLongUrlIsCorrect(): void { $urlToShortCode = $this->urlShortener->urlToShortCode(Argument::cetera())->willReturn( (new ShortUrl(''))->setShortCode('abc123') @@ -47,26 +49,41 @@ class GenerateShortUrlCommandTest extends TestCase ]); $output = $this->commandTester->getDisplay(); + $this->assertEquals(ExitCodes::EXIT_SUCCESS, $this->commandTester->getStatusCode()); $this->assertStringContainsString('http://foo.com/abc123', $output); $urlToShortCode->shouldHaveBeenCalledOnce(); } /** @test */ - public function exceptionWhileParsingLongUrlOutputsError() + public function exceptionWhileParsingLongUrlOutputsError(): void { $this->urlShortener->urlToShortCode(Argument::cetera())->willThrow(new InvalidUrlException()) ->shouldBeCalledOnce(); $this->commandTester->execute(['longUrl' => 'http://domain.com/invalid']); $output = $this->commandTester->getDisplay(); - $this->assertStringContainsString( - 'Provided URL "http://domain.com/invalid" is invalid.', - $output - ); + + $this->assertEquals(ExitCodes::EXIT_FAILURE, $this->commandTester->getStatusCode()); + $this->assertStringContainsString('Provided URL "http://domain.com/invalid" is invalid.', $output); } /** @test */ - public function properlyProcessesProvidedTags() + public function providingNonUniqueSlugOutputsError(): void + { + $urlToShortCode = $this->urlShortener->urlToShortCode(Argument::cetera())->willThrow( + NonUniqueSlugException::class + ); + + $this->commandTester->execute(['longUrl' => 'http://domain.com/invalid', '--customSlug' => 'my-slug']); + $output = $this->commandTester->getDisplay(); + + $this->assertEquals(ExitCodes::EXIT_FAILURE, $this->commandTester->getStatusCode()); + $this->assertStringContainsString('Provided slug "my-slug" is already in use', $output); + $urlToShortCode->shouldHaveBeenCalledOnce(); + } + + /** @test */ + public function properlyProcessesProvidedTags(): void { $urlToShortCode = $this->urlShortener->urlToShortCode( Argument::type(UriInterface::class), @@ -83,6 +100,7 @@ class GenerateShortUrlCommandTest extends TestCase ]); $output = $this->commandTester->getDisplay(); + $this->assertEquals(ExitCodes::EXIT_SUCCESS, $this->commandTester->getStatusCode()); $this->assertStringContainsString('http://foo.com/abc123', $output); $urlToShortCode->shouldHaveBeenCalledOnce(); }