2016-07-31 16:24:00 +02:00
|
|
|
<?php
|
2019-10-05 17:26:10 +02:00
|
|
|
|
2017-10-12 10:13:20 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2018-09-20 19:55:24 +02:00
|
|
|
namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl;
|
2016-07-31 16:24:00 +02:00
|
|
|
|
2020-01-01 21:11:53 +01:00
|
|
|
use Laminas\Diactoros\ServerRequest;
|
2023-02-09 20:42:18 +01:00
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2022-10-23 22:14:28 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2017-03-24 20:34:18 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2022-09-23 19:03:32 +02:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
2021-02-01 22:55:52 +01:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier;
|
2022-09-23 18:05:17 +02:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
|
2022-09-23 18:42:38 +02:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\ShortUrlResolverInterface;
|
2021-02-01 22:55:52 +01:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Transformer\ShortUrlDataTransformer;
|
2018-09-20 19:55:24 +02:00
|
|
|
use Shlinkio\Shlink\Rest\Action\ShortUrl\ResolveShortUrlAction;
|
2021-01-03 13:33:07 +01:00
|
|
|
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
2019-02-26 22:56:43 +01:00
|
|
|
|
2018-09-20 19:55:24 +02:00
|
|
|
class ResolveShortUrlActionTest extends TestCase
|
2016-07-31 16:24:00 +02:00
|
|
|
{
|
2019-12-29 22:48:40 +01:00
|
|
|
private ResolveShortUrlAction $action;
|
2026-05-22 08:42:47 +02:00
|
|
|
private MockObject&ShortUrlResolverInterface $urlResolver;
|
2016-07-31 16:24:00 +02:00
|
|
|
|
2022-09-11 12:02:49 +02:00
|
|
|
protected function setUp(): void
|
2016-07-31 16:24:00 +02:00
|
|
|
{
|
2022-10-23 22:14:28 +02:00
|
|
|
$this->urlResolver = $this->createMock(ShortUrlResolverInterface::class);
|
2026-05-22 08:42:47 +02:00
|
|
|
$this->action = new ResolveShortUrlAction(
|
|
|
|
|
$this->urlResolver,
|
|
|
|
|
new ShortUrlDataTransformer(
|
|
|
|
|
new ShortUrlStringifier(),
|
|
|
|
|
),
|
|
|
|
|
);
|
2016-07-31 16:24:00 +02:00
|
|
|
}
|
|
|
|
|
|
2023-02-09 20:42:18 +01:00
|
|
|
#[Test]
|
2019-10-04 21:17:02 +02:00
|
|
|
public function correctShortCodeReturnsSuccess(): void
|
2016-07-31 16:24:00 +02:00
|
|
|
{
|
|
|
|
|
$shortCode = 'abc123';
|
2021-03-14 09:59:35 +01:00
|
|
|
$apiKey = ApiKey::create();
|
2026-05-22 08:42:47 +02:00
|
|
|
$this->urlResolver
|
|
|
|
|
->expects($this->once())
|
|
|
|
|
->method('resolveShortUrl')
|
|
|
|
|
->with(
|
|
|
|
|
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
|
|
|
|
|
$apiKey,
|
|
|
|
|
)
|
|
|
|
|
->willReturn(ShortUrl::withLongUrl('http://domain.com/foo/bar'));
|
|
|
|
|
|
2026-05-22 18:09:59 +02:00
|
|
|
$request = new ServerRequest()
|
|
|
|
|
->withAttribute('shortCode', $shortCode)
|
2026-05-22 08:42:47 +02:00
|
|
|
->withAttribute(ApiKey::class, $apiKey);
|
2018-03-26 19:02:41 +02:00
|
|
|
$response = $this->action->handle($request);
|
2021-01-03 13:33:07 +01:00
|
|
|
|
2020-10-04 00:35:14 +02:00
|
|
|
self::assertEquals(200, $response->getStatusCode());
|
2021-05-23 12:37:53 +02:00
|
|
|
self::assertStringContainsString('http://domain.com/foo/bar', $response->getBody()->getContents());
|
2016-07-31 16:24:00 +02:00
|
|
|
}
|
|
|
|
|
}
|