2010-06-24 09:40:05 +01:00
|
|
|
<?php
|
|
|
|
|
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;
|
|
|
|
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
|
|
|
|
|
|
|
|
2010-10-21 20:18:27 +01:00
|
|
|
abstract class FieldGroupTest_Field extends TestField
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
public $locales = array();
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function setLocale($locale)
|
|
|
|
{
|
|
|
|
$this->locales[] = $locale;
|
|
|
|
}
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBindForwardsBoundValues()
|
|
|
|
{
|
|
|
|
$field = $this->createMockField('firstName');
|
|
|
|
$field->expects($this->once())
|
|
|
|
->method('bind')
|
|
|
|
->with($this->equalTo('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'));
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
$field = $this->createMockField('firstName');
|
|
|
|
$field->expects($this->once())
|
|
|
|
->method('addError')
|
|
|
|
->with($this->equalTo('Message'));
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
$group->addError('Message', array(), $path->getIterator(), FieldGroup::FIELD_ERROR);
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
2010-07-15 16:36:13 +01:00
|
|
|
public function testAddErrorMapsFieldValidationErrorsOntoFieldsWithinNestedFieldGroups()
|
|
|
|
{
|
|
|
|
$field = $this->createMockField('firstName');
|
|
|
|
$field->expects($this->once())
|
|
|
|
->method('addError')
|
|
|
|
->with($this->equalTo('Message'));
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
$group->addError('Message', array(), $path->getIterator(), FieldGroup::FIELD_ERROR);
|
2010-07-15 16:36:13 +01:00
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testAddErrorKeepsFieldValidationErrorsIfFieldNotFound()
|
|
|
|
{
|
|
|
|
$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');
|
|
|
|
|
|
|
|
$group->addError('Message', array(), $path->getIterator(), FieldGroup::FIELD_ERROR);
|
2010-06-24 10:24:08 +01:00
|
|
|
|
2010-09-09 11:14:06 +01:00
|
|
|
$this->assertEquals(array(array('Message', array())), $group->getErrors());
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddErrorKeepsFieldValidationErrorsIfFieldIsHidden()
|
|
|
|
{
|
|
|
|
$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');
|
|
|
|
|
|
|
|
$group->addError('Message', array(), $path->getIterator(), FieldGroup::FIELD_ERROR);
|
2010-06-24 10:24:08 +01:00
|
|
|
|
2010-09-09 11:14:06 +01:00
|
|
|
$this->assertEquals(array(array('Message', array())), $group->getErrors());
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddErrorMapsDataValidationErrorsOntoFields()
|
|
|
|
{
|
|
|
|
// 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-10-10 22:15:28 +01:00
|
|
|
->with($this->equalTo('Message'), array(), $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');
|
|
|
|
|
|
|
|
$group->addError('Message', array(), $path->getIterator(), FieldGroup::DATA_ERROR);
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddErrorKeepsDataValidationErrorsIfFieldNotFound()
|
|
|
|
{
|
|
|
|
$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');
|
|
|
|
|
|
|
|
$group->addError('Message', array(), $path->getIterator(), FieldGroup::DATA_ERROR);
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddErrorKeepsDataValidationErrorsIfFieldIsHidden()
|
|
|
|
{
|
|
|
|
$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');
|
|
|
|
|
|
|
|
$group->addError('Message', array(), $path->getIterator(), FieldGroup::DATA_ERROR);
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddErrorMapsDataValidationErrorsOntoNestedFields()
|
|
|
|
{
|
|
|
|
// 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-10-10 22:15:28 +01:00
|
|
|
->with($this->equalTo('Message'), array(), $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');
|
|
|
|
|
|
|
|
$group->addError('Message', array(), $path->getIterator(), FieldGroup::DATA_ERROR);
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddErrorMapsErrorsOntoFieldsInAnonymousGroups()
|
|
|
|
{
|
|
|
|
// 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-10-10 22:15:28 +01:00
|
|
|
->with($this->equalTo('Message'), array(), $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');
|
|
|
|
$group2 = new TestFieldGroup('anonymous', array('property_path' => null));
|
2010-06-24 10:24:08 +01:00
|
|
|
$group2->add($field);
|
|
|
|
$group->add($group2);
|
|
|
|
|
2010-10-10 22:15:28 +01:00
|
|
|
$path = new PropertyPath('address');
|
|
|
|
|
|
|
|
$group->addError('Message', array(), $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 testMergeAddsFieldsFromAnotherGroup()
|
|
|
|
{
|
2010-10-21 20:18:27 +01:00
|
|
|
$group1 = new TestFieldGroup('author');
|
2010-06-24 10:24:08 +01:00
|
|
|
$group1->add($field1 = new TestField('firstName'));
|
|
|
|
|
2010-10-21 20:18:27 +01:00
|
|
|
$group2 = new TestFieldGroup('publisher');
|
2010-06-24 10:24:08 +01:00
|
|
|
$group2->add($field2 = new TestField('lastName'));
|
|
|
|
|
|
|
|
$group1->merge($group2);
|
|
|
|
|
|
|
|
$this->assertTrue($group1->has('lastName'));
|
|
|
|
$this->assertEquals(new PropertyPath('publisher.lastName'), $group1->get('lastName')->getPropertyPath());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMergeThrowsExceptionIfOtherGroupAlreadyBound()
|
|
|
|
{
|
2010-10-21 20:18:27 +01:00
|
|
|
$group1 = new TestFieldGroup('author');
|
|
|
|
$group2 = new TestFieldGroup('publisher');
|
2010-06-24 10:24:08 +01:00
|
|
|
$group2->add($this->createMockField('firstName'));
|
|
|
|
|
|
|
|
$group2->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
|
|
|
$group1->merge($group2);
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
->method('updateFromObject')
|
|
|
|
->with($this->equalTo($transformedAuthor));
|
|
|
|
|
|
|
|
$group->add($field);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddDoesNotUpdateFieldsWithEmptyPropertyPath()
|
|
|
|
{
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('author');
|
2010-06-24 10:24:08 +01:00
|
|
|
$group->setData(new Author());
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$field = $this->createMockField('firstName');
|
|
|
|
$field->expects($this->any())
|
|
|
|
->method('getPropertyPath')
|
|
|
|
->will($this->returnValue(null));
|
|
|
|
$field->expects($this->never())
|
|
|
|
->method('updateFromObject');
|
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
|
|
|
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())
|
|
|
|
->method('updateFromObject');
|
|
|
|
|
|
|
|
$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';
|
|
|
|
|
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())
|
|
|
|
->method('updateFromObject')
|
|
|
|
->with($this->equalTo($transformedAuthor));
|
|
|
|
|
|
|
|
$group->add($field);
|
|
|
|
|
|
|
|
$field = $this->createMockField('lastName');
|
|
|
|
$field->expects($this->once())
|
|
|
|
->method('updateFromObject')
|
|
|
|
->with($this->equalTo($transformedAuthor));
|
|
|
|
|
|
|
|
$group->add($field);
|
|
|
|
|
|
|
|
$group->setData($originalAuthor);
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
->method('updateObject')
|
|
|
|
->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())
|
|
|
|
->method('updateObject')
|
|
|
|
->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 testLocaleIsPassedToField_SetBeforeAddingTheField()
|
|
|
|
{
|
2010-08-20 22:09:55 +01:00
|
|
|
$field = $this->getMock('Symfony\Component\Form\Field', array(), array(), '', false, false);
|
2010-06-24 10:24:08 +01:00
|
|
|
$field->expects($this->any())
|
|
|
|
->method('getKey')
|
|
|
|
->will($this->returnValue('firstName'));
|
|
|
|
$field->expects($this->once())
|
|
|
|
->method('setLocale')
|
|
|
|
->with($this->equalTo('de_DE'));
|
|
|
|
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('author');
|
2010-06-24 10:24:08 +01:00
|
|
|
$group->setLocale('de_DE');
|
|
|
|
$group->add($field);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLocaleIsPassedToField_SetAfterAddingTheField()
|
|
|
|
{
|
|
|
|
$field = $this->getMockForAbstractClass(__NAMESPACE__ . '\FieldGroupTest_Field', array(), '', false, false);
|
|
|
|
$field->expects($this->any())
|
|
|
|
->method('getKey')
|
|
|
|
->will($this->returnValue('firstName'));
|
2010-06-24 09:40:05 +01:00
|
|
|
// DOESN'T WORK!
|
|
|
|
// $field = $this->getMock(__NAMESPACE__ . '\Fixtures\Field', array(), array(), '', false, false);
|
|
|
|
// $field->expects($this->once())
|
|
|
|
// ->method('setLocale')
|
|
|
|
// ->with($this->equalTo('de_AT'));
|
|
|
|
// $field->expects($this->once())
|
|
|
|
// ->method('setLocale')
|
|
|
|
// ->with($this->equalTo('de_DE'));
|
|
|
|
|
2010-10-21 20:18:27 +01:00
|
|
|
$group = new TestFieldGroup('author');
|
2010-06-24 10:24:08 +01:00
|
|
|
$group->add($field);
|
|
|
|
$group->setLocale('de_DE');
|
|
|
|
|
2010-06-24 10:39:05 +01:00
|
|
|
$this->assertEquals(array(class_exists('\Locale', false) ? \Locale::getDefault() : 'en', 'de_DE'), $field->locales);
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|