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

109 lines
3.3 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;
2020-01-01 23:11:53 +03:00
use Laminas\Diactoros\ServerRequest;
use Laminas\Diactoros\ServerRequestFactory;
use Laminas\Diactoros\Uri;
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;
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;
use function strpos;
2016-07-31 16:58:18 +03:00
class CreateShortUrlActionTest extends TestCase
2016-07-31 16:58:18 +03:00
{
private const DOMAIN_CONFIG = [
'schema' => 'http',
'hostname' => 'foo.com',
];
private CreateShortUrlAction $action;
private ObjectProphecy $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);
$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
{
$this->expectException(ValidationException::class);
$this->action->handle(new ServerRequest());
2016-07-31 16:58:18 +03:00
}
/**
* @test
* @dataProvider provideRequestBodies
*/
public function properShortcodeConversionReturnsData(array $body, ShortUrlMeta $expectedMeta): void
2016-07-31 16:58:18 +03:00
{
$shortUrl = new ShortUrl('');
$shorten = $this->urlShortener->urlToShortCode(
Argument::type(Uri::class),
Argument::type('array'),
2020-01-01 22:48:31 +03:00
$expectedMeta,
)->willReturn($shortUrl);
2016-07-31 16:58:18 +03:00
$request = ServerRequestFactory::fromGlobals()->withParsedBody($body);
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());
$this->assertTrue(strpos($response->getBody()->getContents(), $shortUrl->toString(self::DOMAIN_CONFIG)) > 0);
$shorten->shouldHaveBeenCalledOnce();
}
public function provideRequestBodies(): iterable
{
$fullMeta = [
'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',
];
yield [['longUrl' => 'http://www.domain.com/foo/bar'], ShortUrlMeta::createEmpty()];
yield [$fullMeta, ShortUrlMeta::createFromRawData($fullMeta)];
2016-07-31 16:58:18 +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,
]);
$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
}