123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Tests\Component\Form;
- use Symfony\Component\Form\ChoiceField;
- use Symfony\Component\Form\Exception\UnexpectedTypeException;
- class ChoiceFieldTest extends \PHPUnit_Framework_TestCase
- {
- protected $choices = array(
- 'a' => 'Bernhard',
- 'b' => 'Fabien',
- 'c' => 'Kris',
- 'd' => 'Jon',
- 'e' => 'Roman',
- );
- protected $preferredChoices = array('d', 'e');
- protected $groupedChoices = array(
- 'Symfony' => array(
- 'a' => 'Bernhard',
- 'b' => 'Fabien',
- 'c' => 'Kris',
- ),
- 'Doctrine' => array(
- 'd' => 'Jon',
- 'e' => 'Roman',
- )
- );
- protected $numericChoices = array(
- 0 => 'Bernhard',
- 1 => 'Fabien',
- 2 => 'Kris',
- 3 => 'Jon',
- 4 => 'Roman',
- );
- public function testIsChoiceSelectedDifferentiatesBetweenZeroAndEmpty_integerZero()
- {
- $field = new ChoiceField('name', array(
- 'choices' => array(
- 0 => 'Foo',
- '' => 'Bar',
- )
- ));
- $field->submit(0);
- $this->assertTrue($field->isChoiceSelected(0));
- $this->assertTrue($field->isChoiceSelected('0'));
- $this->assertFalse($field->isChoiceSelected(''));
- $field->submit('0');
- $this->assertTrue($field->isChoiceSelected(0));
- $this->assertTrue($field->isChoiceSelected('0'));
- $this->assertFalse($field->isChoiceSelected(''));
- $field->submit('');
- $this->assertFalse($field->isChoiceSelected(0));
- $this->assertFalse($field->isChoiceSelected('0'));
- $this->assertTrue($field->isChoiceSelected(''));
- }
- public function testIsChoiceSelectedDifferentiatesBetweenZeroAndEmpty_stringZero()
- {
- $field = new ChoiceField('name', array(
- 'choices' => array(
- '0' => 'Foo',
- '' => 'Bar',
- )
- ));
- $field->submit(0);
- $this->assertTrue($field->isChoiceSelected(0));
- $this->assertTrue($field->isChoiceSelected('0'));
- $this->assertFalse($field->isChoiceSelected(''));
- $field->submit('0');
- $this->assertTrue($field->isChoiceSelected(0));
- $this->assertTrue($field->isChoiceSelected('0'));
- $this->assertFalse($field->isChoiceSelected(''));
- $field->submit('');
- $this->assertFalse($field->isChoiceSelected(0));
- $this->assertFalse($field->isChoiceSelected('0'));
- $this->assertTrue($field->isChoiceSelected(''));
- }
- /**
- * @expectedException Symfony\Component\Form\Exception\InvalidOptionsException
- */
- public function testConfigureChoicesWithNonArray()
- {
- $field = new ChoiceField('name', array(
- 'choices' => new \ArrayObject(),
- ));
- }
- /**
- * @expectedException Symfony\Component\Form\Exception\InvalidOptionsException
- */
- public function testConfigurePreferredChoicesWithNonArray()
- {
- $field = new ChoiceField('name', array(
- 'choices' => $this->choices,
- 'preferred_choices' => new \ArrayObject(),
- ));
- }
- public function getChoicesVariants()
- {
- $choices = $this->choices;
- return array(
- array($choices),
- array(function () use ($choices) { return $choices; }),
- );
- }
- public function getNumericChoicesVariants()
- {
- $choices = $this->numericChoices;
- return array(
- array($choices),
- array(function () use ($choices) { return $choices; }),
- );
- }
- /**
- * @expectedException Symfony\Component\Form\Exception\InvalidOptionsException
- */
- public function testClosureShouldReturnArray()
- {
- $field = new ChoiceField('name', array(
- 'choices' => function () { return 'foobar'; },
- ));
- // trigger closure
- $field->getOtherChoices();
- }
- public function testNonRequiredContainsEmptyField()
- {
- $field = new ChoiceField('name', array(
- 'multiple' => false,
- 'expanded' => false,
- 'choices' => $this->choices,
- 'required' => false,
- ));
- $this->assertEquals(array('' => '') + $this->choices, $field->getOtherChoices());
- }
- public function testRequiredContainsNoEmptyField()
- {
- $field = new ChoiceField('name', array(
- 'multiple' => false,
- 'expanded' => false,
- 'choices' => $this->choices,
- 'required' => true,
- ));
- $this->assertEquals($this->choices, $field->getOtherChoices());
- }
- public function testEmptyValueConfiguresLabelOfEmptyField()
- {
- $field = new ChoiceField('name', array(
- 'multiple' => false,
- 'expanded' => false,
- 'choices' => $this->choices,
- 'required' => false,
- 'empty_value' => 'Foobar',
- ));
- $this->assertEquals(array('' => 'Foobar') + $this->choices, $field->getOtherChoices());
- }
- /**
- * @dataProvider getChoicesVariants
- */
- public function testSubmitSingleNonExpanded($choices)
- {
- $field = new ChoiceField('name', array(
- 'multiple' => false,
- 'expanded' => false,
- 'choices' => $choices,
- ));
- $field->submit('b');
- $this->assertEquals('b', $field->getData());
- $this->assertEquals('b', $field->getDisplayedData());
- }
- /**
- * @dataProvider getChoicesVariants
- */
- public function testSubmitMultipleNonExpanded($choices)
- {
- $field = new ChoiceField('name', array(
- 'multiple' => true,
- 'expanded' => false,
- 'choices' => $choices,
- ));
- $field->submit(array('a', 'b'));
- $this->assertEquals(array('a', 'b'), $field->getData());
- $this->assertEquals(array('a', 'b'), $field->getDisplayedData());
- }
- /**
- * @dataProvider getChoicesVariants
- */
- public function testSubmitSingleExpanded($choices)
- {
- $field = new ChoiceField('name', array(
- 'multiple' => false,
- 'expanded' => true,
- 'choices' => $choices,
- ));
- $field->submit('b');
- $this->assertSame('b', $field->getData());
- $this->assertSame(false, $field['a']->getData());
- $this->assertSame(true, $field['b']->getData());
- $this->assertSame(false, $field['c']->getData());
- $this->assertSame(false, $field['d']->getData());
- $this->assertSame(false, $field['e']->getData());
- $this->assertSame('', $field['a']->getDisplayedData());
- $this->assertSame('1', $field['b']->getDisplayedData());
- $this->assertSame('', $field['c']->getDisplayedData());
- $this->assertSame('', $field['d']->getDisplayedData());
- $this->assertSame('', $field['e']->getDisplayedData());
- $this->assertSame(array('a' => '', 'b' => '1', 'c' => '', 'd' => '', 'e' => ''), $field->getDisplayedData());
- }
- /**
- * @dataProvider getNumericChoicesVariants
- */
- public function testSubmitSingleExpandedNumericChoices($choices)
- {
- $field = new ChoiceField('name', array(
- 'multiple' => false,
- 'expanded' => true,
- 'choices' => $choices,
- ));
- $field->submit('1');
- $this->assertSame(1, $field->getData());
- $this->assertSame(false, $field[0]->getData());
- $this->assertSame(true, $field[1]->getData());
- $this->assertSame(false, $field[2]->getData());
- $this->assertSame(false, $field[3]->getData());
- $this->assertSame(false, $field[4]->getData());
- $this->assertSame('', $field[0]->getDisplayedData());
- $this->assertSame('1', $field[1]->getDisplayedData());
- $this->assertSame('', $field[2]->getDisplayedData());
- $this->assertSame('', $field[3]->getDisplayedData());
- $this->assertSame('', $field[4]->getDisplayedData());
- $this->assertSame(array(0 => '', 1 => '1', 2 => '', 3 => '', 4 => ''), $field->getDisplayedData());
- }
- /**
- * @dataProvider getChoicesVariants
- */
- public function testSubmitMultipleExpanded($choices)
- {
- $field = new ChoiceField('name', array(
- 'multiple' => true,
- 'expanded' => true,
- 'choices' => $choices,
- ));
- $field->submit(array('a' => 'a', 'b' => 'b'));
- $this->assertSame(array('a', 'b'), $field->getData());
- $this->assertSame(true, $field['a']->getData());
- $this->assertSame(true, $field['b']->getData());
- $this->assertSame(false, $field['c']->getData());
- $this->assertSame(false, $field['d']->getData());
- $this->assertSame(false, $field['e']->getData());
- $this->assertSame('1', $field['a']->getDisplayedData());
- $this->assertSame('1', $field['b']->getDisplayedData());
- $this->assertSame('', $field['c']->getDisplayedData());
- $this->assertSame('', $field['d']->getDisplayedData());
- $this->assertSame('', $field['e']->getDisplayedData());
- $this->assertSame(array('a' => '1', 'b' => '1', 'c' => '', 'd' => '', 'e' => ''), $field->getDisplayedData());
- }
- /**
- * @dataProvider getNumericChoicesVariants
- */
- public function testSubmitMultipleExpandedNumericChoices($choices)
- {
- $field = new ChoiceField('name', array(
- 'multiple' => true,
- 'expanded' => true,
- 'choices' => $choices,
- ));
- $field->submit(array(1 => 1, 2 => 2));
- $this->assertSame(array(1, 2), $field->getData());
- $this->assertSame(false, $field[0]->getData());
- $this->assertSame(true, $field[1]->getData());
- $this->assertSame(true, $field[2]->getData());
- $this->assertSame(false, $field[3]->getData());
- $this->assertSame(false, $field[4]->getData());
- $this->assertSame('', $field[0]->getDisplayedData());
- $this->assertSame('1', $field[1]->getDisplayedData());
- $this->assertSame('1', $field[2]->getDisplayedData());
- $this->assertSame('', $field[3]->getDisplayedData());
- $this->assertSame('', $field[4]->getDisplayedData());
- $this->assertSame(array(0 => '', 1 => '1', 2 => '1', 3 => '', 4 => ''), $field->getDisplayedData());
- }
- }
|