Files

87 lines
3.1 KiB
PHP
Raw Permalink Normal View History

2016-07-31 15:58:18 +02:00
<?php
2019-10-05 17:26:10 +02:00
2017-10-12 10:13:20 +02:00
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl;
2016-07-31 15:58:18 +02:00
use Cake\Chronos\Chronos;
use CuyZ\Valinor\MapperBuilder;
use Laminas\Diactoros\Response\JsonResponse;
2020-01-01 21:11:53 +01:00
use Laminas\Diactoros\ServerRequestFactory;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
2017-03-24 20:34:18 +01:00
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlCreation;
use Shlinkio\Shlink\Core\ShortUrl\Model\UrlShorteningResult;
use Shlinkio\Shlink\Core\ShortUrl\Transformer\ShortUrlDataTransformerInterface;
use Shlinkio\Shlink\Core\ShortUrl\UrlShortener;
use Shlinkio\Shlink\Rest\Action\ShortUrl\CreateShortUrlAction;
2020-11-08 11:28:27 +01:00
use Shlinkio\Shlink\Rest\Entity\ApiKey;
2019-02-26 22:56:43 +01:00
class CreateShortUrlActionTest extends TestCase
2016-07-31 15:58:18 +02:00
{
private CreateShortUrlAction $action;
private MockObject&UrlShortener $urlShortener;
private MockObject&ShortUrlDataTransformerInterface $transformer;
2016-07-31 15:58:18 +02:00
protected function setUp(): void
2016-07-31 15:58:18 +02:00
{
$this->urlShortener = $this->createMock(UrlShortener::class);
$this->transformer = $this->createMock(ShortUrlDataTransformerInterface::class);
2026-05-12 08:51:08 +02:00
$mapper = new MapperBuilder()->mapper();
2026-05-12 08:51:08 +02:00
$this->action = new CreateShortUrlAction($this->urlShortener, $this->transformer, $mapper);
2016-07-31 15:58:18 +02:00
}
#[Test]
public function properShortcodeConversionReturnsData(): void
2016-07-31 15:58:18 +02:00
{
$now = Chronos::now()->microsecond(0);
$apiKey = ApiKey::create();
$shortUrl = ShortUrl::createFake();
$expectedCreation = new ShortUrlCreation(
longUrl: 'http://www.domain.com/foo/bar',
validSince: $now,
validUntil: $now,
customSlug: 'foo-bar-baz',
maxVisits: 50,
findIfExists: true,
domain: 'my-domain.com',
apiKey: $apiKey,
);
$body = [
'longUrl' => $expectedCreation->longUrl,
'validSince' => $now->toAtomString(),
'validUntil' => $now->toAtomString(),
'customSlug' => $expectedCreation->customSlug,
'maxVisits' => $expectedCreation->maxVisits,
'findIfExists' => $expectedCreation->findIfExists,
'domain' => $expectedCreation->domain,
];
2020-11-08 11:28:27 +01:00
$this->urlShortener
->expects($this->once())
->method('shorten')
->with(
$expectedCreation,
)
->willReturn(UrlShorteningResult::withoutErrorOnEventDispatching($shortUrl));
$this->transformer
->expects($this->once())
->method('transform')
->with($shortUrl)
->willReturn(
['shortUrl' => 'stringified_short_url'],
);
2016-07-31 15:58:18 +02:00
2020-11-08 11:28:27 +01:00
$request = ServerRequestFactory::fromGlobals()->withParsedBody($body)->withAttribute(ApiKey::class, $apiKey);
/** @var JsonResponse $response */
2018-03-26 19:02:41 +02:00
$response = $this->action->handle($request);
$payload = $response->getPayload();
2020-10-04 00:35:14 +02:00
self::assertEquals(200, $response->getStatusCode());
self::assertEquals('stringified_short_url', $payload['shortUrl']);
}
2016-07-31 15:58:18 +02:00
}