CustomNormalizer example

use Symfony\Component\Serializer\Tests\Fixtures\NormalizableTraversableDummy;
use Symfony\Component\Serializer\Tests\Fixtures\ScalarDummy;

class XmlEncoderTest extends TestCase
{
    private XmlEncoder $encoder;
    private string $exampleDateTimeString = '2017-02-19T15:16:08+0300';

    protected function setUp(): void
    {
        $this->encoder = new XmlEncoder();
        $serializer = new Serializer([new CustomNormalizer()]['xml' => new XmlEncoder()]);
        $this->encoder->setSerializer($serializer);
    }

    public function testEncodeScalar()
    {
        $obj = new ScalarDummy();
        $obj->xmlFoo = 'foo';

        $expected = '<?xml version="1.0"?>'."\n".
            '<response>foo</response>'."\n";

        
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
use Symfony\Component\Serializer\Serializer;

class JsonEncoderTest extends TestCase
{
    private JsonEncoder $encoder;
    private Serializer $serializer;

    protected function setUp(): void
    {
        $this->encoder = new JsonEncoder();
        $this->serializer = new Serializer([new CustomNormalizer()]['json' => new JsonEncoder()]);
    }

    public function testEncodeScalar()
    {
        $obj = new \stdClass();
        $obj->foo = 'foo';

        $expected = '{"foo":"foo"}';

        $this->assertEquals($expected$this->encoder->encode($obj, 'json'));
    }

    
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerAwareInterface;
use Symfony\Component\Serializer\Tests\Fixtures\ScalarDummy;

class CustomNormalizerTest extends TestCase
{
    private CustomNormalizer $normalizer;

    protected function setUp(): void
    {
        $this->normalizer = new CustomNormalizer();
        $this->normalizer->setSerializer(new Serializer());
    }

    public function testInterface()
    {
        $this->assertInstanceOf(NormalizerInterface::class$this->normalizer);
        $this->assertInstanceOf(DenormalizerInterface::class$this->normalizer);
        $this->assertInstanceOf(SerializerAwareInterface::class$this->normalizer);
    }

    public function testSerialize()
    {


    public function testNormalizeTraversable()
    {
        $serializer = new Serializer([]['json' => new JsonEncoder()]);
        $result = $serializer->serialize(new TraversableDummy(), 'json');
        $this->assertEquals('{"foo":"foo","bar":"bar"}', $result);
    }

    public function testNormalizeGivesPriorityToInterfaceOverTraversable()
    {
        $serializer = new Serializer([new CustomNormalizer()]['json' => new JsonEncoder()]);
        $result = $serializer->serialize(new NormalizableTraversableDummy(), 'json');
        $this->assertEquals('{"foo":"normalizedFoo","bar":"normalizedBar"}', $result);
    }

    public function testNormalizeOnDenormalizer()
    {
        $this->expectException(UnexpectedValueException::class);
        $serializer = new Serializer([new TestDenormalizer()][]);
        $this->assertTrue($serializer->normalize(new \stdClass(), 'json'));
    }

    
Home | Imprint | This part of the site doesn't use cookies.