2016-07-31 16:58:18 +03:00
|
|
|
<?php
|
2019-10-05 18:26:10 +03:00
|
|
|
|
2017-10-12 11:13:20 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-09-20 20:55:24 +03:00
|
|
|
namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl;
|
2016-07-31 16:58:18 +03:00
|
|
|
|
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;
|
2018-09-12 21:32:58 +03:00
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
2019-11-25 01:45:40 +03:00
|
|
|
use Shlinkio\Shlink\Core\Exception\ValidationException;
|
2016-07-31 16:58:18 +03:00
|
|
|
use Shlinkio\Shlink\Core\Service\UrlShortener;
|
2018-09-20 20:55:24 +03:00
|
|
|
use Shlinkio\Shlink\Rest\Action\ShortUrl\CreateShortUrlAction;
|
2018-12-26 01:01:30 +03:00
|
|
|
use Zend\Diactoros\ServerRequest;
|
2016-07-31 16:58:18 +03:00
|
|
|
use Zend\Diactoros\Uri;
|
2019-02-27 00:56:43 +03:00
|
|
|
|
2018-10-28 10:34:02 +03:00
|
|
|
use function strpos;
|
2016-07-31 16:58:18 +03:00
|
|
|
|
2018-09-20 20:55:24 +03:00
|
|
|
class CreateShortUrlActionTest extends TestCase
|
2016-07-31 16:58:18 +03:00
|
|
|
{
|
2019-10-11 10:14:25 +03:00
|
|
|
private const DOMAIN_CONFIG = [
|
|
|
|
'schema' => 'http',
|
|
|
|
'hostname' => 'foo.com',
|
|
|
|
];
|
|
|
|
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var CreateShortUrlAction */
|
2018-11-20 21:37:22 +03:00
|
|
|
private $action;
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var ObjectProphecy */
|
2018-11-20 21:37:22 +03:00
|
|
|
private $urlShortener;
|
2016-07-31 16:58:18 +03:00
|
|
|
|
2019-02-16 12:53:45 +03:00
|
|
|
public function setUp(): void
|
2016-07-31 16:58:18 +03:00
|
|
|
{
|
|
|
|
$this->urlShortener = $this->prophesize(UrlShortener::class);
|
2019-10-11 10:14:25 +03:00
|
|
|
$this->action = new CreateShortUrlAction($this->urlShortener->reveal(), self::DOMAIN_CONFIG);
|
2016-07-31 16:58:18 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2019-10-02 21:15:14 +03:00
|
|
|
public function missingLongUrlParamReturnsError(): void
|
2016-07-31 16:58:18 +03:00
|
|
|
{
|
2019-11-25 01:45:40 +03:00
|
|
|
$this->expectException(ValidationException::class);
|
|
|
|
$this->action->handle(new ServerRequest());
|
2016-07-31 16:58:18 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2019-10-02 21:15:14 +03:00
|
|
|
public function properShortcodeConversionReturnsData(): void
|
2016-07-31 16:58:18 +03:00
|
|
|
{
|
2019-10-11 10:14:25 +03:00
|
|
|
$shortUrl = new ShortUrl('');
|
2017-10-21 21:16:39 +03:00
|
|
|
$this->urlShortener->urlToShortCode(Argument::type(Uri::class), Argument::type('array'), Argument::cetera())
|
2019-10-20 11:30:11 +03:00
|
|
|
->willReturn($shortUrl)
|
|
|
|
->shouldBeCalledOnce();
|
2016-07-31 16:58:18 +03:00
|
|
|
|
2018-12-26 01:01:30 +03:00
|
|
|
$request = (new ServerRequest())->withParsedBody([
|
2016-07-31 16:58:18 +03:00
|
|
|
'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());
|
2019-10-11 10:14:25 +03:00
|
|
|
$this->assertTrue(strpos($response->getBody()->getContents(), $shortUrl->toString(self::DOMAIN_CONFIG)) > 0);
|
2016-07-31 16:58:18 +03:00
|
|
|
}
|
|
|
|
|
2019-10-20 11:30:11 +03:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideInvalidDomains
|
|
|
|
*/
|
|
|
|
public function anInvalidDomainReturnsError(string $domain): void
|
|
|
|
{
|
|
|
|
$shortUrl = new ShortUrl('');
|
|
|
|
$urlToShortCode = $this->urlShortener->urlToShortCode(Argument::cetera())->willReturn($shortUrl);
|
|
|
|
|
|
|
|
$request = (new ServerRequest())->withParsedBody([
|
|
|
|
'longUrl' => 'http://www.domain.com/foo/bar',
|
|
|
|
'domain' => $domain,
|
|
|
|
]);
|
|
|
|
|
2019-11-25 01:45:40 +03:00
|
|
|
$this->expectException(ValidationException::class);
|
|
|
|
$urlToShortCode->shouldNotBeCalled();
|
|
|
|
|
|
|
|
$this->action->handle($request);
|
2019-10-20 11:30:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideInvalidDomains(): iterable
|
|
|
|
{
|
|
|
|
yield ['localhost:80000'];
|
|
|
|
yield ['127.0.0.1'];
|
|
|
|
yield ['???/&%$&'];
|
|
|
|
}
|
2016-07-31 16:58:18 +03:00
|
|
|
}
|