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