2018-01-07 22:45:05 +03:00
|
|
|
<?php
|
|
|
|
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
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Prophecy\Argument;
|
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
|
|
|
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
|
|
|
|
use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface;
|
2018-09-20 20:55:24 +03:00
|
|
|
use Shlinkio\Shlink\Rest\Action\ShortUrl\EditShortUrlAction;
|
2018-01-07 22:45:05 +03:00
|
|
|
use Shlinkio\Shlink\Rest\Util\RestUtils;
|
|
|
|
use Zend\Diactoros\Response\JsonResponse;
|
2018-12-26 01:01:30 +03:00
|
|
|
use Zend\Diactoros\ServerRequest;
|
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
|
|
|
{
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var EditShortUrlAction */
|
2018-01-07 22:45:05 +03:00
|
|
|
private $action;
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var ObjectProphecy */
|
2018-01-07 22:45:05 +03:00
|
|
|
private $shortUrlService;
|
|
|
|
|
2019-02-16 12:53:45 +03:00
|
|
|
public function setUp(): void
|
2018-01-07 22:45:05 +03:00
|
|
|
{
|
|
|
|
$this->shortUrlService = $this->prophesize(ShortUrlServiceInterface::class);
|
2018-11-18 18:28:04 +03:00
|
|
|
$this->action = new EditShortUrlAction($this->shortUrlService->reveal());
|
2018-01-07 22:45:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function invalidDataReturnsError()
|
|
|
|
{
|
2018-12-26 01:01:30 +03:00
|
|
|
$request = (new ServerRequest())->withParsedBody([
|
2018-01-07 22:45:05 +03:00
|
|
|
'maxVisits' => 'invalid',
|
|
|
|
]);
|
|
|
|
|
|
|
|
/** @var JsonResponse $resp */
|
2018-03-26 20:02:41 +03:00
|
|
|
$resp = $this->action->handle($request);
|
2018-01-07 22:45:05 +03:00
|
|
|
$payload = $resp->getPayload();
|
|
|
|
|
|
|
|
$this->assertEquals(400, $resp->getStatusCode());
|
|
|
|
$this->assertEquals(RestUtils::INVALID_ARGUMENT_ERROR, $payload['error']);
|
|
|
|
$this->assertEquals('Provided data is invalid.', $payload['message']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function incorrectShortCodeReturnsError()
|
|
|
|
{
|
2018-12-26 01:01:30 +03:00
|
|
|
$request = (new ServerRequest())->withAttribute('shortCode', 'abc123')
|
|
|
|
->withParsedBody([
|
|
|
|
'maxVisits' => 5,
|
|
|
|
]);
|
2018-01-07 22:45:05 +03:00
|
|
|
$updateMeta = $this->shortUrlService->updateMetadataByShortCode(Argument::cetera())->willThrow(
|
|
|
|
InvalidShortCodeException::class
|
|
|
|
);
|
|
|
|
|
|
|
|
/** @var JsonResponse $resp */
|
2018-03-26 20:02:41 +03:00
|
|
|
$resp = $this->action->handle($request);
|
2018-01-07 22:45:05 +03:00
|
|
|
$payload = $resp->getPayload();
|
|
|
|
|
|
|
|
$this->assertEquals(404, $resp->getStatusCode());
|
|
|
|
$this->assertEquals(RestUtils::INVALID_SHORTCODE_ERROR, $payload['error']);
|
|
|
|
$this->assertEquals('No URL found for short code "abc123"', $payload['message']);
|
|
|
|
$updateMeta->shouldHaveBeenCalled();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function correctShortCodeReturnsSuccess()
|
|
|
|
{
|
2018-12-26 01:01:30 +03:00
|
|
|
$request = (new ServerRequest())->withAttribute('shortCode', 'abc123')
|
|
|
|
->withParsedBody([
|
|
|
|
'maxVisits' => 5,
|
|
|
|
]);
|
2018-10-28 18:00:54 +03:00
|
|
|
$updateMeta = $this->shortUrlService->updateMetadataByShortCode(Argument::cetera())->willReturn(
|
|
|
|
new ShortUrl('')
|
|
|
|
);
|
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
|
|
|
|
|
|
|
$this->assertEquals(204, $resp->getStatusCode());
|
|
|
|
$updateMeta->shouldHaveBeenCalled();
|
|
|
|
}
|
|
|
|
}
|