Added EntityFormBuilderService

This commit is contained in:
Kevin Frantz 2019-01-25 22:39:23 +01:00
parent 806c6408e8
commit 36198e8196
3 changed files with 96 additions and 0 deletions

View File

@ -0,0 +1,44 @@
<?php
namespace App\Domain\FormManagement;
use App\Entity\EntityInterface;
use Symfony\Component\Form\FormBuilderInterface;
/**
* @author kevinfrantz
*/
final class EntityFormBuilderService implements EntityFormBuilderServiceInterface
{
/**
* @var FormBuilderInterface
*/
private $formBuilder;
/**
* @var FormClassNameServiceInterface
*/
private $formClassNameService;
/**
* @param FormBuilderInterface $formBuilder
*/
public function __construct(FormBuilderInterface $formBuilder, FormClassNameServiceInterface $formClassNameService)
{
$this->formBuilder = $formBuilder;
$this->formClassNameService = $formClassNameService;
}
/**
* {@inheritdoc}
*
* @see \App\Domain\FormManagement\EntityFormBuilderServiceInterface::create()
*/
public function create(EntityInterface $entity): FormBuilderInterface
{
$class = $this->formClassNameService->getName($entity);
$form = $this->formBuilder->create($class, $entity);
return $form;
}
}

View File

@ -0,0 +1,21 @@
<?php
namespace App\Domain\FormManagement;
use App\Entity\EntityInterface;
use Symfony\Component\Form\FormBuilderInterface;
/**
* Allowes to create an form which fits to an entity.
*
* @author kevinfrantz
*/
interface EntityFormBuilderServiceInterface
{
/**
* @param EntityInterface $entity
*
* @return FormBuilderInterface
*/
public function create(EntityInterface $entity): FormBuilderInterface;
}

View File

@ -0,0 +1,31 @@
<?php
namespace tests\Unit\Domain\FormManagement;
use PHPUnit\Framework\TestCase;
use App\Domain\FormManagement\EntityFormBuilderService;
use Symfony\Component\Form\FormBuilderInterface;
use App\Domain\FormManagement\FormClassNameServiceInterface;
use App\Entity\EntityInterface;
/**
* @author kevinfrantz
*/
class EntityFormBuilderServiceTest extends TestCase
{
/**
* Could be that this test includes a bit to much mocking -.-.
*/
public function testCreate(): void
{
$expectedResult = $this->createMock(FormBuilderInterface::class);
$formBuilder = $this->createMock(FormBuilderInterface::class);
$formBuilder->method('create')->willReturn($expectedResult);
$formClassNameService = $this->createMock(FormClassNameServiceInterface::class);
$formClassNameService->method('getName')->willReturn('dummyNamespace');
$entityFormBuilderService = new EntityFormBuilderService($formBuilder, $formClassNameService);
$entity = $this->createMock(EntityInterface::class);
$result = $entityFormBuilderService->create($entity);
$this->assertEquals($expectedResult, $result);
}
}