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\TestCase;
|
2021-02-02 00:55:52 +03:00
|
|
|
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;
|
2021-02-02 00:55:52 +03:00
|
|
|
use Shlinkio\Shlink\Common\Rest\DataTransformerInterface;
|
2022-08-05 09:38:05 +03:00
|
|
|
use Shlinkio\Shlink\Core\Options\UrlShortenerOptions;
|
2022-09-23 20:03:32 +03:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
2022-09-23 19:05:17 +03:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlCreation;
|
2022-09-23 19:42:38 +03:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\UrlShortenerInterface;
|
2018-09-20 20:55:24 +03:00
|
|
|
use Shlinkio\Shlink\Rest\Action\ShortUrl\SingleStepCreateShortUrlAction;
|
2020-11-08 13:28:27 +03:00
|
|
|
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
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
|
|
|
{
|
2020-11-02 13:50:19 +03:00
|
|
|
use ProphecyTrait;
|
|
|
|
|
2019-12-30 00:48:40 +03:00
|
|
|
private SingleStepCreateShortUrlAction $action;
|
|
|
|
private ObjectProphecy $urlShortener;
|
2021-02-02 00:55:52 +03:00
|
|
|
private ObjectProphecy $transformer;
|
2018-05-03 18:57:43 +03:00
|
|
|
|
2022-09-11 13:02:49 +03:00
|
|
|
protected function setUp(): void
|
2018-05-03 18:57:43 +03:00
|
|
|
{
|
|
|
|
$this->urlShortener = $this->prophesize(UrlShortenerInterface::class);
|
2021-02-02 00:55:52 +03:00
|
|
|
$this->transformer = $this->prophesize(DataTransformerInterface::class);
|
|
|
|
$this->transformer->transform(Argument::type(ShortUrl::class))->willReturn([]);
|
2018-05-03 18:57:43 +03:00
|
|
|
|
2018-09-20 20:55:24 +03:00
|
|
|
$this->action = new SingleStepCreateShortUrlAction(
|
2018-05-03 18:57:43 +03:00
|
|
|
$this->urlShortener->reveal(),
|
2021-02-02 00:55:52 +03:00
|
|
|
$this->transformer->reveal(),
|
2022-08-05 09:38:05 +03:00
|
|
|
new UrlShortenerOptions(),
|
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
|
|
|
{
|
2021-03-14 11:59:35 +03:00
|
|
|
$apiKey = ApiKey::create();
|
2020-11-08 13:28:27 +03:00
|
|
|
|
2018-12-26 01:01:30 +03:00
|
|
|
$request = (new ServerRequest())->withQueryParams([
|
2018-05-03 18:57:43 +03:00
|
|
|
'longUrl' => 'http://foobar.com',
|
2021-01-21 21:26:19 +03:00
|
|
|
])->withAttribute(ApiKey::class, $apiKey);
|
2020-11-06 22:05:57 +03:00
|
|
|
$generateShortCode = $this->urlShortener->shorten(
|
2022-09-23 19:05:17 +03:00
|
|
|
ShortUrlCreation::fromRawData(['apiKey' => $apiKey, 'longUrl' => 'http://foobar.com']),
|
2021-01-30 16:18:44 +03:00
|
|
|
)->willReturn(ShortUrl::createEmpty());
|
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
|
|
|
$generateShortCode->shouldHaveBeenCalled();
|
|
|
|
}
|
|
|
|
}
|