2016-07-31 16:58:18 +03:00
|
|
|
<?php
|
2017-10-12 11:13:20 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-07-31 16:58:18 +03:00
|
|
|
namespace ShlinkioTest\Shlink\Rest\Action;
|
|
|
|
|
2017-03-24 22:34:18 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-07-31 16:58:18 +03:00
|
|
|
use Prophecy\Argument;
|
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
|
|
|
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
|
2017-10-21 21:16:39 +03:00
|
|
|
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
|
2016-07-31 16:58:18 +03:00
|
|
|
use Shlinkio\Shlink\Core\Service\UrlShortener;
|
2018-05-03 18:59:28 +03:00
|
|
|
use Shlinkio\Shlink\Rest\Action\ShortCode\CreateShortCodeAction;
|
2016-07-31 16:58:18 +03:00
|
|
|
use Shlinkio\Shlink\Rest\Util\RestUtils;
|
|
|
|
use Zend\Diactoros\ServerRequestFactory;
|
|
|
|
use Zend\Diactoros\Uri;
|
|
|
|
use Zend\I18n\Translator\Translator;
|
|
|
|
|
2018-05-03 13:19:51 +03:00
|
|
|
class CreateShortCodeActionTest extends TestCase
|
2016-07-31 16:58:18 +03:00
|
|
|
{
|
|
|
|
/**
|
2018-05-03 13:19:51 +03:00
|
|
|
* @var CreateShortCodeAction
|
2016-07-31 16:58:18 +03:00
|
|
|
*/
|
|
|
|
protected $action;
|
|
|
|
/**
|
|
|
|
* @var ObjectProphecy
|
|
|
|
*/
|
|
|
|
protected $urlShortener;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$this->urlShortener = $this->prophesize(UrlShortener::class);
|
2018-05-03 13:19:51 +03:00
|
|
|
$this->action = new CreateShortCodeAction($this->urlShortener->reveal(), Translator::factory([]), [
|
2016-07-31 16:58:18 +03:00
|
|
|
'schema' => 'http',
|
|
|
|
'hostname' => 'foo.com',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function missingLongUrlParamReturnsError()
|
|
|
|
{
|
2018-03-26 20:02:41 +03:00
|
|
|
$response = $this->action->handle(ServerRequestFactory::fromGlobals());
|
2016-07-31 16:58:18 +03:00
|
|
|
$this->assertEquals(400, $response->getStatusCode());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function properShortcodeConversionReturnsData()
|
|
|
|
{
|
2017-10-21 21:16:39 +03:00
|
|
|
$this->urlShortener->urlToShortCode(Argument::type(Uri::class), Argument::type('array'), Argument::cetera())
|
2016-08-21 14:07:12 +03:00
|
|
|
->willReturn('abc123')
|
|
|
|
->shouldBeCalledTimes(1);
|
2016-07-31 16:58:18 +03:00
|
|
|
|
|
|
|
$request = ServerRequestFactory::fromGlobals()->withParsedBody([
|
|
|
|
'longUrl' => 'http://www.domain.com/foo/bar',
|
|
|
|
]);
|
2018-03-26 20:02:41 +03:00
|
|
|
$response = $this->action->handle($request);
|
2016-07-31 16:58:18 +03:00
|
|
|
$this->assertEquals(200, $response->getStatusCode());
|
|
|
|
$this->assertTrue(strpos($response->getBody()->getContents(), 'http://foo.com/abc123') > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function anInvalidUrlReturnsError()
|
|
|
|
{
|
2017-10-21 21:16:39 +03:00
|
|
|
$this->urlShortener->urlToShortCode(Argument::type(Uri::class), Argument::type('array'), Argument::cetera())
|
2016-08-21 14:07:12 +03:00
|
|
|
->willThrow(InvalidUrlException::class)
|
|
|
|
->shouldBeCalledTimes(1);
|
2016-07-31 16:58:18 +03:00
|
|
|
|
|
|
|
$request = ServerRequestFactory::fromGlobals()->withParsedBody([
|
|
|
|
'longUrl' => 'http://www.domain.com/foo/bar',
|
|
|
|
]);
|
2018-03-26 20:02:41 +03:00
|
|
|
$response = $this->action->handle($request);
|
2016-07-31 16:58:18 +03:00
|
|
|
$this->assertEquals(400, $response->getStatusCode());
|
|
|
|
$this->assertTrue(strpos($response->getBody()->getContents(), RestUtils::INVALID_URL_ERROR) > 0);
|
|
|
|
}
|
|
|
|
|
2017-10-21 21:16:39 +03:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function nonUniqueSlugReturnsError()
|
|
|
|
{
|
2017-10-22 10:17:19 +03:00
|
|
|
$this->urlShortener->urlToShortCode(
|
|
|
|
Argument::type(Uri::class),
|
|
|
|
Argument::type('array'),
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
'foo',
|
|
|
|
Argument::cetera()
|
|
|
|
)->willThrow(NonUniqueSlugException::class)->shouldBeCalledTimes(1);
|
2017-10-21 21:16:39 +03:00
|
|
|
|
|
|
|
$request = ServerRequestFactory::fromGlobals()->withParsedBody([
|
|
|
|
'longUrl' => 'http://www.domain.com/foo/bar',
|
|
|
|
'customSlug' => 'foo',
|
|
|
|
]);
|
2018-03-26 20:02:41 +03:00
|
|
|
$response = $this->action->handle($request);
|
2017-10-21 21:16:39 +03:00
|
|
|
$this->assertEquals(400, $response->getStatusCode());
|
|
|
|
$this->assertContains(RestUtils::INVALID_SLUG_ERROR, (string) $response->getBody());
|
|
|
|
}
|
|
|
|
|
2016-07-31 16:58:18 +03:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function aGenericExceptionWillReturnError()
|
|
|
|
{
|
2017-10-21 21:16:39 +03:00
|
|
|
$this->urlShortener->urlToShortCode(Argument::type(Uri::class), Argument::type('array'), Argument::cetera())
|
2016-08-21 14:07:12 +03:00
|
|
|
->willThrow(\Exception::class)
|
|
|
|
->shouldBeCalledTimes(1);
|
2016-07-31 16:58:18 +03:00
|
|
|
|
|
|
|
$request = ServerRequestFactory::fromGlobals()->withParsedBody([
|
|
|
|
'longUrl' => 'http://www.domain.com/foo/bar',
|
|
|
|
]);
|
2018-03-26 20:02:41 +03:00
|
|
|
$response = $this->action->handle($request);
|
2016-07-31 16:58:18 +03:00
|
|
|
$this->assertEquals(500, $response->getStatusCode());
|
|
|
|
$this->assertTrue(strpos($response->getBody()->getContents(), RestUtils::UNKNOWN_ERROR) > 0);
|
|
|
|
}
|
|
|
|
}
|