Improved TagServiceTest

This commit is contained in:
Alejandro Celaya 2017-07-15 09:05:02 +02:00
parent b2d9f2fc01
commit 3e268e2012

View File

@ -7,6 +7,7 @@ use Prophecy\Argument;
use Prophecy\Prophecy\MethodProphecy;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Entity\Tag;
use Shlinkio\Shlink\Core\Repository\TagRepository;
use Shlinkio\Shlink\Core\Service\Tag\TagService;
use PHPUnit\Framework\TestCase;
@ -46,4 +47,45 @@ class TagServiceTest extends TestCase
$find->shouldHaveBeenCalled();
$getRepo->shouldHaveBeenCalled();
}
/**
* @test
*/
public function deleteTagsDelegatesOnRepository()
{
$repo = $this->prophesize(TagRepository::class);
/** @var MethodProphecy $delete */
$delete = $repo->deleteByName(['foo', 'bar'])->willReturn(4);
/** @var MethodProphecy $getRepo */
$getRepo = $this->em->getRepository(Tag::class)->willReturn($repo->reveal());
$this->service->deleteTags(['foo', 'bar']);
$delete->shouldHaveBeenCalled();
$getRepo->shouldHaveBeenCalled();
}
/**
* @test
*/
public function createTagsPersistsEntities()
{
$repo = $this->prophesize(TagRepository::class);
/** @var MethodProphecy $find */
$find = $repo->findOneBy(Argument::cetera())->willReturn(new Tag());
/** @var MethodProphecy $getRepo */
$getRepo = $this->em->getRepository(Tag::class)->willReturn($repo->reveal());
/** @var MethodProphecy $persist */
$persist = $this->em->persist(Argument::type(Tag::class))->willReturn(null);
/** @var MethodProphecy $flush */
$flush = $this->em->flush()->willReturn(null);
$result = $this->service->createTags(['foo', 'bar']);
$this->assertCount(2, $result);
$find->shouldHaveBeenCalled();
$getRepo->shouldHaveBeenCalled();
$persist->shouldHaveBeenCalledTimes(2);
$flush->shouldHaveBeenCalled();
}
}