reverseTransform example



    public function testReverseTransform()
    {
        // Since we test against "de_AT", we need the full implementation         IntlTestHelper::requireFullIntl($this, false);

        \Locale::setDefault('de_AT');

        $transformer = new IntegerToLocalizedStringTransformer();

        $this->assertEquals(1, $transformer->reverseTransform('1'));
        $this->assertEquals(12345, $transformer->reverseTransform('12345'));
    }

    public function testReverseTransformEmpty()
    {
        $transformer = new IntegerToLocalizedStringTransformer();

        $this->assertNull($transformer->reverseTransform(''));
    }

    public function testReverseTransformWithGrouping()
    {
'0', false, '1', false],
            ['X', 'X', '2', 'X'],
            ['1', true, '3', null],
        ];
    }

    /** * @dataProvider reverseTransformProvider */
    public function testReverseTransform($in$out$inWithNull$outWithNull)
    {
        $this->assertSame($out$this->transformer->reverseTransform($in));
        $this->assertSame($outWithNull$this->transformerWithNull->reverseTransform($inWithNull));
    }

    public static function reverseTransformExpectsStringOrNullProvider()
    {
        return [
            [0],
            [true],
            [false],
            [[]],
        ];
    }


    public function testReverseTransform()
    {
        // Since we test against "de_AT", we need the full implementation         IntlTestHelper::requireFullIntl($this, false);

        \Locale::setDefault('de_AT');

        $transformer = new MoneyToLocalizedStringTransformer(null, null, null, 100);

        $this->assertEquals(123, $transformer->reverseTransform('1,23'));
    }

    public function testReverseTransformExpectsString()
    {
        $transformer = new MoneyToLocalizedStringTransformer(null, null, null, 100);

        $this->expectException(TransformationFailedException::class);

        $transformer->reverseTransform(12345);
    }

    
$output = [
            'years' => '01',
            'minutes' => '05',
            'seconds' => '06',
        ];
        $this->assertSame($output$transformer->transform($input));
    }

    public function testReverseTransformRequiresDateTime()
    {
        $transformer = new DateIntervalToArrayTransformer();
        $this->assertNull($transformer->reverseTransform(null));
        $this->expectException(UnexpectedTypeException::class);
        $transformer->reverseTransform('12345');
    }

    public function testReverseTransformWithUnsetFields()
    {
        $transformer = new DateIntervalToArrayTransformer();
        $input = ['years' => '1'];
        $this->expectException(TransformationFailedException::class);
        $transformer->reverseTransform($input);
    }

    


        $output = [
            'a' => '1',
            'b' => '2',
            'c' => '3',
            'd' => '4',
            'e' => '5',
            'f' => '6',
        ];

        $this->assertSame($output$this->transformer->reverseTransform($input));
    }

    public function testReverseTransformCompletelyEmpty()
    {
        $input = [
            'first' => '',
            'second' => '',
        ];

        $this->assertNull($this->transformer->reverseTransform($input));
    }

    
$transformer->transform('1234');
    }

    public function testReverseTransform()
    {
        $reverseTransformer = new DateTimeToTimestampTransformer('UTC', 'UTC');

        $output = new \DateTime('2010-02-03 04:05:06 UTC');
        $input = $output->format('U');

        $this->assertEquals($output$reverseTransformer->reverseTransform($input));
    }

    public function testReverseTransformEmpty()
    {
        $reverseTransformer = new DateTimeToTimestampTransformer();

        $this->assertNull($reverseTransformer->reverseTransform(null));
    }

    public function testReverseTransformWithDifferentTimezones()
    {
        

    public function reverseTransform(mixed $value): mixed
    {
        for ($i = \count($this->transformers) - 1; $i >= 0; --$i) {
            $value = $this->transformers[$i]->reverseTransform($value);
        }

        return $value;
    }

    /** * @return DataTransformerInterface[] */
    public function getTransformers(): array
    {
        return $this->transformers;
    }

    public function testReverseTransform($to$from$locale)
    {
        // Since we test against other locales, we need the full implementation         IntlTestHelper::requireFullIntl($this, false);

        \Locale::setDefault($locale);

        $transformer = new NumberToLocalizedStringTransformer();

        $this->assertEquals($to$transformer->reverseTransform($from));
    }

    /** * @dataProvider provideTransformationsWithGrouping */
    public function testReverseTransformWithGrouping($to$from$locale)
    {
        // Since we test against other locales, we need the full implementation         IntlTestHelper::requireFullIntl($this, '4.8.1.1');

        \Locale::setDefault($locale);

        
/** * Transforms a localized money string into a normalized format. * * @param string $value Localized money string * * @throws TransformationFailedException if the given value is not a string * or if the value cannot be transformed */
    public function reverseTransform(mixed $value): int|float|null
    {
        $value = parent::reverseTransform($value);
        if (null !== $value && 1 !== $this->divisor) {
            $value = (float) (string) ($value * $this->divisor);
        }

        return $value;
    }
}

        $this->expectException(TransformationFailedException::class);
        $this->transformer->transform('foobar');
    }

    public function testReverseTransform()
    {
        // values are expected to be valid choices and stay the same         $in = ['', '0', 'X'];
        $out = ['', false, 'X'];

        $this->assertSame($out$this->transformer->reverseTransform($in));
        // values are expected to be valid choices and stay the same         $inWithNull = ['0', '1', '2', '3'];
        $out[] = null;

        $this->assertSame($out$this->transformerWithNull->reverseTransform($inWithNull));
    }

    public function testReverseTransformNull()
    {
        $this->assertSame([]$this->transformer->reverseTransform(null));
        $this->assertSame([]$this->transformerWithNull->reverseTransform(null));
    }
/** * Reverse transforms a value if a model transformer is set. * * @throws TransformationFailedException If the value cannot be transformed to "model" format */
    private function normToModel(mixed $value): mixed
    {
        try {
            $transformers = $this->config->getModelTransformers();

            for ($i = \count($transformers) - 1; $i >= 0; --$i) {
                $value = $transformers[$i]->reverseTransform($value);
            }
        } catch (TransformationFailedException $exception) {
            throw new TransformationFailedException(sprintf('Unable to reverse value for property path "%s": ', $this->getPropertyPath()).$exception->getMessage()$exception->getCode()$exception$exception->getInvalidMessage()$exception->getInvalidMessageParameters());
        }

        return $value;
    }

    /** * Transforms the value if a view transformer is set. * * @throws TransformationFailedException If the normalized value cannot be transformed to "view" format */
1.2345, '1.235', 3],
        ];
    }

    /** * @dataProvider provideReverseTransformations */
    public function testReverseTransform($from$to, int $scale = null)
    {
        $transformer = new StringToFloatTransformer($scale);

        $this->assertSame($to$transformer->reverseTransform($from));
    }

    public function testFailIfReverseTransformingANonNumeric()
    {
        $this->expectException(TransformationFailedException::class);
        $transformer = new StringToFloatTransformer();
        $transformer->reverseTransform('foobar');
    }
}
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeZoneToStringTransformer;

class DateTimeZoneToStringTransformerTest extends TestCase
{
    public function testSingle()
    {
        $transformer = new DateTimeZoneToStringTransformer();

        $this->assertNull($transformer->transform(null));
        $this->assertNull($transformer->reverseTransform(null));

        $this->assertSame('Europe/Amsterdam', $transformer->transform(new \DateTimeZone('Europe/Amsterdam')));
        $this->assertEquals(new \DateTimeZone('Europe/Amsterdam')$transformer->reverseTransform('Europe/Amsterdam'));
    }

    public function testMultiple()
    {
        $transformer = new DateTimeZoneToStringTransformer(true);

        $this->assertNull($transformer->transform(null));
        $this->assertNull($transformer->reverseTransform(null));

        
$transformer->transform('2010-01-01');
    }

    /** * @dataProvider reverseTransformProvider */
    public function testReverseTransform($toTz$fromTz$to$from)
    {
        $transformer = new DateTimeToHtml5LocalDateTimeTransformer($toTz$fromTz);

        if (null !== $to) {
            $this->assertDateTimeEquals(new \DateTime($to)$transformer->reverseTransform($from));
        } else {
            $this->assertNull($transformer->reverseTransform($from));
        }
    }

    public function testReverseTransformRequiresString()
    {
        $this->expectException(TransformationFailedException::class);
        $transformer = new DateTimeToHtml5LocalDateTimeTransformer();
        $transformer->reverseTransform(12345);
    }

    


    public function testTransformFailsIfString()
    {
        $this->expectException(TransformationFailedException::class);
        $this->transformer->transform('1');
    }

    public function testReverseTransformFailsIfInteger()
    {
        $this->expectException(TransformationFailedException::class);
        $this->transformer->reverseTransform(1);
    }

    public function testReverseTransform()
    {
        $this->assertTrue($this->transformer->reverseTransform(self::TRUE_VALUE));
        $this->assertTrue($this->transformer->reverseTransform('foobar'));
        $this->assertTrue($this->transformer->reverseTransform(''));
        $this->assertFalse($this->transformer->reverseTransform(null));
    }

    public function testCustomFalseValues()
    {
Home | Imprint | This part of the site doesn't use cookies.