-
-
Notifications
You must be signed in to change notification settings - Fork 154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Init form provider #793
Merged
Merged
Init form provider #793
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
191 changes: 191 additions & 0 deletions
191
src/Component/Tests/Symfony/Form/State/FormProviderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Component\Resource\Tests\Symfony\Form\State; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\Argument; | ||
use Prophecy\PhpUnit\ProphecyTrait; | ||
use Prophecy\Prophecy\ObjectProphecy; | ||
use Sylius\Resource\Context\Context; | ||
use Sylius\Resource\Context\Option\RequestOption; | ||
use Sylius\Resource\Metadata\BulkUpdate; | ||
use Sylius\Resource\Metadata\Create; | ||
use Sylius\Resource\Metadata\Operations; | ||
use Sylius\Resource\Metadata\Show; | ||
use Sylius\Resource\State\ProviderInterface; | ||
use Sylius\Resource\Symfony\Form\Factory\FormFactoryInterface; | ||
use Sylius\Resource\Symfony\Form\State\FormProvider; | ||
use Symfony\Component\Form\FormInterface; | ||
use Symfony\Component\HttpFoundation\ParameterBag; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
final class FormProviderTest extends TestCase | ||
{ | ||
use ProphecyTrait; | ||
|
||
private ProviderInterface|ObjectProphecy $decorated; | ||
|
||
private FormFactoryInterface|ObjectProphecy $formFactory; | ||
|
||
private FormProvider $formProvider; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Resource\Symfony\Form\State; | ||
|
||
use Sylius\Resource\Context\Context; | ||
use Sylius\Resource\Context\Option\RequestOption; | ||
use Sylius\Resource\Metadata\BulkOperationInterface; | ||
use Sylius\Resource\Metadata\CreateOperationInterface; | ||
use Sylius\Resource\Metadata\Operation; | ||
use Sylius\Resource\Metadata\UpdateOperationInterface; | ||
use Sylius\Resource\State\ProviderInterface; | ||
use Sylius\Resource\Symfony\Form\Factory\FormFactoryInterface; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
final class FormProvider implements ProviderInterface | ||
{ | ||
public function __construct( | ||
private ProviderInterface $decorated, | ||
private FormFactoryInterface $formFactory, | ||
) { | ||
} | ||
|
||
public function provide(Operation $operation, Context $context): object|array|null | ||
{ | ||
$data = $this->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; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've removed a lot of non-necessary code in this test class, but in the end, this form listener and its PHPUnit test class will be removed.