Skip to content

Commit

Permalink
fixed merging bug: merging with empty array caused error
Browse files Browse the repository at this point in the history
  • Loading branch information
sallyx committed Nov 23, 2015
1 parent 82b727b commit 8972465
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 30 deletions.
14 changes: 11 additions & 3 deletions src/Adapters/XmlAdapter.php
Expand Up @@ -212,7 +212,11 @@ private function parseStatement($child)
}
$arrayAttr = $argsElement[0]->getAttribute(self::ATTR_ARRAY);
if (is_null($arrayAttr)) {
$argsElement[0]->addAttribute(self::ATTR_ARRAY, self::ATTR_ARRAY_NUMERIC);
if($argsElement[0]->count()) {
$argsElement[0]->addAttribute(self::ATTR_ARRAY, self::ATTR_ARRAY_NUMERIC);
} else {
$argsElement[0]->addAttribute(self::ATTR_ARRAY, self::ATTR_ARRAY_STRING);
}
}

$attributes = $argsElement[0]->parseChildren();
Expand All @@ -229,7 +233,7 @@ private function parseStatement($child)
*/
private function parseChildren()
{
$arrayType = $this->getAttribute(self::ATTR_ARRAY, self::ATTR_ARRAY_ASSOCIATIVE);
$arrayType = $this->getAttribute(self::ATTR_ARRAY);
$space = (string) $this->getAttribute(self::ATTR_SPACE);
if ($space !== self::ATTR_SPACE_PRESERVE and ! empty($space)) {
throw new Nette\InvalidStateException("Attribute " . self::ATTR_SPACE . " has an unknown value '$space'");
Expand All @@ -241,7 +245,11 @@ private function parseChildren()
return $res;
}

if (!$this->count()) {
if ($arrayType === NULL && $this->count()) {
$arrayType = self::ATTR_ARRAY_ASSOCIATIVE;
}

if ($arrayType === NULL) {
$res = $this->getValue();
$this->trim($res, $space);
return $res;
Expand Down
36 changes: 36 additions & 0 deletions tests/Adapters/XmlAdapter.merge.phpt
@@ -0,0 +1,36 @@
<?php

/**
* Test: Sallyx\Nette\DI\Config\Adapters\XmlAdapter
*/

use Nette\DI\Config;
use Nette\DI\Statement;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';

$config = new Config\Loader;
$config->addAdapter('xml','Sallyx\Nette\DI\Config\Adapters\XmlAdapter');
$data = $config->load('files/xmlAdapter.merge.xml', 'production');
Assert::same(array(
'webname' => 'the example',
'extensions' => array(
'redis' => 'Kdyby\\Redis\\DI\\RedisExtension',
'streamWrappers' => 'Sallyx\\Bridges\\StreamWrappers\\Nette\\DI\\StreamWrappersExtension',
),
'redis' => array(
'journal' => TRUE,
'storage' => TRUE,
'session' => array('native' => FALSE, 'debugger' => TRUE),
),
), $data);

$data = $config->load('files/xmlAdapter.merge2.xml', 'development');
Assert::same(array(
'webname' => 'the example',
'extensions' => array(
),
'redis' => array(
),
), $data);
27 changes: 0 additions & 27 deletions tests/Adapters/XmlAdapter.phpt
Expand Up @@ -74,30 +74,3 @@ Assert::match(<<<EOD
<config xmlns:nc="http://www.sallyx.org/xmlns/nette/config/1.0" xmlns="http://www.sallyx.org/xmlns/nette/config/1.0"><production><webname>the example</webname><database><adapter>pdo_mysql</adapter><params><host>db.example.com</host><username>dbuser</username><password>secret </password><dbname>dbname</dbname></params></database></production><development extends="production"><database><params><host>dev.example.com</host><username>devuser</username><password>devsecret</password></params></database><timeout number="10"></timeout><display_errors bool="1"></display_errors><html_errors bool="0"></html_errors><items array="numeric"><item number="10"></item><item number="20"></item></items><php><zlib.output_compression bool="1"></zlib.output_compression><date.timezone>Europe/Prague</date.timezone></php></development><nothing></nothing></config>
EOD
, $actual);

$data = $config->load('files/xmlAdapter.entity.xml');
Assert::equal(array(
new Statement('ent', array(1)),
new Statement(array(
new Statement('ent', array(2)),
'inner',
),
array('3', '4')
),
new Statement(array(
new Statement('ent', array('3')),
'inner',
),
array('5','6')
),
), $data);

$data = $config->load('files/xmlAdapter.entity.xml');
$config->save($data, TEMP_FILE);
$actual = file_get_contents(TEMP_FILE);
$actual = preg_replace('/\<([^\s\/>]+)(\s*[^\/>]*)\/\s*\>/i', '<$1$2></$1>', $actual);
Assert::match(<<<EOD
<?xml version="1.0"?>
<config xmlns:nc="http://www.sallyx.org/xmlns/nette/config/1.0" xmlns="http://www.sallyx.org/xmlns/nette/config/1.0" array="numeric"><item statement="statement"><s><ent>ent</ent><args array="numeric"><item number="1"></item></args></s></item><item statement="statement"><s><ent>ent</ent><args array="numeric"><item number="2"></item></args></s><s><ent>inner</ent><args array="numeric"><item>3</item><item>4</item></args></s></item><item statement="statement"><s><ent>ent</ent><args array="numeric"><item>3</item></args></s><s><ent>inner</ent><args array="numeric"><item>5</item><item>6</item></args></s></item></config>
EOD
, $actual);
45 changes: 45 additions & 0 deletions tests/Adapters/XmlAdapter.statement.phpt
@@ -0,0 +1,45 @@
<?php

/**
* Test: Sallyx\Nette\DI\Config\Adapters\XmlAdapter
*/

use Nette\DI\Config;
use Nette\DI\Statement;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';

define('TEMP_FILE', TEMP_DIR . '/cfg.xml');


$config = new Config\Loader;
$config->addAdapter('xml','Sallyx\Nette\DI\Config\Adapters\XmlAdapter');

$data = $config->load('files/xmlAdapter.entity.xml');
Assert::equal(array(
new Statement('ent', array(1)),
new Statement(array(
new Statement('ent', array(2)),
'inner',
),
array('3', '4')
),
new Statement(array(
new Statement('ent', array('3')),
'inner',
),
array('5','6')
),
), $data);

$data = $config->load('files/xmlAdapter.entity.xml');
$config->save($data, TEMP_FILE);
$actual = file_get_contents(TEMP_FILE);
$actual = preg_replace('/\<([^\s\/>]+)(\s*[^\/>]*)\/\s*\>/i', '<$1$2></$1>', $actual);
Assert::match(<<<EOD
<?xml version="1.0"?>
<config xmlns:nc="http://www.sallyx.org/xmlns/nette/config/1.0" xmlns="http://www.sallyx.org/xmlns/nette/config/1.0" array="numeric"><item statement="statement"><s><ent>ent</ent><args array="numeric"><item number="1"></item></args></s></item><item statement="statement"><s><ent>ent</ent><args array="numeric"><item number="2"></item></args></s><s><ent>inner</ent><args array="numeric"><item>3</item><item>4</item></args></s></item><item statement="statement"><s><ent>ent</ent><args array="numeric"><item>3</item></args></s><s><ent>inner</ent><args array="numeric"><item>5</item><item>6</item></args></s></item></config>
EOD
, $actual);
15 changes: 15 additions & 0 deletions tests/Adapters/files/xmlAdapter.merge.xml
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:nc="http://www.sallyx.org/xmlns/nette/config/1.0" xmlns="http://www.sallyx.org/xmlns/nette/config/1.0">
<production>
<webname>the example</webname>
<extensions>
<redis>Kdyby\Redis\DI\RedisExtension</redis>
<streamWrappers>Sallyx\Bridges\StreamWrappers\Nette\DI\StreamWrappersExtension</streamWrappers>
</extensions>
<redis>
<journal bool="true" />
<storage bool="true" />
<session><native bool="false" /><debugger bool="true" /></session>
</redis>
</production>
</config>
8 changes: 8 additions & 0 deletions tests/Adapters/files/xmlAdapter.merge2.xml
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:nc="http://www.sallyx.org/xmlns/nette/config/1.0" xmlns="http://www.sallyx.org/xmlns/nette/config/1.0">
<development>
<webname>the example</webname>
<extensions array="number" merging="replace"> </extensions>
<redis array="number"> </redis>
</development>
</config>

0 comments on commit 8972465

Please sign in to comment.