hassankhan / config

Config is a lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files
MIT License
971 stars 136 forks source link

XML Writer fails on XML content with attributes #131

Open TravisCarden opened 3 years ago

TravisCarden commented 3 years ago

The XML writer doesn't work on XML that contains attributes. I discovered this trying to modify my phpunit.xml.

Here's a passing test case with one of the project's own mocks:

$raw = <<<RAW
<?xml version="1.0" encoding="UTF-8"?>
<config>
    <application>
        <name>configuration</name>
        <secret>s3cr3t</secret>
    </application>
    <host>localhost</host>
    <port>80</port>
    <servers>
        <server1>host1</server1>
        <server2>host2</server2>
        <server3>host3</server3>
    </servers>
</config>
RAW;
$config = new \Noodlehaus\Config($raw, new \Noodlehaus\Parser\Xml(), TRUE);
$xml = $config->toString(new \Noodlehaus\Writer\Xml(), TRUE);
print $xml;

Result:

<?xml version="1.0"?>
<config>
  <application>
    <name>configuration</name>
    <secret>s3cr3t</secret>
  </application>
  <host>localhost</host>
  <port>80</port>
  <servers>
    <server1>host1</server1>
    <server2>host2</server2>
    <server3>host3</server3>
  </servers>
</config>

Add an attribute to any element in the XML, and it will fail:

$raw = <<<RAW
<?xml version="1.0" encoding="UTF-8"?>
<config test="example">
    <application>
        <name>configuration</name>
        <secret>s3cr3t</secret>
    </application>
    <host>localhost</host>
    <port>80</port>
    <servers>
        <server1>host1</server1>
        <server2>host2</server2>
        <server3>host3</server3>
    </servers>
</config>
RAW;
$config = new \Noodlehaus\Config($raw, new \Noodlehaus\Parser\Xml(), TRUE);
$xml = $config->toString(new \Noodlehaus\Writer\Xml(), TRUE);
var_dump($xml);

Result:

<?xml version="1.0"?>
DavidePastore commented 2 years ago

Hi, @TravisCarden. Thanks for opening this issue! I'll work on a fix.