Improved CreateShortcodeActiontest

This commit is contained in:
Alejandro Celaya 2017-10-21 20:16:39 +02:00
parent 5f0d281255
commit 6bbe66e8f1
2 changed files with 23 additions and 4 deletions

View File

@ -74,7 +74,7 @@ class CreateShortcodeAction extends AbstractRestAction
return new JsonResponse([
'longUrl' => $longUrl,
'shortUrl' => $shortUrl->__toString(),
'shortUrl' => (string) $shortUrl,
'shortCode' => $shortCode,
]);
} catch (InvalidUrlException $e) {

View File

@ -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);