[Form] Fixed MoneyToLocalizedStringTransformer and added tests
This commit is contained in:
parent
5860bdd75a
commit
f5b451f5b9
@ -41,11 +41,15 @@ class MoneyToLocalizedStringTransformer extends NumberToLocalizedStringTransform
|
||||
*/
|
||||
public function transform($value)
|
||||
{
|
||||
if (!is_numeric($value)) {
|
||||
throw new \InvalidArgumentException(sprintf('Numeric argument expected, %s given', gettype($value)));
|
||||
if ($value !== null) {
|
||||
if (!is_numeric($value)) {
|
||||
throw new \InvalidArgumentException(sprintf('Numeric argument expected, %s given', gettype($value)));
|
||||
}
|
||||
|
||||
$value /= $this->getOption('divisor');
|
||||
}
|
||||
|
||||
return parent::transform($value / $this->getOption('divisor'));
|
||||
return parent::transform($value);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -56,7 +60,13 @@ class MoneyToLocalizedStringTransformer extends NumberToLocalizedStringTransform
|
||||
*/
|
||||
public function reverseTransform($value, $originalValue)
|
||||
{
|
||||
return parent::reverseTransform($value) * $this->getOption('divisor');
|
||||
$value = parent::reverseTransform($value, $originalValue);
|
||||
|
||||
if ($value !== null) {
|
||||
$value *= $this->getOption('divisor');
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Tests\Component\Form\ValueTransformer;
|
||||
|
||||
require_once __DIR__ . '/../LocalizedTestCase.php';
|
||||
|
||||
use Symfony\Component\Form\ValueTransformer\MoneyToLocalizedStringTransformer;
|
||||
use Symfony\Tests\Component\Form\LocalizedTestCase;
|
||||
|
||||
|
||||
class MoneyToLocalizedStringTransformerTest extends LocalizedTestCase
|
||||
{
|
||||
public function testTransform()
|
||||
{
|
||||
$transformer = new MoneyToLocalizedStringTransformer(array(
|
||||
'divisor' => 100,
|
||||
));
|
||||
$transformer->setLocale('de_AT');
|
||||
|
||||
$this->assertEquals('1,23', $transformer->transform(123));
|
||||
}
|
||||
|
||||
public function testTransformThrowsExceptionIfNotNumeric()
|
||||
{
|
||||
$transformer = new MoneyToLocalizedStringTransformer(array(
|
||||
'divisor' => 100,
|
||||
));
|
||||
|
||||
$this->setExpectedException('InvalidArgumentException');
|
||||
|
||||
$transformer->transform('abcd');
|
||||
}
|
||||
|
||||
public function testTransform_empty()
|
||||
{
|
||||
$transformer = new MoneyToLocalizedStringTransformer();
|
||||
|
||||
$this->assertSame('', $transformer->transform(null));
|
||||
}
|
||||
|
||||
public function testReverseTransform()
|
||||
{
|
||||
$transformer = new MoneyToLocalizedStringTransformer(array(
|
||||
'divisor' => 100,
|
||||
));
|
||||
$transformer->setLocale('de_AT');
|
||||
|
||||
$this->assertEquals(123, $transformer->reverseTransform('1,23', null));
|
||||
}
|
||||
|
||||
public function testReverseTransform_empty()
|
||||
{
|
||||
$transformer = new MoneyToLocalizedStringTransformer();
|
||||
|
||||
$this->assertSame(null, $transformer->reverseTransform('', null));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user