2018-05-03 18:57:43 +03:00
|
|
|
<?php
|
2019-10-05 18:26:10 +03:00
|
|
|
|
2018-05-03 18:57:43 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-09-20 20:55:24 +03:00
|
|
|
namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl;
|
2018-05-03 18:57:43 +03:00
|
|
|
|
|
|
|
use PHPUnit\Framework\Assert;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Prophecy\Argument;
|
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
|
|
|
use Psr\Http\Message\UriInterface;
|
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;
|
2019-01-29 15:55:47 +03:00
|
|
|
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
2018-05-03 18:57:43 +03:00
|
|
|
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
|
2018-09-20 20:55:24 +03:00
|
|
|
use Shlinkio\Shlink\Rest\Action\ShortUrl\SingleStepCreateShortUrlAction;
|
2018-05-03 18:57:43 +03:00
|
|
|
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
|
2018-12-26 01:01:30 +03:00
|
|
|
use Zend\Diactoros\ServerRequest;
|
2018-05-03 18:57:43 +03:00
|
|
|
|
2018-09-20 20:55:24 +03:00
|
|
|
class SingleStepCreateShortUrlActionTest extends TestCase
|
2018-05-03 18:57:43 +03:00
|
|
|
{
|
2019-12-30 00:48:40 +03:00
|
|
|
private SingleStepCreateShortUrlAction $action;
|
|
|
|
private ObjectProphecy $urlShortener;
|
|
|
|
private ObjectProphecy $apiKeyService;
|
2018-05-03 18:57:43 +03:00
|
|
|
|
2019-02-16 12:53:45 +03:00
|
|
|
public function setUp(): void
|
2018-05-03 18:57:43 +03:00
|
|
|
{
|
|
|
|
$this->urlShortener = $this->prophesize(UrlShortenerInterface::class);
|
|
|
|
$this->apiKeyService = $this->prophesize(ApiKeyServiceInterface::class);
|
|
|
|
|
2018-09-20 20:55:24 +03:00
|
|
|
$this->action = new SingleStepCreateShortUrlAction(
|
2018-05-03 18:57:43 +03:00
|
|
|
$this->urlShortener->reveal(),
|
|
|
|
$this->apiKeyService->reveal(),
|
|
|
|
[
|
|
|
|
'schema' => 'http',
|
|
|
|
'hostname' => 'foo.com',
|
2020-01-01 22:48:31 +03:00
|
|
|
],
|
2018-05-03 18:57:43 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2019-11-21 22:05:06 +03:00
|
|
|
public function errorResponseIsReturnedIfInvalidApiKeyIsProvided(): void
|
2018-05-03 18:57:43 +03:00
|
|
|
{
|
2018-12-26 01:01:30 +03:00
|
|
|
$request = (new ServerRequest())->withQueryParams(['apiKey' => 'abc123']);
|
2018-09-28 23:08:01 +03:00
|
|
|
$findApiKey = $this->apiKeyService->check('abc123')->willReturn(false);
|
2018-05-03 18:57:43 +03:00
|
|
|
|
2019-11-25 01:45:40 +03:00
|
|
|
$this->expectException(ValidationException::class);
|
|
|
|
$findApiKey->shouldBeCalledOnce();
|
2018-05-03 18:57:43 +03:00
|
|
|
|
2019-11-25 01:45:40 +03:00
|
|
|
$this->action->handle($request);
|
2018-05-03 18:57:43 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2019-11-21 22:05:06 +03:00
|
|
|
public function errorResponseIsReturnedIfNoUrlIsProvided(): void
|
2018-05-03 18:57:43 +03:00
|
|
|
{
|
2018-12-26 01:01:30 +03:00
|
|
|
$request = (new ServerRequest())->withQueryParams(['apiKey' => 'abc123']);
|
2018-09-28 23:08:01 +03:00
|
|
|
$findApiKey = $this->apiKeyService->check('abc123')->willReturn(true);
|
2018-05-03 18:57:43 +03:00
|
|
|
|
2019-11-25 01:45:40 +03:00
|
|
|
$this->expectException(ValidationException::class);
|
|
|
|
$findApiKey->shouldBeCalledOnce();
|
2018-05-03 18:57:43 +03:00
|
|
|
|
2019-11-25 01:45:40 +03:00
|
|
|
$this->action->handle($request);
|
2018-05-03 18:57:43 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2019-11-21 22:05:06 +03:00
|
|
|
public function properDataIsPassedWhenGeneratingShortCode(): void
|
2018-05-03 18:57:43 +03:00
|
|
|
{
|
2018-12-26 01:01:30 +03:00
|
|
|
$request = (new ServerRequest())->withQueryParams([
|
2018-05-03 18:57:43 +03:00
|
|
|
'apiKey' => 'abc123',
|
|
|
|
'longUrl' => 'http://foobar.com',
|
|
|
|
]);
|
2018-09-28 23:08:01 +03:00
|
|
|
$findApiKey = $this->apiKeyService->check('abc123')->willReturn(true);
|
2018-05-03 18:57:43 +03:00
|
|
|
$generateShortCode = $this->urlShortener->urlToShortCode(
|
|
|
|
Argument::that(function (UriInterface $argument) {
|
|
|
|
Assert::assertEquals('http://foobar.com', (string) $argument);
|
|
|
|
return $argument;
|
|
|
|
}),
|
|
|
|
[],
|
2020-01-01 22:48:31 +03:00
|
|
|
ShortUrlMeta::createEmpty(),
|
2018-10-28 18:00:54 +03:00
|
|
|
)->willReturn(new ShortUrl(''));
|
2018-05-03 18:57:43 +03:00
|
|
|
|
|
|
|
$resp = $this->action->handle($request);
|
|
|
|
|
|
|
|
$this->assertEquals(200, $resp->getStatusCode());
|
|
|
|
$findApiKey->shouldHaveBeenCalled();
|
|
|
|
$generateShortCode->shouldHaveBeenCalled();
|
|
|
|
}
|
|
|
|
}
|