123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430 |
- <?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;
- require_once __DIR__.'/Fixtures/FixedDataTransformer.php';
- use Symfony\Component\Form\Form;
- use Symfony\Component\Form\FormBuilder;
- use Symfony\Component\Form\FormError;
- use Symfony\Tests\Component\Form\Fixtures\FixedDataTransformer;
- class FormTest extends \PHPUnit_Framework_TestCase
- {
- private $dispatcher;
- private $builder;
- private $form;
- protected function setUp()
- {
- $this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
- $this->form = $this->getBuilder()->getForm();
- }
- /**
- * @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
- */
- public function testConstructExpectsValidValidators()
- {
- $validators = array(new \stdClass());
- new Form('name', $this->dispatcher, array(), null, null, null, $validators);
- }
- public function testDataIsInitializedEmpty()
- {
- $norm = new FixedDataTransformer(array(
- '' => 'foo',
- ));
- $client = new FixedDataTransformer(array(
- 'foo' => 'bar',
- ));
- $form = new Form('name', $this->dispatcher, array(), $client, $norm);
- $this->assertNull($form->getData());
- $this->assertSame('foo', $form->getNormData());
- $this->assertSame('bar', $form->getClientData());
- }
- public function testErrorsBubbleUpIfEnabled()
- {
- $error = new FormError('Error!');
- $parent = $this->form;
- $form = $this->getBuilder()->setErrorBubbling(true)->getForm();
- $form->setParent($parent);
- $form->addError($error);
- $this->assertEquals(array(), $form->getErrors());
- $this->assertEquals(array($error), $parent->getErrors());
- }
- public function testErrorsDontBubbleUpIfDisabled()
- {
- $error = new FormError('Error!');
- $parent = $this->form;
- $form = $this->getBuilder()->setErrorBubbling(false)->getForm();
- $form->setParent($parent);
- $form->addError($error);
- $this->assertEquals(array($error), $form->getErrors());
- $this->assertEquals(array(), $parent->getErrors());
- }
- public function testValidIfAllChildrenAreValid()
- {
- $this->form->add($this->getValidForm('firstName'));
- $this->form->add($this->getValidForm('lastName'));
- $this->form->bind(array(
- 'firstName' => 'Bernhard',
- 'lastName' => 'Schussek',
- ));
- $this->assertTrue($this->form->isValid());
- }
- public function testInvalidIfChildrenIsInvalid()
- {
- $this->form->add($this->getValidForm('firstName'));
- $this->form->add($this->getInvalidForm('lastName'));
- $this->form->bind(array(
- 'firstName' => 'Bernhard',
- 'lastName' => 'Schussek',
- ));
- $this->assertFalse($this->form->isValid());
- }
- public function testBind()
- {
- $child = $this->getMockForm('firstName');
- $this->form->add($child);
- $child->expects($this->once())
- ->method('bind')
- ->with($this->equalTo('Bernhard'));
- $this->form->bind(array('firstName' => 'Bernhard'));
- $this->assertEquals(array('firstName' => 'Bernhard'), $this->form->getData());
- }
- public function testBindForwardsNullIfValueIsMissing()
- {
- $child = $this->getMockForm('firstName');
- $this->form->add($child);
- $child->expects($this->once())
- ->method('bind')
- ->with($this->equalTo(null));
- $this->form->bind(array());
- }
- public function testNeverRequiredIfParentNotRequired()
- {
- $parent = $this->getBuilder()->setRequired(false)->getForm();
- $child = $this->getBuilder()->setRequired(true)->getForm();
- $child->setParent($parent);
- $this->assertFalse($child->isRequired());
- }
- public function testRequired()
- {
- $parent = $this->getBuilder()->setRequired(true)->getForm();
- $child = $this->getBuilder()->setRequired(true)->getForm();
- $child->setParent($parent);
- $this->assertTrue($child->isRequired());
- }
- public function testNotRequired()
- {
- $parent = $this->getBuilder()->setRequired(true)->getForm();
- $child = $this->getBuilder()->setRequired(false)->getForm();
- $child->setParent($parent);
- $this->assertFalse($child->isRequired());
- }
- public function testAlwaysReadOnlyIfParentReadOnly()
- {
- $parent = $this->getBuilder()->setReadOnly(true)->getForm();
- $child = $this->getBuilder()->setReadOnly(false)->getForm();
- $child->setParent($parent);
- $this->assertTrue($child->isReadOnly());
- }
- public function testReadOnly()
- {
- $parent = $this->getBuilder()->setReadOnly(false)->getForm();
- $child = $this->getBuilder()->setReadOnly(true)->getForm();
- $child->setParent($parent);
- $this->assertTrue($child->isReadOnly());
- }
- public function testNotReadOnly()
- {
- $parent = $this->getBuilder()->setReadOnly(false)->getForm();
- $child = $this->getBuilder()->setReadOnly(false)->getForm();
- $child->setParent($parent);
- $this->assertFalse($child->isReadOnly());
- }
- public function testCloneChildren()
- {
- $child = $this->getBuilder('child')->getForm();
- $this->form->add($child);
- $clone = clone $this->form;
- $this->assertNotSame($this->form, $clone);
- $this->assertNotSame($child, $clone['child']);
- }
- public function testGetRootReturnsRootOfParent()
- {
- $parent = $this->getMockForm();
- $parent->expects($this->once())
- ->method('getRoot')
- ->will($this->returnValue('ROOT'));
- $this->form->setParent($parent);
- $this->assertEquals('ROOT', $this->form->getRoot());
- }
- public function testGetRootReturnsSelfIfNoParent()
- {
- $this->assertSame($this->form, $this->form->getRoot());
- }
- public function testIsEmptyIfEmptyArray()
- {
- $this->form->setData(array());
- $this->assertTrue($this->form->isEmpty());
- }
- public function testIsEmptyIfNull()
- {
- $this->form->setData(null);
- $this->assertTrue($this->form->isEmpty());
- }
- public function testIsEmptyIfEmptyString()
- {
- $this->form->setData('');
- $this->assertTrue($this->form->isEmpty());
- }
- public function testIsNotEmptyIfText()
- {
- $this->form->setData('foobar');
- $this->assertFalse($this->form->isEmpty());
- }
- public function testIsNotEmptyIfChildNotEmpty()
- {
- $child = $this->getMockForm();
- $child->expects($this->once())
- ->method('isEmpty')
- ->will($this->returnValue(false));
- $this->form->setData(null);
- $this->form->add($child);
- $this->assertFalse($this->form->isEmpty());
- }
- public function testValidIfBound()
- {
- $this->form->bind('foobar');
- $this->assertTrue($this->form->isValid());
- }
- public function testNotValidIfNotBound()
- {
- $this->assertFalse($this->form->isValid());
- }
- public function testNotValidIfErrors()
- {
- $this->form->bind('foobar');
- $this->form->addError(new FormError('Error!'));
- $this->assertFalse($this->form->isValid());
- }
- public function testNotValidIfChildNotValid()
- {
- $child = $this->getMockForm();
- $child->expects($this->once())
- ->method('isValid')
- ->will($this->returnValue(false));
- $this->form->bind('foobar');
- $this->form->add($child);
- $this->assertFalse($this->form->isValid());
- }
- public function testHasErrors()
- {
- $this->form->addError(new FormError('Error!'));
- $this->assertTrue($this->form->hasErrors());
- }
- public function testHasNoErrors()
- {
- $this->assertFalse($this->form->hasErrors());
- }
- public function testHasChildren()
- {
- $this->form->add($this->getBuilder()->getForm());
- $this->assertTrue($this->form->hasChildren());
- }
- public function testHasNoChildren()
- {
- $this->assertFalse($this->form->hasChildren());
- }
- public function testAdd()
- {
- $child = $this->getBuilder('foo')->getForm();
- $this->form->add($child);
- $this->assertSame($this->form, $child->getParent());
- $this->assertSame(array('foo' => $child), $this->form->getChildren());
- }
- public function testRemove()
- {
- $child = $this->getBuilder('foo')->getForm();
- $this->form->add($child);
- $this->form->remove('foo');
- $this->assertNull($child->getParent());
- $this->assertFalse($this->form->hasChildren());
- }
- public function testRemoveIgnoresUnknownName()
- {
- $this->form->remove('notexisting');
- }
- public function testArrayAccess()
- {
- $child = $this->getBuilder('foo')->getForm();
- $this->form[] = $child;
- $this->assertTrue(isset($this->form['foo']));
- $this->assertSame($child, $this->form['foo']);
- unset($this->form['foo']);
- $this->assertFalse(isset($this->form['foo']));
- }
- public function testCountable()
- {
- $this->form->add($this->getBuilder('foo')->getForm());
- $this->form->add($this->getBuilder('bar')->getForm());
- $this->assertEquals(2, count($this->form));
- }
- public function testIterator()
- {
- $this->form->add($this->getBuilder('foo')->getForm());
- $this->form->add($this->getBuilder('bar')->getForm());
- $this->assertSame($this->form->getChildren(), iterator_to_array($this->form));
- }
- public function testIsBound()
- {
- $this->form->bind('foobar');
- $this->assertTrue($this->form->isBound());
- }
- public function testIsNotBound()
- {
- $this->assertFalse($this->form->isBound());
- }
- protected function getBuilder($name = 'name')
- {
- return new FormBuilder($name, $this->dispatcher);
- }
- protected function getMockForm($name = 'name')
- {
- $form = $this->getMock('Symfony\Tests\Component\Form\FormInterface');
- $form->expects($this->any())
- ->method('getName')
- ->will($this->returnValue($name));
- return $form;
- }
- protected function getValidForm($name)
- {
- $form = $this->getMockForm($name);
- $form->expects($this->any())
- ->method('isValid')
- ->will($this->returnValue(true));
- return $form;
- }
- protected function getInvalidForm($name)
- {
- $form = $this->getMockForm($name);
- $form->expects($this->any())
- ->method('isValid')
- ->will($this->returnValue(false));
- return $form;
- }
- }
|