Skip to content

Commit

Permalink
Merge pull request #70 from kschroeder/develop
Browse files Browse the repository at this point in the history
Updated to better handle child nodes of the //element node
  • Loading branch information
kschroeder committed May 25, 2017
2 parents ebd6f52 + d117cb5 commit b7915c1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
22 changes: 19 additions & 3 deletions lib/Config/Builder.php
Expand Up @@ -309,11 +309,27 @@ protected function mergeElements(\SimpleXMLElement $group, \SimpleXMLElement $ne
foreach ($newElement->attributes() as $name => $value) {
$element[$name] = (string)$value;
}
foreach ($newElement->children() as $key => $item) {
$element->$key = (string)$item;
}
$this->mergeAllChildren($element, $newElement);
}
}
}

protected function mergeAllChildren(\SimpleXMLElement $destination, \SimpleXMLElement $source)
{
foreach ($source->attributes() as $name => $value) {
$destination[$name] = (string)$value;
}
foreach ($source->children() as $key => $item) {
$childNodes = $destination->xpath(sprintf('/%s', $key));
if (!empty($childNodes) && $childNodes[0] instanceof \SimpleXMLElement) {
$element = $childNodes[0];
} else {
$value = trim((string)$item);
$element = $destination->addChild($key, $value);
}
$this->mergeAllChildren($element, $item);

}
}

}
27 changes: 23 additions & 4 deletions tests/Config/BuilderTest.php
Expand Up @@ -4,22 +4,21 @@

use Interop\Container\ContainerInterface;
use Magium\Configuration\Config\Builder;
use Magium\Configuration\Config\BuilderFactory;
use Magium\Configuration\Config\BuilderInterface;
use Magium\Configuration\Config\Repository\ConfigInterface;
use Magium\Configuration\Config\Repository\ConfigurationRepository;
use Magium\Configuration\Config\InsufficientContainerException;
use Magium\Configuration\Config\InvalidArgumentException;
use Magium\Configuration\Config\InvalidConfigurationLocationException;
use Magium\Configuration\Config\InvalidDirectoryException;
use Magium\Configuration\Config\MergedStructure;
use Magium\Configuration\Config\MissingConfigurationException;
use Magium\Configuration\Config\Repository\ConfigInterface;
use Magium\Configuration\Config\Repository\ConfigurationRepository;
use Magium\Configuration\Config\Storage\StorageInterface;
use Magium\Configuration\Config\UncallableCallbackException;
use Magium\Configuration\File\Configuration\ConfigurationFileRepository;
use Magium\Configuration\File\Configuration\UnsupportedFileTypeException;
use Magium\Configuration\File\InvalidFileException;
use Magium\Configuration\File\Configuration\XmlFile;
use Magium\Configuration\File\InvalidFileException;
use Magium\Configuration\InvalidConfigurationException;
use Magium\Configuration\Tests\Container\ModelInjected;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -81,6 +80,26 @@ public function testInvalidConfigurationFileThrowsException()
$builder->build();
}

public function testEnsureThatElementChildrenAreIncluded()
{
$repository = ConfigurationFileRepository::getInstance(
[__DIR__],
[
realpath(__DIR__ . '/xml/config-merge-1.xml'),
realpath(__DIR__ . '/xml/config-merge-2.xml')
]
);
$builder = new Builder(
$this->getCacheStorageMock(),
$this->getPersistenceStorageMock(),
$repository
);
$merged = $builder->getMergedStructure();
$merged->registerXPathNamespace('s', 'http://www.magiumlib.com/Configuration');
$thatThing = $merged->xpath('//s:element[@identifier="title"]/descendant::s:value[@identifier]');
self::assertCount(0, $thatThing);
}

public function testBuildConfigurationNewChildren()
{
/*
Expand Down
4 changes: 4 additions & 0 deletions tests/Config/xml/config-merge-2.xml
Expand Up @@ -5,6 +5,10 @@
<section identifier="general" label="General">
<group identifier="website" label="Website">
<element identifier="title" label="Title">
<permittedValues>
<value>My Homepage</value>
<value>Your Homepage</value>
</permittedValues>
<value>My Homepage</value>
</element>
<element identifier="languages" label="Languages" source="Namespace\MySource" type="multi"/>
Expand Down

0 comments on commit b7915c1

Please sign in to comment.