Skip to content

Commit

Permalink
Merge pull request #40 from kschroeder/develop
Browse files Browse the repository at this point in the history
Added a hasValue check
  • Loading branch information
kschroeder authored Feb 23, 2017
2 parents 3b751d3 + 43c9b2c commit 5dd555e
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
11 changes: 11 additions & 0 deletions lib/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ class Config extends \SimpleXMLElement implements ConfigInterface
true, 'true', 1, '1', 'on', 'yes'
];

public function hasValue($path)
{
list($section, $group, $element) = explode('/', $path);
$xpath = sprintf('/*/%s/%s/%s', $section, $group, $element);
$element = $this->xpath($xpath);
if ($element) {
return true;
}
return false;
}

public function getValue($path)
{
list($section, $group, $element) = explode('/', $path);
Expand Down
2 changes: 2 additions & 0 deletions lib/Config/ConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ interface ConfigInterface
{
public function getValue($path);

public function hasValue($path);

public function getValueFlag($path);
}
2 changes: 1 addition & 1 deletion tests/Command/ConfigurationGetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function testFalseFlagIsIndicated()

protected function getConfig($path, $returnValue, $useFlag = false)
{
$config = $this->getMockBuilder(ConfigInterface::class)->setMethods(['getValue' ,'getValueFlag'])->getMock();
$config = $this->getMockBuilder(ConfigInterface::class)->setMethods(['getValue' ,'getValueFlag', 'hasValue'])->getMock();
if ($useFlag) {
$config->expects(self::once())->method('getValueFlag')->with(self::equalTo($path))->willReturn($returnValue);
$config->expects(self::never())->method('getValue');
Expand Down
55 changes: 55 additions & 0 deletions tests/Config/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Magium\Configuration\Tests\Config;

use Magium\Configuration\Config\Config;
use PHPUnit\Framework\TestCase;

class ConfigTest extends TestCase
{

public function testGetValue()
{
$config = $this->getConfig();
self::assertEquals('value', $config->getValue('section/group/element'));
self::assertNull($config->getValue('section/group/element2'));
}

public function testGetFlag()
{
$config = $this->getConfig();
self::assertTrue($config->getValueFlag('section/group/true1'));
self::assertTrue($config->getValueFlag('section/group/true2'));
self::assertTrue($config->getValueFlag('section/group/true3'));
self::assertFalse($config->getValueFlag('section/group/element'));
}

public function testHasValue()
{
$config = $this->getConfig();
self::assertTrue($config->hasValue('section/group/true1'));
self::assertTrue($config->hasValue('section/group/empty'));
self::assertFalse($config->hasValue('section/group/novalue'));
}

protected function getConfig()
{
return new Config(<<<XML
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<section>
<group>
<true1>1</true1>
<true2>on</true2>
<true3>yes</true3>
<element>value</element>
<empty></empty>
</group>
</section>
</configuration>
XML
);
}

}

0 comments on commit 5dd555e

Please sign in to comment.