Skip to content

Commit

Permalink
Merge pull request #21 from phonixor/feature/formsubmitprocessor
Browse files Browse the repository at this point in the history
added setFormSubmitProcessor to HandlerConfigInterface
  • Loading branch information
yannickl88 authored Oct 11, 2017
2 parents 42c647f + b0bfab0 commit 665e38c
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 6 deletions.
22 changes: 17 additions & 5 deletions src/FormHandler/FormSubmitProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,27 @@ final class FormSubmitProcessor
*/
private $on_failure;

/**
* @var callable
*/
private $form_submit_processor;

/**
* @param FormInterface $form
* @param callable|null $on_success
* @param callable|null $on_failure
* @param callable|null $form_submit_processor
*/
public function __construct(
FormInterface $form,
callable $on_success = null,
callable $on_failure = null
callable $on_failure = null,
callable $form_submit_processor = null
) {
$this->on_success = $on_success;
$this->on_failure = $on_failure;
$this->form = $form;
$this->form = $form;
$this->on_success = $on_success;
$this->on_failure = $on_failure;
$this->form_submit_processor = $form_submit_processor;
}

/**
Expand All @@ -55,7 +63,11 @@ public function getForm()
*/
public function process(Request $request)
{
$this->form->handleRequest($request);
if (is_callable($this->form_submit_processor)) {
call_user_func($this->form_submit_processor, $this->form, $request);
} else {
$this->form->handleRequest($request);
}

if (!$this->form->isSubmitted()) {
return null;
Expand Down
11 changes: 10 additions & 1 deletion src/FormHandler/HandlerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ final class HandlerBuilder implements HandlerConfigInterface
private $options = [];
private $on_success;
private $on_failure;
private $form_submit_processor;

/**
* {@inheritdoc}
Expand Down Expand Up @@ -58,6 +59,14 @@ public function onFailure(callable $callback)
$this->on_failure = $callback;
}

/**
* {@inheritdoc}
*/
public function setFormSubmitProcessor(callable $callback)
{
$this->form_submit_processor = $callback;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -114,6 +123,6 @@ public function build(FormFactoryInterface $form_factory, $data = null)
$form = $form_factory->create($this->type, $data, $options);
}

return new FormSubmitProcessor($form, $this->on_success, $this->on_failure);
return new FormSubmitProcessor($form, $this->on_success, $this->on_failure, $this->form_submit_processor);
}
}
12 changes: 12 additions & 0 deletions src/FormHandler/HandlerConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ public function onSuccess(callable $callback);
*/
public function onFailure(callable $callback);

/**
* Register the FormSubmitProcessor.
*
* This will overwrite the default FormSubmitProcessor.
*
* signature:
* function (FormInterface $form, Request $request): void;
*
* @param callable $callback
*/
public function setFormSubmitProcessor(callable $callback);

/**
* Register an action subscriber. If an unknown action is given, a
* UnknownSubscribedActionException is thrown.
Expand Down
3 changes: 3 additions & 0 deletions src/UPGRADE-1.5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- Added a new method to HandlerConfigInterface:
`setFormSubmitProcessor(callable $callback)`. While technically a BC break,
this interface is not designed to be implemented with a custom configuration.
21 changes: 21 additions & 0 deletions test/FormHandler/FormSubmitProcessorTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Hostnet\Component\FormHandler;

use Prophecy\Argument;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;

Expand Down Expand Up @@ -103,4 +104,24 @@ public function testSubmitInvalidNoHandler()

self::assertNull($form_submit_processor->process($request));
}

public function testProcess()
{
$success = false;

$this->form->handleRequest(Argument::cetera())->shouldNotBeCalled();
$this->form->isSubmitted()->willReturn(false);

$form_submit_processor = new FormSubmitProcessor(
$this->form->reveal(),
null,
null,
function () use (&$success) {
$success = true;
}
);

self::assertNull($form_submit_processor->process(Request::create('/', 'POST')));
self::assertTrue($success);
}
}
24 changes: 24 additions & 0 deletions test/FormHandler/HandlerBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Hostnet\Component\FormHandler\Fixtures\ActionSubscriber\HenkSubscriber;
use Hostnet\Component\FormHandler\Fixtures\ActionSubscriber\SuccessSubscriber;
use Hostnet\Component\FormHandler\Fixtures\TestType;
use Prophecy\Argument;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormTypeInterface;
Expand Down Expand Up @@ -233,4 +235,26 @@ public function providerBuildInvalidHandlerType()
[\Exception::class]
];
}

public function testBuildWithCustomFormSubmitProcessor()
{
$success = false;
$builder = new HandlerBuilder();
$builder->setFormSubmitProcessor(function () use (&$success) {
$success = true;
});

$form = $this->prophesize(FormInterface::class);
$form->handleRequest(Argument::cetera())->shouldNotBeCalled();
$form->isSubmitted()->shouldBeCalled()->willReturn(false);

$form_factory = $this->prophesize(FormFactoryInterface::class);
$form_factory->create(FormType::class, null, [])->willReturn($form);

$handler = $builder->build($form_factory->reveal());
$request = Request::create('/', 'POST');

self::assertNull($handler->process($request));
self::assertTrue($success);
}
}

0 comments on commit 665e38c

Please sign in to comment.