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