Skip to content

Commit

Permalink
Merge pull request #24 from kschroeder/develop
Browse files Browse the repository at this point in the history
alpha pre-release?
  • Loading branch information
kschroeder committed Feb 17, 2017
2 parents 79c28c6 + 6c95129 commit b1f5b62
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 3 deletions.
File renamed without changes.
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "magium/configuration-manager",
"description": "Provides a generic plugable configuration management interface similar to Magento",
"minimum-stability": "dev",
"minimum-stability": "stable",
"license": "ASL-2.0",
"authors": [
{
Expand All @@ -16,10 +16,11 @@
}
},
"require": {
"phpunit/phpunit": "5.x.x",
"phpunit/phpunit": "~5.7",
"zendframework/zend-cache": "^2.7",
"psr/container": "^1.0@dev",
"zendframework/zend-db": "^2.8",
"symfony/console": "^3.2"
}
},
"bin": ["bin/magento-configuration"]
}
64 changes: 64 additions & 0 deletions lib/Console/Command/ConfigurationSet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

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;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class ConfigurationSet extends Command
{

const COMMAND = 'magium:configuration:set';

protected $factory;

protected function configure()
{
$this
->setName(self::COMMAND)
->setDescription('Set a configuration value')
->setHelp("This command sets a value for a specific configuration path")
;

$this->addArgument('path', InputArgument::REQUIRED, 'Configuration Path');
$this->addArgument('value', InputArgument::REQUIRED, 'Value');
$this->addArgument('context', InputArgument::OPTIONAL, 'Configuration Context', Config::CONTEXT_DEFAULT);
}

public function setConfigurationFactory(MagiumConfigurationFactoryInterface $factory)
{
$this->factory = $factory;
}

protected function getConfigurationFactory()
{
if (!$this->factory instanceof MagiumConfigurationFactoryInterface) {
$this->factory = new MagiumConfigurationFactory();
}
return $this->factory;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$factory = $this->getConfigurationFactory();
$builderFactory = $factory->getBuilderFactory();
$path = $input->getArgument('path');
$value = $input->getArgument('value');
$context = $input->getArgument('context');
$builderFactory->getPersistence()->setValue($path, $value, $context);

$out = sprintf("Set %s to %s (context: %s)", $path, $value, $context);

$output->writeln($out);
$output->writeln("Don't forget to rebuild your configuration cache with " . ConfigurationBuild::COMMAND);
}


}
4 changes: 4 additions & 0 deletions lib/Console/Symfony/CommandList.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Magium\Configuration\Console\Symfony;

use Magium\Configuration\Console\Command\ConfigurationBuild;
use Magium\Configuration\Console\Command\ConfigurationGet;
use Magium\Configuration\Console\Command\ConfigurationList;
use Magium\Configuration\Console\Command\ConfigurationSet;
use Magium\Configuration\Console\Command\ContextList;
use Magium\Configuration\Console\Command\CreateTable;
use Magium\Configuration\Console\Command\DefaultCommand;
Expand All @@ -16,6 +18,8 @@ public function addCommands(Application $application)
{
$application->add(new DefaultCommand());
$application->add(new ConfigurationBuild());
$application->add(new ConfigurationGet());
$application->add(new ConfigurationSet());
$application->add(new ConfigurationList());
$application->add(new ContextList());
$application->add(new CreateTable());
Expand Down
81 changes: 81 additions & 0 deletions tests/Command/ConfigurationSetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace Magium\Configuration\Tests\Command;

use Magium\Configuration\Config\BuilderFactory;
use Magium\Configuration\Config\BuilderFactoryInterface;
use Magium\Configuration\Config\Config;
use Magium\Configuration\Config\Storage\StorageInterface;
use Magium\Configuration\Console\Command\ConfigurationSet;
use Magium\Configuration\MagiumConfigurationFactory;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ConfigurationSetTest extends TestCase
{

public function testSetWorks()
{
$input = $this->createMock(InputInterface::class);
$input->expects(self::exactly(3))->method('getArgument')->willReturnCallback(function($param) {
$values = [
'path' => 'path value',
'value' => 'string value',
'context' => 'context name'
];
TestCase::assertContains($param, array_keys($values));
return $values[$param];
});
$persistence = $this->getPersistence();
$persistence->expects(self::once())->method('setValue')->with(
self::equalTo('path value'),
self::equalTo('string value'),
self::equalTo('context name')
);

$this->execute($persistence, $input);
}

protected function execute(StorageInterface $persistence, InputInterface $input)
{
$builderFactory = $this->getBuilderFactory();
$builderFactory->expects(self::once())->method('getPersistence')->willReturn($persistence);
/* @var $builderFactory BuilderFactoryInterface */
/* @var $input InputInterface */
$factory = $this->getFactory($builderFactory);
$command = new ConfigurationSet();
$command->setConfigurationFactory($factory);
$command->run(
$input,
$this->createMock(OutputInterface::class)
);
}

protected function getPersistence()
{
$persistence = $this->getMockBuilder(StorageInterface::class)->disableOriginalConstructor()->setMethods(
get_class_methods(StorageInterface::class)
)->getMock();
return $persistence;
}

protected function getBuilderFactory()
{
$factory = $this->getMockBuilder(BuilderFactory::class)->disableOriginalConstructor()->setMethods(
['getPersistence']
)->getMock();
return $factory;
}

protected function getFactory(BuilderFactoryInterface $builderFactory)
{
$factory = $this->getMockBuilder(MagiumConfigurationFactory::class)->disableOriginalConstructor()->setMethods(
['getBuilderFactory']
)->getMock();
$factory->expects(self::once())->method('getBuilderFactory')->willReturn($builderFactory);
return $factory;
}


}
38 changes: 38 additions & 0 deletions tests/Command/Symfony/CommandListTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Magium\Configuration\Tests\Command\Symfony;

use Magium\Configuration\Console\Command\DefaultCommand;
use Magium\Configuration\Console\Symfony\CommandList;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;

class CommandListTest extends TestCase
{

public function testThatAllCommandsAreIncluded()
{
$application = new Application();
$commandList = new CommandList();
$commandList->addCommands($application);
$reflectionClass = new \ReflectionClass(DefaultCommand::class);
$namespace = $reflectionClass->getNamespaceName();
$dir = $reflectionClass->getFileName();
$glob = dirname($dir) . DIRECTORY_SEPARATOR . '*.php';
foreach (glob($glob) as $file) {
$name = basename($file);
$name = substr($name, 0, -4);
$class = sprintf('%s\%s', $namespace, $name);
$object = new $class();
if ($object instanceof Command) {
$consoleName = $object->getName();
$appInstance = $application->get($consoleName);
self::assertInstanceOf($class, $appInstance);
} else {
self::fail('Command classes need to extend Command');
}
}
}

}

0 comments on commit b1f5b62

Please sign in to comment.