mirror of
https://github.com/shlinkio/shlink.git
synced 2024-11-23 21:27:44 +03:00
Created UpdateTagAction
This commit is contained in:
parent
e07c464de8
commit
963d26f59b
7 changed files with 236 additions and 0 deletions
26
module/Core/src/Exception/EntityDoesNotExistException.php
Normal file
26
module/Core/src/Exception/EntityDoesNotExistException.php
Normal file
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
namespace Shlinkio\Shlink\Core\Exception;
|
||||
|
||||
use Shlinkio\Shlink\Common\Exception\ExceptionInterface;
|
||||
|
||||
class EntityDoesNotExistException extends \RuntimeException implements ExceptionInterface
|
||||
{
|
||||
public static function createFromEntityAndConditions($entityName, array $conditions)
|
||||
{
|
||||
return new self(sprintf(
|
||||
'Entity of type %s with params [%s] does not exist',
|
||||
$entityName,
|
||||
static::serializeParams($conditions)
|
||||
));
|
||||
}
|
||||
|
||||
private static function serializeParams(array $params)
|
||||
{
|
||||
$result = [];
|
||||
foreach ($params as $key => $value) {
|
||||
$result[] = sprintf('"%s" => "%s"', $key, $value);
|
||||
}
|
||||
|
||||
return implode(', ', $result);
|
||||
}
|
||||
}
|
|
@ -5,6 +5,7 @@ use Acelaya\ZsmAnnotatedServices\Annotation as DI;
|
|||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Shlinkio\Shlink\Core\Entity\Tag;
|
||||
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
|
||||
use Shlinkio\Shlink\Core\Repository\TagRepository;
|
||||
use Shlinkio\Shlink\Core\Util\TagManagerTrait;
|
||||
|
||||
|
@ -61,4 +62,25 @@ class TagService implements TagServiceInterface
|
|||
|
||||
return $tags;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $oldName
|
||||
* @param string $newName
|
||||
* @return Tag
|
||||
* @throws EntityDoesNotExistException
|
||||
*/
|
||||
public function renameTag($oldName, $newName)
|
||||
{
|
||||
$criteria = ['name' => $oldName];
|
||||
/** @var Tag|null $tag */
|
||||
$tag = $this->em->getRepository(Tag::class)->findOneBy($criteria);
|
||||
if ($tag === null) {
|
||||
throw EntityDoesNotExistException::createFromEntityAndConditions(Tag::class, $criteria);
|
||||
}
|
||||
|
||||
$tag->setName($newName);
|
||||
$this->em->flush($tag);
|
||||
|
||||
return $tag;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ namespace Shlinkio\Shlink\Core\Service\Tag;
|
|||
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Shlinkio\Shlink\Core\Entity\Tag;
|
||||
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
|
||||
|
||||
interface TagServiceInterface
|
||||
{
|
||||
|
@ -24,4 +25,12 @@ interface TagServiceInterface
|
|||
* @return Collection|Tag[]
|
||||
*/
|
||||
public function createTags(array $tagNames);
|
||||
|
||||
/**
|
||||
* @param string $oldName
|
||||
* @param string $newName
|
||||
* @return Tag
|
||||
* @throws EntityDoesNotExistException
|
||||
*/
|
||||
public function renameTag($oldName, $newName);
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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],
|
||||
],
|
||||
],
|
||||
|
||||
];
|
||||
|
|
83
module/Rest/src/Action/Tag/UpdateTagAction.php
Normal file
83
module/Rest/src/Action/Tag/UpdateTagAction.php
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
89
module/Rest/test/Action/Tag/UpdateTagActionTest.php
Normal file
89
module/Rest/test/Action/Tag/UpdateTagActionTest.php
Normal 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();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue