diff --git a/module/CLI/src/Command/GenerateShortcodeCommand.php b/module/CLI/src/Command/GenerateShortcodeCommand.php index 0a0af9eb..f02c110b 100644 --- a/module/CLI/src/Command/GenerateShortcodeCommand.php +++ b/module/CLI/src/Command/GenerateShortcodeCommand.php @@ -52,7 +52,7 @@ class GenerateShortcodeCommand extends Command { $this->setName('shortcode:generate') ->setDescription( - $this->translator->translate('Generates a shortcode for provided URL and returns the short URL') + $this->translator->translate('Generates a short code for provided URL and returns the short URL') ) ->addArgument('longUrl', InputArgument::REQUIRED, $this->translator->translate('The long URL to parse')); } @@ -87,8 +87,8 @@ class GenerateShortcodeCommand extends Command return; } - $shortcode = $this->urlShortener->urlToShortCode(new Uri($longUrl)); - $shortUrl = (new Uri())->withPath($shortcode) + $shortCode = $this->urlShortener->urlToShortCode(new Uri($longUrl)); + $shortUrl = (new Uri())->withPath($shortCode) ->withScheme($this->domainConfig['schema']) ->withHost($this->domainConfig['hostname']); diff --git a/module/CLI/test/Command/GenerateShortcodeCommandTest.php b/module/CLI/test/Command/GenerateShortcodeCommandTest.php new file mode 100644 index 00000000..45cb8130 --- /dev/null +++ b/module/CLI/test/Command/GenerateShortcodeCommandTest.php @@ -0,0 +1,70 @@ +urlShortener = $this->prophesize(UrlShortener::class); + $command = new GenerateShortcodeCommand($this->urlShortener->reveal(), Translator::factory([]), [ + 'schema' => 'http', + 'hostname' => 'foo.com' + ]); + $app = new Application(); + $app->add($command); + $this->commandTester = new CommandTester($command); + } + + /** + * @test + */ + public function properShortCodeIsCreatedIfLongUrlIsCorrect() + { + $this->urlShortener->urlToShortCode(Argument::any())->willReturn('abc123') + ->shouldBeCalledTimes(1); + + $this->commandTester->execute([ + 'command' => 'shortcode:generate', + 'longUrl' => 'http://domain.com/foo/bar' + ]); + $output = $this->commandTester->getDisplay(); + $this->assertTrue(strpos($output, 'http://foo.com/abc123') > 0); + } + + /** + * @test + */ + public function exceptionWhileParsingLongUrlOutputsError() + { + $this->urlShortener->urlToShortCode(Argument::any())->willThrow(new InvalidUrlException()) + ->shouldBeCalledTimes(1); + + $this->commandTester->execute([ + 'command' => 'shortcode:generate', + 'longUrl' => 'http://domain.com/invalid' + ]); + $output = $this->commandTester->getDisplay(); + $this->assertTrue( + strpos($output, 'Provided URL "http://domain.com/invalid" is invalid. Try with a different one.') === 0 + ); + } +}