mirror of
https://github.com/shlinkio/shlink.git
synced 2024-11-26 02:40:41 -06:00
Created TagService
This commit is contained in:
parent
486ea10c3c
commit
c37660f763
@ -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,
|
||||
|
34
module/Core/src/Service/Tag/TagService.php
Normal file
34
module/Core/src/Service/Tag/TagService.php
Normal 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']);
|
||||
}
|
||||
}
|
12
module/Core/src/Service/Tag/TagServiceInterface.php
Normal file
12
module/Core/src/Service/Tag/TagServiceInterface.php
Normal 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();
|
||||
}
|
51
module/Core/test/Service/Tag/TagServiceTest.php
Normal file
51
module/Core/test/Service/Tag/TagServiceTest.php
Normal 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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user