Removed non-used deprecated method and added missing tests

This commit is contained in:
Alejandro Celaya 2021-01-31 13:05:21 +01:00
parent 6b0f6e4541
commit ef12e90ae7
4 changed files with 60 additions and 12 deletions

View File

@ -128,16 +128,6 @@ class ShortUrl extends AbstractEntity
return $this->tags;
}
/**
* @param Collection|Tag[] $tags
* @deprecated Use ShortUrl::update to set the tags on this ShortUrl
*/
public function setTags(Collection $tags): self
{
$this->tags = $tags;
return $this;
}
public function update(
ShortUrlEdit $shortUrlEdit,
?ShortUrlRelationResolverInterface $relationResolver = null

View File

@ -38,8 +38,13 @@ class PersistenceShortUrlRelationResolver implements ShortUrlRelationResolverInt
*/
public function resolveTags(array $tags): Collections\Collection
{
return new Collections\ArrayCollection(map($tags, function (string $tagName): Tag {
$tag = $this->em->getRepository(Tag::class)->findOneBy(['name' => $tagName]) ?? new Tag($tagName);
if (empty($tags)) {
return new Collections\ArrayCollection();
}
$repo = $this->em->getRepository(Tag::class);
return new Collections\ArrayCollection(map($tags, function (string $tagName) use ($repo): Tag {
$tag = $repo->findOneBy(['name' => $tagName]) ?? new Tag($tagName);
$this->em->persist($tag);
return $tag;

View File

@ -7,9 +7,12 @@ namespace ShlinkioTest\Shlink\Core\ShortUrl\Resolver;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ObjectRepository;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Entity\Domain;
use Shlinkio\Shlink\Core\Entity\Tag;
use Shlinkio\Shlink\Core\Repository\TagRepositoryInterface;
use Shlinkio\Shlink\Core\ShortUrl\Resolver\PersistenceShortUrlRelationResolver;
class PersistenceShortUrlRelationResolverTest extends TestCase
@ -62,4 +65,42 @@ class PersistenceShortUrlRelationResolverTest extends TestCase
yield 'not found domain' => [null, $authority];
yield 'found domain' => [new Domain($authority), $authority];
}
/** @test */
public function findsAndPersistsTagsWrappedIntoCollection(): void
{
$tags = ['foo', 'bar', 'baz'];
$tagRepo = $this->prophesize(TagRepositoryInterface::class);
$findTag = $tagRepo->findOneBy(Argument::type('array'))->will(function (array $args): ?Tag {
['name' => $name] = $args[0];
return $name === 'foo' ? new Tag($name) : null;
});
$getRepo = $this->em->getRepository(Tag::class)->willReturn($tagRepo->reveal());
$persist = $this->em->persist(Argument::type(Tag::class));
$result = $this->resolver->resolveTags($tags);
self::assertCount(3, $result);
self::assertEquals([new Tag('foo'), new Tag('bar'), new Tag('baz')], $result->toArray());
$findTag->shouldHaveBeenCalledTimes(3);
$getRepo->shouldHaveBeenCalledOnce();
$persist->shouldHaveBeenCalledTimes(3);
}
/** @test */
public function returnsEmptyCollectionWhenProvidingEmptyListOfTags(): void
{
$tagRepo = $this->prophesize(TagRepositoryInterface::class);
$findTag = $tagRepo->findOneBy(Argument::type('array'))->willReturn(null);
$getRepo = $this->em->getRepository(Tag::class)->willReturn($tagRepo->reveal());
$persist = $this->em->persist(Argument::type(Tag::class));
$result = $this->resolver->resolveTags([]);
self::assertEmpty($result);
$findTag->shouldNotHaveBeenCalled();
$getRepo->shouldNotHaveBeenCalled();
$persist->shouldNotHaveBeenCalled();
}
}

View File

@ -6,6 +6,7 @@ namespace ShlinkioTest\Shlink\Core\ShortUrl\Resolver;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Core\Entity\Domain;
use Shlinkio\Shlink\Core\Entity\Tag;
use Shlinkio\Shlink\Core\ShortUrl\Resolver\SimpleShortUrlRelationResolver;
class SimpleShortUrlRelationResolverTest extends TestCase
@ -38,4 +39,15 @@ class SimpleShortUrlRelationResolverTest extends TestCase
yield 'empty domain' => [null];
yield 'non-empty domain' => ['domain.com'];
}
/** @test */
public function tagsAreWrappedInEntityCollection(): void
{
$tags = ['foo', 'bar', 'baz'];
$result = $this->resolver->resolveTags($tags);
self::assertCount(3, $result);
self::assertEquals([new Tag('foo'), new Tag('bar'), new Tag('baz')], $result->toArray());
}
}