Skip to content

Commit

Permalink
Merge pull request #1432 from rebuy-de/fix-form-interface-handling
Browse files Browse the repository at this point in the history
Define method for the `FormInterface` type in the `FormErrorHandler`
  • Loading branch information
goetas committed Sep 12, 2022
2 parents 32956d3 + 235a751 commit 329e29c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Handler/FormErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public static function getSubscribingMethods()
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
'type' => FormInterface::class,
'format' => $format,
'method' => 'serializeFormTo' . ucfirst($format),
];
$methods[] = [
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
Expand Down
27 changes: 23 additions & 4 deletions tests/Serializer/BaseSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
use Symfony\Component\Form\FormConfigBuilder;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormFactoryBuilder;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Translation\IdentityTranslator;
use Symfony\Component\Uid\Uuid;
use Symfony\Component\Uid\UuidV4;
Expand Down Expand Up @@ -1192,7 +1193,10 @@ public function testFormErrors()
self::assertEquals($this->getContent('form_errors'), $this->serialize($errors));
}

public function testNestedFormErrors()
/**
* @dataProvider initialFormTypeProvider
*/
public function testNestedFormErrors($type)
{
$dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();

Expand All @@ -1210,13 +1214,17 @@ public function testNestedFormErrors()
$child->addError(new FormError('Error of the child form'));
$form->add($child);

self::assertEquals($this->getContent('nested_form_errors'), $this->serialize($form));
$context = SerializationContext::create();
$context->setInitialType($type);

self::assertEquals($this->getContent('nested_form_errors'), $this->serialize($form, $context));
}

/**
* @doesNotPerformAssertions
* @dataProvider initialFormTypeProvider
*/
public function testFormErrorsWithNonFormComponents()
public function testFormErrorsWithNonFormComponents($type)
{
if (!class_exists('Symfony\Component\Form\Extension\Core\Type\SubmitType')) {
$this->markTestSkipped('Not using Symfony Form >= 2.3 with submit type');
Expand All @@ -1238,13 +1246,24 @@ public function testFormErrorsWithNonFormComponents()
$form = new Form($fooConfig);
$form->add('save', SubmitType::class);

$context = SerializationContext::create();
$context->setInitialType($type);

try {
$this->serialize($form);
$this->serialize($form, $context);
} catch (\Throwable $e) {
self::assertTrue(false, 'Serialization should not throw an exception');
}
}

public function initialFormTypeProvider()
{
return [
[Form::class],
[FormInterface::class],
];
}

public function testConstraintViolation()
{
$violation = new ConstraintViolation('Message of violation', 'Message of violation', [], null, 'foo', null);
Expand Down

0 comments on commit 329e29c

Please sign in to comment.