Created UpdateTagAction

This commit is contained in:
Alejandro Celaya
2017-07-15 12:04:12 +02:00
parent e07c464de8
commit 963d26f59b
7 changed files with 236 additions and 0 deletions

View File

@@ -22,6 +22,7 @@ return [
Action\Tag\ListTagsAction::class => AnnotatedFactory::class,
Action\Tag\DeleteTagsAction::class => AnnotatedFactory::class,
Action\Tag\CreateTagsAction::class => AnnotatedFactory::class,
Action\Tag\UpdateTagAction::class => AnnotatedFactory::class,
Middleware\BodyParserMiddleware::class => AnnotatedFactory::class,
Middleware\CrossDomainMiddleware::class => InvokableFactory::class,

View File

@@ -65,6 +65,12 @@ return [
'middleware' => Action\Tag\CreateTagsAction::class,
'allowed_methods' => [RequestMethod::METHOD_POST],
],
[
'name' => Action\Tag\UpdateTagAction::class,
'path' => '/rest/v{version:1}/tags',
'middleware' => Action\Tag\UpdateTagAction::class,
'allowed_methods' => [RequestMethod::METHOD_PUT],
],
],
];

View File

@@ -0,0 +1,83 @@
<?php
namespace Shlinkio\Shlink\Rest\Action\Tag;
use Acelaya\ZsmAnnotatedServices\Annotation as DI;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
use Shlinkio\Shlink\Core\Service\Tag\TagService;
use Shlinkio\Shlink\Core\Service\Tag\TagServiceInterface;
use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
use Shlinkio\Shlink\Rest\Util\RestUtils;
use Zend\Diactoros\Response\EmptyResponse;
use Zend\Diactoros\Response\JsonResponse;
use Zend\I18n\Translator\Translator;
use Zend\I18n\Translator\TranslatorInterface;
class UpdateTagAction extends AbstractRestAction
{
/**
* @var TagServiceInterface
*/
private $tagService;
/**
* @var TranslatorInterface
*/
private $translator;
/**
* UpdateTagAction constructor.
* @param TagServiceInterface $tagService
* @param TranslatorInterface $translator
* @param LoggerInterface|null $logger
*
* @DI\Inject({TagService::class, Translator::class, LoggerInterface::class})
*/
public function __construct(
TagServiceInterface $tagService,
TranslatorInterface $translator,
LoggerInterface $logger = null
) {
parent::__construct($logger);
$this->tagService = $tagService;
$this->translator = $translator;
}
/**
* Process an incoming server request and return a response, optionally delegating
* to the next middleware component to create the response.
*
* @param ServerRequestInterface $request
* @param DelegateInterface $delegate
*
* @return ResponseInterface
* @throws \InvalidArgumentException
*/
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
{
$body = $request->getParsedBody();
if (! isset($body['oldName'], $body['newName'])) {
return new JsonResponse([
'error' => RestUtils::INVALID_ARGUMENT_ERROR,
'message' => $this->translator->translate(
'You have to provide both \'oldName\' and \'newName\' params in order to properly rename the tag'
),
], self::STATUS_BAD_REQUEST);
}
try {
$this->tagService->renameTag($body['oldName'], $body['newName']);
return new EmptyResponse();
} catch (EntityDoesNotExistException $e) {
return new JsonResponse([
'error' => RestUtils::NOT_FOUND_ERROR,
'message' => sprintf(
$this->translator->translate('It wasn\'t possible to find a tag with name \'%s\''),
$body['oldName']
),
], self::STATUS_NOT_FOUND);
}
}
}

View File

@@ -0,0 +1,89 @@
<?php
namespace ShlinkioTest\Shlink\Rest\Action\Tag;
use Interop\Http\ServerMiddleware\DelegateInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\MethodProphecy;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Entity\Tag;
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
use Shlinkio\Shlink\Core\Service\Tag\TagServiceInterface;
use Shlinkio\Shlink\Rest\Action\Tag\UpdateTagAction;
use Zend\Diactoros\ServerRequestFactory;
use Zend\I18n\Translator\Translator;
class UpdateTagActionTest extends TestCase
{
/**
* @var UpdateTagAction
*/
private $action;
/**
* @var ObjectProphecy
*/
private $tagService;
public function setUp()
{
$this->tagService = $this->prophesize(TagServiceInterface::class);
$this->action = new UpdateTagAction($this->tagService->reveal(), Translator::factory([]));
}
/**
* @test
* @dataProvider provideParams
* @param array $bodyParams
*/
public function whenInvalidParamsAreProvidedAnErrorIsReturned(array $bodyParams)
{
$request = ServerRequestFactory::fromGlobals()->withParsedBody($bodyParams);
$resp = $this->action->process($request, $this->prophesize(DelegateInterface::class)->reveal());
$this->assertEquals(400, $resp->getStatusCode());
}
public function provideParams()
{
return [
[['oldName' => 'foo']],
[['newName' => 'foo']],
[[]],
];
}
/**
* @test
*/
public function requestingInvalidTagReturnsError()
{
$request = ServerRequestFactory::fromGlobals()->withParsedBody([
'oldName' => 'foo',
'newName' => 'bar',
]);
/** @var MethodProphecy $rename */
$rename = $this->tagService->renameTag('foo', 'bar')->willThrow(EntityDoesNotExistException::class);
$resp = $this->action->process($request, $this->prophesize(DelegateInterface::class)->reveal());
$this->assertEquals(404, $resp->getStatusCode());
$rename->shouldHaveBeenCalled();
}
/**
* @test
*/
public function correctInvocationRenamesTag()
{
$request = ServerRequestFactory::fromGlobals()->withParsedBody([
'oldName' => 'foo',
'newName' => 'bar',
]);
/** @var MethodProphecy $rename */
$rename = $this->tagService->renameTag('foo', 'bar')->willReturn(new Tag());
$resp = $this->action->process($request, $this->prophesize(DelegateInterface::class)->reveal());
$this->assertEquals(204, $resp->getStatusCode());
$rename->shouldHaveBeenCalled();
}
}