Repository: symfony/symfony
File: src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php (PHP)

Code before:
                </FooBar>
            </Barry>
        </baz>
        <qux>1</qux>
    </response>
    <?instruction <value> ?>
XML;
        $obj = $this->getObject();

        $this->assertEquals(get_object_vars($obj), $this->encoder->decode($source, 'xml'));
    }

    public function testCDataNamePattern()
    {
        $expected = <<<'XML'
<?xml version="1.0"?>
<response><person><firstname><![CDATA[Benjamin]]></firstname><lastname><![CDATA[Alexandre]]></lastname><other><![CDATA[data]]></other></person><person><firstname><![CDATA[Damien]]></firstname><lastname><![CDATA[Clay]]></lastname><other><![CDATA[data]]></other></person></response>

XML;
        $source = ['person' => [
            ['firstname' => 'Benjamin', 'lastname' => 'Alexandre', 'other' => 'data'],
            ['firstname' => 'Damien', 'lastname' => 'Clay', 'other' => 'data'],
        ]];

        $this->assertEquals($expected, $this->encoder->encode($source, 'xml',[
            XmlEncoder::CDATA_WRAPPING_NAME_PATTERN => '(firstname|lastname|)'
        ]));
    }

    public function testDecodeIgnoreWhiteSpace()
    {
        $source = <<<'XML'
<?xml version="1.0"?>
<people>
    <person>
        <firstname>Benjamin</firstname>
        <lastname>Alexandre</lastname>
    </person>
    <person>
        <firstname>Damien</firstname>
        <lastname>Clay</lastname>
    </person>
</people>
XML;
        $expected = ['person' => [
            ['firstname' => 'Benjamin', 'lastname' => 'Alexandre'],
            ['firstname' => 'Damien', 'lastname' => 'Clay'],
        ]];

        $this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
    }

Review comment (suggestion):
using parenthesis as regex delimiter is quite uncommon. People reading the tests could think that we expect a partial pattern where we add the delimiter ourselves. I suggest changing the delimiters being used (and I also suggest using an anchored regex in that test)

Code after:
                </FooBar>
            </Barry>
        </baz>
        <qux>1</qux>
    </response>
    <?instruction <value> ?>
XML;
        $obj = $this->getObject();

        $this->assertEquals(get_object_vars($obj), $this->encoder->decode($source, 'xml'));
    }

    public function testCDataNamePattern()
    {
        $expected = <<<'XML'
<?xml version="1.0"?>
<response><person><firstname><![CDATA[Benjamin]]></firstname><lastname><![CDATA[Alexandre]]></lastname><other>data</other></person><person><firstname><![CDATA[Damien]]></firstname><lastname><![CDATA[Clay]]></lastname><other>data</other></person></response>

XML;
        $source = ['person' => [
            ['firstname' => 'Benjamin', 'lastname' => 'Alexandre', 'other' => 'data'],
            ['firstname' => 'Damien', 'lastname' => 'Clay', 'other' => 'data'],
        ]];

        $this->assertEquals($expected, $this->encoder->encode($source, 'xml', [
            XmlEncoder::CDATA_WRAPPING_NAME_PATTERN => '/(firstname|lastname)/',
        ]));
    }

    public function testDecodeIgnoreWhiteSpace()
    {
        $source = <<<'XML'
<?xml version="1.0"?>
<people>
    <person>
        <firstname>Benjamin</firstname>
        <lastname>Alexandre</lastname>
    </person>
    <person>
        <firstname>Damien</firstname>
        <lastname>Clay</lastname>
    </person>
</people>
XML;
        $expected = ['person' => [
            ['firstname' => 'Benjamin', 'lastname' => 'Alexandre'],
            ['firstname' => 'Damien', 'lastname' => 'Clay'],
        ]];

        $this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
    }