2018-05-03 10:57:43 -05:00
|
|
|
<?php
|
2019-10-05 10:26:10 -05:00
|
|
|
|
2018-05-03 10:57:43 -05:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-09-20 12:55:24 -05:00
|
|
|
namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl;
|
2018-05-03 10:57:43 -05:00
|
|
|
|
2020-01-01 14:11:53 -06:00
|
|
|
use Laminas\Diactoros\ServerRequest;
|
2023-02-09 13:42:18 -06:00
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2022-10-23 15:16:19 -05:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2018-05-03 10:57:43 -05:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2021-02-01 15:55:52 -06:00
|
|
|
use Shlinkio\Shlink\Common\Rest\DataTransformerInterface;
|
2022-08-05 01:38:05 -05:00
|
|
|
use Shlinkio\Shlink\Core\Options\UrlShortenerOptions;
|
2022-09-23 12:03:32 -05:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
2022-09-23 11:05:17 -05:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlCreation;
|
2023-04-12 02:15:36 -05:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Model\UrlShorteningResult;
|
2022-09-23 11:42:38 -05:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\UrlShortenerInterface;
|
2018-09-20 12:55:24 -05:00
|
|
|
use Shlinkio\Shlink\Rest\Action\ShortUrl\SingleStepCreateShortUrlAction;
|
2020-11-08 04:28:27 -06:00
|
|
|
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
2018-05-03 10:57:43 -05:00
|
|
|
|
2018-09-20 12:55:24 -05:00
|
|
|
class SingleStepCreateShortUrlActionTest extends TestCase
|
2018-05-03 10:57:43 -05:00
|
|
|
{
|
2019-12-29 15:48:40 -06:00
|
|
|
private SingleStepCreateShortUrlAction $action;
|
2022-10-24 12:53:13 -05:00
|
|
|
private MockObject & UrlShortenerInterface $urlShortener;
|
2018-05-03 10:57:43 -05:00
|
|
|
|
2022-09-11 05:02:49 -05:00
|
|
|
protected function setUp(): void
|
2018-05-03 10:57:43 -05:00
|
|
|
{
|
2022-10-23 15:16:19 -05:00
|
|
|
$this->urlShortener = $this->createMock(UrlShortenerInterface::class);
|
|
|
|
$transformer = $this->createMock(DataTransformerInterface::class);
|
|
|
|
$transformer->method('transform')->willReturn([]);
|
2018-05-03 10:57:43 -05:00
|
|
|
|
2018-09-20 12:55:24 -05:00
|
|
|
$this->action = new SingleStepCreateShortUrlAction(
|
2022-10-23 15:16:19 -05:00
|
|
|
$this->urlShortener,
|
|
|
|
$transformer,
|
2022-08-05 01:38:05 -05:00
|
|
|
new UrlShortenerOptions(),
|
2018-05-03 10:57:43 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test]
|
2019-11-21 13:05:06 -06:00
|
|
|
public function properDataIsPassedWhenGeneratingShortCode(): void
|
2018-05-03 10:57:43 -05:00
|
|
|
{
|
2021-03-14 03:59:35 -05:00
|
|
|
$apiKey = ApiKey::create();
|
2020-11-08 04:28:27 -06:00
|
|
|
|
2018-12-25 16:01:30 -06:00
|
|
|
$request = (new ServerRequest())->withQueryParams([
|
2018-05-03 10:57:43 -05:00
|
|
|
'longUrl' => 'http://foobar.com',
|
2021-01-21 12:26:19 -06:00
|
|
|
])->withAttribute(ApiKey::class, $apiKey);
|
2022-10-23 15:16:19 -05:00
|
|
|
$this->urlShortener->expects($this->once())->method('shorten')->with(
|
2022-09-23 11:05:17 -05:00
|
|
|
ShortUrlCreation::fromRawData(['apiKey' => $apiKey, 'longUrl' => 'http://foobar.com']),
|
2023-04-12 02:15:36 -05:00
|
|
|
)->willReturn(UrlShorteningResult::withoutErrorOnEventDispatching(ShortUrl::createFake()));
|
2018-05-03 10:57:43 -05:00
|
|
|
|
|
|
|
$resp = $this->action->handle($request);
|
|
|
|
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertEquals(200, $resp->getStatusCode());
|
2018-05-03 10:57:43 -05:00
|
|
|
}
|
|
|
|
}
|