mirror of
				https://github.com/kevinveenbirkenbach/infinito.git
				synced 2025-10-31 01:09:41 +00:00 
			
		
		
		
	Added EntityFormBuilderService
This commit is contained in:
		| @@ -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); | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user