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
|
|
|
|
2019-12-01 14:26:31 +03:00
|
|
|
use Cake\Chronos\Chronos;
|
2021-02-02 00:55:52 +03:00
|
|
|
use Laminas\Diactoros\Response\JsonResponse;
|
2020-01-01 23:11:53 +03:00
|
|
|
use Laminas\Diactoros\ServerRequest;
|
|
|
|
use Laminas\Diactoros\ServerRequestFactory;
|
2022-10-23 23:09:37 +03:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2017-03-24 22:34:18 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2021-02-02 00:55:52 +03:00
|
|
|
use Shlinkio\Shlink\Common\Rest\DataTransformerInterface;
|
2019-11-25 01:45:40 +03:00
|
|
|
use Shlinkio\Shlink\Core\Exception\ValidationException;
|
2022-08-05 09:38:05 +03:00
|
|
|
use Shlinkio\Shlink\Core\Options\UrlShortenerOptions;
|
2022-09-23 20:03:32 +03:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
2022-09-23 19:05:17 +03:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlCreation;
|
2022-09-23 19:42:38 +03:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\UrlShortener;
|
2018-09-20 20:55:24 +03:00
|
|
|
use Shlinkio\Shlink\Rest\Action\ShortUrl\CreateShortUrlAction;
|
2020-11-08 13:28:27 +03:00
|
|
|
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
2019-02-27 00:56:43 +03:00
|
|
|
|
2018-09-20 20:55:24 +03:00
|
|
|
class CreateShortUrlActionTest extends TestCase
|
2016-07-31 16:58:18 +03:00
|
|
|
{
|
2019-12-30 00:48:40 +03:00
|
|
|
private CreateShortUrlAction $action;
|
2022-10-23 23:09:37 +03:00
|
|
|
private MockObject $urlShortener;
|
|
|
|
private MockObject $transformer;
|
2016-07-31 16:58:18 +03:00
|
|
|
|
2022-09-11 13:02:49 +03:00
|
|
|
protected function setUp(): void
|
2016-07-31 16:58:18 +03:00
|
|
|
{
|
2022-10-23 23:09:37 +03:00
|
|
|
$this->urlShortener = $this->createMock(UrlShortener::class);
|
|
|
|
$this->transformer = $this->createMock(DataTransformerInterface::class);
|
|
|
|
|
|
|
|
$this->action = new CreateShortUrlAction($this->urlShortener, $this->transformer, new UrlShortenerOptions());
|
2016-07-31 16:58:18 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2021-01-30 16:18:44 +03:00
|
|
|
public function properShortcodeConversionReturnsData(): void
|
2016-07-31 16:58:18 +03:00
|
|
|
{
|
2021-03-14 11:59:35 +03:00
|
|
|
$apiKey = ApiKey::create();
|
2021-01-30 16:18:44 +03: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 15:32:44 +03:00
|
|
|
$expectedMeta['apiKey'] = $apiKey;
|
2020-11-08 13:28:27 +03:00
|
|
|
|
2022-10-23 23:09:37 +03:00
|
|
|
$this->urlShortener->expects($this->once())->method('shorten')->with(
|
|
|
|
ShortUrlCreation::fromRawData($expectedMeta),
|
|
|
|
)->willReturn($shortUrl);
|
|
|
|
$this->transformer->expects($this->once())->method('transform')->with($shortUrl)->willReturn(
|
|
|
|
['shortUrl' => 'stringified_short_url'],
|
|
|
|
);
|
2016-07-31 16:58:18 +03:00
|
|
|
|
2020-11-08 13:28:27 +03:00
|
|
|
$request = ServerRequestFactory::fromGlobals()->withParsedBody($body)->withAttribute(ApiKey::class, $apiKey);
|
2020-11-07 12:23:08 +03:00
|
|
|
|
2021-02-02 00:55:52 +03:00
|
|
|
/** @var JsonResponse $response */
|
2018-03-26 20:02:41 +03:00
|
|
|
$response = $this->action->handle($request);
|
2021-02-02 00:55:52 +03:00
|
|
|
$payload = $response->getPayload();
|
2019-12-01 14:26:31 +03:00
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertEquals(200, $response->getStatusCode());
|
2021-02-02 00:55:52 +03:00
|
|
|
self::assertEquals('stringified_short_url', $payload['shortUrl']);
|
2019-12-01 14:26:31 +03:00
|
|
|
}
|
|
|
|
|
2019-10-20 11:30:11 +03:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideInvalidDomains
|
|
|
|
*/
|
|
|
|
public function anInvalidDomainReturnsError(string $domain): void
|
|
|
|
{
|
2022-10-23 23:09:37 +03:00
|
|
|
$this->urlShortener->expects($this->never())->method('shorten');
|
|
|
|
$this->transformer->expects($this->never())->method('transform');
|
2019-10-20 11:30:11 +03:00
|
|
|
|
|
|
|
$request = (new ServerRequest())->withParsedBody([
|
|
|
|
'longUrl' => 'http://www.domain.com/foo/bar',
|
|
|
|
'domain' => $domain,
|
2021-03-14 11:59:35 +03:00
|
|
|
])->withAttribute(ApiKey::class, ApiKey::create());
|
2019-10-20 11:30:11 +03:00
|
|
|
|
2019-11-25 01:45:40 +03:00
|
|
|
$this->expectException(ValidationException::class);
|
|
|
|
|
|
|
|
$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
|
|
|
}
|