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);
|
|
|
|
|
2018-09-20 20:55:24 +03:00
|
|
|
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;
|
2018-01-07 22:45:05 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Prophecy\Argument;
|
2020-11-02 13:50:19 +03:00
|
|
|
use Prophecy\PhpUnit\ProphecyTrait;
|
2018-01-07 22:45:05 +03:00
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
2019-11-25 01:45:40 +03:00
|
|
|
use Shlinkio\Shlink\Core\Exception\ValidationException;
|
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:42:38 +03:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\ShortUrlServiceInterface;
|
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\EditShortUrlAction;
|
2021-01-03 18:41:44 +03:00
|
|
|
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
2018-01-07 22:45:05 +03:00
|
|
|
|
2018-09-20 20:55:24 +03:00
|
|
|
class EditShortUrlActionTest extends TestCase
|
2018-01-07 22:45:05 +03:00
|
|
|
{
|
2020-11-02 13:50:19 +03:00
|
|
|
use ProphecyTrait;
|
|
|
|
|
2019-12-30 00:48:40 +03:00
|
|
|
private EditShortUrlAction $action;
|
|
|
|
private ObjectProphecy $shortUrlService;
|
2018-01-07 22:45:05 +03:00
|
|
|
|
2022-09-11 13:02:49 +03:00
|
|
|
protected function setUp(): void
|
2018-01-07 22:45:05 +03:00
|
|
|
{
|
|
|
|
$this->shortUrlService = $this->prophesize(ShortUrlServiceInterface::class);
|
2021-02-02 00:55:52 +03:00
|
|
|
$this->action = new EditShortUrlAction($this->shortUrlService->reveal(), new ShortUrlDataTransformer(
|
|
|
|
new ShortUrlStringifier([]),
|
|
|
|
));
|
2018-01-07 22:45:05 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2019-11-25 01:45:40 +03:00
|
|
|
public function invalidDataThrowsError(): void
|
2018-01-07 22:45:05 +03:00
|
|
|
{
|
2018-12-26 01:01:30 +03:00
|
|
|
$request = (new ServerRequest())->withParsedBody([
|
2018-01-07 22:45:05 +03:00
|
|
|
'maxVisits' => 'invalid',
|
|
|
|
]);
|
|
|
|
|
2019-11-25 01:45:40 +03:00
|
|
|
$this->expectException(ValidationException::class);
|
2018-01-07 22:45:05 +03:00
|
|
|
|
2019-11-25 01:45:40 +03:00
|
|
|
$this->action->handle($request);
|
2018-01-07 22:45:05 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2019-11-24 14:41:12 +03:00
|
|
|
public function correctShortCodeReturnsSuccess(): void
|
2018-01-07 22:45:05 +03:00
|
|
|
{
|
2018-12-26 01:01:30 +03:00
|
|
|
$request = (new ServerRequest())->withAttribute('shortCode', 'abc123')
|
2021-03-14 11:59:35 +03:00
|
|
|
->withAttribute(ApiKey::class, ApiKey::create())
|
2018-12-26 01:01:30 +03:00
|
|
|
->withParsedBody([
|
|
|
|
'maxVisits' => 5,
|
|
|
|
]);
|
2021-01-31 12:53:18 +03:00
|
|
|
$updateMeta = $this->shortUrlService->updateShortUrl(Argument::cetera())->willReturn(
|
2021-01-30 16:18:44 +03:00
|
|
|
ShortUrl::createEmpty(),
|
2018-10-28 18:00:54 +03:00
|
|
|
);
|
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
|
|
|
|
2021-01-31 15:21:23 +03:00
|
|
|
self::assertEquals(200, $resp->getStatusCode());
|
2018-01-07 22:45:05 +03:00
|
|
|
$updateMeta->shouldHaveBeenCalled();
|
|
|
|
}
|
|
|
|
}
|