mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Improved public API in Tag entity, avoiding anemic model
This commit is contained in:
parent
f7ceeff05a
commit
084b1169d7
@ -53,7 +53,7 @@ class ListTagsCommand extends Command
|
|||||||
}
|
}
|
||||||
|
|
||||||
return array_map(function (Tag $tag) {
|
return array_map(function (Tag $tag) {
|
||||||
return [$tag->getName()];
|
return [(string) $tag];
|
||||||
}, $tags);
|
}, $tags);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -61,8 +61,8 @@ class ListTagsCommandTest extends TestCase
|
|||||||
{
|
{
|
||||||
/** @var MethodProphecy $listTags */
|
/** @var MethodProphecy $listTags */
|
||||||
$listTags = $this->tagService->listTags()->willReturn([
|
$listTags = $this->tagService->listTags()->willReturn([
|
||||||
(new Tag())->setName('foo'),
|
new Tag('foo'),
|
||||||
(new Tag())->setName('bar'),
|
new Tag('bar'),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->commandTester->execute([]);
|
$this->commandTester->execute([]);
|
||||||
|
@ -68,7 +68,7 @@ class RenameTagCommandTest extends TestCase
|
|||||||
$oldName = 'foo';
|
$oldName = 'foo';
|
||||||
$newName = 'bar';
|
$newName = 'bar';
|
||||||
/** @var MethodProphecy $renameTag */
|
/** @var MethodProphecy $renameTag */
|
||||||
$renameTag = $this->tagService->renameTag($oldName, $newName)->willReturn(new Tag());
|
$renameTag = $this->tagService->renameTag($oldName, $newName)->willReturn(new Tag($newName));
|
||||||
|
|
||||||
$this->commandTester->execute([
|
$this->commandTester->execute([
|
||||||
'oldName' => $oldName,
|
'oldName' => $oldName,
|
||||||
|
@ -24,24 +24,23 @@ class Tag extends AbstractEntity implements JsonSerializable
|
|||||||
*/
|
*/
|
||||||
private $name;
|
private $name;
|
||||||
|
|
||||||
public function __construct($name = null)
|
public function __construct(string $name)
|
||||||
{
|
{
|
||||||
$this->name = $name;
|
$this->name = $name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getName(): string
|
public function rename(string $name): void
|
||||||
{
|
|
||||||
return $this->name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setName(string $name)
|
|
||||||
{
|
{
|
||||||
$this->name = $name;
|
$this->name = $name;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function jsonSerialize(): string
|
public function jsonSerialize(): string
|
||||||
{
|
{
|
||||||
return $this->name;
|
return $this->name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function __toString(): string
|
||||||
|
{
|
||||||
|
return $this->name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -76,7 +76,7 @@ class TagService implements TagServiceInterface
|
|||||||
throw EntityDoesNotExistException::createFromEntityAndConditions(Tag::class, $criteria);
|
throw EntityDoesNotExistException::createFromEntityAndConditions(Tag::class, $criteria);
|
||||||
}
|
}
|
||||||
|
|
||||||
$tag->setName($newName);
|
$tag->rename($newName);
|
||||||
|
|
||||||
/** @var ORM\EntityManager $em */
|
/** @var ORM\EntityManager $em */
|
||||||
$em = $this->em;
|
$em = $this->em;
|
||||||
|
@ -47,6 +47,6 @@ class ShortUrlDataTransformer implements DataTransformerInterface
|
|||||||
|
|
||||||
private function serializeTag(Tag $tag): string
|
private function serializeTag(Tag $tag): string
|
||||||
{
|
{
|
||||||
return $tag->getName();
|
return (string) $tag;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,7 @@ trait TagManagerTrait
|
|||||||
$entities = [];
|
$entities = [];
|
||||||
foreach ($tags as $tagName) {
|
foreach ($tags as $tagName) {
|
||||||
$tagName = $this->normalizeTagName($tagName);
|
$tagName = $this->normalizeTagName($tagName);
|
||||||
$tag = $em->getRepository(Tag::class)->findOneBy(['name' => $tagName]) ?: (new Tag())->setName($tagName);
|
$tag = $em->getRepository(Tag::class)->findOneBy(['name' => $tagName]) ?: new Tag($tagName);
|
||||||
$em->persist($tag);
|
$em->persist($tag);
|
||||||
$entities[] = $tag;
|
$entities[] = $tag;
|
||||||
}
|
}
|
||||||
|
@ -11,10 +11,9 @@ class TagTest extends TestCase
|
|||||||
/**
|
/**
|
||||||
* @test
|
* @test
|
||||||
*/
|
*/
|
||||||
public function jsonSerializationOfTagsReturnsItsName()
|
public function jsonSerializationOfTagsReturnsItsStringRepresentation()
|
||||||
{
|
{
|
||||||
$tag = new Tag();
|
$tag = new Tag('This is my name');
|
||||||
$tag->setName('This is my name');
|
$this->assertEquals((string) $tag, $tag->jsonSerialize());
|
||||||
$this->assertEquals($tag->getName(), $tag->jsonSerialize());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -86,7 +86,7 @@ class ShortUrlServiceTest extends TestCase
|
|||||||
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
|
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
|
||||||
|
|
||||||
$tagRepo = $this->prophesize(EntityRepository::class);
|
$tagRepo = $this->prophesize(EntityRepository::class);
|
||||||
$tagRepo->findOneBy(['name' => 'foo'])->willReturn(new Tag())->shouldbeCalledTimes(1);
|
$tagRepo->findOneBy(['name' => 'foo'])->willReturn(new Tag('foo'))->shouldbeCalledTimes(1);
|
||||||
$tagRepo->findOneBy(['name' => 'bar'])->willReturn(null)->shouldbeCalledTimes(1);
|
$tagRepo->findOneBy(['name' => 'bar'])->willReturn(null)->shouldbeCalledTimes(1);
|
||||||
$this->em->getRepository(Tag::class)->willReturn($tagRepo->reveal());
|
$this->em->getRepository(Tag::class)->willReturn($tagRepo->reveal());
|
||||||
|
|
||||||
|
@ -36,7 +36,7 @@ class TagServiceTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function listTagsDelegatesOnRepository()
|
public function listTagsDelegatesOnRepository()
|
||||||
{
|
{
|
||||||
$expected = [new Tag(), new Tag()];
|
$expected = [new Tag('foo'), new Tag('bar')];
|
||||||
|
|
||||||
$repo = $this->prophesize(EntityRepository::class);
|
$repo = $this->prophesize(EntityRepository::class);
|
||||||
/** @var MethodProphecy $find */
|
/** @var MethodProphecy $find */
|
||||||
@ -75,7 +75,7 @@ class TagServiceTest extends TestCase
|
|||||||
{
|
{
|
||||||
$repo = $this->prophesize(TagRepository::class);
|
$repo = $this->prophesize(TagRepository::class);
|
||||||
/** @var MethodProphecy $find */
|
/** @var MethodProphecy $find */
|
||||||
$find = $repo->findOneBy(Argument::cetera())->willReturn(new Tag());
|
$find = $repo->findOneBy(Argument::cetera())->willReturn(new Tag('foo'));
|
||||||
/** @var MethodProphecy $getRepo */
|
/** @var MethodProphecy $getRepo */
|
||||||
$getRepo = $this->em->getRepository(Tag::class)->willReturn($repo->reveal());
|
$getRepo = $this->em->getRepository(Tag::class)->willReturn($repo->reveal());
|
||||||
/** @var MethodProphecy $persist */
|
/** @var MethodProphecy $persist */
|
||||||
@ -115,7 +115,7 @@ class TagServiceTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function renameValidTagChangesItsName()
|
public function renameValidTagChangesItsName()
|
||||||
{
|
{
|
||||||
$expected = new Tag();
|
$expected = new Tag('foo');
|
||||||
|
|
||||||
$repo = $this->prophesize(TagRepository::class);
|
$repo = $this->prophesize(TagRepository::class);
|
||||||
/** @var MethodProphecy $find */
|
/** @var MethodProphecy $find */
|
||||||
@ -128,7 +128,7 @@ class TagServiceTest extends TestCase
|
|||||||
$tag = $this->service->renameTag('foo', 'bar');
|
$tag = $this->service->renameTag('foo', 'bar');
|
||||||
|
|
||||||
$this->assertSame($expected, $tag);
|
$this->assertSame($expected, $tag);
|
||||||
$this->assertEquals('bar', $tag->getName());
|
$this->assertEquals('bar', (string) $tag);
|
||||||
$find->shouldHaveBeenCalled();
|
$find->shouldHaveBeenCalled();
|
||||||
$getRepo->shouldHaveBeenCalled();
|
$getRepo->shouldHaveBeenCalled();
|
||||||
$flush->shouldHaveBeenCalled();
|
$flush->shouldHaveBeenCalled();
|
||||||
|
@ -80,7 +80,7 @@ class UpdateTagActionTest extends TestCase
|
|||||||
'newName' => 'bar',
|
'newName' => 'bar',
|
||||||
]);
|
]);
|
||||||
/** @var MethodProphecy $rename */
|
/** @var MethodProphecy $rename */
|
||||||
$rename = $this->tagService->renameTag('foo', 'bar')->willReturn(new Tag());
|
$rename = $this->tagService->renameTag('foo', 'bar')->willReturn(new Tag('bar'));
|
||||||
|
|
||||||
$resp = $this->action->handle($request);
|
$resp = $this->action->handle($request);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user