Renamed FormBuilder to FormFactory and implemented test draft for CreateSourceAction

This commit is contained in:
Kevin Frantz 2019-02-12 16:40:01 +01:00
parent 0fcd4c93bd
commit 6347308672
6 changed files with 75 additions and 13 deletions

View File

@ -41,7 +41,7 @@ final class CreateSourceAction extends AbstractCreateAction
*/
protected function isValidByForm(): bool
{
$this->actionService->getForm();
return $this->actionService->getCurrentFormBuilder()->getForm()->isValid();
}
/**

View File

@ -4,6 +4,7 @@ namespace App\Domain\FormManagement;
use Symfony\Component\Form\FormBuilderInterface;
use App\Domain\RequestManagement\Action\RequestedActionInterface;
use Symfony\Component\Form\FormFactoryInterface;
/**
* @author kevinfrantz
@ -11,9 +12,9 @@ use App\Domain\RequestManagement\Action\RequestedActionInterface;
class RequestedActionFormBuilder implements RequestedActionFormBuilderInterface
{
/**
* @var FormBuilderInterface
* @var FormFactoryInterface
*/
private $formBuilder;
private $formFactory;
/**
* @var FormClassNameServiceInterface
@ -21,12 +22,12 @@ class RequestedActionFormBuilder implements RequestedActionFormBuilderInterface
private $formClassNameService;
/**
* @param FormBuilderInterface $formBuilder
* @param FormFactoryInterface $formFactory
* @param FormClassNameServiceInterface $formClassNameService
*/
public function __construct(FormBuilderInterface $formBuilder, FormClassNameServiceInterface $formClassNameService)
public function __construct(FormFactoryInterface $formFactory, FormClassNameServiceInterface $formClassNameService)
{
$this->formBuilder = $formBuilder;
$this->formFactory = $formFactory;
$this->formClassNameService = $formClassNameService;
}
@ -44,7 +45,7 @@ class RequestedActionFormBuilder implements RequestedActionFormBuilderInterface
if ($requestedEntity->hasIdentity()) {
$entity = $requestedEntity->getEntity();
}
$form = $this->formBuilder->create($class, $entity);
$form = $this->formFactory->createBuilder($class, $entity);
return $form;
}

View File

@ -4,6 +4,8 @@ namespace App\Domain\FormManagement;
use Symfony\Component\Form\FormBuilderInterface;
use App\Domain\RequestManagement\Action\RequestedActionServiceInterface;
use FOS\UserBundle\Form\Factory\FormFactory;
use Symfony\Component\Form\FormFactoryInterface;
/**
* @author kevinfrantz
@ -20,9 +22,9 @@ final class RequestedActionFormBuilderService extends RequestedActionFormBuilder
*
* @see \App\Domain\FormManagement\RequestedActionFormBuilder::__construct()
*/
public function __construct(FormBuilderInterface $formBuilder, FormClassNameServiceInterface $formClassNameService, RequestedActionServiceInterface $requestedActionService)
public function __construct(FormFactoryInterface $formFactory, FormClassNameServiceInterface $formClassNameService, RequestedActionServiceInterface $requestedActionService)
{
parent::__construct($formBuilder, $formClassNameService);
parent::__construct($formFactory, $formClassNameService);
$this->requestedActionService = $requestedActionService;
}

View File

@ -9,5 +9,9 @@ use App\Form\AbstractType;
*/
class SourceType extends AbstractType
{
/**
* @deprecated
* @var string
*/
const CLASS_PARAMETER_NAME = 'class';
}

View File

@ -0,0 +1,52 @@
<?php
namespace tests\Unit\Domain\ActionManagement\Create;
use PHPUnit\Framework\TestCase;
use App\Domain\ActionManagement\Create\CreateSourceAction;
use Symfony\Component\HttpFoundation\Request;
use App\Entity\Source\PureSource;
use App\Attribut\ClassAttributInterface;
use App\Attribut\SlugAttributInterface;
use App\Entity\Source\PureSourceInterface;
use App\Domain\ActionManagement\ActionService;
use Doctrine\ORM\EntityManagerInterface;
use App\Domain\RequestManagement\Action\RequestedActionServiceInterface;
use App\Domain\SecureManagement\SecureRequestedRightCheckerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use App\Domain\RepositoryManagement\LayerRepositoryFactoryServiceInterface;
use App\Domain\FormManagement\RequestedActionFormBuilderServiceInterface;
use App\Form\Source\PureSourceType;
use Symfony\Component\Form\FormBuilderInterface;
use App\Form\Source\PureSourceTypeInterface;
/**
* @author kevinfrantz
*/
class CreateSourceActionTest extends TestCase
{
public function testCreatePureSource(): void
{
$request = new Request();
$request->setMethod(Request::METHOD_POST);
$request->query->set(ClassAttributInterface::CLASS_ATTRIBUT_NAME, PureSource::class);
$request->request->set(SlugAttributInterface::SLUG_ATTRIBUT_NAME, 'randomname');
$requestedActionService = $this->createMock(RequestedActionServiceInterface::class);
$secureRequestedRightChecker = $this->createMock(SecureRequestedRightCheckerInterface::class);
$entityManager = $this->createMock(EntityManagerInterface::class);
$pureSourceType = $this->createMock(PureSourceTypeInterface::class);
$pureSourceType->method('isValid')->willReturn(true);
$formBuilderInterface = $this->createMock(FormBuilderInterface::class);
$formBuilderInterface->method('getForm')->willReturn($pureSourceType);
// $formBuilderInterface->method('isValid')->willReturn(true);
$requestedActionFormBuilderService = $this->createMock(RequestedActionFormBuilderServiceInterface::class);
$requestedActionFormBuilderService->method('createByService')->willReturn($formBuilderInterface);
$requestStack = $this->createMock(RequestStack::class);
$requestStack->method('getCurrentRequest')->willReturn($request);
$layerRepositoryFactoryService = $this->createMock(LayerRepositoryFactoryServiceInterface::class);
$actionService = new ActionService($requestedActionService, $secureRequestedRightChecker, $requestStack, $layerRepositoryFactoryService, $requestedActionFormBuilderService, $entityManager);
$createSourceAction = new CreateSourceAction($actionService);
$result = $createSourceAction->execute();
$this->assertInstanceOf(PureSourceInterface::class, $result);
}
}

View File

@ -9,6 +9,7 @@ use App\Domain\RequestManagement\Entity\RequestedEntityInterface;
use App\Entity\Source\PureSource;
use App\Domain\FormManagement\RequestedActionFormBuilderService;
use App\Domain\RequestManagement\Action\RequestedActionServiceInterface;
use Symfony\Component\Form\FormFactoryInterface;
/**
* @author kevinfrantz
@ -20,9 +21,9 @@ class RequestedActionFormBuilderServiceTest extends TestCase
*/
public function testCreate(): void
{
$expectedResult = $this->createMock(FormBuilderInterface::class);
$formBuilder = $this->createMock(FormBuilderInterface::class);
$formBuilder->method('create')->willReturn($expectedResult);
$formFactory = $this->createMock(FormFactoryInterface::class);
$formFactory->method('create')->willReturn($formBuilder);
$formClassNameService = $this->createMock(FormClassNameServiceInterface::class);
$formClassNameService->method('getClass')->willReturn('dummyNamespace');
$entity = new PureSource();
@ -31,9 +32,11 @@ class RequestedActionFormBuilderServiceTest extends TestCase
$requestedEntity->method('getEntity')->willReturn($entity);
$requestedAction = $this->createMock(RequestedActionServiceInterface::class);
$requestedAction->method('getRequestedEntity')->willReturn($requestedEntity);
$entityFormBuilderService = new RequestedActionFormBuilderService($formBuilder, $formClassNameService, $requestedAction);
$entityFormBuilderService = new RequestedActionFormBuilderService($formFactory, $formClassNameService, $requestedAction);
$result = $entityFormBuilderService->create($requestedAction);
$this->assertEquals($expectedResult, $result);
//$this->assertTrue(method_exists($result, 'isValid'));
$this->assertEquals($formBuilder, $result);
$this->assertEquals($entityFormBuilderService->create($requestedAction), $entityFormBuilderService->createByService());
}
}