Skip to content

Commit

Permalink
Add service Tag repository and revamping code
Browse files Browse the repository at this point in the history
  • Loading branch information
yceruto committed Jan 26, 2018
1 parent f2aecf4 commit 5209859
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 24 deletions.
10 changes: 5 additions & 5 deletions src/Form/DataTransformer/TagArrayToStringTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace App\Form\DataTransformer;

use App\Entity\Tag;
use Doctrine\Common\Persistence\ObjectManager;
use App\Repository\TagRepository;
use Symfony\Component\Form\DataTransformerInterface;

/**
Expand All @@ -26,11 +26,11 @@
*/
class TagArrayToStringTransformer implements DataTransformerInterface
{
private $manager;
private $tags;

public function __construct(ObjectManager $manager)
public function __construct(TagRepository $tags)
{
$this->manager = $manager;
$this->tags = $tags;
}

/**
Expand Down Expand Up @@ -58,7 +58,7 @@ public function reverseTransform($string): array
$names = array_filter(array_unique(array_map('trim', explode(',', $string))));

// Get the current tags and find the new ones that should be created.
$tags = $this->manager->getRepository(Tag::class)->findBy([
$tags = $this->tags->findBy([
'name' => $names,
]);
$newNames = array_diff($names, $tags);
Expand Down
13 changes: 6 additions & 7 deletions src/Form/Type/TagsInputType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@

namespace App\Form\Type;

use App\Entity\Tag;
use App\Form\DataTransformer\TagArrayToStringTransformer;
use Doctrine\Common\Persistence\ObjectManager;
use App\Repository\TagRepository;
use Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
Expand All @@ -31,11 +30,11 @@
*/
class TagsInputType extends AbstractType
{
private $manager;
private $tags;

public function __construct(ObjectManager $manager)
public function __construct(TagRepository $tags)
{
$this->manager = $manager;
$this->tags = $tags;
}

/**
Expand All @@ -49,7 +48,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
// but here we're doing the transformation in two steps (Collection <-> array <-> string)
// and reuse the existing CollectionToArrayTransformer.
->addModelTransformer(new CollectionToArrayTransformer(), true)
->addModelTransformer(new TagArrayToStringTransformer($this->manager), true)
->addModelTransformer(new TagArrayToStringTransformer($this->tags), true)
;
}

Expand All @@ -58,7 +57,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['tags'] = $this->manager->getRepository(Tag::class)->findAll();
$view->vars['tags'] = $this->tags->findAll();
}

/**
Expand Down
33 changes: 33 additions & 0 deletions src/Repository/TagRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App\Repository;

use App\Entity\Tag;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;

/**
* This custom Doctrine repository is empty because so far we don't need any custom
* method to query for application user information. But it's always a good practice
* to define a custom repository that will be used when the application grows.
*
* See https://symfony.com/doc/current/doctrine/repository.html
*
* @author Yonel Ceruto <yonelceruto@gmail.com>
*/
class TagRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Tag::class);
}
}
15 changes: 3 additions & 12 deletions tests/Form/DataTransformer/TagArrayToStringTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@

use App\Entity\Tag;
use App\Form\DataTransformer\TagArrayToStringTransformer;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityRepository;
use App\Repository\TagRepository;
use PHPUnit\Framework\TestCase;

/**
Expand Down Expand Up @@ -108,22 +107,14 @@ public function testTransform()
*/
private function getMockedTransformer(array $findByReturnValues = []): TagArrayToStringTransformer
{
$tagRepository = $this->getMockBuilder(EntityRepository::class)
$tagRepository = $this->getMockBuilder(TagRepository::class)
->disableOriginalConstructor()
->getMock();
$tagRepository->expects($this->any())
->method('findBy')
->will($this->returnValue($findByReturnValues));

$entityManager = $this
->getMockBuilder(ObjectManager::class)
->disableOriginalConstructor()
->getMock();
$entityManager->expects($this->any())
->method('getRepository')
->will($this->returnValue($tagRepository));

return new TagArrayToStringTransformer($entityManager);
return new TagArrayToStringTransformer($tagRepository);
}

/**
Expand Down

0 comments on commit 5209859

Please sign in to comment.