diff --git a/src/Form/DataTransformer/TagArrayToStringTransformer.php b/src/Form/DataTransformer/TagArrayToStringTransformer.php index eb8a4278b..4dde60eca 100644 --- a/src/Form/DataTransformer/TagArrayToStringTransformer.php +++ b/src/Form/DataTransformer/TagArrayToStringTransformer.php @@ -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; /** @@ -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; } /** @@ -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); diff --git a/src/Form/Type/TagsInputType.php b/src/Form/Type/TagsInputType.php index 563d83da5..2a30e2d0d 100644 --- a/src/Form/Type/TagsInputType.php +++ b/src/Form/Type/TagsInputType.php @@ -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; @@ -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; } /** @@ -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) ; } @@ -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(); } /** diff --git a/src/Repository/TagRepository.php b/src/Repository/TagRepository.php new file mode 100644 index 000000000..9d469548f --- /dev/null +++ b/src/Repository/TagRepository.php @@ -0,0 +1,33 @@ + + * + * 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 + */ +class TagRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, Tag::class); + } +} diff --git a/tests/Form/DataTransformer/TagArrayToStringTransformerTest.php b/tests/Form/DataTransformer/TagArrayToStringTransformerTest.php index 290242cb7..3563a7d48 100644 --- a/tests/Form/DataTransformer/TagArrayToStringTransformerTest.php +++ b/tests/Form/DataTransformer/TagArrayToStringTransformerTest.php @@ -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; /** @@ -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); } /**