From 084b1169d7f2d0258906fa0da8adbe0955aebe8c Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 28 Oct 2018 14:38:43 +0100 Subject: [PATCH] Improved public API in Tag entity, avoiding anemic model --- module/CLI/src/Command/Tag/ListTagsCommand.php | 2 +- .../CLI/test/Command/Tag/ListTagsCommandTest.php | 4 ++-- .../CLI/test/Command/Tag/RenameTagCommandTest.php | 2 +- module/Core/src/Entity/Tag.php | 15 +++++++-------- module/Core/src/Service/Tag/TagService.php | 2 +- .../src/Transformer/ShortUrlDataTransformer.php | 2 +- module/Core/src/Util/TagManagerTrait.php | 2 +- module/Core/test/Entity/TagTest.php | 7 +++---- module/Core/test/Service/ShortUrlServiceTest.php | 2 +- module/Core/test/Service/Tag/TagServiceTest.php | 8 ++++---- .../Rest/test/Action/Tag/UpdateTagActionTest.php | 2 +- 11 files changed, 23 insertions(+), 25 deletions(-) diff --git a/module/CLI/src/Command/Tag/ListTagsCommand.php b/module/CLI/src/Command/Tag/ListTagsCommand.php index e82617dd..eac0afd2 100644 --- a/module/CLI/src/Command/Tag/ListTagsCommand.php +++ b/module/CLI/src/Command/Tag/ListTagsCommand.php @@ -53,7 +53,7 @@ class ListTagsCommand extends Command } return array_map(function (Tag $tag) { - return [$tag->getName()]; + return [(string) $tag]; }, $tags); } } diff --git a/module/CLI/test/Command/Tag/ListTagsCommandTest.php b/module/CLI/test/Command/Tag/ListTagsCommandTest.php index 584fa0a8..cc45d182 100644 --- a/module/CLI/test/Command/Tag/ListTagsCommandTest.php +++ b/module/CLI/test/Command/Tag/ListTagsCommandTest.php @@ -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([]); diff --git a/module/CLI/test/Command/Tag/RenameTagCommandTest.php b/module/CLI/test/Command/Tag/RenameTagCommandTest.php index 032bfcc7..ab9e1589 100644 --- a/module/CLI/test/Command/Tag/RenameTagCommandTest.php +++ b/module/CLI/test/Command/Tag/RenameTagCommandTest.php @@ -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, diff --git a/module/Core/src/Entity/Tag.php b/module/Core/src/Entity/Tag.php index 0148421a..78ddad80 100644 --- a/module/Core/src/Entity/Tag.php +++ b/module/Core/src/Entity/Tag.php @@ -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; + } } diff --git a/module/Core/src/Service/Tag/TagService.php b/module/Core/src/Service/Tag/TagService.php index 5369769d..30328738 100644 --- a/module/Core/src/Service/Tag/TagService.php +++ b/module/Core/src/Service/Tag/TagService.php @@ -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; diff --git a/module/Core/src/Transformer/ShortUrlDataTransformer.php b/module/Core/src/Transformer/ShortUrlDataTransformer.php index d6c9046e..04b992cc 100644 --- a/module/Core/src/Transformer/ShortUrlDataTransformer.php +++ b/module/Core/src/Transformer/ShortUrlDataTransformer.php @@ -47,6 +47,6 @@ class ShortUrlDataTransformer implements DataTransformerInterface private function serializeTag(Tag $tag): string { - return $tag->getName(); + return (string) $tag; } } diff --git a/module/Core/src/Util/TagManagerTrait.php b/module/Core/src/Util/TagManagerTrait.php index 614dac3a..f19dda65 100644 --- a/module/Core/src/Util/TagManagerTrait.php +++ b/module/Core/src/Util/TagManagerTrait.php @@ -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; } diff --git a/module/Core/test/Entity/TagTest.php b/module/Core/test/Entity/TagTest.php index 53ba6e21..f3a31d14 100644 --- a/module/Core/test/Entity/TagTest.php +++ b/module/Core/test/Entity/TagTest.php @@ -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()); } } diff --git a/module/Core/test/Service/ShortUrlServiceTest.php b/module/Core/test/Service/ShortUrlServiceTest.php index 5b7e52f4..a9ca03c1 100644 --- a/module/Core/test/Service/ShortUrlServiceTest.php +++ b/module/Core/test/Service/ShortUrlServiceTest.php @@ -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()); diff --git a/module/Core/test/Service/Tag/TagServiceTest.php b/module/Core/test/Service/Tag/TagServiceTest.php index e245c17a..56784353 100644 --- a/module/Core/test/Service/Tag/TagServiceTest.php +++ b/module/Core/test/Service/Tag/TagServiceTest.php @@ -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(); diff --git a/module/Rest/test/Action/Tag/UpdateTagActionTest.php b/module/Rest/test/Action/Tag/UpdateTagActionTest.php index c073f270..27162ebe 100644 --- a/module/Rest/test/Action/Tag/UpdateTagActionTest.php +++ b/module/Rest/test/Action/Tag/UpdateTagActionTest.php @@ -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);