Ensured a specific exception is thrown from TagService when trying to rename a tag to the name of another tag which already exists

This commit is contained in:
Alejandro Celaya
2019-12-06 20:44:41 +01:00
parent f62ed66e26
commit b9b3295b52
3 changed files with 47 additions and 5 deletions

View File

@@ -11,6 +11,7 @@ use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Entity\Tag;
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
use Shlinkio\Shlink\Core\Exception\TagConflictException;
use Shlinkio\Shlink\Core\Repository\TagRepository;
use Shlinkio\Shlink\Core\Service\Tag\TagService;
@@ -88,22 +89,51 @@ class TagServiceTest extends TestCase
$this->service->renameTag('foo', 'bar');
}
/** @test */
public function renameValidTagChangesItsName()
/**
* @test
* @dataProvider provideValidRenames
*/
public function renameValidTagChangesItsName(string $oldName, string $newName, int $count): void
{
$expected = new Tag('foo');
$repo = $this->prophesize(TagRepository::class);
$find = $repo->findOneBy(Argument::cetera())->willReturn($expected);
$countTags = $repo->count(Argument::cetera())->willReturn($count);
$getRepo = $this->em->getRepository(Tag::class)->willReturn($repo->reveal());
$flush = $this->em->flush($expected)->willReturn(null);
$tag = $this->service->renameTag('foo', 'bar');
$tag = $this->service->renameTag($oldName, $newName);
$this->assertSame($expected, $tag);
$this->assertEquals('bar', (string) $tag);
$this->assertEquals($newName, (string) $tag);
$find->shouldHaveBeenCalled();
$getRepo->shouldHaveBeenCalled();
$flush->shouldHaveBeenCalled();
$countTags->shouldHaveBeenCalledTimes($count > 0 ? 0 : 1);
}
public function provideValidRenames(): iterable
{
yield 'same names' => ['foo', 'foo', 1];
yield 'different names names' => ['foo', 'bar', 0];
}
/** @test */
public function renameTagToAnExistingNameThrowsException(): void
{
$repo = $this->prophesize(TagRepository::class);
$find = $repo->findOneBy(Argument::cetera())->willReturn(new Tag('foo'));
$countTags = $repo->count(Argument::cetera())->willReturn(1);
$getRepo = $this->em->getRepository(Tag::class)->willReturn($repo->reveal());
$flush = $this->em->flush(Argument::any())->willReturn(null);
$find->shouldBeCalled();
$getRepo->shouldBeCalled();
$countTags->shouldBeCalled();
$flush->shouldNotBeCalled();
$this->expectException(TagConflictException::class);
$this->service->renameTag('foo', 'bar');
}
}