4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Form\Tests\Extension\Core\EventListener
;
14 use Symfony\Component\Form\FormEvent
;
15 use Symfony\Component\Form\Extension\Core\EventListener\FixRadioInputListener
;
16 use Symfony\Component\Form\Extension\Core\ChoiceList\SimpleChoiceList
;
18 class FixRadioInputListenerTest
extends \PHPUnit_Framework_TestCase
22 protected function setUp()
24 if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) {
25 $this->markTestSkipped('The "EventDispatcher" component is not available');
30 $this->choiceList
= new SimpleChoiceList(array('' => 'Empty', 0 => 'A', 1 => 'B'));
33 protected function tearDown()
40 public function testFixRadio()
43 $form = $this->getMock('Symfony\Component\Form\Test\FormInterface');
44 $event = new FormEvent($form, $data);
46 $listener = new FixRadioInputListener($this->choiceList
, true);
47 $listener->preSubmit($event);
49 // Indices in SimpleChoiceList are zero-based generated integers
50 $this->assertEquals(array(2 => '1'), $event->getData());
53 public function testFixZero()
56 $form = $this->getMock('Symfony\Component\Form\Test\FormInterface');
57 $event = new FormEvent($form, $data);
59 $listener = new FixRadioInputListener($this->choiceList
, true);
60 $listener->preSubmit($event);
62 // Indices in SimpleChoiceList are zero-based generated integers
63 $this->assertEquals(array(1 => '0'), $event->getData());
66 public function testFixEmptyString()
69 $form = $this->getMock('Symfony\Component\Form\Test\FormInterface');
70 $event = new FormEvent($form, $data);
72 $listener = new FixRadioInputListener($this->choiceList
, true);
73 $listener->preSubmit($event);
75 // Indices in SimpleChoiceList are zero-based generated integers
76 $this->assertEquals(array(0 => ''), $event->getData());
79 public function testConvertEmptyStringToPlaceholderIfNotFound()
81 $list = new SimpleChoiceList(array(0 => 'A', 1 => 'B'));
84 $form = $this->getMock('Symfony\Component\Form\Test\FormInterface');
85 $event = new FormEvent($form, $data);
87 $listener = new FixRadioInputListener($list, true);
88 $listener->preSubmit($event);
90 $this->assertEquals(array('placeholder' => ''), $event->getData());
93 public function testDontConvertEmptyStringToPlaceholderIfNoPlaceholderUsed()
95 $list = new SimpleChoiceList(array(0 => 'A', 1 => 'B'));
98 $form = $this->getMock('Symfony\Component\Form\Test\FormInterface');
99 $event = new FormEvent($form, $data);
101 $listener = new FixRadioInputListener($list, false);
102 $listener->preSubmit($event);
104 $this->assertEquals(array(), $event->getData());