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\DataTransformer
;
14 use Symfony\Component\Form\Extension\Core\ChoiceList\SimpleChoiceList
;
16 use Symfony\Component\Form\Extension\Core\DataTransformer\ChoicesToValuesTransformer
;
18 class ChoicesToValuesTransformerTest
extends \PHPUnit_Framework_TestCase
20 protected $transformer;
22 protected function setUp()
24 $list = new SimpleChoiceList(array(0 => 'A', 1 => 'B', 2 => 'C'));
25 $this->transformer
= new ChoicesToValuesTransformer($list);
28 protected function tearDown()
30 $this->transformer
= null;
33 public function testTransform()
35 // Value strategy in SimpleChoiceList is to copy and convert to string
37 $out = array('0', '1', '2');
39 $this->assertSame($out, $this->transformer
->transform($in));
42 public function testTransformNull()
44 $this->assertSame(array(), $this->transformer
->transform(null));
48 * @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
50 public function testTransformExpectsArray()
52 $this->transformer
->transform('foobar');
55 public function testReverseTransform()
57 // values are expected to be valid choices and stay the same
58 $in = array('0', '1', '2');
59 $out = array(0, 1, 2);
61 $this->assertSame($out, $this->transformer
->reverseTransform($in));
64 public function testReverseTransformNull()
66 $this->assertSame(array(), $this->transformer
->reverseTransform(null));
70 * @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
72 public function testReverseTransformExpectsArray()
74 $this->transformer
->reverseTransform('foobar');