Add tests for FormTypeParser

This commit is contained in:
Adrien SAMSON 2013-03-19 15:43:30 +01:00
parent dee44ee2e1
commit eef1b7db0a
3 changed files with 110 additions and 4 deletions

View File

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

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