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