-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented configuration check command
- Loading branch information
1 parent
ac40512
commit e92d5ae
Showing
6 changed files
with
311 additions
and
3 deletions.
There are no files selected for viewing
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
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,106 @@ | ||
<?php | ||
|
||
namespace Nanbando\Bundle\Command; | ||
|
||
use Nanbando\Core\Plugin\PluginRegistry; | ||
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Symfony\Component\OptionsResolver\Exception\InvalidArgumentException; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
class CheckCommand extends ContainerAwareCommand | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName('check') | ||
->setDescription('Checks configuration issues') | ||
->setHelp( | ||
<<<EOT | ||
The <info>{$this->getName()}</info> command looks for configuration issues | ||
EOT | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
|
||
$io->title('Configuration Check Report'); | ||
|
||
$io->writeln('Local directory: ' . $this->getContainer()->getParameter('nanbando.storage.local_directory')); | ||
|
||
if (!$this->getContainer()->has('filesystem.remote')) { | ||
$io->warning( | ||
'No remote storage configuration found. Please follow documentation for global configuration.' . | ||
'This leads into disabled "fetch" and "push" commands' . PHP_EOL . PHP_EOL . | ||
'http://nanbando.readthedocs.io/en/latest/configuration.html#global-configuration' | ||
); | ||
} else { | ||
$io->writeln('Remote Storage: YES'); | ||
} | ||
|
||
$this->checkBackups($io, $this->getContainer()->getParameter('nanbando.backup')); | ||
|
||
$io->writeln(''); | ||
} | ||
|
||
/** | ||
* Check backup-configuration. | ||
* | ||
* @param SymfonyStyle $io | ||
* @param array $backups | ||
*/ | ||
private function checkBackups(SymfonyStyle $io, array $backups) | ||
{ | ||
/** @var PluginRegistry $plugins */ | ||
$plugins = $this->getContainer()->get('plugins'); | ||
foreach ($backups as $name => $backup) { | ||
$this->checkBackup($plugins, $io, $name, $backup); | ||
} | ||
} | ||
|
||
/** | ||
* Check single backup-configuration. | ||
* | ||
* @param PluginRegistry $plugins | ||
* @param SymfonyStyle $io | ||
* @param string $name | ||
* @param array $backup | ||
* | ||
* @return bool | ||
*/ | ||
private function checkBackup(PluginRegistry $plugins, SymfonyStyle $io, $name, array $backup) | ||
{ | ||
$io->section('Backup: ' . $name); | ||
if (!$plugins->has($backup['plugin'])) { | ||
$io->warning(sprintf('Plugin "%s" not found', $backup['plugin'])); | ||
|
||
return false; | ||
} | ||
|
||
$optionsResolver = new OptionsResolver(); | ||
$plugins->getPlugin($backup['plugin'])->configureOptionsResolver($optionsResolver); | ||
|
||
try { | ||
$optionsResolver->resolve($backup['parameter']); | ||
} catch (InvalidArgumentException $e) { | ||
$io->warning(sprintf('Parameter not valid' . PHP_EOL . PHP_EOL . 'Message: "%s"', $e->getMessage())); | ||
|
||
return false; | ||
} | ||
|
||
$io->writeln('OK'); | ||
|
||
return true; | ||
} | ||
} |
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,199 @@ | ||
<?php | ||
|
||
namespace Nanbando\Unit\Bundle\Command; | ||
|
||
use Nanbando\Bundle\Command\CheckCommand; | ||
use Nanbando\Core\Plugin\PluginInterface; | ||
use Nanbando\Core\Plugin\PluginRegistry; | ||
use Prophecy\Argument; | ||
use Symfony\Component\Console\Application; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
class CheckCommandTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var ContainerInterface | ||
*/ | ||
private $container; | ||
|
||
/** | ||
* @var PluginRegistry | ||
*/ | ||
private $plugins; | ||
|
||
protected function setUp() | ||
{ | ||
$this->container = $this->prophesize(ContainerInterface::class); | ||
$this->plugins = $this->prophesize(PluginRegistry::class); | ||
} | ||
|
||
private function getCommandTester($remote = false, $backup = []) | ||
{ | ||
$this->container->getParameter('nanbando.storage.local_directory')->willReturn('/User/test/nanbando'); | ||
$this->container->getParameter('nanbando.backup')->willReturn($backup); | ||
$this->container->has('filesystem.remote')->willReturn($remote); | ||
|
||
$this->container->get('plugins')->willReturn($this->plugins->reveal()); | ||
|
||
$command = new CheckCommand(); | ||
$command->setContainer($this->container->reveal()); | ||
|
||
$application = new Application(); | ||
$application->add($command); | ||
|
||
$command = $application->find('check'); | ||
|
||
return new CommandTester($command); | ||
} | ||
|
||
public function testExecute() | ||
{ | ||
$commandTester = $this->getCommandTester(); | ||
$commandTester->execute([]); | ||
|
||
$this->assertContains('Local directory: /User/test/nanbando', $commandTester->getDisplay()); | ||
$this->assertContains('No remote storage configuration found.', $commandTester->getDisplay()); | ||
} | ||
|
||
public function testExecuteWithRemote() | ||
{ | ||
$commandTester = $this->getCommandTester(true); | ||
$commandTester->execute([]); | ||
|
||
$this->assertContains('Local directory: /User/test/nanbando', $commandTester->getDisplay()); | ||
$this->assertContains('Remote Storage: YES', $commandTester->getDisplay()); | ||
} | ||
|
||
public function testExecutePluginNotFound() | ||
{ | ||
$this->plugins->has('my-plugin')->willReturn(false); | ||
|
||
$commandTester = $this->getCommandTester(true, ['test' => ['plugin' => 'my-plugin']]); | ||
$commandTester->execute([]); | ||
|
||
$this->assertContains('Plugin "my-plugin" not found', $commandTester->getDisplay()); | ||
} | ||
|
||
public function testExecutePluginNotFoundMultiple() | ||
{ | ||
$plugin = $this->prophesize(PluginInterface::class); | ||
$plugin->configureOptionsResolver(Argument::type(OptionsResolver::class))->shouldBeCalled(); | ||
|
||
$this->plugins->has('my-plugin-1')->willReturn(true); | ||
$this->plugins->getPlugin('my-plugin-1')->willReturn($plugin->reveal()); | ||
$this->plugins->has('my-plugin-2')->willReturn(false); | ||
|
||
$commandTester = $this->getCommandTester( | ||
true, | ||
[ | ||
'test-1' => ['plugin' => 'my-plugin-1', 'parameter' => []], | ||
'test-2' => ['plugin' => 'my-plugin-2'], | ||
] | ||
); | ||
$commandTester->execute([]); | ||
|
||
|
||
$this->assertRegExp('/test-1[-\s]*OK/', $commandTester->getDisplay()); | ||
$this->assertRegExp('/test-2[-\s]*\[WARNING\] Plugin "my-plugin-2" not found/', $commandTester->getDisplay()); | ||
} | ||
|
||
public function testExecuteParameterNotValid() | ||
{ | ||
$plugin = $this->prophesize(PluginInterface::class); | ||
$plugin->configureOptionsResolver(Argument::type(OptionsResolver::class)) | ||
->will( | ||
function ($args) { | ||
$args[0]->setRequired(['chmod', 'directory']); | ||
} | ||
); | ||
|
||
$this->plugins->has('my-plugin')->willReturn(true); | ||
$this->plugins->getPlugin('my-plugin')->willReturn($plugin->reveal()); | ||
|
||
$commandTester = $this->getCommandTester( | ||
true, | ||
['test' => ['plugin' => 'my-plugin', 'parameter' => ['directory' => '/test']]] | ||
); | ||
$commandTester->execute([]); | ||
|
||
$this->assertContains('Parameter not valid', $commandTester->getDisplay()); | ||
} | ||
|
||
public function testExecuteParameterNotValidMultiple() | ||
{ | ||
$plugin = $this->prophesize(PluginInterface::class); | ||
$plugin->configureOptionsResolver(Argument::type(OptionsResolver::class)) | ||
->will( | ||
function ($args) { | ||
$args[0]->setRequired(['chmod', 'directory']); | ||
} | ||
); | ||
|
||
$this->plugins->has('my-plugin-1')->willReturn(true); | ||
$this->plugins->has('my-plugin-2')->willReturn(true); | ||
$this->plugins->getPlugin('my-plugin-1')->willReturn($plugin->reveal()); | ||
$this->plugins->getPlugin('my-plugin-2')->willReturn($plugin->reveal()); | ||
|
||
$commandTester = $this->getCommandTester( | ||
true, | ||
[ | ||
'test-1' => ['plugin' => 'my-plugin-1', 'parameter' => ['directory' => '/test']], | ||
'test-2' => ['plugin' => 'my-plugin-2', 'parameter' => ['directory' => '/test', 'chmod' => 0777]], | ||
] | ||
); | ||
$commandTester->execute([]); | ||
|
||
$this->assertRegExp('/test-1[-\s]*\[WARNING\] Parameter not valid/', $commandTester->getDisplay()); | ||
$this->assertRegExp('/test-2[-\s]*OK/', $commandTester->getDisplay()); | ||
} | ||
|
||
public function testExecuteOK() | ||
{ | ||
$plugin = $this->prophesize(PluginInterface::class); | ||
$plugin->configureOptionsResolver(Argument::type(OptionsResolver::class)) | ||
->will( | ||
function ($args) { | ||
$args[0]->setRequired(['chmod', 'directory']); | ||
} | ||
); | ||
|
||
$this->plugins->has('my-plugin')->willReturn(true); | ||
$this->plugins->getPlugin('my-plugin')->willReturn($plugin->reveal()); | ||
|
||
$commandTester = $this->getCommandTester( | ||
true, | ||
['test' => ['plugin' => 'my-plugin', 'parameter' => ['directory' => '/test', 'chmod' => 0777]]] | ||
); | ||
$commandTester->execute([]); | ||
|
||
$this->assertContains('OK', $commandTester->getDisplay()); | ||
} | ||
|
||
public function testExecuteOKMultiple() | ||
{ | ||
$plugin = $this->prophesize(PluginInterface::class); | ||
$plugin->configureOptionsResolver(Argument::type(OptionsResolver::class)) | ||
->will( | ||
function ($args) { | ||
$args[0]->setRequired(['chmod', 'directory']); | ||
} | ||
); | ||
|
||
$this->plugins->has('my-plugin')->willReturn(true); | ||
$this->plugins->getPlugin('my-plugin')->willReturn($plugin->reveal()); | ||
|
||
$commandTester = $this->getCommandTester( | ||
true, | ||
[ | ||
'test-1' => ['plugin' => 'my-plugin', 'parameter' => ['directory' => '/test', 'chmod' => 0777]], | ||
'test-2' => ['plugin' => 'my-plugin', 'parameter' => ['directory' => '/test', 'chmod' => 0777]], | ||
] | ||
); | ||
$commandTester->execute([]); | ||
|
||
$this->assertRegExp('/test-1[-\s]*OK/', $commandTester->getDisplay()); | ||
$this->assertRegExp('/test-2[-\s]*OK/', $commandTester->getDisplay()); | ||
} | ||
} |