Created TagService

This commit is contained in:
Alejandro Celaya 2017-07-07 12:49:41 +02:00
parent 486ea10c3c
commit c37660f763
4 changed files with 98 additions and 0 deletions

View file

@ -16,6 +16,7 @@ return [
Service\VisitsTracker::class => AnnotatedFactory::class,
Service\ShortUrlService::class => AnnotatedFactory::class,
Service\VisitService::class => AnnotatedFactory::class,
Service\Tag\TagService::class => AnnotatedFactory::class,
// Middleware
Action\RedirectAction::class => AnnotatedFactory::class,

View file

@ -0,0 +1,34 @@
<?php
namespace Shlinkio\Shlink\Core\Service\Tag;
use Acelaya\ZsmAnnotatedServices\Annotation as DI;
use Doctrine\ORM\EntityManagerInterface;
use Shlinkio\Shlink\Core\Entity\Tag;
class TagService implements TagServiceInterface
{
/**
* @var EntityManagerInterface
*/
private $em;
/**
* VisitService constructor.
* @param EntityManagerInterface $em
*
* @DI\Inject({"em"})
*/
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
/**
* @return Tag[]
* @throws \UnexpectedValueException
*/
public function listTags()
{
return $this->em->getRepository(Tag::class)->findBy([], ['name' => 'DESC']);
}
}

View file

@ -0,0 +1,12 @@
<?php
namespace Shlinkio\Shlink\Core\Service\Tag;
use Shlinkio\Shlink\Core\Entity\Tag;
interface TagServiceInterface
{
/**
* @return Tag[]
*/
public function listTags();
}

View file

@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\Service\Tag;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Prophecy\Argument;
use Prophecy\Prophecy\MethodProphecy;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Entity\Tag;
use Shlinkio\Shlink\Core\Service\Tag\TagService;
use PHPUnit\Framework\TestCase;
class TagServiceTest extends TestCase
{
/**
* @var TagService
*/
private $service;
/**
* @var ObjectProphecy
*/
private $em;
public function setUp()
{
$this->em = $this->prophesize(EntityManagerInterface::class);
$this->service = new TagService($this->em->reveal());
}
/**
* @test
*/
public function listTagsDelegatesOnRepository()
{
$expected = [new Tag(), new Tag()];
$repo = $this->prophesize(EntityRepository::class);
/** @var MethodProphecy $find */
$find = $repo->findBy(Argument::cetera())->willReturn($expected);
/** @var MethodProphecy $getRepo */
$getRepo = $this->em->getRepository(Tag::class)->willReturn($repo->reveal());
$result = $this->service->listTags();
$this->assertEquals($expected, $result);
$find->shouldHaveBeenCalled();
$getRepo->shouldHaveBeenCalled();
}
}