XmlEncoder example


  protected $testArray = ['test' => 'test'];

  /** * {@inheritdoc} */
  protected function setUp(): void {
    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'));
  }

  

  protected $serializer;

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();
    $this->cookies = new CookieJar();
    $encoders = [new JsonEncoder()new XmlEncoder()];
    $this->serializer = new Serializer([]$encoders);
  }

  /** * Executes a login HTTP request for a given serialization format. * * @param string $name * The username. * @param string $pass * The user password. * @param string $format * The format to use to make the request. * * @return \Psr\Http\Message\ResponseInterface * The HTTP response. */
$this->serializer = $serializer ?? self::create()->serializer;
        $this->format = $format;
        $this->context = $context + [self::MESSENGER_SERIALIZATION_CONTEXT => true];
    }

    public static function create(): self
    {
        if (!class_exists(SymfonySerializer::class)) {
            throw new LogicException(sprintf('The "%s" class requires Symfony\'s Serializer component. Try running "composer require symfony/serializer" or use "%s" instead.', __CLASS__, PhpSerializer::class));
        }

        $encoders = [new XmlEncoder()new JsonEncoder()];
        $normalizers = [new DateTimeNormalizer()new ArrayDenormalizer()new ObjectNormalizer()];
        $serializer = new SymfonySerializer($normalizers$encoders);

        return new self($serializer);
    }

    public function decode(array $encodedEnvelope): Envelope
    {
        if (empty($encodedEnvelope['body']) || empty($encodedEnvelope['headers'])) {
            throw new MessageDecodingFailedException('Encoded envelope should have at least a "body" and some "headers", or maybe you should implement your own serializer.');
        }

        
$this->expectException(\LogicException::class);
        $this->expectExceptionMessage('Mapping variadic argument "$variadic" is not supported.');
        $resolver->resolve($request$argument);
    }

    /** * @dataProvider provideMatchedFormatContext */
    public function testAcceptFormatPassed(mixed $acceptFormat, string $contentType, string $content)
    {
        $encoders = ['json' => new JsonEncoder(), 'xml' => new XmlEncoder()];
        $serializer = new Serializer([new ObjectNormalizer()]$encoders);
        $validator = (new ValidatorBuilder())->getValidator();
        $resolver = new RequestPayloadValueResolver($serializer$validator);

        $request = Request::create('/', 'POST', server: ['CONTENT_TYPE' => $contentType], content: $content);

        $argument = new ArgumentMetadata('valid', RequestPayload::class, false, false, null, false, [
            MapRequestPayload::class => new MapRequestPayload(acceptFormat: $acceptFormat),
        ]);

        $kernel = $this->createMock(HttpKernelInterface::class);
        
protected function getFunctioningResourceResponseSubscriber(RouteMatchInterface $route_match) {
    // Create a dummy of the renderer service.     $renderer = $this->prophesize(RendererInterface::class);
    $renderer->executeInRenderContext(Argument::type(RenderContext::class), Argument::type('callable'))
      ->will(function D$args) {
        $callable = $args[1];
        return $callable();
      });

    // Instantiate the ResourceResponseSubscriber we will test.     $resource_response_subscriber = new ResourceResponseSubscriber(
      new Serializer([][new JsonEncoder()new XmlEncoder()]),
      $renderer->reveal(),
      $route_match
    );

    return $resource_response_subscriber;
  }

  /** * Generates route requirements based on supported formats. * * @param array $supported_response_formats * The supported response formats to add to the route requirements. * @param array $supported_request_formats * The supported request formats to add to the route requirements. * * @return array * An array of route requirements. */
use Symfony\Component\Serializer\Tests\Fixtures\EnvelopeObject;
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";

        

  public function testFileExists() {
    $this->assertFileExists($this->filePath);
  }

  /** * Tests that the phpcs.xml.dist file is properly sorted. */
  public function testSorted() {
    $content = file_get_contents($this->filePath);
    $xml_encoder = new XmlEncoder();
    $xml_encoded = $xml_encoder->decode($content, 'xml');
    $this->assertIsArray($xml_encoded);

    $top_level_keys = array_keys($xml_encoded);
    $this->assertSorted($top_level_keys);

    $this->assertArrayHasKey('file', $xml_encoded);
    $files = $xml_encoded['file'];
    $this->assertSorted($files);

    $this->assertArrayHasKey('exclude-pattern', $xml_encoded);
    
Home | Imprint | This part of the site doesn't use cookies.