From dee44ee2e1a8a34a1a38eb703460cf4ba80c8d26 Mon Sep 17 00:00:00 2001 From: Adrien SAMSON <asamson.externe@m6.fr> Date: Tue, 19 Mar 2013 15:42:32 +0100 Subject: [PATCH 1/3] Fix #147 --- Parser/FormTypeParser.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Parser/FormTypeParser.php b/Parser/FormTypeParser.php index 75bb680..ecace3f 100644 --- a/Parser/FormTypeParser.php +++ b/Parser/FormTypeParser.php @@ -90,8 +90,10 @@ class FormTypeParser implements ParserInterface for ($type = $config->getType(); null !== $type; $type = $type->getParent()) { if (isset($this->mapTypes[$type->getName()])) { $bestType = $this->mapTypes[$type->getName()]; - } elseif ('collection' === $type->getName() && isset($this->mapTypes[$config->getOption('type')])) { - $bestType = sprintf('array of %ss', $this->mapTypes[$config->getOption('type')]); + } elseif ('collection' === $type->getName()) { + if (is_string($config->getOption('type')) && isset($this->mapTypes[$config->getOption('type')])) { + $bestType = sprintf('array of %ss', $this->mapTypes[$config->getOption('type')]); + } } } From eef1b7db0aa39e293aa4758060d22cc0ea892565 Mon Sep 17 00:00:00 2001 From: Adrien SAMSON <asamson.externe@m6.fr> Date: Tue, 19 Mar 2013 15:43:30 +0100 Subject: [PATCH 2/3] Add tests for FormTypeParser --- Parser/FormTypeParser.php | 9 ++-- Tests/Fixtures/Form/CollectionType.php | 35 +++++++++++++ Tests/Parser/FormTypeParserTest.php | 70 ++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 4 deletions(-) create mode 100644 Tests/Fixtures/Form/CollectionType.php create mode 100644 Tests/Parser/FormTypeParserTest.php diff --git a/Parser/FormTypeParser.php b/Parser/FormTypeParser.php index ecace3f..6519c03 100644 --- a/Parser/FormTypeParser.php +++ b/Parser/FormTypeParser.php @@ -11,8 +11,8 @@ namespace Nelmio\ApiDocBundle\Parser; +use Symfony\Component\Form\Exception\UnexpectedTypeException; use Symfony\Component\OptionsResolver\Exception\MissingOptionsException; -use Symfony\Component\Form\FormRegistry; use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\Form\Exception\FormException; @@ -38,10 +38,9 @@ class FormTypeParser implements ParserInterface 'country' => 'string', ); - public function __construct(FormFactoryInterface $formFactory, FormRegistry $formRegistry) + public function __construct(FormFactoryInterface $formFactory) { $this->formFactory = $formFactory; - $this->formRegistry = $formRegistry; } /** @@ -165,8 +164,10 @@ class FormTypeParser implements ParserInterface return $this->formFactory->create($type); } - if ($this->formRegistry->hasType($item)) { + try { return $this->formFactory->create($item); + } catch (UnexpectedTypeException $e) { + // nothing } } } diff --git a/Tests/Fixtures/Form/CollectionType.php b/Tests/Fixtures/Form/CollectionType.php new file mode 100644 index 0000000..1d16dbc --- /dev/null +++ b/Tests/Fixtures/Form/CollectionType.php @@ -0,0 +1,35 @@ +<?php + +/* + * This file is part of the NelmioApiDocBundle. + * + * (c) Nelmio <hello@nelm.io> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Nelmio\ApiDocBundle\Tests\Fixtures\Form; + +use Symfony\Component\Form\AbstractType; +use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\OptionsResolver\OptionsResolverInterface; + +class CollectionType extends AbstractType +{ + /** + * {@inheritdoc} + */ + public function buildForm(FormBuilderInterface $builder, array $options) + { + $builder + ->add('a', 'collection', array('type' => 'text')) + ->add('b', 'collection', array('type' => new TestType())) + ; + } + + public function getName() + { + return 'collection_type'; + } +} diff --git a/Tests/Parser/FormTypeParserTest.php b/Tests/Parser/FormTypeParserTest.php new file mode 100644 index 0000000..5c6f98a --- /dev/null +++ b/Tests/Parser/FormTypeParserTest.php @@ -0,0 +1,70 @@ +<?php +namespace NelmioApiDocBundle\Tests\Parser; + +use Nelmio\ApiDocBundle\Form\Extension\DescriptionFormTypeExtension; +use Nelmio\ApiDocBundle\Parser\FormTypeParser; +use Nelmio\ApiDocBundle\Tests\Fixtures; +use Symfony\Component\Form\Extension\Core\CoreExtension; +use Symfony\Component\Form\FormFactory; +use Symfony\Component\Form\FormFactoryBuilder; +use Symfony\Component\Form\FormRegistry; +use Symfony\Component\Form\ResolvedFormTypeFactory; + +class FormTypeParserTest extends \PHPUnit_Framework_TestCase +{ + /** + * @dataProvider dataTestParse + */ + public function testParse($typeName, $expected) + { + $resolvedTypeFactory = new ResolvedFormTypeFactory(); + $formFactoryBuilder = new FormFactoryBuilder(); + $formFactoryBuilder->setResolvedTypeFactory($resolvedTypeFactory); + $formFactoryBuilder->addExtension(new CoreExtension()); + $formFactoryBuilder->addTypeExtension(new DescriptionFormTypeExtension()); + $formFactory = $formFactoryBuilder->getFormFactory(); + $formTypeParser = new FormTypeParser($formFactory); + $output = $formTypeParser->parse($typeName); + + $this->assertEquals($expected, $output); + } + + public function dataTestParse() + { + return array( + array('Nelmio\ApiDocBundle\Tests\Fixtures\Form\TestType', array( + 'a' => array( + 'dataType' => 'string', + 'required' => true, + 'description' => 'A nice description', + 'readonly' => false + ), + 'b' => array( + 'dataType' => 'string', + 'required' => true, + 'description' => '', + 'readonly' => false + ), + 'c' => array( + 'dataType' => 'boolean', + 'required' => true, + 'description' => '', + 'readonly' => false + )) + ), + array('Nelmio\ApiDocBundle\Tests\Fixtures\Form\CollectionType', array( + 'a' => array( + 'dataType' => 'array of strings', + 'required' => true, + 'description' => '', + 'readonly' => false + ), 'b' => array( + 'dataType' => 'string', + 'required' => true, + 'description' => '', + 'readonly' => false + )) + ), + ); + } +} From 8fa11944b888147babe7d3675d22f4aef8b87a08 Mon Sep 17 00:00:00 2001 From: Adrien SAMSON <asamson.externe@m6.fr> Date: Thu, 21 Mar 2013 10:11:26 +0100 Subject: [PATCH 3/3] Update FormTypeParser service definition --- Resources/config/formatters.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/Resources/config/formatters.xml b/Resources/config/formatters.xml index 98f29c5..424c0a0 100644 --- a/Resources/config/formatters.xml +++ b/Resources/config/formatters.xml @@ -15,7 +15,6 @@ <services> <service id="nelmio_api_doc.parser.form_type_parser" class="%nelmio_api_doc.parser.form_type_parser.class%"> <argument type="service" id="form.factory" /> - <argument type="service" id="form.registry" /> <tag name="nelmio_api_doc.extractor.parser" /> </service> <service id="nelmio_api_doc.formatter.abstract_formatter" class="%nelmio_api_doc.formatter.abstract_formatter.class%" />