Improved public API in Tag entity, avoiding anemic model

This commit is contained in:
Alejandro Celaya 2018-10-28 14:38:43 +01:00
parent f7ceeff05a
commit 084b1169d7
11 changed files with 23 additions and 25 deletions

View File

@ -53,7 +53,7 @@ class ListTagsCommand extends Command
}
return array_map(function (Tag $tag) {
return [$tag->getName()];
return [(string) $tag];
}, $tags);
}
}

View File

@ -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([]);

View File

@ -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,

View File

@ -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;
}
}

View File

@ -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;

View File

@ -47,6 +47,6 @@ class ShortUrlDataTransformer implements DataTransformerInterface
private function serializeTag(Tag $tag): string
{
return $tag->getName();
return (string) $tag;
}
}

View File

@ -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;
}

View File

@ -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());
}
}

View File

@ -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());

View File

@ -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();

View File

@ -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);