supportsEncoding example


        return $this->serializer->supportsNormalization($data$format$context);
    }

    public function supportsDenormalization(mixed $data, string $type, string $format = null, array $context = []): bool
    {
        return $this->serializer->supportsDenormalization($data$type$format$context);
    }

    public function supportsEncoding(string $format, array $context = []): bool
    {
        return $this->serializer->supportsEncoding($format$context);
    }

    public function supportsDecoding(string $format, array $context = []): bool
    {
        return $this->serializer->supportsDecoding($format$context);
    }

    /** * Proxies all method calls to the original serializer. */
    public function __call(string $method, array $arguments): mixed
    {
private function getEncoder(string $format, array $context): EncoderInterface
    {
        if (isset($this->encoderByFormat[$format])
            && isset($this->encoders[$this->encoderByFormat[$format]])
        ) {
            return $this->encoders[$this->encoderByFormat[$format]];
        }

        $cache = true;
        foreach ($this->encoders as $i => $encoder) {
            $cache = $cache && !$encoder instanceof ContextAwareEncoderInterface;
            if ($encoder->supportsEncoding($format$context)) {
                if ($cache) {
                    $this->encoderByFormat[$format] = $i;
                }

                return $encoder;
            }
        }

        throw new RuntimeException(sprintf('No encoder found for format "%s".', $format));
    }
}
public function testSupports()
    {
        $encoder = $this->createMock(EncoderInterface::class);
        $encoder->method('supportsEncoding')->willReturn(true);

        $decoder = $this->createMock(DecoderInterface::class);
        $decoder->method('supportsDecoding')->willReturn(true);

        $traceableEncoder = new TraceableEncoder($encodernew SerializerDataCollector());
        $traceableDecoder = new TraceableEncoder($decodernew SerializerDataCollector());

        $this->assertTrue($traceableEncoder->supportsEncoding('data'));
        $this->assertTrue($traceableDecoder->supportsDecoding('data'));
        $this->assertFalse($traceableEncoder->supportsDecoding('data'));
        $this->assertFalse($traceableDecoder->supportsEncoding('data'));
    }
}


        return $encoded;
    }

    public function supportsEncoding(string $format, array $context = []): bool
    {
        if (!$this->encoder instanceof EncoderInterface) {
            return false;
        }

        return $this->encoder->supportsEncoding($format$context);
    }

    public function decode(string $data, string $format, array $context = []): mixed
    {
        if (!$this->encoder instanceof DecoderInterface) {
            throw new \BadMethodCallException(sprintf('The "%s()" method cannot be called as nested encoder doesn\'t implements "%s".', __METHOD__, DecoderInterface::class));
        }

        $startTime = microtime(true);
        $encoded = $this->encoder->decode($data$format$context);
        $time = microtime(true) - $startTime;

        

        return $this->serializer->supportsNormalization($data$format$context);
    }

    public function supportsDenormalization(mixed $data, string $type, string $format = null, array $context = []): bool
    {
        return $this->serializer->supportsDenormalization($data$type$format$context);
    }

    public function supportsEncoding(string $format, array $context = []): bool
    {
        return $this->serializer->supportsEncoding($format$context);
    }

    public function supportsDecoding(string $format, array $context = []): bool
    {
        return $this->serializer->supportsDecoding($format$context);
    }

    /** * Proxies all method calls to the original serializer. */
    public function __call(string $method, array $arguments): mixed
    {
if (!($encoder instanceof EncoderInterface || $encoder instanceof DecoderInterface)) {
                throw new InvalidArgumentException(sprintf('The class "%s" neither implements "%s" nor "%s".', get_debug_type($encoder), EncoderInterface::class, DecoderInterface::class));
            }
        }
        $this->encoder = new ChainEncoder($realEncoders);
        $this->decoder = new ChainDecoder($decoders);
    }

    final public function serialize(mixed $data, string $format, array $context = []): string
    {
        if (!$this->supportsEncoding($format$context)) {
            throw new UnsupportedFormatException(sprintf('Serialization for the format "%s" is not supported.', $format));
        }

        if ($this->encoder->needsNormalization($format$context)) {
            $data = $this->normalize($data$format$context);
        }

        return $this->encode($data$format$context);
    }

    final public function deserialize(mixed $data, string $type, string $format, array $context = []): mixed
    {


        return $encoded;
    }

    public function supportsEncoding(string $format, array $context = []): bool
    {
        if (!$this->encoder instanceof EncoderInterface) {
            return false;
        }

        return $this->encoder->supportsEncoding($format$context);
    }

    public function decode(string $data, string $format, array $context = []): mixed
    {
        if (!$this->encoder instanceof DecoderInterface) {
            throw new \BadMethodCallException(sprintf('The "%s()" method cannot be called as nested encoder doesn\'t implements "%s".', __METHOD__, DecoderInterface::class));
        }

        $startTime = microtime(true);
        $encoded = $this->encoder->decode($data$format$context);
        $time = microtime(true) - $startTime;

        
if (!($encoder instanceof EncoderInterface || $encoder instanceof DecoderInterface)) {
                throw new InvalidArgumentException(sprintf('The class "%s" neither implements "%s" nor "%s".', get_debug_type($encoder), EncoderInterface::class, DecoderInterface::class));
            }
        }
        $this->encoder = new ChainEncoder($realEncoders);
        $this->decoder = new ChainDecoder($decoders);
    }

    final public function serialize(mixed $data, string $format, array $context = []): string
    {
        if (!$this->supportsEncoding($format$context)) {
            throw new UnsupportedFormatException(sprintf('Serialization for the format "%s" is not supported.', $format));
        }

        if ($this->encoder->needsNormalization($format$context)) {
            $data = $this->normalize($data$format$context);
        }

        return $this->encode($data$format$context);
    }

    final public function deserialize(mixed $data, string $type, string $format, array $context = []): mixed
    {


    public function testSupportsEncoding()
    {
        $this->encoder1
            ->method('encode')
            ->willReturn('result1');
        $this->encoder2
            ->method('encode')
            ->willReturn('result2');

        $this->assertTrue($this->chainEncoder->supportsEncoding(self::FORMAT_1));
        $this->assertEquals('result1', $this->chainEncoder->encode('', self::FORMAT_1, []));

        $this->assertTrue($this->chainEncoder->supportsEncoding(self::FORMAT_2));
        $this->assertEquals('result2', $this->chainEncoder->encode('', self::FORMAT_2, []));

        $this->assertFalse($this->chainEncoder->supportsEncoding(self::FORMAT_3));

        $this->assertTrue($this->chainEncoder->supportsEncoding(self::FORMAT_3, ['foo' => 'bar']));
        $this->assertEquals('result1', $this->chainEncoder->encode('', self::FORMAT_3, ['foo' => 'bar']));

        $this->assertTrue($this->chainEncoder->supportsEncoding(self::FORMAT_3, ['foo' => 'bar2']));
        
parent::setUp();

    $this->baseEncoder = $this->createMock(BaseXmlEncoder::class);
    $this->encoder = new XmlEncoder();
    $this->encoder->setBaseEncoder($this->baseEncoder);
  }

  /** * Tests the supportsEncoding() method. */
  public function testSupportsEncoding() {
    $this->assertTrue($this->encoder->supportsEncoding('xml'));
    $this->assertFalse($this->encoder->supportsEncoding('json'));
  }

  /** * Tests the supportsDecoding() method. */
  public function testSupportsDecoding() {
    $this->assertTrue($this->encoder->supportsDecoding('xml'));
    $this->assertFalse($this->encoder->supportsDecoding('json'));
  }

  
$this->assertEquals('foo', $encoder->encode('foo', 'yaml'));
        $this->assertEquals('{ foo: 1 }', $encoder->encode(['foo' => 1], 'yaml'));
        $this->assertEquals('null', $encoder->encode(new \ArrayObject(['foo' => 1]), 'yaml'));
        $this->assertEquals('{ foo: 1 }', $encoder->encode(new \ArrayObject(['foo' => 1]), 'yaml', ['preserve_empty_objects' => true]));
    }

    public function testSupportsEncoding()
    {
        $encoder = new YamlEncoder();

        $this->assertTrue($encoder->supportsEncoding('yaml'));
        $this->assertTrue($encoder->supportsEncoding('yml'));
        $this->assertFalse($encoder->supportsEncoding('json'));
    }

    public function testDecode()
    {
        $encoder = new YamlEncoder();

        $this->assertEquals('foo', $encoder->decode('foo', 'yaml'));
        $this->assertEquals(['foo' => 1]$encoder->decode('{ foo: 1 }', 'yaml'));
    }

    
private function getEncoder(string $format, array $context): EncoderInterface
    {
        if (isset($this->encoderByFormat[$format])
            && isset($this->encoders[$this->encoderByFormat[$format]])
        ) {
            return $this->encoders[$this->encoderByFormat[$format]];
        }

        $cache = true;
        foreach ($this->encoders as $i => $encoder) {
            $cache = $cache && !$encoder instanceof ContextAwareEncoderInterface;
            if ($encoder->supportsEncoding($format$context)) {
                if ($cache) {
                    $this->encoderByFormat[$format] = $i;
                }

                return $encoder;
            }
        }

        throw new RuntimeException(sprintf('No encoder found for format "%s".', $format));
    }
}
class JsonEncodeTest extends TestCase
{
    private JsonEncode $encode;

    protected function setUp(): void
    {
        $this->encode = new JsonEncode();
    }

    public function testSupportsEncoding()
    {
        $this->assertTrue($this->encode->supportsEncoding(JsonEncoder::FORMAT));
        $this->assertFalse($this->encode->supportsEncoding('foobar'));
    }

    /** * @dataProvider encodeProvider */
    public function testEncode($toEncode$expected$context)
    {
        $this->assertEquals(
            $expected,
            $this->encode->encode($toEncode, JsonEncoder::FORMAT, $context)
        );


    public function testSingleSlash()
    {
        $this->assertSame($csv = "0\n\\\n", $this->encoder->encode($data = ['\\'], 'csv'));
        $this->assertSame($data$this->encoder->decode($csv, 'csv', [CsvEncoder::AS_COLLECTION_KEY => false]));
        $this->assertSame($data$this->encoder->decode(trim($csv), 'csv', [CsvEncoder::AS_COLLECTION_KEY => false]));
    }

    public function testSupportEncoding()
    {
        $this->assertTrue($this->encoder->supportsEncoding('csv'));
        $this->assertFalse($this->encoder->supportsEncoding('foo'));
    }

    public function testEncode()
    {
        $value = ['foo' => 'hello', 'bar' => 'hey ho'];

        $this->assertEquals(<<<'CSV' foo,bar hello,"hey ho"

class JsonEncoderTest extends UnitTestCase {

  /** * Tests the supportsEncoding() method. */
  public function testSupportsEncoding() {
    $encoder = new JsonEncoder();

    $this->assertTrue($encoder->supportsEncoding('json'));
    $this->assertTrue($encoder->supportsEncoding('ajax'));
    $this->assertFalse($encoder->supportsEncoding('xml'));
  }

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