123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690 |
- <?php
- namespace Symfony\Tests\Component\Form;
- require_once __DIR__ . '/Fixtures/Author.php';
- require_once __DIR__ . '/Fixtures/TestField.php';
- require_once __DIR__ . '/Fixtures/TestFieldGroup.php';
- use Symfony\Component\Form\Field;
- use Symfony\Component\Form\FieldError;
- use Symfony\Component\Form\FieldInterface;
- use Symfony\Component\Form\FieldGroup;
- 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\TestFieldGroup;
- class FieldGroupTest extends \PHPUnit_Framework_TestCase
- {
- public function testSupportsArrayAccess()
- {
- $group = new TestFieldGroup('author');
- $group->add($this->createMockField('firstName'));
- $this->assertEquals($group->get('firstName'), $group['firstName']);
- $this->assertTrue(isset($group['firstName']));
- }
- public function testSupportsUnset()
- {
- $group = new TestFieldGroup('author');
- $group->add($this->createMockField('firstName'));
- unset($group['firstName']);
- $this->assertFalse(isset($group['firstName']));
- }
- public function testDoesNotSupportAddingFields()
- {
- $group = new TestFieldGroup('author');
- $this->setExpectedException('LogicException');
- $group[] = $this->createMockField('lastName');
- }
- public function testSupportsCountable()
- {
- $group = new TestFieldGroup('group');
- $group->add($this->createMockField('firstName'));
- $group->add($this->createMockField('lastName'));
- $this->assertEquals(2, count($group));
- $group->add($this->createMockField('australian'));
- $this->assertEquals(3, count($group));
- }
- public function testSupportsIterable()
- {
- $group = new TestFieldGroup('group');
- $group->add($field1 = $this->createMockField('field1'));
- $group->add($field2 = $this->createMockField('field2'));
- $group->add($field3 = $this->createMockField('field3'));
- $expected = array(
- 'field1' => $field1,
- 'field2' => $field2,
- 'field3' => $field3,
- );
- $this->assertEquals($expected, iterator_to_array($group));
- }
- public function testIsBound()
- {
- $group = new TestFieldGroup('author');
- $this->assertFalse($group->isBound());
- $group->bind(array('firstName' => 'Bernhard'));
- $this->assertTrue($group->isBound());
- }
- public function testValidIfAllFieldsAreValid()
- {
- $group = new TestFieldGroup('author');
- $group->add($this->createValidMockField('firstName'));
- $group->add($this->createValidMockField('lastName'));
- $group->bind(array('firstName' => 'Bernhard', 'lastName' => 'Potencier'));
- $this->assertTrue($group->isValid());
- }
- public function testInvalidIfFieldIsInvalid()
- {
- $group = new TestFieldGroup('author');
- $group->add($this->createInvalidMockField('firstName'));
- $group->add($this->createValidMockField('lastName'));
- $group->bind(array('firstName' => 'Bernhard', 'lastName' => 'Potencier'));
- $this->assertFalse($group->isValid());
- }
- public function testInvalidIfBoundWithExtraFields()
- {
- $group = new TestFieldGroup('author');
- $group->add($this->createValidMockField('firstName'));
- $group->add($this->createValidMockField('lastName'));
- $group->bind(array('foo' => 'bar', 'firstName' => 'Bernhard', 'lastName' => 'Potencier'));
- $this->assertTrue($group->isBoundWithExtraFields());
- }
- public function testHasNoErrorsIfOnlyFieldHasErrors()
- {
- $group = new TestFieldGroup('author');
- $group->add($this->createInvalidMockField('firstName'));
- $group->bind(array('firstName' => 'Bernhard'));
- $this->assertFalse($group->hasErrors());
- }
- public function testBindForwardsPreprocessedData()
- {
- $field = $this->createMockField('firstName');
- $group = $this->getMock(
- 'Symfony\Tests\Component\Form\Fixtures\TestFieldGroup',
- array('preprocessData'), // only mock preprocessData()
- array('author')
- );
- // The data array is prepared directly after binding
- $group->expects($this->once())
- ->method('preprocessData')
- ->with($this->equalTo(array('firstName' => 'Bernhard')))
- ->will($this->returnValue(array('firstName' => 'preprocessed[Bernhard]')));
- $group->add($field);
- // The preprocessed data is then forwarded to the fields
- $field->expects($this->once())
- ->method('bind')
- ->with($this->equalTo('preprocessed[Bernhard]'));
- $group->bind(array('firstName' => 'Bernhard'));
- }
- public function testBindForwardsNullIfValueIsMissing()
- {
- $field = $this->createMockField('firstName');
- $field->expects($this->once())
- ->method('bind')
- ->with($this->equalTo(null));
- $group = new TestFieldGroup('author');
- $group->add($field);
- $group->bind(array());
- }
- public function testAddErrorMapsFieldValidationErrorsOntoFields()
- {
- $error = new FieldError('Message');
- $field = $this->createMockField('firstName');
- $field->expects($this->once())
- ->method('addError')
- ->with($this->equalTo($error));
- $group = new TestFieldGroup('author');
- $group->add($field);
- $path = new PropertyPath('fields[firstName].data');
- $group->addError($error, $path->getIterator(), FieldGroup::FIELD_ERROR);
- }
- public function testAddErrorMapsFieldValidationErrorsOntoFieldsWithinNestedFieldGroups()
- {
- $error = new FieldError('Message');
- $field = $this->createMockField('firstName');
- $field->expects($this->once())
- ->method('addError')
- ->with($this->equalTo($error));
- $group = new TestFieldGroup('author');
- $innerGroup = new TestFieldGroup('names');
- $innerGroup->add($field);
- $group->add($innerGroup);
- $path = new PropertyPath('fields[names].fields[firstName].data');
- $group->addError($error, $path->getIterator(), FieldGroup::FIELD_ERROR);
- }
- public function testAddErrorKeepsFieldValidationErrorsIfFieldNotFound()
- {
- $error = new FieldError('Message');
- $field = $this->createMockField('foo');
- $field->expects($this->never())
- ->method('addError');
- $group = new TestFieldGroup('author');
- $group->add($field);
- $path = new PropertyPath('fields[bar].data');
- $group->addError($error, $path->getIterator(), FieldGroup::FIELD_ERROR);
- $this->assertEquals(array($error), $group->getErrors());
- }
- public function testAddErrorKeepsFieldValidationErrorsIfFieldIsHidden()
- {
- $error = new FieldError('Message');
- $field = $this->createMockField('firstName');
- $field->expects($this->any())
- ->method('isHidden')
- ->will($this->returnValue(true));
- $field->expects($this->never())
- ->method('addError');
- $group = new TestFieldGroup('author');
- $group->add($field);
- $path = new PropertyPath('fields[firstName].data');
- $group->addError($error, $path->getIterator(), FieldGroup::FIELD_ERROR);
- $this->assertEquals(array($error), $group->getErrors());
- }
- public function testAddErrorMapsDataValidationErrorsOntoFields()
- {
- $error = new FieldError('Message');
- // path is expected to point at "firstName"
- $expectedPath = new PropertyPath('firstName');
- $expectedPathIterator = $expectedPath->getIterator();
- $field = $this->createMockField('firstName');
- $field->expects($this->any())
- ->method('getPropertyPath')
- ->will($this->returnValue(new PropertyPath('firstName')));
- $field->expects($this->once())
- ->method('addError')
- ->with($this->equalTo($error), $this->equalTo($expectedPathIterator), $this->equalTo(FieldGroup::DATA_ERROR));
- $group = new TestFieldGroup('author');
- $group->add($field);
- $path = new PropertyPath('firstName');
- $group->addError($error, $path->getIterator(), FieldGroup::DATA_ERROR);
- }
- public function testAddErrorKeepsDataValidationErrorsIfFieldNotFound()
- {
- $error = new FieldError('Message');
- $field = $this->createMockField('foo');
- $field->expects($this->any())
- ->method('getPropertyPath')
- ->will($this->returnValue(new PropertyPath('foo')));
- $field->expects($this->never())
- ->method('addError');
- $group = new TestFieldGroup('author');
- $group->add($field);
- $path = new PropertyPath('bar');
- $group->addError($error, $path->getIterator(), FieldGroup::DATA_ERROR);
- }
- public function testAddErrorKeepsDataValidationErrorsIfFieldIsHidden()
- {
- $error = new FieldError('Message');
- $field = $this->createMockField('firstName');
- $field->expects($this->any())
- ->method('isHidden')
- ->will($this->returnValue(true));
- $field->expects($this->any())
- ->method('getPropertyPath')
- ->will($this->returnValue(new PropertyPath('firstName')));
- $field->expects($this->never())
- ->method('addError');
- $group = new TestFieldGroup('author');
- $group->add($field);
- $path = new PropertyPath('firstName');
- $group->addError($error, $path->getIterator(), FieldGroup::DATA_ERROR);
- }
- public function testAddErrorMapsDataValidationErrorsOntoNestedFields()
- {
- $error = new FieldError('Message');
- // path is expected to point at "street"
- $expectedPath = new PropertyPath('address.street');
- $expectedPathIterator = $expectedPath->getIterator();
- $expectedPathIterator->next();
- $field = $this->createMockField('address');
- $field->expects($this->any())
- ->method('getPropertyPath')
- ->will($this->returnValue(new PropertyPath('address')));
- $field->expects($this->once())
- ->method('addError')
- ->with($this->equalTo($error), $this->equalTo($expectedPathIterator), $this->equalTo(FieldGroup::DATA_ERROR));
- $group = new TestFieldGroup('author');
- $group->add($field);
- $path = new PropertyPath('address.street');
- $group->addError($error, $path->getIterator(), FieldGroup::DATA_ERROR);
- }
- public function testAddErrorMapsErrorsOntoFieldsInAnonymousGroups()
- {
- $error = new FieldError('Message');
- // path is expected to point at "address"
- $expectedPath = new PropertyPath('address');
- $expectedPathIterator = $expectedPath->getIterator();
- $field = $this->createMockField('address');
- $field->expects($this->any())
- ->method('getPropertyPath')
- ->will($this->returnValue(new PropertyPath('address')));
- $field->expects($this->once())
- ->method('addError')
- ->with($this->equalTo($error), $this->equalTo($expectedPathIterator), $this->equalTo(FieldGroup::DATA_ERROR));
- $group = new TestFieldGroup('author');
- $group2 = new TestFieldGroup('anonymous', array('property_path' => null));
- $group2->add($field);
- $group->add($group2);
- $path = new PropertyPath('address');
- $group->addError($error, $path->getIterator(), FieldGroup::DATA_ERROR);
- }
- public function testAddThrowsExceptionIfAlreadyBound()
- {
- $group = new TestFieldGroup('author');
- $group->add($this->createMockField('firstName'));
- $group->bind(array('firstName' => 'Bernhard'));
- $this->setExpectedException('Symfony\Component\Form\Exception\AlreadyBoundException');
- $group->add($this->createMockField('lastName'));
- }
- public function testAddSetsFieldParent()
- {
- $group = new TestFieldGroup('author');
- $field = $this->createMockField('firstName');
- $field->expects($this->once())
- ->method('setParent');
- // PHPUnit fails to compare infinitely recursive objects
- //->with($this->equalTo($group));
- $group->add($field);
- }
- public function testRemoveUnsetsFieldParent()
- {
- $group = new TestFieldGroup('author');
- $field = $this->createMockField('firstName');
- $field->expects($this->exactly(2))
- ->method('setParent');
- // PHPUnit fails to compare subsequent method calls with different arguments
- $group->add($field);
- $group->remove('firstName');
- }
- public function testAddUpdatesFieldFromTransformedData()
- {
- $originalAuthor = new Author();
- $transformedAuthor = new Author();
- // the authors should differ to make sure the test works
- $transformedAuthor->firstName = 'Foo';
- $group = new TestFieldGroup('author');
- $transformer = $this->createMockTransformer();
- $transformer->expects($this->once())
- ->method('transform')
- ->with($this->equalTo($originalAuthor))
- ->will($this->returnValue($transformedAuthor));
- $group->setValueTransformer($transformer);
- $group->setData($originalAuthor);
- $field = $this->createMockField('firstName');
- $field->expects($this->any())
- ->method('getPropertyPath')
- ->will($this->returnValue(new PropertyPath('firstName')));
- $field->expects($this->once())
- ->method('updateFromProperty')
- ->with($this->equalTo($transformedAuthor));
- $group->add($field);
- }
- public function testAddDoesNotUpdateFieldIfTransformedDataIsEmpty()
- {
- $originalAuthor = new Author();
- $group = new TestFieldGroup('author');
- $transformer = $this->createMockTransformer();
- $transformer->expects($this->once())
- ->method('transform')
- ->with($this->equalTo($originalAuthor))
- ->will($this->returnValue(''));
- $group->setValueTransformer($transformer);
- $group->setData($originalAuthor);
- $field = $this->createMockField('firstName');
- $field->expects($this->never())
- ->method('updateFromProperty');
- $group->add($field);
- }
- public function testSetDataUpdatesAllFieldsFromTransformedData()
- {
- $originalAuthor = new Author();
- $transformedAuthor = new Author();
- // the authors should differ to make sure the test works
- $transformedAuthor->firstName = 'Foo';
- $group = new TestFieldGroup('author');
- $transformer = $this->createMockTransformer();
- $transformer->expects($this->once())
- ->method('transform')
- ->with($this->equalTo($originalAuthor))
- ->will($this->returnValue($transformedAuthor));
- $group->setValueTransformer($transformer);
- $field = $this->createMockField('firstName');
- $field->expects($this->once())
- ->method('updateFromProperty')
- ->with($this->equalTo($transformedAuthor));
- $group->add($field);
- $field = $this->createMockField('lastName');
- $field->expects($this->once())
- ->method('updateFromProperty')
- ->with($this->equalTo($transformedAuthor));
- $group->add($field);
- $group->setData($originalAuthor);
- }
- public function testSetDataThrowsAnExceptionIfArgumentIsNotObjectOrArray()
- {
- $group = new TestFieldGroup('author');
- $this->setExpectedException('InvalidArgumentException');
- $group->setData('foobar');
- }
- public function testBindUpdatesTransformedDataFromAllFields()
- {
- $originalAuthor = new Author();
- $transformedAuthor = new Author();
- // the authors should differ to make sure the test works
- $transformedAuthor->firstName = 'Foo';
- $group = new TestFieldGroup('author');
- $transformer = $this->createMockTransformer();
- $transformer->expects($this->exactly(2))
- ->method('transform')
- // the method is first called with NULL, then
- // with $originalAuthor -> not testable by PHPUnit
- // ->with($this->equalTo(null))
- // ->with($this->equalTo($originalAuthor))
- ->will($this->returnValue($transformedAuthor));
- $group->setValueTransformer($transformer);
- $group->setData($originalAuthor);
- $field = $this->createMockField('firstName');
- $field->expects($this->once())
- ->method('updateProperty')
- ->with($this->equalTo($transformedAuthor));
- $group->add($field);
- $field = $this->createMockField('lastName');
- $field->expects($this->once())
- ->method('updateProperty')
- ->with($this->equalTo($transformedAuthor));
- $group->add($field);
- $group->bind(array()); // irrelevant
- }
- public function testGetDataReturnsObject()
- {
- $group = new TestFieldGroup('author');
- $object = new \stdClass();
- $group->setData($object);
- $this->assertEquals($object, $group->getData());
- }
- public function testGetDisplayedDataForwardsCall()
- {
- $field = $this->createValidMockField('firstName');
- $field->expects($this->atLeastOnce())
- ->method('getDisplayedData')
- ->will($this->returnValue('Bernhard'));
- $group = new TestFieldGroup('author');
- $group->add($field);
- $this->assertEquals(array('firstName' => 'Bernhard'), $group->getDisplayedData());
- }
- public function testIsMultipartIfAnyFieldIsMultipart()
- {
- $group = new TestFieldGroup('author');
- $group->add($this->createMultipartMockField('firstName'));
- $group->add($this->createNonMultipartMockField('lastName'));
- $this->assertTrue($group->isMultipart());
- }
- public function testIsNotMultipartIfNoFieldIsMultipart()
- {
- $group = new TestFieldGroup('author');
- $group->add($this->createNonMultipartMockField('firstName'));
- $group->add($this->createNonMultipartMockField('lastName'));
- $this->assertFalse($group->isMultipart());
- }
- public function testSupportsClone()
- {
- $group = new TestFieldGroup('author');
- $group->add($this->createMockField('firstName'));
- $clone = clone $group;
- $this->assertNotSame($clone['firstName'], $group['firstName']);
- }
- public function testBindWithoutPriorSetData()
- {
- return; // TODO
- $field = $this->createMockField('firstName');
- $field->expects($this->any())
- ->method('getData')
- ->will($this->returnValue('Bernhard'));
- $group = new TestFieldGroup('author');
- $group->add($field);
- $group->bind(array('firstName' => 'Bernhard'));
- $this->assertEquals(array('firstName' => 'Bernhard'), $group->getData());
- }
- public function testGetHiddenFieldsReturnsOnlyHiddenFields()
- {
- $group = $this->getGroupWithBothVisibleAndHiddenField();
- $hiddenFields = $group->getHiddenFields(true, false);
- $this->assertSame(array($group['hiddenField']), $hiddenFields);
- }
- public function testGetVisibleFieldsReturnsOnlyVisibleFields()
- {
- $group = $this->getGroupWithBothVisibleAndHiddenField();
- $visibleFields = $group->getVisibleFields(true, false);
- $this->assertSame(array($group['visibleField']), $visibleFields);
- }
- /**
- * Create a group containing two fields, "visibleField" and "hiddenField"
- *
- * @return FieldGroup
- */
- protected function getGroupWithBothVisibleAndHiddenField()
- {
- $group = new TestFieldGroup('testGroup');
- // add a visible field
- $visibleField = $this->createMockField('visibleField');
- $visibleField->expects($this->once())
- ->method('isHidden')
- ->will($this->returnValue(false));
- $group->add($visibleField);
- // add a hidden field
- $hiddenField = $this->createMockField('hiddenField');
- $hiddenField->expects($this->once())
- ->method('isHidden')
- ->will($this->returnValue(true));
- $group->add($hiddenField);
- return $group;
- }
- protected function createMockField($key)
- {
- $field = $this->getMock(
- 'Symfony\Component\Form\FieldInterface',
- array(),
- array(),
- '',
- false, // don't use constructor
- false // don't call parent::__clone
- );
- $field->expects($this->any())
- ->method('getKey')
- ->will($this->returnValue($key));
- return $field;
- }
- protected function createInvalidMockField($key)
- {
- $field = $this->createMockField($key);
- $field->expects($this->any())
- ->method('isValid')
- ->will($this->returnValue(false));
- return $field;
- }
- protected function createValidMockField($key)
- {
- $field = $this->createMockField($key);
- $field->expects($this->any())
- ->method('isValid')
- ->will($this->returnValue(true));
- return $field;
- }
- protected function createNonMultipartMockField($key)
- {
- $field = $this->createMockField($key);
- $field->expects($this->any())
- ->method('isMultipart')
- ->will($this->returnValue(false));
- return $field;
- }
- protected function createMultipartMockField($key)
- {
- $field = $this->createMockField($key);
- $field->expects($this->any())
- ->method('isMultipart')
- ->will($this->returnValue(true));
- return $field;
- }
- protected function createMockTransformer()
- {
- return $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface', array(), array(), '', false, false);
- }
- }
|