Merge pull request #152 from adriensamson/issue-147

Fix Illegal offset warning in FormTypeParser (closes #147)
This commit is contained in:
William Durand 2013-06-18 06:44:06 -07:00
commit 0eb7ec27ec
4 changed files with 114 additions and 7 deletions

View File

@ -11,8 +11,8 @@
namespace Nelmio\ApiDocBundle\Parser; namespace Nelmio\ApiDocBundle\Parser;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\OptionsResolver\Exception\MissingOptionsException; use Symfony\Component\OptionsResolver\Exception\MissingOptionsException;
use Symfony\Component\Form\FormRegistry;
use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\Exception\FormException; use Symfony\Component\Form\Exception\FormException;
@ -38,10 +38,9 @@ class FormTypeParser implements ParserInterface
'country' => 'string', 'country' => 'string',
); );
public function __construct(FormFactoryInterface $formFactory, FormRegistry $formRegistry) public function __construct(FormFactoryInterface $formFactory)
{ {
$this->formFactory = $formFactory; $this->formFactory = $formFactory;
$this->formRegistry = $formRegistry;
} }
/** /**
@ -94,10 +93,12 @@ class FormTypeParser implements ParserInterface
for ($type = $config->getType(); null !== $type; $type = $type->getParent()) { for ($type = $config->getType(); null !== $type; $type = $type->getParent()) {
if (isset($this->mapTypes[$type->getName()])) { if (isset($this->mapTypes[$type->getName()])) {
$bestType = $this->mapTypes[$type->getName()]; $bestType = $this->mapTypes[$type->getName()];
} elseif ('collection' === $type->getName() && isset($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')]); $bestType = sprintf('array of %ss', $this->mapTypes[$config->getOption('type')]);
} }
} }
}
if ('' === $bestType) { if ('' === $bestType) {
if ($type = $config->getType()) { if ($type = $config->getType()) {
@ -167,8 +168,10 @@ class FormTypeParser implements ParserInterface
return $this->formFactory->create($type); return $this->formFactory->create($type);
} }
if ($this->formRegistry->hasType($item)) { try {
return $this->formFactory->create($item); return $this->formFactory->create($item);
} catch (UnexpectedTypeException $e) {
// nothing
} }
} }
} }

View File

@ -15,7 +15,6 @@
<services> <services>
<service id="nelmio_api_doc.parser.form_type_parser" class="%nelmio_api_doc.parser.form_type_parser.class%"> <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.factory" />
<argument type="service" id="form.registry" />
<tag name="nelmio_api_doc.extractor.parser" /> <tag name="nelmio_api_doc.extractor.parser" />
</service> </service>
<service id="nelmio_api_doc.formatter.abstract_formatter" class="%nelmio_api_doc.formatter.abstract_formatter.class%" /> <service id="nelmio_api_doc.formatter.abstract_formatter" class="%nelmio_api_doc.formatter.abstract_formatter.class%" />

View File

@ -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';
}
}

View File

@ -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
))
),
);
}
}