123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694 |
- <?php
- namespace Symfony\Tests\Component\Form;
- require_once __DIR__ . '/Fixtures/Author.php';
- require_once __DIR__ . '/Fixtures/TestField.php';
- require_once __DIR__ . '/Fixtures/InvalidField.php';
- require_once __DIR__ . '/Fixtures/RequiredOptionsField.php';
- use Symfony\Component\Form\ValueTransformer\ValueTransformerInterface;
- use Symfony\Component\Form\PropertyPath;
- use Symfony\Tests\Component\Form\Fixtures\Author;
- use Symfony\Tests\Component\Form\Fixtures\TestField;
- use Symfony\Tests\Component\Form\Fixtures\InvalidField;
- use Symfony\Tests\Component\Form\Fixtures\RequiredOptionsField;
- class FieldTest extends \PHPUnit_Framework_TestCase
- {
- protected $field;
- protected function setUp()
- {
- $this->field = new TestField('title');
- }
- public function testGetPropertyPath_defaultPath()
- {
- $field = new TestField('title');
- $this->assertEquals(new PropertyPath('title'), $field->getPropertyPath());
- }
- public function testGetPropertyPath_pathIsZero()
- {
- $field = new TestField('title', array('property_path' => '0'));
- $this->assertEquals(new PropertyPath('0'), $field->getPropertyPath());
- }
- public function testGetPropertyPath_pathIsEmpty()
- {
- $field = new TestField('title', array('property_path' => ''));
- $this->assertEquals(null, $field->getPropertyPath());
- }
- public function testGetPropertyPath_pathIsNull()
- {
- $field = new TestField('title', array('property_path' => null));
- $this->assertEquals(null, $field->getPropertyPath());
- }
- public function testPassRequiredAsOption()
- {
- $field = new TestField('title', array('required' => false));
- $this->assertFalse($field->isRequired());
- $field = new TestField('title', array('required' => true));
- $this->assertTrue($field->isRequired());
- }
- public function testPassDisabledAsOption()
- {
- $field = new TestField('title', array('disabled' => false));
- $this->assertFalse($field->isDisabled());
- $field = new TestField('title', array('disabled' => true));
- $this->assertTrue($field->isDisabled());
- }
- public function testFieldIsDisabledIfParentIsDisabled()
- {
- $field = new TestField('title', array('disabled' => false));
- $field->setParent(new TestField('title', array('disabled' => true)));
- $this->assertTrue($field->isDisabled());
- }
- public function testFieldWithNoErrorsIsValid()
- {
- $this->field->bind('data');
- $this->assertTrue($this->field->isValid());
- }
- public function testFieldWithErrorsIsInvalid()
- {
- $this->field->bind('data');
- $this->field->addError('Some error');
- $this->assertFalse($this->field->isValid());
- }
- public function testBindResetsErrors()
- {
- $this->field->addError('Some error');
- $this->field->bind('data');
- $this->assertTrue($this->field->isValid());
- }
- public function testUnboundFieldIsInvalid()
- {
- $this->assertFalse($this->field->isValid());
- }
- public function testGetNameReturnsKey()
- {
- $this->assertEquals('title', $this->field->getName());
- }
- public function testGetNameIncludesParent()
- {
- $this->field->setParent($this->createMockGroupWithName('news[article]'));
- $this->assertEquals('news[article][title]', $this->field->getName());
- }
- public function testGetIdReturnsKey()
- {
- $this->assertEquals('title', $this->field->getId());
- }
- public function testGetIdIncludesParent()
- {
- $this->field->setParent($this->createMockGroupWithId('news_article'));
- $this->assertEquals('news_article_title', $this->field->getId());
- }
- public function testLocaleIsPassedToLocalizableValueTransformer_setLocaleCalledBefore()
- {
- $transformer = $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface');
- $transformer->expects($this->once())
- ->method('setLocale')
- ->with($this->equalTo('de_DE'));
- $this->field->setLocale('de_DE');
- $this->field->setValueTransformer($transformer);
- }
- public function testLocaleIsPassedToValueTransformer_setLocaleCalledAfter()
- {
- $transformer = $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface');
- $transformer->expects($this->exactly(2))
- ->method('setLocale'); // we can't test the params cause they differ :(
- $this->field->setValueTransformer($transformer);
- $this->field->setLocale('de_DE');
- }
- public function testIsRequiredReturnsOwnValueIfNoParent()
- {
- $this->field->setRequired(true);
- $this->assertTrue($this->field->isRequired());
- $this->field->setRequired(false);
- $this->assertFalse($this->field->isRequired());
- }
- public function testIsRequiredReturnsOwnValueIfParentIsRequired()
- {
- $group = $this->createMockGroup();
- $group->expects($this->any())
- ->method('isRequired')
- ->will($this->returnValue(true));
- $this->field->setParent($group);
- $this->field->setRequired(true);
- $this->assertTrue($this->field->isRequired());
- $this->field->setRequired(false);
- $this->assertFalse($this->field->isRequired());
- }
- public function testIsRequiredReturnsFalseIfParentIsNotRequired()
- {
- $group = $this->createMockGroup();
- $group->expects($this->any())
- ->method('isRequired')
- ->will($this->returnValue(false));
- $this->field->setParent($group);
- $this->field->setRequired(true);
- $this->assertFalse($this->field->isRequired());
- }
- public function testExceptionIfUnknownOption()
- {
- $this->setExpectedException('Symfony\Component\Form\Exception\InvalidOptionsException');
- new RequiredOptionsField('name', array('bar' => 'baz', 'moo' => 'maa'));
- }
- public function testExceptionIfMissingOption()
- {
- $this->setExpectedException('Symfony\Component\Form\Exception\MissingOptionsException');
- new RequiredOptionsField('name');
- }
- public function testIsBound()
- {
- $this->assertFalse($this->field->isBound());
- $this->field->bind('symfony');
- $this->assertTrue($this->field->isBound());
- }
- public function testDefaultValuesAreTransformedCorrectly()
- {
- $field = new TestField('name');
- $this->assertEquals(null, $this->field->getData());
- $this->assertEquals('', $this->field->getDisplayedData());
- }
- public function testValuesAreTransformedCorrectlyIfNull()
- {
- // The value is converted to an empty string and NOT passed to the
- // value transformer
- $transformer = $this->createMockTransformer();
- $transformer->expects($this->never())
- ->method('transform');
- $this->field->setValueTransformer($transformer);
- $this->field->setData(null);
- $this->assertSame(null, $this->field->getData());
- $this->assertSame('', $this->field->getDisplayedData());
- }
- public function testValuesAreTransformedCorrectlyIfNull_noValueTransformer()
- {
- $this->field->setData(null);
- $this->assertSame(null, $this->field->getData());
- $this->assertSame('', $this->field->getDisplayedData());
- }
- public function testBoundValuesAreTransformedCorrectly()
- {
- $field = $this->getMock(
- 'Symfony\Tests\Component\Form\Fixtures\TestField',
- array('processData'), // only mock processData()
- array('title')
- );
- // 1. The value is converted to a string and passed to the value transformer
- $transformer = $this->createMockTransformer();
- $transformer->expects($this->once())
- ->method('reverseTransform')
- ->with($this->identicalTo('0'))
- ->will($this->returnValue('reverse[0]'));
- $field->setValueTransformer($transformer);
- // 2. The output of the reverse transformation is passed to processData()
- $field->expects($this->once())
- ->method('processData')
- ->with($this->equalTo('reverse[0]'))
- ->will($this->returnValue('processed[reverse[0]]'));
- // 3. The processed data is transformed again (for displayed data)
- $transformer->expects($this->once())
- ->method('transform')
- ->with($this->equalTo('processed[reverse[0]]'))
- ->will($this->returnValue('transform[processed[reverse[0]]]'));
- $field->bind(0);
- $this->assertEquals('processed[reverse[0]]', $field->getData());
- $this->assertEquals('transform[processed[reverse[0]]]', $field->getDisplayedData());
- }
- public function testBoundValuesAreTransformedCorrectlyIfEmpty_processDataReturnsValue()
- {
- $field = $this->getMock(
- 'Symfony\Tests\Component\Form\Fixtures\TestField',
- array('processData'), // only mock processData()
- array('title')
- );
- // 1. Empty values are always converted to NULL. They are never passed to
- // the value transformer
- $transformer = $this->createMockTransformer();
- $transformer->expects($this->never())
- ->method('reverseTransform');
- $field->setValueTransformer($transformer);
- // 2. NULL is passed to processData()
- $field->expects($this->once())
- ->method('processData')
- ->with($this->identicalTo(null))
- ->will($this->returnValue('processed'));
- // 3. The processed data is transformed (for displayed data)
- $transformer->expects($this->once())
- ->method('transform')
- ->with($this->equalTo('processed'))
- ->will($this->returnValue('transform[processed]'));
- $field->bind('');
- $this->assertSame('processed', $field->getData());
- $this->assertEquals('transform[processed]', $field->getDisplayedData());
- }
- public function testBoundValuesAreTransformedCorrectlyIfEmpty_processDataReturnsNull()
- {
- // 1. Empty values are always converted to NULL. They are never passed to
- // the value transformer
- $transformer = $this->createMockTransformer();
- $transformer->expects($this->never())
- ->method('reverseTransform');
- $this->field->setValueTransformer($transformer);
- // 2. The processed data is NULL and therefore transformed to an empty
- // string. It is NOT passed to the value transformer
- $transformer->expects($this->never())
- ->method('transform');
- $this->field->bind('');
- $this->assertSame(null, $this->field->getData());
- $this->assertEquals('', $this->field->getDisplayedData());
- }
- public function testBoundValuesAreTransformedCorrectlyIfEmpty_processDataReturnsNull_noValueTransformer()
- {
- $this->field->bind('');
- $this->assertSame(null, $this->field->getData());
- $this->assertEquals('', $this->field->getDisplayedData());
- }
- public function testValuesAreTransformedCorrectly()
- {
- // The value is passed to the value transformer
- $transformer = $this->createMockTransformer();
- $transformer->expects($this->once())
- ->method('transform')
- ->with($this->identicalTo(0))
- ->will($this->returnValue('transform[0]'));
- $this->field->setValueTransformer($transformer);
- $this->field->setData(0);
- $this->assertEquals(0, $this->field->getData());
- $this->assertEquals('transform[0]', $this->field->getDisplayedData());
- }
- public function testBoundValuesAreTrimmedBeforeTransforming()
- {
- // The value is passed to the value transformer
- $transformer = $this->createMockTransformer();
- $transformer->expects($this->once())
- ->method('reverseTransform')
- ->with($this->identicalTo('a'))
- ->will($this->returnValue('reverse[a]'));
- $transformer->expects($this->once())
- ->method('transform')
- ->with($this->identicalTo('reverse[a]'))
- ->will($this->returnValue('a'));
- $this->field->setValueTransformer($transformer);
- $this->field->bind(' a ');
- $this->assertEquals('a', $this->field->getDisplayedData());
- $this->assertEquals('reverse[a]', $this->field->getData());
- }
- public function testBoundValuesAreNotTrimmedBeforeTransformingIfDisabled()
- {
- // The value is passed to the value transformer
- $transformer = $this->createMockTransformer();
- $transformer->expects($this->once())
- ->method('reverseTransform')
- ->with($this->identicalTo(' a '))
- ->will($this->returnValue('reverse[ a ]'));
- $transformer->expects($this->once())
- ->method('transform')
- ->with($this->identicalTo('reverse[ a ]'))
- ->will($this->returnValue(' a '));
- $field = new TestField('title', array('trim' => false));
- $field->setValueTransformer($transformer);
- $field->bind(' a ');
- $this->assertEquals(' a ', $field->getDisplayedData());
- $this->assertEquals('reverse[ a ]', $field->getData());
- }
- public function testUpdateFromObjectReadsArray()
- {
- $array = array('firstName' => 'Bernhard');
- $field = new TestField('firstName');
- $field->updateFromObject($array);
- $this->assertEquals('Bernhard', $field->getData());
- }
- public function testUpdateFromObjectReadsArrayWithCustomPropertyPath()
- {
- $array = array('child' => array('index' => array('firstName' => 'Bernhard')));
- $field = new TestField('firstName', array('property_path' => 'child[index].firstName'));
- $field->updateFromObject($array);
- $this->assertEquals('Bernhard', $field->getData());
- }
- public function testUpdateFromObjectReadsProperty()
- {
- $object = new Author();
- $object->firstName = 'Bernhard';
- $field = new TestField('firstName');
- $field->updateFromObject($object);
- $this->assertEquals('Bernhard', $field->getData());
- }
- public function testUpdateFromObjectReadsPropertyWithCustomPropertyPath()
- {
- $object = new Author();
- $object->child = array();
- $object->child['index'] = new Author();
- $object->child['index']->firstName = 'Bernhard';
- $field = new TestField('firstName', array('property_path' => 'child[index].firstName'));
- $field->updateFromObject($object);
- $this->assertEquals('Bernhard', $field->getData());
- }
- public function testUpdateFromObjectReadsArrayAccess()
- {
- $object = new \ArrayObject();
- $object['firstName'] = 'Bernhard';
- $field = new TestField('firstName', array('property_path' => '[firstName]'));
- $field->updateFromObject($object);
- $this->assertEquals('Bernhard', $field->getData());
- }
- public function testUpdateFromObjectThrowsExceptionIfArrayAccessExpected()
- {
- $field = new TestField('firstName', array('property_path' => '[firstName]'));
- $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyException');
- $field->updateFromObject(new Author());
- }
- /*
- * The use case of this test is a field group with an empty property path.
- * Even if the field group itself is not associated to a specific property,
- * nested fields might be.
- */
- public function testUpdateFromObjectPassesObjectThroughIfPropertyPathIsEmpty()
- {
- $object = new Author();
- $object->firstName = 'Bernhard';
- $field = new TestField('firstName', array('property_path' => null));
- $field->updateFromObject($object);
- $this->assertEquals($object, $field->getData());
- }
- public function testUpdateFromObjectThrowsExceptionIfPropertyIsNotPublic()
- {
- $field = new TestField('privateProperty');
- $this->setExpectedException('Symfony\Component\Form\Exception\PropertyAccessDeniedException');
- $field->updateFromObject(new Author());
- }
- public function testUpdateFromObjectReadsGetters()
- {
- $object = new Author();
- $object->setLastName('Schussek');
- $field = new TestField('lastName');
- $field->updateFromObject($object);
- $this->assertEquals('Schussek', $field->getData());
- }
- public function testUpdateFromObjectThrowsExceptionIfGetterIsNotPublic()
- {
- $field = new TestField('privateGetter');
- $this->setExpectedException('Symfony\Component\Form\Exception\PropertyAccessDeniedException');
- $field->updateFromObject(new Author());
- }
- public function testUpdateFromObjectReadsIssers()
- {
- $object = new Author();
- $object->setAustralian(false);
- $field = new TestField('australian');
- $field->updateFromObject($object);
- $this->assertSame(false, $field->getData());
- }
- public function testUpdateFromObjectThrowsExceptionIfIsserIsNotPublic()
- {
- $field = new TestField('privateIsser');
- $this->setExpectedException('Symfony\Component\Form\Exception\PropertyAccessDeniedException');
- $field->updateFromObject(new Author());
- }
- public function testUpdateFromObjectThrowsExceptionIfPropertyDoesNotExist()
- {
- $field = new TestField('foobar');
- $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyException');
- $field->updateFromObject(new Author());
- }
- public function testUpdateObjectUpdatesArrays()
- {
- $array = array();
- $field = new TestField('firstName');
- $field->bind('Bernhard');
- $field->updateObject($array);
- $this->assertEquals(array('firstName' => 'Bernhard'), $array);
- }
- public function testUpdateObjectUpdatesArraysWithCustomPropertyPath()
- {
- $array = array();
- $field = new TestField('firstName', array('property_path' => 'child[index].firstName'));
- $field->bind('Bernhard');
- $field->updateObject($array);
- $this->assertEquals(array('child' => array('index' => array('firstName' => 'Bernhard'))), $array);
- }
- /*
- * This is important so that bind() can work even if setData() was not called
- * before
- */
- public function testUpdateObjectTreatsEmptyValuesAsArrays()
- {
- $array = null;
- $field = new TestField('firstName');
- $field->bind('Bernhard');
- $field->updateObject($array);
- $this->assertEquals(array('firstName' => 'Bernhard'), $array);
- }
- public function testUpdateObjectUpdatesProperties()
- {
- $object = new Author();
- $field = new TestField('firstName');
- $field->bind('Bernhard');
- $field->updateObject($object);
- $this->assertEquals('Bernhard', $object->firstName);
- }
- public function testUpdateObjectUpdatesPropertiesWithCustomPropertyPath()
- {
- $object = new Author();
- $object->child = array();
- $object->child['index'] = new Author();
- $field = new TestField('firstName', array('property_path' => 'child[index].firstName'));
- $field->bind('Bernhard');
- $field->updateObject($object);
- $this->assertEquals('Bernhard', $object->child['index']->firstName);
- }
- public function testUpdateObjectUpdatesArrayAccess()
- {
- $object = new \ArrayObject();
- $field = new TestField('firstName', array('property_path' => '[firstName]'));
- $field->bind('Bernhard');
- $field->updateObject($object);
- $this->assertEquals('Bernhard', $object['firstName']);
- }
- public function testUpdateObjectThrowsExceptionIfArrayAccessExpected()
- {
- $field = new TestField('firstName', array('property_path' => '[firstName]'));
- $field->bind('Bernhard');
- $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyException');
- $field->updateObject(new Author());
- }
- public function testUpdateObjectDoesNotUpdatePropertyIfPropertyPathIsEmpty()
- {
- $object = new Author();
- $field = new TestField('firstName', array('property_path' => null));
- $field->bind('Bernhard');
- $field->updateObject($object);
- $this->assertEquals(null, $object->firstName);
- }
- public function testUpdateObjectUpdatesSetters()
- {
- $object = new Author();
- $field = new TestField('lastName');
- $field->bind('Schussek');
- $field->updateObject($object);
- $this->assertEquals('Schussek', $object->getLastName());
- }
- public function testUpdateObjectThrowsExceptionIfGetterIsNotPublic()
- {
- $field = new TestField('privateSetter');
- $field->bind('foobar');
- $this->setExpectedException('Symfony\Component\Form\Exception\PropertyAccessDeniedException');
- $field->updateObject(new Author());
- }
- protected function createMockTransformer()
- {
- return $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface', array(), array(), '', false, false);
- }
- protected function createMockTransformerTransformingTo($value)
- {
- $transformer = $this->createMockTransformer();
- $transformer->expects($this->any())
- ->method('reverseTransform')
- ->will($this->returnValue($value));
- return $transformer;
- }
- protected function createMockGroup()
- {
- return $this->getMock(
- 'Symfony\Component\Form\FieldGroup',
- array(),
- array(),
- '',
- false // don't call constructor
- );
- }
- protected function createMockGroupWithName($name)
- {
- $group = $this->createMockGroup();
- $group->expects($this->any())
- ->method('getName')
- ->will($this->returnValue($name));
- return $group;
- }
- protected function createMockGroupWithId($id)
- {
- $group = $this->createMockGroup();
- $group->expects($this->any())
- ->method('getId')
- ->will($this->returnValue($id));
- return $group;
- }
- }
|