shlink/module/Rest/test/Action/ShortUrl/EditShortUrlActionTest.php

60 lines
2.1 KiB
PHP
Raw Normal View History

2018-01-07 22:45:05 +03:00
<?php
2019-10-05 18:26:10 +03:00
2018-01-07 22:45:05 +03:00
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl;
2018-01-07 22:45:05 +03:00
2020-01-01 23:11:53 +03:00
use Laminas\Diactoros\ServerRequest;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
2018-01-07 22:45:05 +03:00
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Core\Exception\ValidationException;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier;
use Shlinkio\Shlink\Core\ShortUrl\ShortUrlServiceInterface;
use Shlinkio\Shlink\Core\ShortUrl\Transformer\ShortUrlDataTransformer;
use Shlinkio\Shlink\Rest\Action\ShortUrl\EditShortUrlAction;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
2018-01-07 22:45:05 +03:00
class EditShortUrlActionTest extends TestCase
2018-01-07 22:45:05 +03:00
{
private EditShortUrlAction $action;
2022-10-24 20:53:13 +03:00
private MockObject & ShortUrlServiceInterface $shortUrlService;
2018-01-07 22:45:05 +03:00
protected function setUp(): void
2018-01-07 22:45:05 +03:00
{
$this->shortUrlService = $this->createMock(ShortUrlServiceInterface::class);
$this->action = new EditShortUrlAction($this->shortUrlService, new ShortUrlDataTransformer(
new ShortUrlStringifier([]),
));
2018-01-07 22:45:05 +03:00
}
#[Test]
public function invalidDataThrowsError(): void
2018-01-07 22:45:05 +03:00
{
$request = (new ServerRequest())->withParsedBody([
2018-01-07 22:45:05 +03:00
'maxVisits' => 'invalid',
]);
$this->shortUrlService->expects($this->never())->method('updateShortUrl');
2018-01-07 22:45:05 +03:00
$this->expectException(ValidationException::class);
2018-01-07 22:45:05 +03:00
$this->action->handle($request);
2018-01-07 22:45:05 +03:00
}
#[Test]
public function correctShortCodeReturnsSuccess(): void
2018-01-07 22:45:05 +03:00
{
$request = (new ServerRequest())->withAttribute('shortCode', 'abc123')
->withAttribute(ApiKey::class, ApiKey::create())
->withParsedBody([
'maxVisits' => 5,
]);
$this->shortUrlService->expects($this->once())->method('updateShortUrl')->willReturn(ShortUrl::createFake());
2018-01-07 22:45:05 +03:00
2018-03-26 20:02:41 +03:00
$resp = $this->action->handle($request);
2018-01-07 22:45:05 +03:00
self::assertEquals(200, $resp->getStatusCode());
2018-01-07 22:45:05 +03:00
}
}