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