[Form] Simplified choice list API

This commit is contained in:
Bernhard Schussek 2012-04-10 15:32:55 +02:00
parent 26451201e0
commit 2e07256921
21 changed files with 106 additions and 302 deletions

View File

@ -158,14 +158,11 @@ UPGRADE FROM 2.0 to 2.1
`getChoices()` and `getChoicesByValues()`. For the latter two, no `getChoices()` and `getChoicesByValues()`. For the latter two, no
replacement exists. replacement exists.
* The strategy for generating the `id` and `name` HTML attributes for choices * The strategy for generating the `id` and `name` HTML attributes for
in a choice field has changed. checkboxes and radio buttons in a choice field has changed.
Instead of appending the choice value, a generated integer is now appended Instead of appending the choice value, a generated integer is now appended
by default. Take care if your JavaScript relies on the old behavior. If you by default. Take care if your JavaScript relies on that.
can guarantee that your choice values only contain ASCII letters, digits,
colons and underscores, you can restore the old behavior by setting the
`index_strategy` choice field option to `ChoiceList::COPY_CHOICE`.
* In the choice field type's template, the structure of the `choices` variable * In the choice field type's template, the structure of the `choices` variable
has changed. has changed.

View File

@ -319,7 +319,7 @@ class EntityChoiceList extends ObjectChoiceList
protected function createValue($entity) protected function createValue($entity)
{ {
if (count($this->identifier) === 1) { if (count($this->identifier) === 1) {
return current($this->getIdentifierValues($entity)); return (string) current($this->getIdentifierValues($entity));
} }
return parent::createValue($entity); return parent::createValue($entity);

View File

@ -304,7 +304,7 @@ class ModelChoiceList extends ObjectChoiceList
protected function createValue($model) protected function createValue($model)
{ {
if (1 === count($this->identifier)) { if (1 === count($this->identifier)) {
return current($this->getIdentifierValues($model)); return (string) current($this->getIdentifierValues($model));
} }
return parent::createValue($model); return parent::createValue($model);

View File

@ -32,29 +32,6 @@ use Symfony\Component\Form\Extension\Core\View\ChoiceView;
*/ */
class ChoiceList implements ChoiceListInterface class ChoiceList implements ChoiceListInterface
{ {
/**
* Strategy creating new indices/values by creating a copy of the choice.
*
* This strategy can only be used for index creation if choices are
* guaranteed to only contain ASCII letters, digits and underscores.
*
* It can be used for value creation if choices can safely be cast into
* a (unique) string.
*
* @var integer
*/
const COPY_CHOICE = 0;
/**
* Strategy creating new indices/values by generating a new integer.
*
* This strategy can always be applied, but leads to loss of information
* in the HTML source code.
*
* @var integer
*/
const GENERATE = 1;
/** /**
* The choices with their indices as keys. * The choices with their indices as keys.
* *
@ -85,24 +62,6 @@ class ChoiceList implements ChoiceListInterface
*/ */
private $remainingViews = array(); private $remainingViews = array();
/**
* The strategy used for creating choice indices.
*
* @var integer
* @see COPY_CHOICE
* @see GENERATE
*/
private $indexStrategy;
/**
* The strategy used for creating choice values.
*
* @var integer
* @see COPY_CHOICE
* @see GENERATE
*/
private $valueStrategy;
/** /**
* Creates a new choice list. * Creates a new choice list.
* *
@ -115,16 +74,9 @@ class ChoiceList implements ChoiceListInterface
* should match the structure of $choices. * should match the structure of $choices.
* @param array $preferredChoices A flat array of choices that should be * @param array $preferredChoices A flat array of choices that should be
* presented to the user with priority. * presented to the user with priority.
* @param integer $valueStrategy The strategy used to create choice values.
* One of COPY_CHOICE and GENERATE.
* @param integer $indexStrategy The strategy used to create choice indices.
* One of COPY_CHOICE and GENERATE.
*/ */
public function __construct($choices, array $labels, array $preferredChoices = array(), $valueStrategy = self::GENERATE, $indexStrategy = self::GENERATE) public function __construct($choices, array $labels, array $preferredChoices = array())
{ {
$this->valueStrategy = $valueStrategy;
$this->indexStrategy = $indexStrategy;
$this->initialize($choices, $labels, $preferredChoices); $this->initialize($choices, $labels, $preferredChoices);
} }
@ -191,13 +143,6 @@ class ChoiceList implements ChoiceListInterface
public function getChoicesForValues(array $values) public function getChoicesForValues(array $values)
{ {
$values = $this->fixValues($values); $values = $this->fixValues($values);
// If the values are identical to the choices, we can just return them
// to improve performance a little bit
if (self::COPY_CHOICE === $this->valueStrategy) {
return $this->fixChoices(array_intersect($values, $this->values));
}
$choices = array(); $choices = array();
foreach ($values as $j => $givenValue) { foreach ($values as $j => $givenValue) {
@ -222,13 +167,6 @@ class ChoiceList implements ChoiceListInterface
public function getValuesForChoices(array $choices) public function getValuesForChoices(array $choices)
{ {
$choices = $this->fixChoices($choices); $choices = $this->fixChoices($choices);
// If the values are identical to the choices, we can just return them
// to improve performance a little bit
if (self::COPY_CHOICE === $this->valueStrategy) {
return $this->fixValues(array_intersect($choices, $this->choices));
}
$values = array(); $values = array();
foreach ($this->choices as $i => $choice) { foreach ($this->choices as $i => $choice) {
@ -398,17 +336,15 @@ class ChoiceList implements ChoiceListInterface
$index = $this->createIndex($choice); $index = $this->createIndex($choice);
if ('' === $index || null === $index || !Form::isValidName((string)$index)) { if ('' === $index || null === $index || !Form::isValidName((string)$index)) {
throw new InvalidConfigurationException('The choice list index "' . $index . '" is invalid. Please set the choice field option "index_generation" to ChoiceList::GENERATE.'); throw new InvalidConfigurationException('The index "' . $index . '" created by the choice list is invalid. It should be a valid, non-empty Form name.');
} }
$value = $this->createValue($choice); $value = $this->createValue($choice);
if (!is_scalar($value)) { if (!is_string($value)) {
throw new InvalidConfigurationException('The choice list value of type "' . gettype($value) . '" should be a scalar. Please set the choice field option "value_generation" to ChoiceList::GENERATE.'); throw new InvalidConfigurationException('The value created by the choice list is of type "' . gettype($value) . '", but should be a string.');
} }
// Always store values as strings to facilitate comparisons
$value = $this->fixValue($value);
$view = new ChoiceView($value, $label); $view = new ChoiceView($value, $label);
$this->choices[$index] = $this->fixChoice($choice); $this->choices[$index] = $this->fixChoice($choice);
@ -448,29 +384,23 @@ class ChoiceList implements ChoiceListInterface
*/ */
protected function createIndex($choice) protected function createIndex($choice)
{ {
if (self::COPY_CHOICE === $this->indexStrategy) {
return $choice;
}
return count($this->choices); return count($this->choices);
} }
/** /**
* Creates a new unique value for this choice. * Creates a new unique value for this choice.
* *
* Extension point to change the value strategy. * By default, an integer is generated since it cannot be guaranteed that
* all values in the list are convertible to (unique) strings. Subclasses
* can override this behaviour if they can guarantee this property.
* *
* @param mixed $choice The choice to create a value for * @param mixed $choice The choice to create a value for
* *
* @return integer|string A unique value without character limitations. * @return string A unique string.
*/ */
protected function createValue($choice) protected function createValue($choice)
{ {
if (self::COPY_CHOICE === $this->valueStrategy) { return (string) count($this->values);
return $choice;
}
return count($this->values);
} }
/** /**

View File

@ -14,17 +14,12 @@ namespace Symfony\Component\Form\Extension\Core\ChoiceList;
/** /**
* Contains choices that can be selected in a form field. * Contains choices that can be selected in a form field.
* *
* Each choice has four different properties: * Each choice has three different properties:
* *
* - Choice: The choice that should be returned to the application by the * - Choice: The choice that should be returned to the application by the
* choice field. Can be any scalar value or an object, but no * choice field. Can be any scalar value or an object, but no
* array. * array.
* - Label: A text representing the choice that is displayed to the user. * - Label: A text representing the choice that is displayed to the user.
* - Index: A uniquely identifying index that should only contain ASCII
* characters, digits and underscores. This index is used to
* identify the choice in the HTML "id" and "name" attributes.
* It is also used as index of the arrays returned by the various
* getters of this class.
* - Value: A uniquely identifying value that can contain arbitrary * - Value: A uniquely identifying value that can contain arbitrary
* characters, but no arrays or objects. This value is displayed * characters, but no arrays or objects. This value is displayed
* in the HTML "value" attribute. * in the HTML "value" attribute.

View File

@ -19,8 +19,8 @@ use Symfony\Component\Form\Exception\InvalidPropertyException;
/** /**
* A choice list for object choices. * A choice list for object choices.
* *
* Supports generation of choice labels, choice groups, choice values and * Supports generation of choice labels, choice groups and choice values
* choice indices by calling getters of the object (or associated objects). * by calling getters of the object (or associated objects).
* *
* <code> * <code>
* $choices = array($user1, $user2); * $choices = array($user1, $user2);
@ -54,13 +54,6 @@ class ObjectChoiceList extends ChoiceList
*/ */
private $valuePath; private $valuePath;
/**
* The property path used to obtain the choice index.
*
* @var PropertyPath
*/
private $indexPath;
/** /**
* Creates a new object choice list. * Creates a new object choice list.
* *
@ -82,18 +75,14 @@ class ObjectChoiceList extends ChoiceList
* @param string $valuePath A property path pointing to the property used * @param string $valuePath A property path pointing to the property used
* for the choice values. If not given, integers * for the choice values. If not given, integers
* are generated instead. * are generated instead.
* @param string $indexPath A property path pointing to the property used
* for the choice indices. If not given, integers
* are generated instead.
*/ */
public function __construct($choices, $labelPath = null, array $preferredChoices = array(), $groupPath = null, $valuePath = null, $indexPath = null) public function __construct($choices, $labelPath = null, array $preferredChoices = array(), $groupPath = null, $valuePath = null)
{ {
$this->labelPath = $labelPath ? new PropertyPath($labelPath) : null; $this->labelPath = $labelPath ? new PropertyPath($labelPath) : null;
$this->groupPath = $groupPath ? new PropertyPath($groupPath) : null; $this->groupPath = $groupPath ? new PropertyPath($groupPath) : null;
$this->valuePath = $valuePath ? new PropertyPath($valuePath) : null; $this->valuePath = $valuePath ? new PropertyPath($valuePath) : null;
$this->indexPath = $indexPath ? new PropertyPath($indexPath) : null;
parent::__construct($choices, array(), $preferredChoices, self::GENERATE, self::GENERATE); parent::__construct($choices, array(), $preferredChoices);
} }
/** /**
@ -148,27 +137,6 @@ class ObjectChoiceList extends ChoiceList
parent::initialize($choices, $labels, $preferredChoices); parent::initialize($choices, $labels, $preferredChoices);
} }
/**
* Creates a new unique index for this choice.
*
* If a property path for the index was given at object creation,
* the getter behind that path is now called to obtain a new value.
*
* Otherwise a new integer is generated.
*
* @param mixed $choice The choice to create an index for
* @return integer|string A unique index containing only ASCII letters,
* digits and underscores.
*/
protected function createIndex($choice)
{
if ($this->indexPath) {
return $this->indexPath->getValue($choice);
}
return parent::createIndex($choice);
}
/** /**
* Creates a new unique value for this choice. * Creates a new unique value for this choice.
* *
@ -183,7 +151,7 @@ class ObjectChoiceList extends ChoiceList
protected function createValue($choice) protected function createValue($choice)
{ {
if ($this->valuePath) { if ($this->valuePath) {
return $this->valuePath->getValue($choice); return (string) $this->valuePath->getValue($choice);
} }
return parent::createValue($choice); return parent::createValue($choice);

View File

@ -28,27 +28,6 @@ use Symfony\Component\Form\Exception\UnexpectedTypeException;
* )); * ));
* </code> * </code>
* *
* The default value generation strategy is `ChoiceList::COPY_CHOICE`, because
* choice values must be scalar, and the choices passed to this choice list
* are guaranteed to be scalar.
*
* The default index generation strategy is `ChoiceList::GENERATE`, so that
* your choices can also contain values that are illegal as indices. If your
* choices are guaranteed to start with a letter, digit or underscore and only
* contain letters, digits, underscores, hyphens and colons, you can set the
* strategy to `ChoiceList::COPY_CHOICE` instead.
*
* <code>
* $choices = array(
* 'creditcard' => 'Credit card payment',
* 'cash' => 'Cash payment',
* );
*
* // value generation: COPY_CHOICE (the default)
* // index generation: COPY_CHOICE (custom)
* $choiceList = new SimpleChoiceList($choices, array(), ChoiceList::COPY_CHOICE, ChoiceList::COPY_CHOICE);
* </code>
*
* @author Bernhard Schussek <bschussek@gmail.com> * @author Bernhard Schussek <bschussek@gmail.com>
*/ */
class SimpleChoiceList extends ChoiceList class SimpleChoiceList extends ChoiceList
@ -64,15 +43,35 @@ class SimpleChoiceList extends ChoiceList
* key pointing to the nested array. * key pointing to the nested array.
* @param array $preferredChoices A flat array of choices that should be * @param array $preferredChoices A flat array of choices that should be
* presented to the user with priority. * presented to the user with priority.
* @param integer $valueStrategy The strategy used to create choice values.
* One of COPY_CHOICE and GENERATE.
* @param integer $indexStrategy The strategy used to create choice indices.
* One of COPY_CHOICE and GENERATE.
*/ */
public function __construct(array $choices, array $preferredChoices = array(), $valueStrategy = self::COPY_CHOICE, $indexStrategy = self::GENERATE) public function __construct(array $choices, array $preferredChoices = array())
{ {
// Flip preferred choices to speed up lookup // Flip preferred choices to speed up lookup
parent::__construct($choices, $choices, array_flip($preferredChoices), $valueStrategy, $indexStrategy); parent::__construct($choices, $choices, array_flip($preferredChoices));
}
/**
* {@inheritdoc}
*/
public function getChoicesForValues(array $values)
{
$values = $this->fixValues($values);
// The values are identical to the choices, so we can just return them
// to improve performance a little bit
return $this->fixChoices(array_intersect($values, $this->getValues()));
}
/**
* {@inheritdoc}
*/
public function getValuesForChoices(array $choices)
{
$choices = $this->fixChoices($choices);
// The choices are identical to the values, so we can just return them
// to improve performance a little bit
return $this->fixValues(array_intersect($choices, $this->getValues()));
} }
/** /**
@ -147,16 +146,21 @@ class SimpleChoiceList extends ChoiceList
return $this->fixIndex($choice); return $this->fixIndex($choice);
} }
/** /**
* Converts the choices to valid PHP array keys. * {@inheritdoc}
*
* @param array $choices The choices.
*
* @return array Valid PHP array keys.
*/ */
protected function fixChoices(array $choices) protected function fixChoices(array $choices)
{ {
return $this->fixIndices($choices); return $this->fixIndices($choices);
} }
/**
* {@inheritdoc}
*/
protected function createValue($choice)
{
// Choices are guaranteed to be unique and scalar, so we can simply
// convert them to strings
return (string) $choice;
}
} }

View File

@ -45,9 +45,7 @@ class ChoiceType extends AbstractType
if (!$options['choice_list']) { if (!$options['choice_list']) {
$options['choice_list'] = new SimpleChoiceList( $options['choice_list'] = new SimpleChoiceList(
$options['choices'], $options['choices'],
$options['preferred_choices'], $options['preferred_choices']
$options['value_strategy'],
$options['index_strategy']
); );
} }
@ -159,8 +157,6 @@ class ChoiceType extends AbstractType
'choice_list' => null, 'choice_list' => null,
'choices' => null, 'choices' => null,
'preferred_choices' => array(), 'preferred_choices' => array(),
'value_strategy' => ChoiceList::COPY_CHOICE,
'index_strategy' => ChoiceList::GENERATE,
'empty_data' => $multiple || $expanded ? array() : '', 'empty_data' => $multiple || $expanded ? array() : '',
'empty_value' => $multiple || $expanded || !isset($options['empty_value']) ? null : '', 'empty_value' => $multiple || $expanded || !isset($options['empty_value']) ? null : '',
'error_bubbling' => false, 'error_bubbling' => false,

View File

@ -24,8 +24,6 @@ class CountryType extends AbstractType
{ {
return array( return array(
'choices' => Locale::getDisplayCountries(\Locale::getDefault()), 'choices' => Locale::getDisplayCountries(\Locale::getDefault()),
'value_strategy' => ChoiceList::COPY_CHOICE,
'index_strategy' => ChoiceList::COPY_CHOICE,
); );
} }

View File

@ -89,20 +89,14 @@ class DateType extends AbstractType
// Only pass a subset of the options to children // Only pass a subset of the options to children
$yearOptions = array( $yearOptions = array(
'choices' => $years, 'choices' => $years,
'value_strategy' => ChoiceList::COPY_CHOICE,
'index_strategy' => ChoiceList::COPY_CHOICE,
'empty_value' => $options['empty_value']['year'], 'empty_value' => $options['empty_value']['year'],
); );
$monthOptions = array( $monthOptions = array(
'choices' => $this->formatMonths($formatter, $months), 'choices' => $this->formatMonths($formatter, $months),
'value_strategy' => ChoiceList::COPY_CHOICE,
'index_strategy' => ChoiceList::COPY_CHOICE,
'empty_value' => $options['empty_value']['month'], 'empty_value' => $options['empty_value']['month'],
); );
$dayOptions = array( $dayOptions = array(
'choices' => $days, 'choices' => $days,
'value_strategy' => ChoiceList::COPY_CHOICE,
'index_strategy' => ChoiceList::COPY_CHOICE,
'empty_value' => $options['empty_value']['day'], 'empty_value' => $options['empty_value']['day'],
); );

View File

@ -24,7 +24,6 @@ class LanguageType extends AbstractType
{ {
return array( return array(
'choices' => Locale::getDisplayLanguages(\Locale::getDefault()), 'choices' => Locale::getDisplayLanguages(\Locale::getDefault()),
'value_strategy' => ChoiceList::COPY_CHOICE,
); );
} }

View File

@ -24,7 +24,6 @@ class LocaleType extends AbstractType
{ {
return array( return array(
'choices' => Locale::getDisplayLocales(\Locale::getDefault()), 'choices' => Locale::getDisplayLocales(\Locale::getDefault()),
'value_strategy' => ChoiceList::COPY_CHOICE,
); );
} }

View File

@ -59,14 +59,10 @@ class TimeType extends AbstractType
// Only pass a subset of the options to children // Only pass a subset of the options to children
$hourOptions = array( $hourOptions = array(
'choices' => $hours, 'choices' => $hours,
'value_strategy' => ChoiceList::COPY_CHOICE,
'index_strategy' => ChoiceList::COPY_CHOICE,
'empty_value' => $options['empty_value']['hour'], 'empty_value' => $options['empty_value']['hour'],
); );
$minuteOptions = array( $minuteOptions = array(
'choices' => $minutes, 'choices' => $minutes,
'value_strategy' => ChoiceList::COPY_CHOICE,
'index_strategy' => ChoiceList::COPY_CHOICE,
'empty_value' => $options['empty_value']['minute'], 'empty_value' => $options['empty_value']['minute'],
); );
@ -79,8 +75,6 @@ class TimeType extends AbstractType
$secondOptions = array( $secondOptions = array(
'choices' => $seconds, 'choices' => $seconds,
'value_strategy' => ChoiceList::COPY_CHOICE,
'index_strategy' => ChoiceList::COPY_CHOICE,
'empty_value' => $options['empty_value']['second'], 'empty_value' => $options['empty_value']['second'],
); );
} }

View File

@ -27,9 +27,7 @@ class TimezoneType extends AbstractType
*/ */
public function getDefaultOptions(array $options) public function getDefaultOptions(array $options)
{ {
$defaultOptions = array( $defaultOptions = array();
'value_strategy' => ChoiceList::COPY_CHOICE,
);
if (empty($options['choice_list']) && empty($options['choices'])) { if (empty($options['choice_list']) && empty($options['choices'])) {
$defaultOptions['choices'] = self::getTimezones(); $defaultOptions['choices'] = self::getTimezones();

View File

@ -87,34 +87,6 @@ class ChoiceListTest extends \PHPUnit_Framework_TestCase
), $this->list->getRemainingViews()); ), $this->list->getRemainingViews());
} }
/**
* @expectedException Symfony\Component\Form\Exception\InvalidConfigurationException
*/
public function testInitIndexCopyChoiceWithInvalidIndex()
{
new ChoiceList(
array('a.'),
array('A'),
array(),
ChoiceList::GENERATE,
ChoiceList::COPY_CHOICE
);
}
/**
* @expectedException Symfony\Component\Form\Exception\InvalidConfigurationException
*/
public function testInitValueCopyChoiceWithInvalidValue()
{
new ChoiceList(
array($this->obj1),
array('A'),
array(),
ChoiceList::COPY_CHOICE,
ChoiceList::GENERATE
);
}
public function testGetIndicesForChoices() public function testGetIndicesForChoices()
{ {
$choices = array($this->obj2, $this->obj3); $choices = array($this->obj2, $this->obj3);

View File

@ -176,28 +176,6 @@ class ObjectChoiceListTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array(0 => new ChoiceView('10', 'A'), 3 => new ChoiceView('40', 'D')), $this->list->getRemainingViews()); $this->assertEquals(array(0 => new ChoiceView('10', 'A'), 3 => new ChoiceView('40', 'D')), $this->list->getRemainingViews());
} }
public function testInitArrayWithIndexPath()
{
$this->obj1 = (object) array('name' => 'A', 'id' => 10);
$this->obj2 = (object) array('name' => 'B', 'id' => 20);
$this->obj3 = (object) array('name' => 'C', 'id' => 30);
$this->obj4 = (object) array('name' => 'D', 'id' => 40);
$this->list = new ObjectChoiceList(
array($this->obj1, $this->obj2, $this->obj3, $this->obj4),
'name',
array($this->obj2, $this->obj3),
null,
null,
'id'
);
$this->assertSame(array(10 => $this->obj1, 20 => $this->obj2, 30 => $this->obj3, 40 => $this->obj4), $this->list->getChoices());
$this->assertSame(array(10 => '0', 20 => '1', 30 => '2', 40 => '3'), $this->list->getValues());
$this->assertEquals(array(20 => new ChoiceView('1', 'B'), 30 => new ChoiceView('2', 'C')), $this->list->getPreferredViews());
$this->assertEquals(array(10 => new ChoiceView('0', 'A'), 40 => new ChoiceView('3', 'D')), $this->list->getRemainingViews());
}
public function testInitArrayUsesToString() public function testInitArrayUsesToString()
{ {
$this->obj1 = new ObjectChoiceListTest_EntityWithToString('A'); $this->obj1 = new ObjectChoiceListTest_EntityWithToString('A');

View File

@ -34,10 +34,10 @@ class SimpleChoiceListTest extends \PHPUnit_Framework_TestCase
'Group 2' => array(2 => 'C', 3 => 'D'), 'Group 2' => array(2 => 'C', 3 => 'D'),
); );
$this->list = new SimpleChoiceList($choices, array('b', 'c'), ChoiceList::GENERATE, ChoiceList::GENERATE); $this->list = new SimpleChoiceList($choices, array('b', 'c'));
// Use COPY_CHOICE strategy to test for the various associated problems // Use COPY_CHOICE strategy to test for the various associated problems
$this->numericList = new SimpleChoiceList($numericChoices, array(1, 2), ChoiceList::COPY_CHOICE, ChoiceList::GENERATE); $this->numericList = new SimpleChoiceList($numericChoices, array(1, 2));
} }
protected function tearDown() protected function tearDown()
@ -51,18 +51,7 @@ class SimpleChoiceListTest extends \PHPUnit_Framework_TestCase
public function testInitArray() public function testInitArray()
{ {
$choices = array('a' => 'A', 'b' => 'B', 'c' => 'C'); $choices = array('a' => 'A', 'b' => 'B', 'c' => 'C');
$this->list = new SimpleChoiceList($choices, array('b'), ChoiceList::GENERATE, ChoiceList::GENERATE); $this->list = new SimpleChoiceList($choices, array('b'));
$this->assertSame(array(0 => 'a', 1 => 'b', 2 => 'c'), $this->list->getChoices());
$this->assertSame(array(0 => '0', 1 => '1', 2 => '2'), $this->list->getValues());
$this->assertEquals(array(1 => new ChoiceView('1', 'B')), $this->list->getPreferredViews());
$this->assertEquals(array(0 => new ChoiceView('0', 'A'), 2 => new ChoiceView('2', 'C')), $this->list->getRemainingViews());
}
public function testInitArrayValueCopyChoice()
{
$choices = array('a' => 'A', 'b' => 'B', 'c' => 'C');
$this->list = new SimpleChoiceList($choices, array('b'), ChoiceList::COPY_CHOICE, ChoiceList::GENERATE);
$this->assertSame(array(0 => 'a', 1 => 'b', 2 => 'c'), $this->list->getChoices()); $this->assertSame(array(0 => 'a', 1 => 'b', 2 => 'c'), $this->list->getChoices());
$this->assertSame(array(0 => 'a', 1 => 'b', 2 => 'c'), $this->list->getValues()); $this->assertSame(array(0 => 'a', 1 => 'b', 2 => 'c'), $this->list->getValues());
@ -70,28 +59,17 @@ class SimpleChoiceListTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array(0 => new ChoiceView('a', 'A'), 2 => new ChoiceView('c', 'C')), $this->list->getRemainingViews()); $this->assertEquals(array(0 => new ChoiceView('a', 'A'), 2 => new ChoiceView('c', 'C')), $this->list->getRemainingViews());
} }
public function testInitArrayIndexCopyChoice()
{
$choices = array('a' => 'A', 'b' => 'B', 'c' => 'C');
$this->list = new SimpleChoiceList($choices, array('b'), ChoiceList::GENERATE, ChoiceList::COPY_CHOICE);
$this->assertSame(array('a' => 'a', 'b' => 'b', 'c' => 'c'), $this->list->getChoices());
$this->assertSame(array('a' => '0', 'b' => '1', 'c' => '2'), $this->list->getValues());
$this->assertEquals(array('b' => new ChoiceView('1', 'B')), $this->list->getPreferredViews());
$this->assertEquals(array('a' => new ChoiceView('0', 'A'), 'c' => new ChoiceView('2', 'C')), $this->list->getRemainingViews());
}
public function testInitNestedArray() public function testInitNestedArray()
{ {
$this->assertSame(array(0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd'), $this->list->getChoices()); $this->assertSame(array(0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd'), $this->list->getChoices());
$this->assertSame(array(0 => '0', 1 => '1', 2 => '2', 3 => '3'), $this->list->getValues()); $this->assertSame(array(0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd'), $this->list->getValues());
$this->assertEquals(array( $this->assertEquals(array(
'Group 1' => array(1 => new ChoiceView('1', 'B')), 'Group 1' => array(1 => new ChoiceView('b', 'B')),
'Group 2' => array(2 => new ChoiceView('2', 'C')) 'Group 2' => array(2 => new ChoiceView('c', 'C'))
), $this->list->getPreferredViews()); ), $this->list->getPreferredViews());
$this->assertEquals(array( $this->assertEquals(array(
'Group 1' => array(0 => new ChoiceView('0', 'A')), 'Group 1' => array(0 => new ChoiceView('a', 'A')),
'Group 2' => array(3 => new ChoiceView('3', 'D')) 'Group 2' => array(3 => new ChoiceView('d', 'D'))
), $this->list->getRemainingViews()); ), $this->list->getRemainingViews());
} }
@ -116,13 +94,13 @@ class SimpleChoiceListTest extends \PHPUnit_Framework_TestCase
public function testGetIndicesForValues() public function testGetIndicesForValues()
{ {
$values = array('1', '2'); $values = array('b', 'c');
$this->assertSame(array(1, 2), $this->list->getIndicesForValues($values)); $this->assertSame(array(1, 2), $this->list->getIndicesForValues($values));
} }
public function testGetIndicesForValuesIgnoresNonExistingValues() public function testGetIndicesForValuesIgnoresNonExistingValues()
{ {
$values = array('1', '2', '100'); $values = array('b', 'c', '100');
$this->assertSame(array(1, 2), $this->list->getIndicesForValues($values)); $this->assertSame(array(1, 2), $this->list->getIndicesForValues($values));
} }
@ -135,13 +113,13 @@ class SimpleChoiceListTest extends \PHPUnit_Framework_TestCase
public function testGetChoicesForValues() public function testGetChoicesForValues()
{ {
$values = array('1', '2'); $values = array('b', 'c');
$this->assertSame(array('b', 'c'), $this->list->getChoicesForValues($values)); $this->assertSame(array('b', 'c'), $this->list->getChoicesForValues($values));
} }
public function testGetChoicesForValuesIgnoresNonExistingValues() public function testGetChoicesForValuesIgnoresNonExistingValues()
{ {
$values = array('1', '2', '100'); $values = array('b', 'c', '100');
$this->assertSame(array('b', 'c'), $this->list->getChoicesForValues($values)); $this->assertSame(array('b', 'c'), $this->list->getChoicesForValues($values));
} }
@ -155,13 +133,13 @@ class SimpleChoiceListTest extends \PHPUnit_Framework_TestCase
public function testGetValuesForChoices() public function testGetValuesForChoices()
{ {
$choices = array('b', 'c'); $choices = array('b', 'c');
$this->assertSame(array('1', '2'), $this->list->getValuesForChoices($choices)); $this->assertSame(array('b', 'c'), $this->list->getValuesForChoices($choices));
} }
public function testGetValuesForChoicesIgnoresNonExistingValues() public function testGetValuesForChoicesIgnoresNonExistingValues()
{ {
$choices = array('b', 'c', 'foobar'); $choices = array('b', 'c', 'foobar');
$this->assertSame(array('1', '2'), $this->list->getValuesForChoices($choices)); $this->assertSame(array('b', 'c'), $this->list->getValuesForChoices($choices));
} }
public function testGetValuesForChoicesDealsWithNumericValues() public function testGetValuesForChoicesDealsWithNumericValues()
@ -187,7 +165,7 @@ class SimpleChoiceListTest extends \PHPUnit_Framework_TestCase
); );
// use COPY_CHOICE strategy to test the problems // use COPY_CHOICE strategy to test the problems
$this->list = new SimpleChoiceList($choices, array(), ChoiceList::COPY_CHOICE, ChoiceList::GENERATE); $this->list = new SimpleChoiceList($choices, array());
$this->assertSame(array($value), $this->list->getValuesForChoices(array($choice))); $this->assertSame(array($value), $this->list->getValuesForChoices(array($choice)));
} }

View File

@ -408,8 +408,7 @@ class ChoiceTypeTest extends TypeTestCase
'' => 'Empty', '' => 'Empty',
1 => 'Not Empty', 1 => 'Not Empty',
2 => 'Not Empty 2', 2 => 'Not Empty 2',
), )
'value_strategy' => ChoiceList::COPY_CHOICE,
)); ));
$form->bind(array('', '2')); $form->bind(array('', '2'));

View File

@ -23,11 +23,12 @@ class CountryTypeTest extends LocalizedTestCase
$view = $form->createView(); $view = $form->createView();
$choices = $view->get('choices'); $choices = $view->get('choices');
$this->assertEquals(new ChoiceView('DE', 'Deutschland'), $choices['DE']); // Don't check objects for identity
$this->assertEquals(new ChoiceView('GB', 'Vereinigtes Königreich'), $choices['GB']); $this->assertContains(new ChoiceView('DE', 'Deutschland'), $choices, '', false, false);
$this->assertEquals(new ChoiceView('US', 'Vereinigte Staaten'), $choices['US']); $this->assertContains(new ChoiceView('GB', 'Vereinigtes Königreich'), $choices, '', false, false);
$this->assertEquals(new ChoiceView('FR', 'Frankreich'), $choices['FR']); $this->assertContains(new ChoiceView('US', 'Vereinigte Staaten'), $choices, '', false, false);
$this->assertEquals(new ChoiceView('MY', 'Malaysia'), $choices['MY']); $this->assertContains(new ChoiceView('FR', 'Frankreich'), $choices, '', false, false);
$this->assertContains(new ChoiceView('MY', 'Malaysia'), $choices, '', false, false);
} }
public function testUnknownCountryIsNotIncluded() public function testUnknownCountryIsNotIncluded()
@ -36,6 +37,10 @@ class CountryTypeTest extends LocalizedTestCase
$view = $form->createView(); $view = $form->createView();
$choices = $view->get('choices'); $choices = $view->get('choices');
$this->assertArrayNotHasKey('ZZ', $choices); foreach ($choices as $choice) {
if ('ZZ' === $choice->getValue()) {
$this->fail('Should not contain choice "ZZ"');
}
}
} }
} }

View File

@ -325,8 +325,8 @@ class DateTypeTest extends LocalizedTestCase
$view = $form->createView(); $view = $form->createView();
$this->assertEquals(array( $this->assertEquals(array(
2010 => new ChoiceView('2010', '2010'), new ChoiceView('2010', '2010'),
2011 => new ChoiceView('2011', '2011'), new ChoiceView('2011', '2011'),
), $view->getChild('year')->get('choices')); ), $view->getChild('year')->get('choices'));
} }
@ -339,8 +339,8 @@ class DateTypeTest extends LocalizedTestCase
$view = $form->createView(); $view = $form->createView();
$this->assertEquals(array( $this->assertEquals(array(
6 => new ChoiceView('6', '06'), new ChoiceView('6', '06'),
7 => new ChoiceView('7', '07'), new ChoiceView('7', '07'),
), $view->getChild('month')->get('choices')); ), $view->getChild('month')->get('choices'));
} }
@ -354,8 +354,8 @@ class DateTypeTest extends LocalizedTestCase
$view = $form->createView(); $view = $form->createView();
$this->assertEquals(array( $this->assertEquals(array(
6 => new ChoiceView('6', '06'), new ChoiceView('6', '06'),
7 => new ChoiceView('7', '07'), new ChoiceView('7', '07'),
), $view->getChild('month')->get('choices')); ), $view->getChild('month')->get('choices'));
} }
@ -369,8 +369,8 @@ class DateTypeTest extends LocalizedTestCase
$view = $form->createView(); $view = $form->createView();
$this->assertEquals(array( $this->assertEquals(array(
1 => new ChoiceView('1', 'Jän'), new ChoiceView('1', 'Jän'),
4 => new ChoiceView('4', 'Apr') new ChoiceView('4', 'Apr')
), $view->getChild('month')->get('choices')); ), $view->getChild('month')->get('choices'));
} }
@ -384,8 +384,8 @@ class DateTypeTest extends LocalizedTestCase
$view = $form->createView(); $view = $form->createView();
$this->assertEquals(array( $this->assertEquals(array(
1 => new ChoiceView('1', 'Jänner'), new ChoiceView('1', 'Jänner'),
4 => new ChoiceView('4', 'April'), new ChoiceView('4', 'April'),
), $view->getChild('month')->get('choices')); ), $view->getChild('month')->get('choices'));
} }
@ -399,8 +399,8 @@ class DateTypeTest extends LocalizedTestCase
$view = $form->createView(); $view = $form->createView();
$this->assertEquals(array( $this->assertEquals(array(
1 => new ChoiceView('1', 'Jänner'), new ChoiceView('1', 'Jänner'),
4 => new ChoiceView('4', 'April'), new ChoiceView('4', 'April'),
), $view->getChild('month')->get('choices')); ), $view->getChild('month')->get('choices'));
} }
@ -413,8 +413,8 @@ class DateTypeTest extends LocalizedTestCase
$view = $form->createView(); $view = $form->createView();
$this->assertEquals(array( $this->assertEquals(array(
6 => new ChoiceView('6', '06'), new ChoiceView('6', '06'),
7 => new ChoiceView('7', '07'), new ChoiceView('7', '07'),
), $view->getChild('day')->get('choices')); ), $view->getChild('day')->get('choices'));
} }

View File

@ -243,8 +243,8 @@ class TimeTypeTest extends LocalizedTestCase
$view = $form->createView(); $view = $form->createView();
$this->assertEquals(array( $this->assertEquals(array(
6 => new ChoiceView('6', '06'), new ChoiceView('6', '06'),
7 => new ChoiceView('7', '07'), new ChoiceView('7', '07'),
), $view->getChild('hour')->get('choices')); ), $view->getChild('hour')->get('choices'));
} }
@ -257,8 +257,8 @@ class TimeTypeTest extends LocalizedTestCase
$view = $form->createView(); $view = $form->createView();
$this->assertEquals(array( $this->assertEquals(array(
6 => new ChoiceView('6', '06'), new ChoiceView('6', '06'),
7 => new ChoiceView('7', '07'), new ChoiceView('7', '07'),
), $view->getChild('minute')->get('choices')); ), $view->getChild('minute')->get('choices'));
} }
@ -272,8 +272,8 @@ class TimeTypeTest extends LocalizedTestCase
$view = $form->createView(); $view = $form->createView();
$this->assertEquals(array( $this->assertEquals(array(
6 => new ChoiceView('6', '06'), new ChoiceView('6', '06'),
7 => new ChoiceView('7', '07'), new ChoiceView('7', '07'),
), $view->getChild('second')->get('choices')); ), $view->getChild('second')->get('choices'));
} }