From 3672d1e0e2ce6ba288dea3f03a66cb6d1bcac34e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Fr=C3=A9mont?= Date: Thu, 23 Nov 2023 12:44:27 +0100 Subject: [PATCH] Init form provider --- .../EventListener/FormListenerTest.php | 40 +--- .../Symfony/Form/State/FormProviderTest.php | 191 ++++++++++++++++++ .../src/Symfony/Form/State/FormProvider.php | 64 ++++++ 3 files changed, 260 insertions(+), 35 deletions(-) create mode 100644 src/Component/Tests/Symfony/Form/State/FormProviderTest.php create mode 100644 src/Component/src/Symfony/Form/State/FormProvider.php diff --git a/src/Component/Tests/Symfony/EventListener/FormListenerTest.php b/src/Component/Tests/Symfony/EventListener/FormListenerTest.php index 38c102fa0..0797c3bb4 100644 --- a/src/Component/Tests/Symfony/EventListener/FormListenerTest.php +++ b/src/Component/Tests/Symfony/EventListener/FormListenerTest.php @@ -14,6 +14,7 @@ namespace Sylius\Component\Resource\Tests\Symfony\EventListener; use PHPUnit\Framework\TestCase; +use Prophecy\Argument; use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; use Sylius\Resource\Context\Context; @@ -21,7 +22,6 @@ use Sylius\Resource\Metadata\BulkUpdate; use Sylius\Resource\Metadata\Create; use Sylius\Resource\Metadata\Operation\HttpOperationInitiatorInterface; -use Sylius\Resource\Metadata\Operations; use Sylius\Resource\Metadata\Show; use Sylius\Resource\Symfony\EventListener\FormListener; use Sylius\Resource\Symfony\Form\Factory\FormFactoryInterface; @@ -75,16 +75,10 @@ public function it_handles_forms(): void $request->attributes = $attributes; $request->getRequestFormat()->willReturn('html'); - $attributes->get('_route')->willReturn('app_dummy_show'); - $attributes->all('_sylius')->willReturn(['resource' => 'app.dummy']); - $operation = new Create(formType: 'App\Type\DummyType'); $this->operationInitiator->initializeOperation($request)->willReturn($operation); - $operations = new Operations(); - $operations->add('app_dummy_show', $operation); - $context = new Context(); $this->contextInitiator->initializeContext($request)->willReturn($context); @@ -119,20 +113,14 @@ public function it_does_nothing_when_controller_result_is_a_response(): void $request->attributes = $attributes; $request->getRequestFormat()->willReturn('html'); - $attributes->get('_route')->willReturn('app_dummy_show'); - $attributes->all('_sylius')->willReturn(['resource' => 'app.dummy']); - $operation = new Create(formType: 'App\Type\DummyType'); $this->operationInitiator->initializeOperation($request)->willReturn($operation); - $operations = new Operations(); - $operations->add('app_dummy_show', $operation); - $context = new Context(); $this->contextInitiator->initializeContext($request)->willReturn($context); - $this->formFactory->create($operation, $context, ['foo' => 'fighters']) + $this->formFactory->create(Argument::cetera()) ->willReturn($form) ->shouldNotBeCalled() ; @@ -162,20 +150,14 @@ public function it_does_nothing_when_operation_has_no_form_type(): void $request->attributes = $attributes; $request->getRequestFormat()->willReturn('html'); - $attributes->get('_route')->willReturn('app_dummy_show'); - $attributes->all('_sylius')->willReturn(['resource' => 'app.dummy']); - $operation = new Create(formType: null); $this->operationInitiator->initializeOperation($request)->willReturn($operation); - $operations = new Operations(); - $operations->add('app_dummy_show', $operation); - $context = new Context(); $this->contextInitiator->initializeContext($request)->willReturn($context); - $this->formFactory->create($operation, $context, ['foo' => 'fighters']) + $this->formFactory->create(Argument::cetera()) ->willReturn($form) ->shouldNotBeCalled() ; @@ -205,20 +187,14 @@ public function it_does_nothing_when_operation_is_not_a_create_or_update(): void $request->attributes = $attributes; $request->getRequestFormat()->willReturn('html'); - $attributes->get('_route')->willReturn('app_dummy_show'); - $attributes->all('_sylius')->willReturn(['resource' => 'app.dummy']); - $operation = new Show(formType: 'App\Type\DummyType'); $this->operationInitiator->initializeOperation($request)->willReturn($operation); - $operations = new Operations(); - $operations->add('app_dummy_show', $operation); - $context = new Context(); $this->contextInitiator->initializeContext($request)->willReturn($context); - $this->formFactory->create($operation, $context, ['foo' => 'fighters']) + $this->formFactory->create(Argument::cetera()) ->willReturn($form) ->shouldNotBeCalled() ; @@ -248,20 +224,14 @@ public function it_does_nothing_when_operation_is_a_bulk_update(): void $request->attributes = $attributes; $request->getRequestFormat()->willReturn('html'); - $attributes->get('_route')->willReturn('app_dummy_show'); - $attributes->all('_sylius')->willReturn(['resource' => 'app.dummy']); - $operation = new BulkUpdate(formType: 'App\Type\DummyType'); $this->operationInitiator->initializeOperation($request)->willReturn($operation); - $operations = new Operations(); - $operations->add('app_dummy_show', $operation); - $context = new Context(); $this->contextInitiator->initializeContext($request)->willReturn($context); - $this->formFactory->create($operation, $context, ['foo' => 'fighters']) + $this->formFactory->create(Argument::cetera()) ->willReturn($form) ->shouldNotBeCalled() ; diff --git a/src/Component/Tests/Symfony/Form/State/FormProviderTest.php b/src/Component/Tests/Symfony/Form/State/FormProviderTest.php new file mode 100644 index 000000000..98599b4e7 --- /dev/null +++ b/src/Component/Tests/Symfony/Form/State/FormProviderTest.php @@ -0,0 +1,191 @@ +decorated = $this->prophesize(ProviderInterface::class); + $this->formFactory = $this->prophesize(FormFactoryInterface::class); + + $this->formProvider = new FormProvider( + $this->decorated->reveal(), + $this->formFactory->reveal(), + ); + } + + /** @test */ + public function it_handles_forms(): void + { + $request = $this->prophesize(Request::class); + $attributes = $this->prophesize(ParameterBag::class); + $form = $this->prophesize(FormInterface::class); + + $request->attributes = $attributes; + $request->getRequestFormat()->willReturn('html')->shouldBeCalled(); + + $operation = new Create(formType: 'App\Type\DummyType'); + + $context = new Context(new RequestOption($request->reveal())); + + $this->decorated->provide($operation, $context)->willReturn(['foo' => 'fighters'])->shouldBeCalled(); + + $this->formFactory->create($operation, $context, ['foo' => 'fighters']) + ->willReturn($form) + ->shouldBeCalled() + ; + + $form->handleRequest($request)->willReturn($form)->shouldBeCalled(); + + $attributes->set('form', $form)->shouldBeCalled(); + + $this->formProvider->provide($operation, $context); + } + + /** @test */ + public function it_does_nothing_when_data_is_a_response(): void + { + $request = $this->prophesize(Request::class); + $attributes = $this->prophesize(ParameterBag::class); + $form = $this->prophesize(FormInterface::class); + $response = $this->prophesize(Response::class); + + $request->attributes = $attributes; + $request->getRequestFormat()->willReturn('html'); + + $operation = new Create(formType: 'App\Type\DummyType'); + + $context = new Context(new RequestOption($request->reveal())); + $this->decorated->provide($operation, $context)->willReturn($response)->shouldBeCalled(); + + $this->formFactory->create(Argument::cetera()) + ->willReturn($form) + ->shouldNotBeCalled() + ; + + $form->handleRequest($request)->willReturn($form)->shouldNotBeCalled(); + + $attributes->set('form', $form)->shouldNotBeCalled(); + + $this->formProvider->provide($operation, $context); + } + + /** @test */ + public function it_does_nothing_when_operation_has_no_form_type(): void + { + $request = $this->prophesize(Request::class); + $attributes = $this->prophesize(ParameterBag::class); + $form = $this->prophesize(FormInterface::class); + + $request->attributes = $attributes; + $request->getRequestFormat()->willReturn('html')->shouldBeCalled(); + + $operation = new Create(formType: null); + + $context = new Context(new RequestOption($request->reveal())); + + $this->formFactory->create(Argument::cetera()) + ->willReturn($form) + ->shouldNotBeCalled() + ; + + $form->handleRequest($request)->willReturn($form)->shouldNotBeCalled(); + + $attributes->set('form', $form)->shouldNotBeCalled(); + + $this->formProvider->provide($operation, $context); + } + + /** @test */ + public function it_does_nothing_when_operation_is_not_a_create_or_update(): void + { + $request = $this->prophesize(Request::class); + $attributes = $this->prophesize(ParameterBag::class); + $form = $this->prophesize(FormInterface::class); + + $request->attributes = $attributes; + $request->getRequestFormat()->willReturn('html'); + + $operation = new Show(formType: 'App\Type\DummyType'); + + $context = new Context(new RequestOption($request->reveal())); + + $this->formFactory->create(Argument::cetera()) + ->willReturn($form) + ->shouldNotBeCalled() + ; + + $form->handleRequest($request)->willReturn($form)->shouldNotBeCalled(); + + $attributes->set('form', $form)->shouldNotBeCalled(); + + $this->formProvider->provide($operation, $context); + } + + /** @test */ + public function it_does_nothing_when_operation_is_a_bulk_update(): void + { + $request = $this->prophesize(Request::class); + $attributes = $this->prophesize(ParameterBag::class); + $form = $this->prophesize(FormInterface::class); + + $request->attributes = $attributes; + $request->getRequestFormat()->willReturn('html'); + + $operation = new BulkUpdate(formType: 'App\Type\DummyType'); + + $operations = new Operations(); + $operations->add('app_dummy_show', $operation); + + $context = new Context(new RequestOption($request->reveal())); + + $this->formFactory->create(Argument::cetera()) + ->willReturn($form) + ->shouldNotBeCalled() + ; + + $form->handleRequest($request)->willReturn($form)->shouldNotBeCalled(); + + $attributes->set('form', $form)->shouldNotBeCalled(); + + $this->formProvider->provide($operation, $context); + } +} diff --git a/src/Component/src/Symfony/Form/State/FormProvider.php b/src/Component/src/Symfony/Form/State/FormProvider.php new file mode 100644 index 000000000..6d9fd71a6 --- /dev/null +++ b/src/Component/src/Symfony/Form/State/FormProvider.php @@ -0,0 +1,64 @@ +decorated->provide($operation, $context); + + $request = $context->get(RequestOption::class)?->request(); + + if (null === $request) { + return $data; + } + + /** @var string $format */ + $format = $request->getRequestFormat(); + + if ( + $data instanceof Response || + $operation instanceof BulkOperationInterface || + !($operation instanceof CreateOperationInterface || $operation instanceof UpdateOperationInterface) || + 'html' !== $format || + null === $operation->getFormType() + ) { + return $data; + } + + $form = $this->formFactory->create($operation, $context, $data); + $form->handleRequest($request); + + $request->attributes->set('form', $form); + + return $data; + } +}