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.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-15 13:29:43 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Tests\Component\Form;
|
2011-01-03 15:06:55 +00:00
|
|
|
|
2011-02-22 00:06:06 +00:00
|
|
|
require_once __DIR__ . '/TestCase.php';
|
2010-06-24 09:40:05 +01:00
|
|
|
require_once __DIR__ . '/Fixtures/Author.php';
|
|
|
|
require_once __DIR__ . '/Fixtures/InvalidField.php';
|
2011-02-24 11:22:00 +00:00
|
|
|
require_once __DIR__ . '/Fixtures/FixedValueTransformer.php';
|
2011-03-16 17:20:13 +00:00
|
|
|
require_once __DIR__ . '/Fixtures/FixedFilterListener.php';
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\Form\ValueTransformer\ValueTransformerInterface;
|
|
|
|
use Symfony\Component\Form\PropertyPath;
|
2010-11-18 22:33:44 +00:00
|
|
|
use Symfony\Component\Form\FieldError;
|
2011-01-31 14:40:31 +00:00
|
|
|
use Symfony\Component\Form\Form;
|
2011-01-03 15:06:55 +00:00
|
|
|
use Symfony\Component\Form\ValueTransformer\TransformationFailedException;
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Tests\Component\Form\Fixtures\Author;
|
|
|
|
use Symfony\Tests\Component\Form\Fixtures\InvalidField;
|
2011-02-24 11:22:00 +00:00
|
|
|
use Symfony\Tests\Component\Form\Fixtures\FixedValueTransformer;
|
2011-03-16 17:20:13 +00:00
|
|
|
use Symfony\Tests\Component\Form\Fixtures\FixedFilterListener;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2011-02-22 00:06:06 +00:00
|
|
|
class FieldTest extends TestCase
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
protected $field;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
protected function setUp()
|
|
|
|
{
|
2011-02-22 00:06:06 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
2011-03-18 13:37:54 +00:00
|
|
|
$this->field = $this->factory->create('field', 'title');
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-07-04 16:03:03 +01:00
|
|
|
public function testGetPropertyPath_defaultPath()
|
|
|
|
{
|
2011-03-18 13:37:54 +00:00
|
|
|
$field = $this->factory->create('field', 'title');
|
2010-07-04 16:03:03 +01:00
|
|
|
|
2011-03-18 14:26:02 +00:00
|
|
|
$this->assertEquals(new PropertyPath('title'), $field->getAttribute('property_path'));
|
2010-07-04 16:03:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetPropertyPath_pathIsZero()
|
|
|
|
{
|
2011-03-18 13:37:54 +00:00
|
|
|
$field = $this->factory->create('field', 'title', array('property_path' => '0'));
|
2010-07-04 16:03:03 +01:00
|
|
|
|
2011-03-18 14:26:02 +00:00
|
|
|
$this->assertEquals(new PropertyPath('0'), $field->getAttribute('property_path'));
|
2010-07-04 16:03:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetPropertyPath_pathIsEmpty()
|
|
|
|
{
|
2011-03-18 13:37:54 +00:00
|
|
|
$field = $this->factory->create('field', 'title', array('property_path' => ''));
|
2010-07-04 16:03:03 +01:00
|
|
|
|
2011-03-18 14:26:02 +00:00
|
|
|
$this->assertEquals(null, $field->getAttribute('property_path'));
|
2010-07-04 16:03:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetPropertyPath_pathIsNull()
|
|
|
|
{
|
2011-03-18 13:37:54 +00:00
|
|
|
$field = $this->factory->create('field', 'title', array('property_path' => null));
|
2010-07-04 16:03:03 +01:00
|
|
|
|
2011-03-18 14:26:02 +00:00
|
|
|
$this->assertEquals(null, $field->getAttribute('property_path'));
|
2010-07-04 16:03:03 +01:00
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testPassRequiredAsOption()
|
|
|
|
{
|
2011-03-18 13:37:54 +00:00
|
|
|
$field = $this->factory->create('field', 'title', array('required' => false));
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->assertFalse($field->isRequired());
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2011-03-18 13:37:54 +00:00
|
|
|
$field = $this->factory->create('field', 'title', array('required' => true));
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->assertTrue($field->isRequired());
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testPassDisabledAsOption()
|
|
|
|
{
|
2011-03-18 13:37:54 +00:00
|
|
|
$field = $this->factory->create('field', 'title', array('disabled' => false));
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->assertFalse($field->isDisabled());
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2011-03-18 13:37:54 +00:00
|
|
|
$field = $this->factory->create('field', 'title', array('disabled' => true));
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->assertTrue($field->isDisabled());
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testFieldIsDisabledIfParentIsDisabled()
|
|
|
|
{
|
2011-03-18 13:37:54 +00:00
|
|
|
$field = $this->factory->create('field', 'title', array('disabled' => false));
|
|
|
|
$field->setParent($this->factory->create('field', 'title', array('disabled' => true)));
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->assertTrue($field->isDisabled());
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testFieldWithNoErrorsIsValid()
|
|
|
|
{
|
2011-03-17 13:55:11 +00:00
|
|
|
$this->field->bind('data');
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->assertTrue($this->field->isValid());
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testFieldWithErrorsIsInvalid()
|
|
|
|
{
|
2011-03-17 13:55:11 +00:00
|
|
|
$this->field->bind('data');
|
2010-11-18 22:33:44 +00:00
|
|
|
$this->field->addError(new FieldError('Some error'));
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->assertFalse($this->field->isValid());
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2011-02-01 09:59:18 +00:00
|
|
|
public function testSubmitResetsErrors()
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
2010-11-18 22:33:44 +00:00
|
|
|
$this->field->addError(new FieldError('Some error'));
|
2011-03-17 13:55:11 +00:00
|
|
|
$this->field->bind('data');
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->assertTrue($this->field->isValid());
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2011-03-17 13:55:11 +00:00
|
|
|
public function testUnboundFieldIsInvalid()
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
|
|
|
$this->assertFalse($this->field->isValid());
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testIsRequiredReturnsOwnValueIfNoParent()
|
|
|
|
{
|
2011-03-18 13:37:54 +00:00
|
|
|
$field = $this->factory->create('field', 'test', array(
|
2011-03-18 11:50:26 +00:00
|
|
|
'required' => true,
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertTrue($field->isRequired());
|
2010-06-24 10:24:08 +01:00
|
|
|
|
2011-03-18 13:37:54 +00:00
|
|
|
$field = $this->factory->create('field', 'test', array(
|
2011-03-18 11:50:26 +00:00
|
|
|
'required' => false,
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertFalse($field->isRequired());
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsRequiredReturnsOwnValueIfParentIsRequired()
|
|
|
|
{
|
|
|
|
$group = $this->createMockGroup();
|
|
|
|
$group->expects($this->any())
|
|
|
|
->method('isRequired')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
2011-03-18 13:37:54 +00:00
|
|
|
$field = $this->factory->create('field', 'test', array(
|
2011-03-18 11:50:26 +00:00
|
|
|
'required' => true,
|
|
|
|
));
|
|
|
|
$field->setParent($group);
|
|
|
|
|
|
|
|
$this->assertTrue($field->isRequired());
|
2010-06-24 10:24:08 +01:00
|
|
|
|
2011-03-18 13:37:54 +00:00
|
|
|
$field = $this->factory->create('field', 'test', array(
|
2011-03-18 11:50:26 +00:00
|
|
|
'required' => false,
|
|
|
|
));
|
|
|
|
$field->setParent($group);
|
2010-06-24 10:24:08 +01:00
|
|
|
|
2011-03-18 11:50:26 +00:00
|
|
|
$this->assertFalse($field->isRequired());
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsRequiredReturnsFalseIfParentIsNotRequired()
|
|
|
|
{
|
|
|
|
$group = $this->createMockGroup();
|
|
|
|
$group->expects($this->any())
|
|
|
|
->method('isRequired')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
2011-03-18 13:37:54 +00:00
|
|
|
$field = $this->factory->create('field', 'test', array(
|
2011-03-18 11:50:26 +00:00
|
|
|
'required' => true,
|
|
|
|
));
|
|
|
|
$field->setParent($group);
|
2010-06-24 10:24:08 +01:00
|
|
|
|
2011-03-18 11:50:26 +00:00
|
|
|
$this->assertFalse($field->isRequired());
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
2011-03-17 13:55:11 +00:00
|
|
|
public function testIsBound()
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
2011-03-17 13:55:11 +00:00
|
|
|
$this->assertFalse($this->field->isBound());
|
|
|
|
$this->field->bind('symfony');
|
|
|
|
$this->assertTrue($this->field->isBound());
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
2011-03-01 13:19:28 +00:00
|
|
|
public function testDefaultDataIsTransformedCorrectly()
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
2011-03-18 13:37:54 +00:00
|
|
|
$field = $this->factory->create('field', 'name');
|
2010-06-24 10:24:08 +01:00
|
|
|
|
|
|
|
$this->assertEquals(null, $this->field->getData());
|
2011-03-17 13:28:59 +00:00
|
|
|
$this->assertEquals('', $this->field->getTransformedData());
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
2011-03-01 13:19:28 +00:00
|
|
|
public function testDataIsTransformedCorrectlyIfNull_noValueTransformer()
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
|
|
|
$this->field->setData(null);
|
|
|
|
|
|
|
|
$this->assertSame(null, $this->field->getData());
|
2011-03-17 13:28:59 +00:00
|
|
|
$this->assertSame('', $this->field->getTransformedData());
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
2011-03-01 13:19:28 +00:00
|
|
|
public function testDataIsTransformedCorrectlyIfNotNull_noValueTransformer()
|
2011-02-16 10:20:01 +00:00
|
|
|
{
|
|
|
|
$this->field->setData(123);
|
|
|
|
|
2011-02-24 11:22:00 +00:00
|
|
|
// The values are synchronized
|
|
|
|
// Without value transformer, the field can't know that the data
|
|
|
|
// should be casted to an integer when the field is bound
|
|
|
|
// Even without binding, the data will thus be a string
|
|
|
|
$this->assertSame('123', $this->field->getData());
|
2011-03-17 13:28:59 +00:00
|
|
|
$this->assertSame('123', $this->field->getTransformedData());
|
2011-02-16 10:20:01 +00:00
|
|
|
}
|
|
|
|
|
2011-03-17 13:55:11 +00:00
|
|
|
public function testBoundDataIsTransformedCorrectly()
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
2011-03-16 17:20:13 +00:00
|
|
|
$filter = new FixedFilterListener(array(
|
2011-03-01 13:19:28 +00:00
|
|
|
'filterBoundDataFromClient' => array(
|
|
|
|
// 1. The value is converted to a string and passed to the
|
|
|
|
// first filter
|
|
|
|
'0' => 'filter1[0]',
|
|
|
|
),
|
|
|
|
'filterBoundData' => array(
|
|
|
|
// 3. The normalized value is passed to the second filter
|
|
|
|
'norm[filter1[0]]' => 'filter2[norm[filter1[0]]]',
|
|
|
|
),
|
|
|
|
));
|
|
|
|
$valueTransformer = new FixedValueTransformer(array(
|
2011-03-18 11:50:26 +00:00
|
|
|
// 0. Empty initialization
|
|
|
|
null => null,
|
2011-03-01 13:19:28 +00:00
|
|
|
// 2. The filtered value is normalized
|
|
|
|
'norm[filter1[0]]' => 'filter1[0]',
|
|
|
|
// 4a. The filtered normalized value is converted to client
|
|
|
|
// representation
|
2011-03-18 11:50:26 +00:00
|
|
|
'filter2[norm[filter1[0]]]' => 'client[filter2[norm[filter1[0]]]]',
|
2011-03-01 13:19:28 +00:00
|
|
|
));
|
|
|
|
$normTransformer = new FixedValueTransformer(array(
|
2011-03-18 11:50:26 +00:00
|
|
|
// 0. Empty initialization
|
|
|
|
null => null,
|
2011-03-01 13:19:28 +00:00
|
|
|
// 4b. The filtered normalized value is converted to app
|
|
|
|
// representation
|
|
|
|
'app[filter2[norm[filter1[0]]]]' => 'filter2[norm[filter1[0]]]',
|
2010-11-15 21:31:44 +00:00
|
|
|
));
|
|
|
|
|
2011-03-18 11:50:26 +00:00
|
|
|
$this->builder->addEventSubscriber($filter);
|
|
|
|
$this->builder->setValueTransformer($valueTransformer);
|
|
|
|
$this->builder->setNormalizationTransformer($normTransformer);
|
|
|
|
|
|
|
|
$field = $this->builder->getInstance();
|
|
|
|
$field->bind(0);
|
2010-06-24 10:24:08 +01:00
|
|
|
|
2011-03-18 11:50:26 +00:00
|
|
|
$this->assertEquals('app[filter2[norm[filter1[0]]]]', $field->getData());
|
|
|
|
$this->assertEquals('filter2[norm[filter1[0]]]', $field->getNormalizedData());
|
|
|
|
$this->assertEquals('client[filter2[norm[filter1[0]]]]', $field->getTransformedData());
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
2011-03-17 13:55:11 +00:00
|
|
|
public function testBoundDataIsTransformedCorrectlyIfEmpty_noValueTransformer()
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
2011-03-17 13:55:11 +00:00
|
|
|
$this->field->bind('');
|
2010-06-24 10:24:08 +01:00
|
|
|
|
|
|
|
$this->assertSame(null, $this->field->getData());
|
2011-03-17 13:28:59 +00:00
|
|
|
$this->assertEquals('', $this->field->getTransformedData());
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
2011-03-01 13:19:28 +00:00
|
|
|
public function testSetDataIsTransformedCorrectly()
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
2011-02-24 11:22:00 +00:00
|
|
|
$normTransformer = new FixedValueTransformer(array(
|
|
|
|
null => '',
|
|
|
|
0 => 'norm[0]',
|
|
|
|
));
|
2010-06-24 10:24:08 +01:00
|
|
|
|
2011-02-24 11:22:00 +00:00
|
|
|
$valueTransformer = new FixedValueTransformer(array(
|
|
|
|
'' => '',
|
|
|
|
'norm[0]' => 'transform[norm[0]]',
|
|
|
|
));
|
2010-10-21 20:18:27 +01:00
|
|
|
|
2011-03-18 13:37:54 +00:00
|
|
|
$field = $this->factory->create('field', 'title', array(
|
2010-11-15 21:31:44 +00:00
|
|
|
'value_transformer' => $valueTransformer,
|
|
|
|
'normalization_transformer' => $normTransformer,
|
|
|
|
));
|
|
|
|
|
|
|
|
$field->setData(0);
|
2010-06-24 10:24:08 +01:00
|
|
|
|
2010-11-15 21:31:44 +00:00
|
|
|
$this->assertEquals(0, $field->getData());
|
|
|
|
$this->assertEquals('norm[0]', $field->getNormalizedData());
|
2011-03-17 13:28:59 +00:00
|
|
|
$this->assertEquals('transform[norm[0]]', $field->getTransformedData());
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
2011-03-17 13:55:11 +00:00
|
|
|
public function testBoundDataIsTrimmedBeforeTransforming()
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
2011-02-24 11:22:00 +00:00
|
|
|
$transformer = new FixedValueTransformer(array(
|
|
|
|
null => '',
|
|
|
|
'reverse[a]' => 'a',
|
|
|
|
));
|
2010-06-24 10:24:08 +01:00
|
|
|
|
2011-03-18 13:37:54 +00:00
|
|
|
$field = $this->factory->create('field', 'title', array(
|
2010-11-15 21:31:44 +00:00
|
|
|
'value_transformer' => $transformer,
|
|
|
|
));
|
2010-06-24 10:24:08 +01:00
|
|
|
|
2011-03-17 13:55:11 +00:00
|
|
|
$field->bind(' a ');
|
2010-11-15 21:31:44 +00:00
|
|
|
|
2011-03-17 13:28:59 +00:00
|
|
|
$this->assertEquals('a', $field->getTransformedData());
|
2010-11-15 21:31:44 +00:00
|
|
|
$this->assertEquals('reverse[a]', $field->getData());
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
2011-03-17 13:55:11 +00:00
|
|
|
public function testBoundDataIsNotTrimmedBeforeTransformingIfDisabled()
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
2011-02-24 11:22:00 +00:00
|
|
|
$transformer = new FixedValueTransformer(array(
|
|
|
|
null => '',
|
|
|
|
'reverse[ a ]' => ' a ',
|
|
|
|
));
|
2010-06-24 10:24:08 +01:00
|
|
|
|
2011-03-18 13:37:54 +00:00
|
|
|
$field = $this->factory->create('field', 'title', array(
|
2010-12-13 10:09:41 +00:00
|
|
|
'trim' => false,
|
2010-11-15 21:31:44 +00:00
|
|
|
'value_transformer' => $transformer,
|
|
|
|
));
|
|
|
|
|
2011-03-17 13:55:11 +00:00
|
|
|
$field->bind(' a ');
|
2010-06-24 10:24:08 +01:00
|
|
|
|
2011-03-17 13:28:59 +00:00
|
|
|
$this->assertEquals(' a ', $field->getTransformedData());
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->assertEquals('reverse[ a ]', $field->getData());
|
|
|
|
}
|
|
|
|
|
2011-01-03 15:06:55 +00:00
|
|
|
public function testIsTransformationSuccessfulReturnsTrueIfReverseTransformSucceeded()
|
|
|
|
{
|
2011-03-18 13:37:54 +00:00
|
|
|
$field = $this->factory->create('field', 'title', array(
|
2011-01-03 15:06:55 +00:00
|
|
|
'trim' => false,
|
|
|
|
));
|
|
|
|
|
2011-03-17 13:55:11 +00:00
|
|
|
$field->bind('a');
|
2011-01-03 15:06:55 +00:00
|
|
|
|
2011-03-17 13:28:59 +00:00
|
|
|
$this->assertEquals('a', $field->getTransformedData());
|
2011-01-03 15:06:55 +00:00
|
|
|
$this->assertTrue($field->isTransformationSuccessful());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsTransformationSuccessfulReturnsFalseIfReverseTransformThrowsException()
|
|
|
|
{
|
|
|
|
// The value is passed to the value transformer
|
|
|
|
$transformer = $this->createMockTransformer();
|
|
|
|
|
2011-03-18 13:37:54 +00:00
|
|
|
$field = $this->factory->create('field', 'title', array(
|
2011-01-03 15:06:55 +00:00
|
|
|
'trim' => false,
|
|
|
|
'value_transformer' => $transformer,
|
|
|
|
));
|
|
|
|
|
2011-02-24 11:22:00 +00:00
|
|
|
$transformer->expects($this->once())
|
|
|
|
->method('reverseTransform')
|
|
|
|
->will($this->throwException(new TransformationFailedException()));
|
|
|
|
|
2011-03-17 13:55:11 +00:00
|
|
|
$field->bind('a');
|
2011-01-03 15:06:55 +00:00
|
|
|
|
2011-03-17 13:28:59 +00:00
|
|
|
$this->assertEquals('a', $field->getTransformedData());
|
2011-01-03 15:06:55 +00:00
|
|
|
$this->assertFalse($field->isTransformationSuccessful());
|
|
|
|
}
|
|
|
|
|
2011-01-05 11:36:04 +00:00
|
|
|
public function testGetRootReturnsRootOfParentIfSet()
|
|
|
|
{
|
|
|
|
$parent = $this->createMockGroup();
|
|
|
|
$parent->expects($this->any())
|
|
|
|
->method('getRoot')
|
|
|
|
->will($this->returnValue('ROOT'));
|
|
|
|
|
|
|
|
$this->field->setParent($parent);
|
|
|
|
|
|
|
|
$this->assertEquals('ROOT', $this->field->getRoot());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetRootReturnsFieldIfNoParent()
|
|
|
|
{
|
|
|
|
$this->assertEquals($this->field, $this->field->getRoot());
|
|
|
|
}
|
|
|
|
|
2011-02-16 09:28:33 +00:00
|
|
|
public function testIsEmptyReturnsTrueIfNull()
|
|
|
|
{
|
|
|
|
$this->field->setData(null);
|
|
|
|
|
|
|
|
$this->assertTrue($this->field->isEmpty());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsEmptyReturnsTrueIfEmptyString()
|
|
|
|
{
|
|
|
|
$this->field->setData('');
|
|
|
|
|
|
|
|
$this->assertTrue($this->field->isEmpty());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsEmptyReturnsFalseIfZero()
|
|
|
|
{
|
|
|
|
$this->field->setData(0);
|
|
|
|
|
|
|
|
$this->assertFalse($this->field->isEmpty());
|
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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(
|
2011-01-29 21:39:36 +00:00
|
|
|
'Symfony\Component\Form\Form',
|
2010-06-24 10:24:08 +01:00
|
|
|
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;
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|