123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?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\Form;
- use Symfony\Component\Form\FormBuilder;
- use Symfony\Component\Form\FormError;
- class FormTest extends \PHPUnit_Framework_TestCase
- {
- private $dispatcher;
- private $builder;
- private $form;
- protected function setUp()
- {
- $this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
- $this->builder = new FormBuilder($this->dispatcher);
- $this->form = $this->builder->getForm();
- }
- public function testErrorsBubbleUpIfEnabled()
- {
- $error = new FormError('Error!');
- $parent = $this->form;
- $form = $this->builder->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->builder->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 testAddSetsFieldParent()
- {
- $child = $this->getMockForm('firstName');
- $child->expects($this->once())
- ->method('setParent')
- ->with($this->equalTo($this->form));
- $this->form->add($child);
- }
- protected function getMockForm($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;
- }
- }
|