mirror of
https://github.com/kevinveenbirkenbach/infinito.git
synced 2025-01-09 22:17:26 +01:00
Added EntityFormBuilderService
This commit is contained in:
parent
806c6408e8
commit
36198e8196
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user