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
|
|
|
|
2020-01-01 23:11:53 +03:00
|
|
|
use Laminas\Diactoros\ServerRequest;
|
2018-05-03 18:57:43 +03:00
|
|
|
use PHPUnit\Framework\Assert;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Prophecy\Argument;
|
2020-11-02 13:50:19 +03:00
|
|
|
use Prophecy\PhpUnit\ProphecyTrait;
|
2018-05-03 18:57:43 +03:00
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
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-09-20 20:55:24 +03:00
|
|
|
class SingleStepCreateShortUrlActionTest extends TestCase
|
2018-05-03 18:57:43 +03:00
|
|
|
{
|
2020-11-02 13:50:19 +03:00
|
|
|
use ProphecyTrait;
|
|
|
|
|
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);
|
2020-11-06 22:05:57 +03:00
|
|
|
$generateShortCode = $this->urlShortener->shorten(
|
2020-06-27 12:09:56 +03:00
|
|
|
Argument::that(function (string $argument): string {
|
|
|
|
Assert::assertEquals('http://foobar.com', $argument);
|
2018-05-03 18:57:43 +03:00
|
|
|
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);
|
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertEquals(200, $resp->getStatusCode());
|
2018-05-03 18:57:43 +03:00
|
|
|
$findApiKey->shouldHaveBeenCalled();
|
|
|
|
$generateShortCode->shouldHaveBeenCalled();
|
|
|
|
}
|
|
|
|
}
|