-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from kschroeder/develop
alpha pre-release?
- Loading branch information
Showing
6 changed files
with
191 additions
and
3 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} | ||
} | ||
|
||
} |