Skip to content

Commit

Permalink
Merge pull request #23 from kschroeder/develop
Browse files Browse the repository at this point in the history
Oh, just stuff
  • Loading branch information
kschroeder committed Feb 16, 2017
2 parents 6eac478 + 75efb93 commit 79c28c6
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 19 deletions.
46 changes: 29 additions & 17 deletions lib/Console/Command/ConfigurationGet.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Magium\Configuration\Console\Command;

use Magium\Configuration\Config\Config;
use Magium\Configuration\Config\ConfigInterface;
use Magium\Configuration\MagiumConfigurationFactory;
use Magium\Configuration\MagiumConfigurationFactoryInterface;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -43,6 +44,32 @@ protected function getConfigurationFactory()
return $this->factory;
}

protected function getValueFlag(ConfigInterface $config, $path)
{
$value = $config->getValueFlag($path);
if ($value) {
$value = 'flag:true';
} else {
$value = 'flag:false';
}
return $value;
}

protected function getValue(ConfigInterface $config, $path)
{
$value = $config->getValue($path);
if (is_null($value)) {
$value = '<null>';
} else if (!is_string($value) && !is_numeric($value)) {
$type = gettype($value);
$value = json_encode($value);
$value = sprintf('%s:%s', $type, $value);
} else if ($value == '') {
$value = '<empty>';
}
return $value;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$factory = $this->getConfigurationFactory();
Expand All @@ -52,24 +79,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
$config = $manager->getConfiguration($context);
$useFlag = $input->getOption('use-flag');
if ($useFlag) {
$value = $config->getValueFlag($path);
if ($value) {
$value = 'flag:true';
} else {
$value = 'flag:false';
}
$value = $this->getValueFlag($config, $path);
} else {
$value = $config->getValue($path);
if (is_null($value)) {
$value = '<null>';
} else if (!is_string($value) && !is_numeric($value)) {
ob_start();
var_dump($value);
$value = ob_get_clean();
$value = trim($value);
} else if ($value == '') {
$value = '<empty>';
}
$value = $this->getValue($config, $path);
}
$out = sprintf("Value for %s (context: %s): %s", $path, $context, $value);

Expand Down
2 changes: 1 addition & 1 deletion lib/MagiumConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function getManager()
if (!$this->manager instanceof ManagerInterface) {
$managerClass = Manager::class;
if (isset($this->xml->manager['class'])) {
$managerClass = $this->xml->manager['class'];
$managerClass = (string)$this->xml->manager['class'];
}
$reflectionClass = new \ReflectionClass($managerClass);
if ($managerClass == Manager::class) {
Expand Down
2 changes: 1 addition & 1 deletion tests/Command/ConfigurationGetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function testThatTypedConfigurationShowsType()
{
$factory = $this->getFactory($this->getManager($this->getConfig('path', true)));
$output = $this->getMockBuilder(OutputInterface::class)->setMethods(get_class_methods(OutputInterface::class))->getMock();
$output->expects(self::once())->method('writeln')->with(self::stringContains('bool(true)'));
$output->expects(self::once())->method('writeln')->with(self::stringContains('boolean:true'));
$this->executeTest($factory, $output);
}

Expand Down
24 changes: 24 additions & 0 deletions tests/Factory/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,30 @@ public function testGetManager()
self::assertInstanceOf(Manager::class, $manager);
}

public function testGetNotManagerAndMakeSureSettersAreCalled()
{
$this->setFile(<<<XML
<?xml version="1.0" encoding="utf-8"?>
<magium xmlns="http://www.magiumlib.com/BaseConfiguration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.magiumlib.com/BaseConfiguration">
<persistenceConfiguration><driver>pdo_sqlite</driver><database>:memory:</database></persistenceConfiguration>
<manager class="Magium\Configuration\Tests\Factory\NotManagerManager" />
<cache><adapter>filesystem</adapter></cache>
<localCache><adapter>filesystem</adapter></localCache>
</magium>
XML
);
$factory = new MagiumConfigurationFactory();
$manager = $factory->getManager();
self::assertInstanceOf(NotManagerManager::class, $manager);
/* @var $manager NotManagerManager */
self::assertInstanceOf(Builder::class, $manager->getBuilder());
self::assertInstanceOf(StorageInterface::class, $manager->getLocalCache());
self::assertInstanceOf(StorageInterface::class, $manager->getRemoteCache());
}

public function testInvalidBaseDirectoryThrowsException()
{
$base = $tmp = sys_get_temp_dir();
Expand Down
70 changes: 70 additions & 0 deletions tests/Factory/NotManagerManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Magium\Configuration\Tests\Factory;

use Magium\Configuration\Config\BuilderInterface;
use Magium\Configuration\Config\Config;
use Magium\Configuration\Manager\ManagerInterface;
use Zend\Cache\Storage\StorageInterface;

class NotManagerManager implements ManagerInterface
{

protected $configuration;
protected $localCache;
protected $remoteCache;
protected $builder;

/**
* @return mixed
*/
public function getLocalCache()
{
return $this->localCache;
}

/**
* @return mixed
*/
public function getRemoteCache()
{
return $this->remoteCache;
}

/**
* @return mixed
*/
public function getBuilder()
{
return $this->builder;
}

/**
* @param mixed $configuration
*/
public function setConfiguration($configuration)
{
$this->configuration = $configuration;
}

public function getConfiguration($context = Config::CONTEXT_DEFAULT)
{
return $this->configuration;
}

public function setLocalCache(StorageInterface $storage = null)
{
$this->localCache = $storage;
}

public function setRemoteCache(StorageInterface $storage)
{
$this->remoteCache = $storage;
}

public function setBuilder(BuilderInterface $builder)
{
$this->builder = $builder;
}

}

0 comments on commit 79c28c6

Please sign in to comment.