2016-07-31 08:58:18 -05:00
|
|
|
<?php
|
2019-10-05 10:26:10 -05:00
|
|
|
|
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
|
|
|
|
2019-12-01 05:26:31 -06:00
|
|
|
use Cake\Chronos\Chronos;
|
2021-02-01 15:55:52 -06:00
|
|
|
use Laminas\Diactoros\Response\JsonResponse;
|
2020-01-01 14:11:53 -06:00
|
|
|
use Laminas\Diactoros\ServerRequest;
|
|
|
|
use Laminas\Diactoros\ServerRequestFactory;
|
2017-03-24 14:34:18 -05:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-07-31 08:58:18 -05:00
|
|
|
use Prophecy\Argument;
|
2020-11-02 04:50:19 -06:00
|
|
|
use Prophecy\PhpUnit\ProphecyTrait;
|
2016-07-31 08:58:18 -05:00
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
2021-02-01 15:55:52 -06:00
|
|
|
use Shlinkio\Shlink\Common\Rest\DataTransformerInterface;
|
2018-09-12 13:32:58 -05:00
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
2019-11-24 16:45:40 -06:00
|
|
|
use Shlinkio\Shlink\Core\Exception\ValidationException;
|
2019-12-01 05:26:31 -06:00
|
|
|
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
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;
|
2020-11-08 04:28:27 -06:00
|
|
|
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
2019-02-26 15:56:43 -06:00
|
|
|
|
2018-09-20 12:55:24 -05:00
|
|
|
class CreateShortUrlActionTest extends TestCase
|
2016-07-31 08:58:18 -05:00
|
|
|
{
|
2020-11-02 04:50:19 -06:00
|
|
|
use ProphecyTrait;
|
|
|
|
|
2019-12-29 15:48:40 -06:00
|
|
|
private CreateShortUrlAction $action;
|
|
|
|
private ObjectProphecy $urlShortener;
|
2021-02-01 15:55:52 -06:00
|
|
|
private ObjectProphecy $transformer;
|
2016-07-31 08:58:18 -05:00
|
|
|
|
2019-02-16 03:53:45 -06:00
|
|
|
public function setUp(): void
|
2016-07-31 08:58:18 -05:00
|
|
|
{
|
|
|
|
$this->urlShortener = $this->prophesize(UrlShortener::class);
|
2021-02-01 15:55:52 -06:00
|
|
|
$this->transformer = $this->prophesize(DataTransformerInterface::class);
|
|
|
|
$this->transformer->transform(Argument::type(ShortUrl::class))->willReturn([]);
|
|
|
|
|
|
|
|
$this->action = new CreateShortUrlAction($this->urlShortener->reveal(), $this->transformer->reveal());
|
2016-07-31 08:58:18 -05:00
|
|
|
}
|
|
|
|
|
2019-02-17 13:28:34 -06:00
|
|
|
/** @test */
|
2021-01-30 07:18:44 -06:00
|
|
|
public function properShortcodeConversionReturnsData(): void
|
2016-07-31 08:58:18 -05:00
|
|
|
{
|
2021-03-14 03:59:35 -05:00
|
|
|
$apiKey = ApiKey::create();
|
2021-01-30 07:18:44 -06:00
|
|
|
$shortUrl = ShortUrl::createEmpty();
|
|
|
|
$expectedMeta = $body = [
|
|
|
|
'longUrl' => 'http://www.domain.com/foo/bar',
|
|
|
|
'validSince' => Chronos::now()->toAtomString(),
|
|
|
|
'validUntil' => Chronos::now()->toAtomString(),
|
|
|
|
'customSlug' => 'foo-bar-baz',
|
|
|
|
'maxVisits' => 50,
|
|
|
|
'findIfExists' => true,
|
|
|
|
'domain' => 'my-domain.com',
|
|
|
|
];
|
2021-01-04 06:32:44 -06:00
|
|
|
$expectedMeta['apiKey'] = $apiKey;
|
2020-11-08 04:28:27 -06:00
|
|
|
|
2021-01-30 12:17:12 -06:00
|
|
|
$shorten = $this->urlShortener->shorten(ShortUrlMeta::fromRawData($expectedMeta))->willReturn($shortUrl);
|
2021-02-01 15:55:52 -06:00
|
|
|
$transform = $this->transformer->transform($shortUrl)->willReturn(['shortUrl' => 'stringified_short_url']);
|
2016-07-31 08:58:18 -05:00
|
|
|
|
2020-11-08 04:28:27 -06:00
|
|
|
$request = ServerRequestFactory::fromGlobals()->withParsedBody($body)->withAttribute(ApiKey::class, $apiKey);
|
2020-11-07 03:23:08 -06:00
|
|
|
|
2021-02-01 15:55:52 -06:00
|
|
|
/** @var JsonResponse $response */
|
2018-03-26 12:02:41 -05:00
|
|
|
$response = $this->action->handle($request);
|
2021-02-01 15:55:52 -06:00
|
|
|
$payload = $response->getPayload();
|
2019-12-01 05:26:31 -06:00
|
|
|
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertEquals(200, $response->getStatusCode());
|
2021-02-01 15:55:52 -06:00
|
|
|
self::assertEquals('stringified_short_url', $payload['shortUrl']);
|
2019-12-01 05:26:31 -06:00
|
|
|
$shorten->shouldHaveBeenCalledOnce();
|
2021-02-01 15:55:52 -06:00
|
|
|
$transform->shouldHaveBeenCalledOnce();
|
2019-12-01 05:26:31 -06:00
|
|
|
}
|
|
|
|
|
2019-10-20 03:30:11 -05:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideInvalidDomains
|
|
|
|
*/
|
|
|
|
public function anInvalidDomainReturnsError(string $domain): void
|
|
|
|
{
|
2021-01-30 07:18:44 -06:00
|
|
|
$shortUrl = ShortUrl::createEmpty();
|
2020-11-06 13:05:57 -06:00
|
|
|
$urlToShortCode = $this->urlShortener->shorten(Argument::cetera())->willReturn($shortUrl);
|
2019-10-20 03:30:11 -05:00
|
|
|
|
|
|
|
$request = (new ServerRequest())->withParsedBody([
|
|
|
|
'longUrl' => 'http://www.domain.com/foo/bar',
|
|
|
|
'domain' => $domain,
|
2021-03-14 03:59:35 -05:00
|
|
|
])->withAttribute(ApiKey::class, ApiKey::create());
|
2019-10-20 03:30:11 -05:00
|
|
|
|
2019-11-24 16:45:40 -06:00
|
|
|
$this->expectException(ValidationException::class);
|
|
|
|
$urlToShortCode->shouldNotBeCalled();
|
|
|
|
|
|
|
|
$this->action->handle($request);
|
2019-10-20 03:30:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideInvalidDomains(): iterable
|
|
|
|
{
|
|
|
|
yield ['localhost:80000'];
|
|
|
|
yield ['127.0.0.1'];
|
|
|
|
yield ['???/&%$&'];
|
|
|
|
}
|
2016-07-31 08:58:18 -05:00
|
|
|
}
|