123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Tests\Component\Form;
- use Symfony\Component\Form\FormBuilder;
- use Symfony\Component\Form\FormFactory;
- use Symfony\Component\Form\Guess\Guess;
- use Symfony\Component\Form\Guess\ValueGuess;
- use Symfony\Component\Form\Guess\TypeGuess;
- use Symfony\Tests\Component\Form\Fixtures\TestExtension;
- use Symfony\Tests\Component\Form\Fixtures\FooType;
- use Symfony\Tests\Component\Form\Fixtures\FooTypeBarExtension;
- use Symfony\Tests\Component\Form\Fixtures\FooTypeBazExtension;
- class FormFactoryTest extends \PHPUnit_Framework_TestCase
- {
- private $extension1;
- private $extension2;
- private $guesser1;
- private $guesser2;
- private $factory;
- protected function setUp()
- {
- $this->guesser1 = $this->getMock('Symfony\Component\Form\FormTypeGuesserInterface');
- $this->guesser2 = $this->getMock('Symfony\Component\Form\FormTypeGuesserInterface');
- $this->extension1 = new TestExtension($this->guesser1);
- $this->extension2 = new TestExtension($this->guesser2);
- $this->factory = new FormFactory(array($this->extension1, $this->extension2));
- }
- protected function tearDown()
- {
- $this->extension1 = null;
- $this->extension2 = null;
- $this->guesser1 = null;
- $this->guesser2 = null;
- $this->factory = null;
- }
- public function testAddType()
- {
- $this->assertFalse($this->factory->hasType('foo'));
- $type = new FooType();
- $this->factory->addType($type);
- $this->assertTrue($this->factory->hasType('foo'));
- $this->assertSame($type, $this->factory->getType('foo'));
- }
- public function testAddTypeAddsExtensions()
- {
- $type = new FooType();
- $ext1 = new FooTypeBarExtension();
- $ext2 = new FooTypeBazExtension();
- $this->extension1->addTypeExtension($ext1);
- $this->extension2->addTypeExtension($ext2);
- $this->factory->addType($type);
- $this->assertEquals(array($ext1, $ext2), $type->getExtensions());
- }
- public function testGetTypeFromExtension()
- {
- $type = new FooType();
- $this->extension2->addType($type);
- $this->assertSame($type, $this->factory->getType('foo'));
- }
- public function testGetTypeAddsExtensions()
- {
- $type = new FooType();
- $ext1 = new FooTypeBarExtension();
- $ext2 = new FooTypeBazExtension();
- $this->extension1->addTypeExtension($ext1);
- $this->extension2->addTypeExtension($ext2);
- $this->extension2->addType($type);
- $type = $this->factory->getType('foo');
- $this->assertEquals(array($ext1, $ext2), $type->getExtensions());
- }
- /**
- * @expectedException Symfony\Component\Form\Exception\FormException
- */
- public function testGetTypeExpectsExistingType()
- {
- $this->factory->getType('bar');
- }
- public function testCreateNamedBuilder()
- {
- $type = new FooType();
- $this->extension1->addType($type);
- $builder = $this->factory->createNamedBuilder('foo', 'bar');
- $this->assertTrue($builder instanceof FormBuilder);
- $this->assertEquals('bar', $builder->getName());
- }
- public function testCreateNamedBuilderCallsBuildFormMethods()
- {
- $type = new FooType();
- $ext1 = new FooTypeBarExtension();
- $ext2 = new FooTypeBazExtension();
- $this->extension1->addTypeExtension($ext1);
- $this->extension2->addTypeExtension($ext2);
- $this->extension2->addType($type);
- $builder = $this->factory->createNamedBuilder('foo', 'bar');
- $this->assertTrue($builder->hasAttribute('foo'));
- $this->assertTrue($builder->hasAttribute('bar'));
- $this->assertTrue($builder->hasAttribute('baz'));
- }
- public function testCreateNamedBuilderFillsDataOption()
- {
- $type = new FooType();
- $this->extension1->addType($type);
- $builder = $this->factory->createNamedBuilder('foo', 'bar', 'xyz');
- // see FooType::buildForm()
- $this->assertEquals('xyz', $builder->getAttribute('data_option'));
- }
- public function testCreateNamedBuilderDoesNotOverrideExistingDataOption()
- {
- $type = new FooType();
- $this->extension1->addType($type);
- $builder = $this->factory->createNamedBuilder('foo', 'bar', 'xyz', array(
- 'data' => 'abc',
- ));
- // see FooType::buildForm()
- $this->assertEquals('abc', $builder->getAttribute('data_option'));
- }
- /**
- * @expectedException Symfony\Component\Form\Exception\TypeDefinitionException
- */
- public function testCreateNamedBuilderExpectsDataOptionToBeSupported()
- {
- $type = $this->getMock('Symfony\Component\Form\FormTypeInterface');
- $type->expects($this->any())
- ->method('getName')
- ->will($this->returnValue('foo'));
- $type->expects($this->any())
- ->method('getExtensions')
- ->will($this->returnValue(array()));
- $type->expects($this->any())
- ->method('getAllowedOptionValues')
- ->will($this->returnValue(array()));
- $type->expects($this->any())
- ->method('getDefaultOptions')
- ->will($this->returnValue(array(
- 'required' => false,
- 'max_length' => null,
- )));
- $this->extension1->addType($type);
- $this->factory->createNamedBuilder('foo', 'bar');
- }
- /**
- * @expectedException Symfony\Component\Form\Exception\TypeDefinitionException
- */
- public function testCreateNamedBuilderExpectsRequiredOptionToBeSupported()
- {
- $type = $this->getMock('Symfony\Component\Form\FormTypeInterface');
- $type->expects($this->any())
- ->method('getName')
- ->will($this->returnValue('foo'));
- $type->expects($this->any())
- ->method('getExtensions')
- ->will($this->returnValue(array()));
- $type->expects($this->any())
- ->method('getAllowedOptionValues')
- ->will($this->returnValue(array()));
- $type->expects($this->any())
- ->method('getDefaultOptions')
- ->will($this->returnValue(array(
- 'data' => null,
- 'max_length' => null,
- )));
- $this->extension1->addType($type);
- $this->factory->createNamedBuilder('foo', 'bar');
- }
- /**
- * @expectedException Symfony\Component\Form\Exception\TypeDefinitionException
- */
- public function testCreateNamedBuilderExpectsMaxLengthOptionToBeSupported()
- {
- $type = $this->getMock('Symfony\Component\Form\FormTypeInterface');
- $type->expects($this->any())
- ->method('getName')
- ->will($this->returnValue('foo'));
- $type->expects($this->any())
- ->method('getExtensions')
- ->will($this->returnValue(array()));
- $type->expects($this->any())
- ->method('getAllowedOptionValues')
- ->will($this->returnValue(array()));
- $type->expects($this->any())
- ->method('getDefaultOptions')
- ->will($this->returnValue(array(
- 'data' => null,
- 'required' => false,
- )));
- $this->extension1->addType($type);
- $this->factory->createNamedBuilder('foo', 'bar');
- }
- /**
- * @expectedException Symfony\Component\Form\Exception\TypeDefinitionException
- */
- public function testCreateNamedBuilderExpectsBuilderToBeReturned()
- {
- $type = $this->getMock('Symfony\Component\Form\FormTypeInterface');
- $type->expects($this->any())
- ->method('getName')
- ->will($this->returnValue('foo'));
- $type->expects($this->any())
- ->method('getExtensions')
- ->will($this->returnValue(array()));
- $type->expects($this->any())
- ->method('getAllowedOptionValues')
- ->will($this->returnValue(array()));
- $type->expects($this->any())
- ->method('getDefaultOptions')
- ->will($this->returnValue(array(
- 'data' => null,
- 'required' => false,
- 'max_length' => null,
- )));
- $type->expects($this->any())
- ->method('createBuilder')
- ->will($this->returnValue(null));
- $this->extension1->addType($type);
- $this->factory->createNamedBuilder('foo', 'bar');
- }
- /**
- * @expectedException Symfony\Component\Form\Exception\CreationException
- */
- public function testCreateNamedBuilderExpectsOptionsToExist()
- {
- $type = new FooType();
- $this->extension1->addType($type);
- $this->factory->createNamedBuilder('foo', 'bar', null, array(
- 'invalid' => 'xyz',
- ));
- }
- /**
- * @expectedException Symfony\Component\Form\Exception\CreationException
- */
- public function testCreateNamedBuilderExpectsOptionsToBeInValidRange()
- {
- $type = new FooType();
- $this->extension1->addType($type);
- $this->factory->createNamedBuilder('foo', 'bar', null, array(
- 'a_or_b' => 'c',
- ));
- }
- public function testCreateNamedBuilderAllowsExtensionsToExtendAllowedOptionValues()
- {
- $type = new FooType();
- $this->extension1->addType($type);
- $this->extension1->addTypeExtension(new FooTypeBarExtension());
- // no exception this time
- $this->factory->createNamedBuilder('foo', 'bar', null, array(
- 'a_or_b' => 'c',
- ));
- }
- public function testCreateNamedBuilderAddsTypeInstances()
- {
- $type = new FooType();
- $this->assertFalse($this->factory->hasType('foo'));
- $builder = $this->factory->createNamedBuilder($type, 'bar');
- $this->assertTrue($builder instanceof FormBuilder);
- $this->assertTrue($this->factory->hasType('foo'));
- }
- /**
- * @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
- * @expectedExceptionMessage Expected argument of type "string or Symfony\Component\Form\FormTypeInterface", "stdClass" given
- */
- public function testCreateNamedBuilderThrowsUnderstandableException()
- {
- $this->factory->createNamedBuilder(new \StdClass, 'name');
- }
- public function testCreateUsesTypeNameAsName()
- {
- $type = new FooType();
- $this->extension1->addType($type);
- $builder = $this->factory->createBuilder('foo');
- $this->assertEquals('foo', $builder->getName());
- }
- public function testCreateBuilderForPropertyCreatesFieldWithHighestConfidence()
- {
- $this->guesser1->expects($this->once())
- ->method('guessType')
- ->with('Application\Author', 'firstName')
- ->will($this->returnValue(new TypeGuess(
- 'text',
- array('max_length' => 10),
- Guess::MEDIUM_CONFIDENCE
- )));
- $this->guesser2->expects($this->once())
- ->method('guessType')
- ->with('Application\Author', 'firstName')
- ->will($this->returnValue(new TypeGuess(
- 'password',
- array('max_length' => 7),
- Guess::HIGH_CONFIDENCE
- )));
- $factory = $this->createMockFactory(array('createNamedBuilder'));
- $factory->expects($this->once())
- ->method('createNamedBuilder')
- ->with('password', 'firstName', null, array('max_length' => 7))
- ->will($this->returnValue('builderInstance'));
- $builder = $factory->createBuilderForProperty('Application\Author', 'firstName');
- $this->assertEquals('builderInstance', $builder);
- }
- public function testCreateBuilderCreatesTextFieldIfNoGuess()
- {
- $this->guesser1->expects($this->once())
- ->method('guessType')
- ->with('Application\Author', 'firstName')
- ->will($this->returnValue(null));
- $factory = $this->createMockFactory(array('createNamedBuilder'));
- $factory->expects($this->once())
- ->method('createNamedBuilder')
- ->with('text', 'firstName')
- ->will($this->returnValue('builderInstance'));
- $builder = $factory->createBuilderForProperty('Application\Author', 'firstName');
- $this->assertEquals('builderInstance', $builder);
- }
- public function testOptionsCanBeOverridden()
- {
- $this->guesser1->expects($this->once())
- ->method('guessType')
- ->with('Application\Author', 'firstName')
- ->will($this->returnValue(new TypeGuess(
- 'text',
- array('max_length' => 10),
- Guess::MEDIUM_CONFIDENCE
- )));
- $factory = $this->createMockFactory(array('createNamedBuilder'));
- $factory->expects($this->once())
- ->method('createNamedBuilder')
- ->with('text', 'firstName', null, array('max_length' => 11))
- ->will($this->returnValue('builderInstance'));
- $builder = $factory->createBuilderForProperty(
- 'Application\Author',
- 'firstName',
- null,
- array('max_length' => 11)
- );
- $this->assertEquals('builderInstance', $builder);
- }
- public function testCreateBuilderUsesMaxLengthIfFound()
- {
- $this->guesser1->expects($this->once())
- ->method('guessMaxLength')
- ->with('Application\Author', 'firstName')
- ->will($this->returnValue(new ValueGuess(
- 15,
- Guess::MEDIUM_CONFIDENCE
- )));
- $this->guesser2->expects($this->once())
- ->method('guessMaxLength')
- ->with('Application\Author', 'firstName')
- ->will($this->returnValue(new ValueGuess(
- 20,
- Guess::HIGH_CONFIDENCE
- )));
- $factory = $this->createMockFactory(array('createNamedBuilder'));
- $factory->expects($this->once())
- ->method('createNamedBuilder')
- ->with('text', 'firstName', null, array('max_length' => 20))
- ->will($this->returnValue('builderInstance'));
- $builder = $factory->createBuilderForProperty(
- 'Application\Author',
- 'firstName'
- );
- $this->assertEquals('builderInstance', $builder);
- }
- public function testCreateBuilderUsesRequiredSettingWithHighestConfidence()
- {
- $this->guesser1->expects($this->once())
- ->method('guessRequired')
- ->with('Application\Author', 'firstName')
- ->will($this->returnValue(new ValueGuess(
- true,
- Guess::MEDIUM_CONFIDENCE
- )));
- $this->guesser2->expects($this->once())
- ->method('guessRequired')
- ->with('Application\Author', 'firstName')
- ->will($this->returnValue(new ValueGuess(
- false,
- Guess::HIGH_CONFIDENCE
- )));
- $factory = $this->createMockFactory(array('createNamedBuilder'));
- $factory->expects($this->once())
- ->method('createNamedBuilder')
- ->with('text', 'firstName', null, array('required' => false))
- ->will($this->returnValue('builderInstance'));
- $builder = $factory->createBuilderForProperty(
- 'Application\Author',
- 'firstName'
- );
- $this->assertEquals('builderInstance', $builder);
- }
- private function createMockFactory(array $methods = array())
- {
- return $this->getMockBuilder('Symfony\Component\Form\FormFactory')
- ->setMethods($methods)
- ->setConstructorArgs(array(array($this->extension1, $this->extension2)))
- ->getMock();
- }
- }
|