GetSetMethodNormalizer example

$denormalizer2->method('denormalize')->willReturn('test2');

        $serializer = new Serializer([$denormalizer1$denormalizer2]);

        $this->assertEquals('test1', $serializer->denormalize(['test1' => true], 'test'));

        $this->assertEquals('test2', $serializer->denormalize([], 'test'));
    }

    public function testSerialize()
    {
        $serializer = new Serializer([new GetSetMethodNormalizer()]['json' => new JsonEncoder()]);
        $data = ['title' => 'foo', 'numbers' => [5, 3]];
        $result = $serializer->serialize(Model::fromArray($data), 'json');
        $this->assertEquals(json_encode($data)$result);
    }

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

    
$this->assertEquals($this->testArray, $this->encoder->decode('test', 'test'));
  }

  /** * @covers ::getBaseEncoder */
  public function testDefaultEncoderHasSerializer() {
    // The serializer should be set on the Drupal encoder, which should then     // set it on our default encoder.     $encoder = new XmlEncoder();
    $serializer = new Serializer([new GetSetMethodNormalizer()]);
    $encoder->setSerializer($serializer);
    $base_encoder = $encoder->getBaseEncoder();
    $this->assertInstanceOf(BaseXmlEncoder::class$base_encoder);
    // Test the encoder.     $base_encoder->encode(['a' => new TestObject()], 'xml');
  }

}

class TestObject {

  
private GetSetMethodNormalizer $normalizer;
    private SerializerInterface&NormalizerInterface&MockObject $serializer;

    protected function setUp(): void
    {
        $this->createNormalizer();
    }

    private function createNormalizer(array $defaultContext = []): void
    {
        $this->serializer = $this->createMock(SerializerNormalizer::class);
        $this->normalizer = new GetSetMethodNormalizer(null, null, null, null, null, $defaultContext);
        $this->normalizer->setSerializer($this->serializer);
    }

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

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