shlink/module/Rest/test/Action/ShortUrl/CreateShortUrlActionTest.php

98 lines
3.4 KiB
PHP
Raw Normal View History

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);
namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl;
2016-07-31 16:58:18 +03:00
use Cake\Chronos\Chronos;
use Laminas\Diactoros\Response\JsonResponse;
2020-01-01 23:11:53 +03:00
use Laminas\Diactoros\ServerRequest;
use Laminas\Diactoros\ServerRequestFactory;
2017-03-24 22:34:18 +03:00
use PHPUnit\Framework\TestCase;
2016-07-31 16:58:18 +03:00
use Prophecy\Argument;
2020-11-02 13:50:19 +03:00
use Prophecy\PhpUnit\ProphecyTrait;
2016-07-31 16:58:18 +03:00
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Common\Rest\DataTransformerInterface;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Exception\ValidationException;
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
2016-07-31 16:58:18 +03:00
use Shlinkio\Shlink\Core\Service\UrlShortener;
use Shlinkio\Shlink\Rest\Action\ShortUrl\CreateShortUrlAction;
2020-11-08 13:28:27 +03:00
use Shlinkio\Shlink\Rest\Entity\ApiKey;
class CreateShortUrlActionTest extends TestCase
2016-07-31 16:58:18 +03:00
{
2020-11-02 13:50:19 +03:00
use ProphecyTrait;
private CreateShortUrlAction $action;
private ObjectProphecy $urlShortener;
private ObjectProphecy $transformer;
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);
$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 16:58:18 +03:00
}
2019-02-17 22:28:34 +03:00
/** @test */
public function properShortcodeConversionReturnsData(): void
2016-07-31 16:58:18 +03:00
{
$apiKey = ApiKey::create();
$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',
];
$expectedMeta['apiKey'] = $apiKey;
2020-11-08 13:28:27 +03:00
$shorten = $this->urlShortener->shorten(ShortUrlMeta::fromRawData($expectedMeta))->willReturn($shortUrl);
$transform = $this->transformer->transform($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);
/** @var JsonResponse $response */
2018-03-26 20:02:41 +03:00
$response = $this->action->handle($request);
$payload = $response->getPayload();
2020-10-04 01:35:14 +03:00
self::assertEquals(200, $response->getStatusCode());
self::assertEquals('stringified_short_url', $payload['shortUrl']);
$shorten->shouldHaveBeenCalledOnce();
$transform->shouldHaveBeenCalledOnce();
}
/**
* @test
* @dataProvider provideInvalidDomains
*/
public function anInvalidDomainReturnsError(string $domain): void
{
$shortUrl = ShortUrl::createEmpty();
$urlToShortCode = $this->urlShortener->shorten(Argument::cetera())->willReturn($shortUrl);
$request = (new ServerRequest())->withParsedBody([
'longUrl' => 'http://www.domain.com/foo/bar',
'domain' => $domain,
])->withAttribute(ApiKey::class, ApiKey::create());
$this->expectException(ValidationException::class);
$urlToShortCode->shouldNotBeCalled();
$this->action->handle($request);
}
public function provideInvalidDomains(): iterable
{
yield ['localhost:80000'];
yield ['127.0.0.1'];
yield ['???/&%$&'];
}
2016-07-31 16:58:18 +03:00
}