2018-01-07 13:45:05 -06:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-09-20 12:55:24 -05:00
|
|
|
namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl;
|
2018-01-07 13:45:05 -06: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 12:55:24 -05:00
|
|
|
use Shlinkio\Shlink\Rest\Action\ShortUrl\EditShortUrlAction;
|
2018-01-07 13:45:05 -06:00
|
|
|
use Shlinkio\Shlink\Rest\Util\RestUtils;
|
|
|
|
use Zend\Diactoros\Response\JsonResponse;
|
|
|
|
use Zend\Diactoros\ServerRequestFactory;
|
|
|
|
use Zend\I18n\Translator\Translator;
|
|
|
|
|
2018-09-20 12:55:24 -05:00
|
|
|
class EditShortUrlActionTest extends TestCase
|
2018-01-07 13:45:05 -06:00
|
|
|
{
|
|
|
|
/**
|
2018-09-20 12:55:24 -05:00
|
|
|
* @var EditShortUrlAction
|
2018-01-07 13:45:05 -06:00
|
|
|
*/
|
|
|
|
private $action;
|
|
|
|
/**
|
|
|
|
* @var ObjectProphecy
|
|
|
|
*/
|
|
|
|
private $shortUrlService;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$this->shortUrlService = $this->prophesize(ShortUrlServiceInterface::class);
|
2018-09-20 12:55:24 -05:00
|
|
|
$this->action = new EditShortUrlAction($this->shortUrlService->reveal(), Translator::factory([]));
|
2018-01-07 13:45:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function invalidDataReturnsError()
|
|
|
|
{
|
|
|
|
$request = ServerRequestFactory::fromGlobals()->withParsedBody([
|
|
|
|
'maxVisits' => 'invalid',
|
|
|
|
]);
|
|
|
|
|
|
|
|
/** @var JsonResponse $resp */
|
2018-03-26 12:02:41 -05:00
|
|
|
$resp = $this->action->handle($request);
|
2018-01-07 13:45:05 -06: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()
|
|
|
|
{
|
|
|
|
$request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', 'abc123')
|
|
|
|
->withParsedBody([
|
|
|
|
'maxVisits' => 5,
|
|
|
|
]);
|
|
|
|
$updateMeta = $this->shortUrlService->updateMetadataByShortCode(Argument::cetera())->willThrow(
|
|
|
|
InvalidShortCodeException::class
|
|
|
|
);
|
|
|
|
|
|
|
|
/** @var JsonResponse $resp */
|
2018-03-26 12:02:41 -05:00
|
|
|
$resp = $this->action->handle($request);
|
2018-01-07 13:45:05 -06: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()
|
|
|
|
{
|
|
|
|
$request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', 'abc123')
|
|
|
|
->withParsedBody([
|
|
|
|
'maxVisits' => 5,
|
|
|
|
]);
|
2018-10-28 10:00:54 -05:00
|
|
|
$updateMeta = $this->shortUrlService->updateMetadataByShortCode(Argument::cetera())->willReturn(
|
|
|
|
new ShortUrl('')
|
|
|
|
);
|
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
|
|
|
|
|
|
|
$this->assertEquals(204, $resp->getStatusCode());
|
|
|
|
$updateMeta->shouldHaveBeenCalled();
|
|
|
|
}
|
|
|
|
}
|