diff --git a/module/Rest/src/Action/CreateShortcodeAction.php b/module/Rest/src/Action/CreateShortcodeAction.php index a4f7919b..790c361b 100644 --- a/module/Rest/src/Action/CreateShortcodeAction.php +++ b/module/Rest/src/Action/CreateShortcodeAction.php @@ -74,7 +74,7 @@ class CreateShortcodeAction extends AbstractRestAction return new JsonResponse([ 'longUrl' => $longUrl, - 'shortUrl' => $shortUrl->__toString(), + 'shortUrl' => (string) $shortUrl, 'shortCode' => $shortCode, ]); } catch (InvalidUrlException $e) { diff --git a/module/Rest/test/Action/CreateShortcodeActionTest.php b/module/Rest/test/Action/CreateShortcodeActionTest.php index adb1ada4..ce297105 100644 --- a/module/Rest/test/Action/CreateShortcodeActionTest.php +++ b/module/Rest/test/Action/CreateShortcodeActionTest.php @@ -7,6 +7,7 @@ use PHPUnit\Framework\TestCase; use Prophecy\Argument; use Prophecy\Prophecy\ObjectProphecy; use Shlinkio\Shlink\Core\Exception\InvalidUrlException; +use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException; use Shlinkio\Shlink\Core\Service\UrlShortener; use Shlinkio\Shlink\Rest\Action\CreateShortcodeAction; use Shlinkio\Shlink\Rest\Util\RestUtils; @@ -52,7 +53,7 @@ class CreateShortcodeActionTest extends TestCase */ public function properShortcodeConversionReturnsData() { - $this->urlShortener->urlToShortCode(Argument::type(Uri::class), Argument::type('array'), null, null) + $this->urlShortener->urlToShortCode(Argument::type(Uri::class), Argument::type('array'), Argument::cetera()) ->willReturn('abc123') ->shouldBeCalledTimes(1); @@ -69,7 +70,7 @@ class CreateShortcodeActionTest extends TestCase */ public function anInvalidUrlReturnsError() { - $this->urlShortener->urlToShortCode(Argument::type(Uri::class), Argument::type('array'), null, null) + $this->urlShortener->urlToShortCode(Argument::type(Uri::class), Argument::type('array'), Argument::cetera()) ->willThrow(InvalidUrlException::class) ->shouldBeCalledTimes(1); @@ -81,12 +82,30 @@ class CreateShortcodeActionTest extends TestCase $this->assertTrue(strpos($response->getBody()->getContents(), RestUtils::INVALID_URL_ERROR) > 0); } + /** + * @test + */ + public function nonUniqueSlugReturnsError() + { + $this->urlShortener->urlToShortCode(Argument::type(Uri::class), Argument::type('array'), null, null, 'foo') + ->willThrow(NonUniqueSlugException::class) + ->shouldBeCalledTimes(1); + + $request = ServerRequestFactory::fromGlobals()->withParsedBody([ + 'longUrl' => 'http://www.domain.com/foo/bar', + 'customSlug' => 'foo', + ]); + $response = $this->action->process($request, TestUtils::createDelegateMock()->reveal()); + $this->assertEquals(400, $response->getStatusCode()); + $this->assertContains(RestUtils::INVALID_SLUG_ERROR, (string) $response->getBody()); + } + /** * @test */ public function aGenericExceptionWillReturnError() { - $this->urlShortener->urlToShortCode(Argument::type(Uri::class), Argument::type('array'), null, null) + $this->urlShortener->urlToShortCode(Argument::type(Uri::class), Argument::type('array'), Argument::cetera()) ->willThrow(\Exception::class) ->shouldBeCalledTimes(1);