2018-05-03 18:57:43 +03:00
|
|
|
<?php
|
|
|
|
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-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;
|
|
|
|
use Zend\Diactoros\Response\JsonResponse;
|
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
|
|
|
{
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var SingleStepCreateShortUrlAction */
|
2018-05-03 18:57:43 +03:00
|
|
|
private $action;
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var ObjectProphecy */
|
2018-05-03 18:57:43 +03:00
|
|
|
private $urlShortener;
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var ObjectProphecy */
|
2018-05-03 18:57:43 +03:00
|
|
|
private $apiKeyService;
|
|
|
|
|
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',
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2018-09-28 23:08:01 +03:00
|
|
|
public function errorResponseIsReturnedIfInvalidApiKeyIsProvided()
|
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
|
|
|
|
|
|
|
/** @var JsonResponse $resp */
|
|
|
|
$resp = $this->action->handle($request);
|
|
|
|
$payload = $resp->getPayload();
|
|
|
|
|
|
|
|
$this->assertEquals(400, $resp->getStatusCode());
|
|
|
|
$this->assertEquals('INVALID_ARGUMENT', $payload['error']);
|
|
|
|
$this->assertEquals('No API key was provided or it is not valid', $payload['message']);
|
|
|
|
$findApiKey->shouldHaveBeenCalled();
|
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2018-05-03 18:57:43 +03:00
|
|
|
public function errorResponseIsReturnedIfNoUrlIsProvided()
|
|
|
|
{
|
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
|
|
|
|
|
|
|
/** @var JsonResponse $resp */
|
|
|
|
$resp = $this->action->handle($request);
|
|
|
|
$payload = $resp->getPayload();
|
|
|
|
|
|
|
|
$this->assertEquals(400, $resp->getStatusCode());
|
|
|
|
$this->assertEquals('INVALID_ARGUMENT', $payload['error']);
|
|
|
|
$this->assertEquals('A URL was not provided', $payload['message']);
|
|
|
|
$findApiKey->shouldHaveBeenCalled();
|
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2018-05-03 18:57:43 +03:00
|
|
|
public function properDataIsPassedWhenGeneratingShortCode()
|
|
|
|
{
|
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;
|
|
|
|
}),
|
|
|
|
[],
|
2019-01-29 15:55:47 +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();
|
|
|
|
}
|
|
|
|
}
|