2010-06-24 09:40:05 +01:00
|
|
|
<?php
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
namespace Symfony\Tests\Component\Form;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
|
|
|
require_once __DIR__ . '/Fixtures/Author.php';
|
|
|
|
require_once __DIR__ . '/Fixtures/TestField.php';
|
2010-10-21 20:18:27 +01:00
|
|
|
require_once __DIR__ . '/Fixtures/TestFieldGroup.php';
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\Form\Field;
|
2010-11-18 22:33:44 +00:00
|
|
|
use Symfony\Component\Form\FieldError;
|
2010-08-20 22:09:55 +01:00
|
|
|
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;
|
2010-10-21 20:18:27 +01:00
|
|
|
use Symfony\Tests\Component\Form\Fixtures\TestFieldGroup;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
|
|
|
class FieldGroupTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testSupportsArrayAccess()
|
|
|
|
{
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('author');
|
2010-06-24 10:24:08 +01:00
|
|
|
$group->add($this->createMockField('firstName'));
|
|
|
|
$this->assertEquals($group->get('firstName'), $group['firstName']);
|
|
|
|
$this->assertTrue(isset($group['firstName']));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSupportsUnset()
|
|
|
|
{
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('author');
|
2010-06-24 10:24:08 +01:00
|
|
|
$group->add($this->createMockField('firstName'));
|
|
|
|
unset($group['firstName']);
|
|
|
|
$this->assertFalse(isset($group['firstName']));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDoesNotSupportAddingFields()
|
|
|
|
{
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('author');
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->setExpectedException('LogicException');
|
|
|
|
$group[] = $this->createMockField('lastName');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSupportsCountable()
|
|
|
|
{
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('group');
|
2010-06-24 10:24:08 +01:00
|
|
|
$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()
|
|
|
|
{
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('group');
|
2010-06-24 10:24:08 +01:00
|
|
|
$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()
|
|
|
|
{
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('author');
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->assertFalse($group->isBound());
|
|
|
|
$group->bind(array('firstName' => 'Bernhard'));
|
|
|
|
$this->assertTrue($group->isBound());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidIfAllFieldsAreValid()
|
|
|
|
{
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('author');
|
2010-06-24 10:24:08 +01:00
|
|
|
$group->add($this->createValidMockField('firstName'));
|
|
|
|
$group->add($this->createValidMockField('lastName'));
|
|
|
|
|
|
|
|
$group->bind(array('firstName' => 'Bernhard', 'lastName' => 'Potencier'));
|
|
|
|
|
|
|
|
$this->assertTrue($group->isValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvalidIfFieldIsInvalid()
|
|
|
|
{
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('author');
|
2010-06-24 10:24:08 +01:00
|
|
|
$group->add($this->createInvalidMockField('firstName'));
|
|
|
|
$group->add($this->createValidMockField('lastName'));
|
|
|
|
|
|
|
|
$group->bind(array('firstName' => 'Bernhard', 'lastName' => 'Potencier'));
|
|
|
|
|
|
|
|
$this->assertFalse($group->isValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvalidIfBoundWithExtraFields()
|
|
|
|
{
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('author');
|
2010-06-24 10:24:08 +01:00
|
|
|
$group->add($this->createValidMockField('firstName'));
|
|
|
|
$group->add($this->createValidMockField('lastName'));
|
|
|
|
|
|
|
|
$group->bind(array('foo' => 'bar', 'firstName' => 'Bernhard', 'lastName' => 'Potencier'));
|
|
|
|
|
|
|
|
$this->assertTrue($group->isBoundWithExtraFields());
|
|
|
|
}
|
|
|
|
|
2010-11-16 18:54:26 +00:00
|
|
|
public function testHasNoErrorsIfOnlyFieldHasErrors()
|
|
|
|
{
|
|
|
|
$group = new TestFieldGroup('author');
|
|
|
|
$group->add($this->createInvalidMockField('firstName'));
|
|
|
|
|
|
|
|
$group->bind(array('firstName' => 'Bernhard'));
|
|
|
|
|
|
|
|
$this->assertFalse($group->hasErrors());
|
|
|
|
}
|
|
|
|
|
2010-11-22 22:15:29 +00:00
|
|
|
public function testBindForwardsPreprocessedData()
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
|
|
|
$field = $this->createMockField('firstName');
|
|
|
|
|
2010-11-22 22:15:29 +00:00
|
|
|
$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]')));
|
2010-06-24 10:24:08 +01:00
|
|
|
$group->add($field);
|
|
|
|
|
2010-11-22 22:15:29 +00:00
|
|
|
// The preprocessed data is then forwarded to the fields
|
|
|
|
$field->expects($this->once())
|
|
|
|
->method('bind')
|
|
|
|
->with($this->equalTo('preprocessed[Bernhard]'));
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$group->bind(array('firstName' => 'Bernhard'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBindForwardsNullIfValueIsMissing()
|
|
|
|
{
|
|
|
|
$field = $this->createMockField('firstName');
|
|
|
|
$field->expects($this->once())
|
|
|
|
->method('bind')
|
|
|
|
->with($this->equalTo(null));
|
|
|
|
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('author');
|
2010-06-24 10:24:08 +01:00
|
|
|
$group->add($field);
|
|
|
|
|
|
|
|
$group->bind(array());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddErrorMapsFieldValidationErrorsOntoFields()
|
|
|
|
{
|
2010-11-18 22:33:44 +00:00
|
|
|
$error = new FieldError('Message');
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$field = $this->createMockField('firstName');
|
|
|
|
$field->expects($this->once())
|
|
|
|
->method('addError')
|
2010-11-18 22:33:44 +00:00
|
|
|
->with($this->equalTo($error));
|
2010-06-24 10:24:08 +01:00
|
|
|
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('author');
|
2010-06-24 10:24:08 +01:00
|
|
|
$group->add($field);
|
|
|
|
|
2010-10-10 22:15:28 +01:00
|
|
|
$path = new PropertyPath('fields[firstName].data');
|
|
|
|
|
2010-11-18 22:33:44 +00:00
|
|
|
$group->addError($error, $path->getIterator(), FieldGroup::FIELD_ERROR);
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
2010-07-15 16:36:13 +01:00
|
|
|
public function testAddErrorMapsFieldValidationErrorsOntoFieldsWithinNestedFieldGroups()
|
|
|
|
{
|
2010-11-18 22:33:44 +00:00
|
|
|
$error = new FieldError('Message');
|
|
|
|
|
2010-07-15 16:36:13 +01:00
|
|
|
$field = $this->createMockField('firstName');
|
|
|
|
$field->expects($this->once())
|
|
|
|
->method('addError')
|
2010-11-18 22:33:44 +00:00
|
|
|
->with($this->equalTo($error));
|
2010-07-15 16:36:13 +01:00
|
|
|
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('author');
|
|
|
|
$innerGroup = new TestFieldGroup('names');
|
2010-07-15 16:36:13 +01:00
|
|
|
$innerGroup->add($field);
|
|
|
|
$group->add($innerGroup);
|
|
|
|
|
2010-10-10 22:15:28 +01:00
|
|
|
$path = new PropertyPath('fields[names].fields[firstName].data');
|
|
|
|
|
2010-11-18 22:33:44 +00:00
|
|
|
$group->addError($error, $path->getIterator(), FieldGroup::FIELD_ERROR);
|
2010-07-15 16:36:13 +01:00
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testAddErrorKeepsFieldValidationErrorsIfFieldNotFound()
|
|
|
|
{
|
2010-11-18 22:33:44 +00:00
|
|
|
$error = new FieldError('Message');
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$field = $this->createMockField('foo');
|
|
|
|
$field->expects($this->never())
|
|
|
|
->method('addError');
|
|
|
|
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('author');
|
2010-06-24 10:24:08 +01:00
|
|
|
$group->add($field);
|
|
|
|
|
2010-10-10 22:15:28 +01:00
|
|
|
$path = new PropertyPath('fields[bar].data');
|
|
|
|
|
2010-11-18 22:33:44 +00:00
|
|
|
$group->addError($error, $path->getIterator(), FieldGroup::FIELD_ERROR);
|
2010-06-24 10:24:08 +01:00
|
|
|
|
2010-11-18 22:33:44 +00:00
|
|
|
$this->assertEquals(array($error), $group->getErrors());
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddErrorKeepsFieldValidationErrorsIfFieldIsHidden()
|
|
|
|
{
|
2010-11-18 22:33:44 +00:00
|
|
|
$error = new FieldError('Message');
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$field = $this->createMockField('firstName');
|
|
|
|
$field->expects($this->any())
|
|
|
|
->method('isHidden')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
$field->expects($this->never())
|
|
|
|
->method('addError');
|
|
|
|
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('author');
|
2010-06-24 10:24:08 +01:00
|
|
|
$group->add($field);
|
|
|
|
|
2010-10-10 22:15:28 +01:00
|
|
|
$path = new PropertyPath('fields[firstName].data');
|
|
|
|
|
2010-11-18 22:33:44 +00:00
|
|
|
$group->addError($error, $path->getIterator(), FieldGroup::FIELD_ERROR);
|
2010-06-24 10:24:08 +01:00
|
|
|
|
2010-11-18 22:33:44 +00:00
|
|
|
$this->assertEquals(array($error), $group->getErrors());
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddErrorMapsDataValidationErrorsOntoFields()
|
|
|
|
{
|
2010-11-18 22:33:44 +00:00
|
|
|
$error = new FieldError('Message');
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
// path is expected to point at "firstName"
|
|
|
|
$expectedPath = new PropertyPath('firstName');
|
2010-10-10 22:15:28 +01:00
|
|
|
$expectedPathIterator = $expectedPath->getIterator();
|
2010-06-24 10:24:08 +01:00
|
|
|
|
|
|
|
$field = $this->createMockField('firstName');
|
|
|
|
$field->expects($this->any())
|
|
|
|
->method('getPropertyPath')
|
|
|
|
->will($this->returnValue(new PropertyPath('firstName')));
|
|
|
|
$field->expects($this->once())
|
|
|
|
->method('addError')
|
2010-11-18 22:33:44 +00:00
|
|
|
->with($this->equalTo($error), $this->equalTo($expectedPathIterator), $this->equalTo(FieldGroup::DATA_ERROR));
|
2010-06-24 10:24:08 +01:00
|
|
|
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('author');
|
2010-06-24 10:24:08 +01:00
|
|
|
$group->add($field);
|
|
|
|
|
2010-10-10 22:15:28 +01:00
|
|
|
$path = new PropertyPath('firstName');
|
|
|
|
|
2010-11-18 22:33:44 +00:00
|
|
|
$group->addError($error, $path->getIterator(), FieldGroup::DATA_ERROR);
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddErrorKeepsDataValidationErrorsIfFieldNotFound()
|
|
|
|
{
|
2010-11-18 22:33:44 +00:00
|
|
|
$error = new FieldError('Message');
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$field = $this->createMockField('foo');
|
|
|
|
$field->expects($this->any())
|
|
|
|
->method('getPropertyPath')
|
|
|
|
->will($this->returnValue(new PropertyPath('foo')));
|
|
|
|
$field->expects($this->never())
|
|
|
|
->method('addError');
|
|
|
|
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('author');
|
2010-06-24 10:24:08 +01:00
|
|
|
$group->add($field);
|
|
|
|
|
2010-10-10 22:15:28 +01:00
|
|
|
$path = new PropertyPath('bar');
|
|
|
|
|
2010-11-18 22:33:44 +00:00
|
|
|
$group->addError($error, $path->getIterator(), FieldGroup::DATA_ERROR);
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddErrorKeepsDataValidationErrorsIfFieldIsHidden()
|
|
|
|
{
|
2010-11-18 22:33:44 +00:00
|
|
|
$error = new FieldError('Message');
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$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');
|
|
|
|
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('author');
|
2010-06-24 10:24:08 +01:00
|
|
|
$group->add($field);
|
|
|
|
|
2010-10-10 22:15:28 +01:00
|
|
|
$path = new PropertyPath('firstName');
|
|
|
|
|
2010-11-18 22:33:44 +00:00
|
|
|
$group->addError($error, $path->getIterator(), FieldGroup::DATA_ERROR);
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddErrorMapsDataValidationErrorsOntoNestedFields()
|
|
|
|
{
|
2010-11-18 22:33:44 +00:00
|
|
|
$error = new FieldError('Message');
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
// path is expected to point at "street"
|
|
|
|
$expectedPath = new PropertyPath('address.street');
|
2010-10-10 22:15:28 +01:00
|
|
|
$expectedPathIterator = $expectedPath->getIterator();
|
|
|
|
$expectedPathIterator->next();
|
2010-06-24 10:24:08 +01:00
|
|
|
|
|
|
|
$field = $this->createMockField('address');
|
|
|
|
$field->expects($this->any())
|
|
|
|
->method('getPropertyPath')
|
|
|
|
->will($this->returnValue(new PropertyPath('address')));
|
|
|
|
$field->expects($this->once())
|
|
|
|
->method('addError')
|
2010-11-18 22:33:44 +00:00
|
|
|
->with($this->equalTo($error), $this->equalTo($expectedPathIterator), $this->equalTo(FieldGroup::DATA_ERROR));
|
2010-06-24 10:24:08 +01:00
|
|
|
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('author');
|
2010-06-24 10:24:08 +01:00
|
|
|
$group->add($field);
|
|
|
|
|
2010-10-10 22:15:28 +01:00
|
|
|
$path = new PropertyPath('address.street');
|
|
|
|
|
2010-11-18 22:33:44 +00:00
|
|
|
$group->addError($error, $path->getIterator(), FieldGroup::DATA_ERROR);
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
2011-01-02 17:49:33 +00:00
|
|
|
public function testAddErrorMapsErrorsOntoFieldsInVirtualGroups()
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
2010-11-18 22:33:44 +00:00
|
|
|
$error = new FieldError('Message');
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
// path is expected to point at "address"
|
|
|
|
$expectedPath = new PropertyPath('address');
|
2010-10-10 22:15:28 +01:00
|
|
|
$expectedPathIterator = $expectedPath->getIterator();
|
2010-06-24 10:24:08 +01:00
|
|
|
|
|
|
|
$field = $this->createMockField('address');
|
|
|
|
$field->expects($this->any())
|
|
|
|
->method('getPropertyPath')
|
|
|
|
->will($this->returnValue(new PropertyPath('address')));
|
|
|
|
$field->expects($this->once())
|
|
|
|
->method('addError')
|
2010-11-18 22:33:44 +00:00
|
|
|
->with($this->equalTo($error), $this->equalTo($expectedPathIterator), $this->equalTo(FieldGroup::DATA_ERROR));
|
2010-06-24 10:24:08 +01:00
|
|
|
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('author');
|
2011-01-02 17:49:33 +00:00
|
|
|
$nestedGroup = new TestFieldGroup('nested', array('virtual' => true));
|
|
|
|
$nestedGroup->add($field);
|
|
|
|
$group->add($nestedGroup);
|
2010-06-24 10:24:08 +01:00
|
|
|
|
2010-10-10 22:15:28 +01:00
|
|
|
$path = new PropertyPath('address');
|
|
|
|
|
2010-11-18 22:33:44 +00:00
|
|
|
$group->addError($error, $path->getIterator(), FieldGroup::DATA_ERROR);
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddThrowsExceptionIfAlreadyBound()
|
|
|
|
{
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('author');
|
2010-06-24 10:24:08 +01:00
|
|
|
$group->add($this->createMockField('firstName'));
|
|
|
|
$group->bind(array('firstName' => 'Bernhard'));
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
$this->setExpectedException('Symfony\Component\Form\Exception\AlreadyBoundException');
|
2010-06-24 10:24:08 +01:00
|
|
|
$group->add($this->createMockField('lastName'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddSetsFieldParent()
|
|
|
|
{
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('author');
|
2010-06-24 10:24:08 +01:00
|
|
|
|
|
|
|
$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()
|
|
|
|
{
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('author');
|
2010-06-24 10:24:08 +01:00
|
|
|
|
|
|
|
$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';
|
|
|
|
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('author');
|
2010-06-24 10:24:08 +01:00
|
|
|
|
|
|
|
$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())
|
2010-12-10 09:19:51 +00:00
|
|
|
->method('updateFromProperty')
|
2010-06-24 10:24:08 +01:00
|
|
|
->with($this->equalTo($transformedAuthor));
|
|
|
|
|
|
|
|
$group->add($field);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddDoesNotUpdateFieldIfTransformedDataIsEmpty()
|
|
|
|
{
|
|
|
|
$originalAuthor = new Author();
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('author');
|
2010-06-24 10:24:08 +01:00
|
|
|
|
|
|
|
$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())
|
2010-12-10 09:19:51 +00:00
|
|
|
->method('updateFromProperty');
|
2010-06-24 10:24:08 +01:00
|
|
|
|
|
|
|
$group->add($field);
|
|
|
|
}
|
|
|
|
|
2011-01-05 11:36:04 +00:00
|
|
|
/**
|
|
|
|
* @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
|
|
|
|
*/
|
|
|
|
public function testAddThrowsExceptionIfNoFieldOrString()
|
|
|
|
{
|
|
|
|
$group = new TestFieldGroup('author');
|
|
|
|
|
|
|
|
$group->add(1234);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Symfony\Component\Form\Exception\DanglingFieldException
|
|
|
|
*/
|
|
|
|
public function testAddThrowsExceptionIfStringButNoRootForm()
|
|
|
|
{
|
|
|
|
$group = new TestFieldGroup('author');
|
|
|
|
|
|
|
|
$group->add('firstName');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddThrowsExceptionIfStringButNoFieldFactory()
|
|
|
|
{
|
|
|
|
$form = $this->createMockForm();
|
|
|
|
$form->expects($this->once())
|
|
|
|
->method('getFieldFactory')
|
|
|
|
->will($this->returnValue(null));
|
|
|
|
|
|
|
|
$group = new TestFieldGroup('author');
|
|
|
|
$group->setParent($form);
|
|
|
|
|
|
|
|
$this->setExpectedException('\LogicException');
|
|
|
|
|
|
|
|
$group->add('firstName');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddUsesFieldFromFactoryIfStringIsGiven()
|
|
|
|
{
|
|
|
|
$author = new \stdClass();
|
|
|
|
$field = $this->createMockField('firstName');
|
|
|
|
|
|
|
|
$factory = $this->getMock('Symfony\Component\Form\FieldFactory\FieldFactoryInterface');
|
|
|
|
$factory->expects($this->once())
|
|
|
|
->method('getInstance')
|
|
|
|
->with($this->equalTo($author), $this->equalTo('firstName'), $this->equalTo(array('foo' => 'bar')))
|
|
|
|
->will($this->returnValue($field));
|
|
|
|
$form = $this->createMockForm();
|
|
|
|
$form->expects($this->once())
|
|
|
|
->method('getFieldFactory')
|
|
|
|
->will($this->returnValue($factory));
|
|
|
|
|
|
|
|
$group = new TestFieldGroup('author');
|
|
|
|
$group->setParent($form);
|
|
|
|
$group->setData($author);
|
|
|
|
$group->add('firstName', array('foo' => 'bar'));
|
|
|
|
|
|
|
|
$this->assertSame($field, $group['firstName']);
|
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testSetDataUpdatesAllFieldsFromTransformedData()
|
|
|
|
{
|
|
|
|
$originalAuthor = new Author();
|
|
|
|
$transformedAuthor = new Author();
|
|
|
|
// the authors should differ to make sure the test works
|
|
|
|
$transformedAuthor->firstName = 'Foo';
|
|
|
|
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('author');
|
2010-06-24 10:24:08 +01:00
|
|
|
|
|
|
|
$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())
|
2010-12-10 09:19:51 +00:00
|
|
|
->method('updateFromProperty')
|
2010-06-24 10:24:08 +01:00
|
|
|
->with($this->equalTo($transformedAuthor));
|
|
|
|
|
|
|
|
$group->add($field);
|
|
|
|
|
|
|
|
$field = $this->createMockField('lastName');
|
|
|
|
$field->expects($this->once())
|
2010-12-10 09:19:51 +00:00
|
|
|
->method('updateFromProperty')
|
2010-06-24 10:24:08 +01:00
|
|
|
->with($this->equalTo($transformedAuthor));
|
|
|
|
|
|
|
|
$group->add($field);
|
|
|
|
|
|
|
|
$group->setData($originalAuthor);
|
|
|
|
}
|
|
|
|
|
2011-01-02 17:49:33 +00:00
|
|
|
/**
|
|
|
|
* The use case for this test are groups whose fields should be mapped
|
|
|
|
* directly onto properties of the form's object.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* $dateRangeField = new FieldGroup('dateRange');
|
|
|
|
* $dateRangeField->add(new DateField('startDate'));
|
|
|
|
* $dateRangeField->add(new DateField('endDate'));
|
|
|
|
* $form->add($dateRangeField);
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* If $dateRangeField is not virtual, the property "dateRange" must be
|
|
|
|
* present on the form's object. In this property, an object or array
|
|
|
|
* with the properties "startDate" and "endDate" is expected.
|
|
|
|
*
|
|
|
|
* If $dateRangeField is virtual though, it's children are mapped directly
|
|
|
|
* onto the properties "startDate" and "endDate" of the form's object.
|
|
|
|
*/
|
|
|
|
public function testSetDataSkipsVirtualFieldGroups()
|
|
|
|
{
|
|
|
|
$author = new Author();
|
|
|
|
$author->firstName = 'Foo';
|
|
|
|
|
|
|
|
$group = new TestFieldGroup('author');
|
|
|
|
$nestedGroup = new TestFieldGroup('personal_data', array(
|
|
|
|
'virtual' => true,
|
|
|
|
));
|
|
|
|
|
|
|
|
// both fields are in the nested group but receive the object of the
|
|
|
|
// top-level group because the nested group is virtual
|
|
|
|
$field = $this->createMockField('firstName');
|
|
|
|
$field->expects($this->once())
|
|
|
|
->method('updateFromProperty')
|
|
|
|
->with($this->equalTo($author));
|
|
|
|
|
|
|
|
$nestedGroup->add($field);
|
|
|
|
|
|
|
|
$field = $this->createMockField('lastName');
|
|
|
|
$field->expects($this->once())
|
|
|
|
->method('updateFromProperty')
|
|
|
|
->with($this->equalTo($author));
|
|
|
|
|
|
|
|
$nestedGroup->add($field);
|
|
|
|
|
|
|
|
$group->add($nestedGroup);
|
|
|
|
$group->setData($author);
|
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testSetDataThrowsAnExceptionIfArgumentIsNotObjectOrArray()
|
|
|
|
{
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('author');
|
2010-06-24 10:24:08 +01:00
|
|
|
|
|
|
|
$this->setExpectedException('InvalidArgumentException');
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$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';
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('author');
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$transformer = $this->createMockTransformer();
|
2010-10-19 14:08:10 +01:00
|
|
|
$transformer->expects($this->exactly(2))
|
2010-06-24 10:24:08 +01:00
|
|
|
->method('transform')
|
2010-10-19 14:08:10 +01:00
|
|
|
// the method is first called with NULL, then
|
|
|
|
// with $originalAuthor -> not testable by PHPUnit
|
|
|
|
// ->with($this->equalTo(null))
|
|
|
|
// ->with($this->equalTo($originalAuthor))
|
2010-06-24 10:24:08 +01:00
|
|
|
->will($this->returnValue($transformedAuthor));
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$group->setValueTransformer($transformer);
|
|
|
|
$group->setData($originalAuthor);
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$field = $this->createMockField('firstName');
|
|
|
|
$field->expects($this->once())
|
2010-12-10 09:19:51 +00:00
|
|
|
->method('updateProperty')
|
2010-06-24 10:24:08 +01:00
|
|
|
->with($this->equalTo($transformedAuthor));
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$group->add($field);
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$field = $this->createMockField('lastName');
|
|
|
|
$field->expects($this->once())
|
2010-12-10 09:19:51 +00:00
|
|
|
->method('updateProperty')
|
2010-06-24 10:24:08 +01:00
|
|
|
->with($this->equalTo($transformedAuthor));
|
|
|
|
|
|
|
|
$group->add($field);
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$group->bind(array()); // irrelevant
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetDataReturnsObject()
|
|
|
|
{
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('author');
|
2010-06-24 10:24:08 +01:00
|
|
|
$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'));
|
|
|
|
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('author');
|
2010-06-24 10:24:08 +01:00
|
|
|
$group->add($field);
|
|
|
|
|
|
|
|
$this->assertEquals(array('firstName' => 'Bernhard'), $group->getDisplayedData());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsMultipartIfAnyFieldIsMultipart()
|
|
|
|
{
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('author');
|
2010-06-24 10:24:08 +01:00
|
|
|
$group->add($this->createMultipartMockField('firstName'));
|
|
|
|
$group->add($this->createNonMultipartMockField('lastName'));
|
|
|
|
|
|
|
|
$this->assertTrue($group->isMultipart());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsNotMultipartIfNoFieldIsMultipart()
|
|
|
|
{
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('author');
|
2010-06-24 10:24:08 +01:00
|
|
|
$group->add($this->createNonMultipartMockField('firstName'));
|
|
|
|
$group->add($this->createNonMultipartMockField('lastName'));
|
|
|
|
|
|
|
|
$this->assertFalse($group->isMultipart());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSupportsClone()
|
|
|
|
{
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('author');
|
2010-06-24 10:24:08 +01:00
|
|
|
$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'));
|
|
|
|
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('author');
|
2010-06-24 10:24:08 +01:00
|
|
|
$group->add($field);
|
|
|
|
|
|
|
|
$group->bind(array('firstName' => 'Bernhard'));
|
|
|
|
|
|
|
|
$this->assertEquals(array('firstName' => 'Bernhard'), $group->getData());
|
|
|
|
}
|
|
|
|
|
2010-10-08 15:03:27 +01:00
|
|
|
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()
|
|
|
|
{
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('testGroup');
|
2010-10-08 15:03:27 +01:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
protected function createMockField($key)
|
|
|
|
{
|
|
|
|
$field = $this->getMock(
|
2010-08-20 22:09:55 +01:00
|
|
|
'Symfony\Component\Form\FieldInterface',
|
2010-06-24 10:24:08 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-01-05 11:36:04 +00:00
|
|
|
protected function createMockForm()
|
|
|
|
{
|
|
|
|
$form = $this->getMock(
|
2011-01-26 08:54:11 +00:00
|
|
|
'Symfony\Component\Form\Form',
|
2011-01-05 11:36:04 +00:00
|
|
|
array(),
|
|
|
|
array(),
|
|
|
|
'',
|
|
|
|
false, // don't use constructor
|
|
|
|
false // don't call parent::__clone)
|
|
|
|
);
|
|
|
|
|
|
|
|
$form->expects($this->any())
|
|
|
|
->method('getRoot')
|
|
|
|
->will($this->returnValue($form));
|
|
|
|
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
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()
|
|
|
|
{
|
2010-08-20 22:09:55 +01:00
|
|
|
return $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface', array(), array(), '', false, false);
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|