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

65 lines
2.1 KiB
PHP
Raw Normal View History

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