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\DataTransformer\PercentToLocalizedStringTransformer
;
15 use Symfony\Component\Intl\Util\IntlTestHelper
;
17 class PercentToLocalizedStringTransformerTest
extends \PHPUnit_Framework_TestCase
19 protected function setUp()
23 // Since we test against "de_AT", we need the full implementation
24 IntlTestHelper
::requireFullIntl($this);
26 \Locale
::setDefault('de_AT');
29 public function testTransform()
31 $transformer = new PercentToLocalizedStringTransformer();
33 $this->assertEquals('10', $transformer->transform(0.1));
34 $this->assertEquals('15', $transformer->transform(0.15));
35 $this->assertEquals('12', $transformer->transform(0.1234));
36 $this->assertEquals('200', $transformer->transform(2));
39 public function testTransformEmpty()
41 $transformer = new PercentToLocalizedStringTransformer();
43 $this->assertEquals('', $transformer->transform(null));
46 public function testTransformWithInteger()
48 $transformer = new PercentToLocalizedStringTransformer(null, 'integer');
50 $this->assertEquals('0', $transformer->transform(0.1));
51 $this->assertEquals('1', $transformer->transform(1));
52 $this->assertEquals('15', $transformer->transform(15));
53 $this->assertEquals('16', $transformer->transform(15.9));
56 public function testTransformWithPrecision()
58 $transformer = new PercentToLocalizedStringTransformer(2);
60 $this->assertEquals('12,34', $transformer->transform(0.1234));
63 public function testReverseTransform()
65 $transformer = new PercentToLocalizedStringTransformer();
67 $this->assertEquals(0.1, $transformer->reverseTransform('10'));
68 $this->assertEquals(0.15, $transformer->reverseTransform('15'));
69 $this->assertEquals(0.12, $transformer->reverseTransform('12'));
70 $this->assertEquals(2, $transformer->reverseTransform('200'));
73 public function testReverseTransformEmpty()
75 $transformer = new PercentToLocalizedStringTransformer();
77 $this->assertNull($transformer->reverseTransform(''));
80 public function testReverseTransformWithInteger()
82 $transformer = new PercentToLocalizedStringTransformer(null, 'integer');
84 $this->assertEquals(10, $transformer->reverseTransform('10'));
85 $this->assertEquals(15, $transformer->reverseTransform('15'));
86 $this->assertEquals(12, $transformer->reverseTransform('12'));
87 $this->assertEquals(200, $transformer->reverseTransform('200'));
90 public function testReverseTransformWithPrecision()
92 $transformer = new PercentToLocalizedStringTransformer(2);
94 $this->assertEquals(0.1234, $transformer->reverseTransform('12,34'));
97 public function testTransformExpectsNumeric()
99 $transformer = new PercentToLocalizedStringTransformer();
101 $this->setExpectedException('Symfony\Component\Form\Exception\TransformationFailedException');
103 $transformer->transform('foo');
106 public function testReverseTransformExpectsString()
108 $transformer = new PercentToLocalizedStringTransformer();
110 $this->setExpectedException('Symfony\Component\Form\Exception\TransformationFailedException');
112 $transformer->reverseTransform(1);