Skip to content

Commit

Permalink
Added testing, refactored a little bit the configuration building ser…
Browse files Browse the repository at this point in the history
…vices
  • Loading branch information
Markcial committed Mar 3, 2015
1 parent d44d773 commit f1d7610
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 110 deletions.
36 changes: 0 additions & 36 deletions src/Psy/Command/DemoCommand.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Psy/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Psy\ExecutionLoop\Loop;
use Psy\Output\OutputPager;
use Psy\Output\ShellOutput;
use Psy\Plugin\Manager;
use Psy\Plugin\PluginManager;
use Psy\Presenter\PresenterManager;
use Psy\Readline\GNUReadline;
use Psy\Readline\Libedit;
Expand Down Expand Up @@ -101,7 +101,7 @@ public function __construct(array $config = array())

// ask the plugin manager for configurations
if ($this->getRegisterPlugins()) {
$this->loadConfig(Manager::getConfiguration($config));
$this->loadConfig(PluginManager::getConfiguration($config));
}
}

Expand Down
26 changes: 4 additions & 22 deletions src/Psy/Plugin/AbstractPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,17 @@ abstract class AbstractPlugin
{
public static function register()
{
Manager::register(new static(), static::getName());
PluginManager::register(new static(), static::getName());
}

/**
* @return string
*/
final public static function getName()
{
$class = new \ReflectionClass(get_called_class());

return preg_replace('#Plugin$#', '', $class->getShortName());
}

/**
* @param array $configuration
*
* @return array
* @throws \Exception
*/
final public static function getConfiguration($configuration = array())
public static function getName()
{
return array_merge_recursive(
$configuration,
array(
'commands' => static::getCommands(),
'presenters' => static::getPresenters(),
'matchers' => static::getMatchers(),
// if any more parts of the config are exposed publicly, remember to add here with the static ref.
)
);
throw new \Exception('Missing plugin name');
}

// any publicly exposed configuration piece below here ↓
Expand Down
18 changes: 0 additions & 18 deletions src/Psy/Plugin/DemoPlugin.php

This file was deleted.

32 changes: 0 additions & 32 deletions src/Psy/Plugin/Manager.php

This file was deleted.

63 changes: 63 additions & 0 deletions src/Psy/Plugin/PluginManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Psy\Plugin;

class PluginManager
{
/**
* @var array
*/
protected static $exposedConfigurationItems = array(
'commands', 'matchers', 'presenters',
);

/** @var AbstractPlugin[] */
protected static $plugins = array();

/**
* @param AbstractPlugin $plugin
* @param $name
*
* @throws \Exception
*/
public static function register(AbstractPlugin $plugin, $name)
{
if (array_key_exists($name, self::$plugins)) {
throw new \Exception(
sprintf('The plugin "%s" was already registered.', $name)
);
}
self::$plugins[$name] = $plugin;
}

/**
* @param array $configuration
*
* @return array
*/
public static function getConfiguration($configuration = array())
{
foreach (self::$plugins as $plugin) {
foreach (self::$exposedConfigurationItems as $cfgBlock) {
$getter = sprintf('get%s', ucfirst($cfgBlock));
$cfgData = call_user_func(array($plugin, $getter));
if (array_key_exists($cfgBlock, $configuration)) {
if (is_array($configuration[$cfgBlock])) {
// is array, let's merge
$configuration[$cfgBlock] = array_merge(
$configuration[$cfgBlock],
$cfgData
);
} else {
// not an array, it will be overwritten
$configuration[$cfgBlock] = $cfgData;
}
} else {
$configuration[$cfgBlock] = $cfgData;
}
}
}

return $configuration;
}
}
101 changes: 101 additions & 0 deletions test/Psy/Test/Plugin/ManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace Psy\Test\Plugin;

use Psy\Plugin\PluginManager;

class ManagerTest extends \PHPUnit_Framework_TestCase
{
public function setup()
{
$prop = new \ReflectionProperty('Psy\Plugin\PluginManager', 'plugins');
$prop->setAccessible(true);
$prop->setValue('Psy\Plugin\Manager', array());
}

public function testRegisterMultiplePlugins()
{
$mockedPlugin = $this->getMock('Psy\Plugin\AbstractPlugin');
PluginManager::register($mockedPlugin, 'mock1');
PluginManager::register($mockedPlugin, 'mock2');

$prop = new \ReflectionProperty('Psy\Plugin\PluginManager', 'plugins');
$prop->setAccessible(true);
$plugins = $prop->getValue('Psy\Plugin\PluginManager');
$this->assertArrayHasKey('mock1', $plugins);
$this->assertArrayHasKey('mock2', $plugins);
}

public function testConfigurationWithSinglePlugin()
{
$commands = array(
'cmd1', 'cmd2',
);

$presenters = array(
'presenter1', 'presenter2',
);

$matchers = array(
'matcher1', 'matcher2',
);

$stub = new PluginStub();
$stub->setCommands($commands);
$stub->setPresenters($presenters);
$stub->setMatchers($matchers);

PluginManager::register($stub, 'mock');

$config = PluginManager::getConfiguration();
$this->assertArraySubset($commands, $config['commands']);
$this->assertArraySubset($presenters, $config['presenters']);
$this->assertArraySubset($matchers, $config['matchers']);
}

public function testConfigurationWithMultiplePlugins()
{
$commands1 = array(
'cmd1', 'cmd2',
);

$presenters1 = array(
'presenter1', 'presenter2',
);

$matchers1 = array(
'matcher1', 'matcher2',
);

$stub1 = new PluginStub();
$stub1->setCommands($commands1);
$stub1->setPresenters($presenters1);
$stub1->setMatchers($matchers1);

PluginManager::register($stub1, 'mock1');

$commands2 = array(
'cmd3', 'cmd4',
);

$presenters2 = array(
'presenter3', 'presenter4',
);

$matchers2 = array(
'matcher3', 'matcher4',
);

$stub2 = new PluginStub();
$stub2->setCommands($commands2);
$stub2->setPresenters($presenters2);
$stub2->setMatchers($matchers2);

PluginManager::register($stub2, 'mock2');

$config = PluginManager::getConfiguration();
$this->assertArraySubset($commands1, $config['commands']);
$this->assertArraySubset($presenters1, $config['presenters']);
$this->assertArraySubset($matchers1, $config['matchers']);
}
}
44 changes: 44 additions & 0 deletions test/Psy/Test/Plugin/PluginStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Psy\Test\Plugin;

use Psy\Plugin\AbstractPlugin;

class PluginStub extends AbstractPlugin
{
// due to the static nature, and the negative of Mr bergmann to audit static code
// the data here is treated as a FIFO pile.
protected static $matchers = array();
protected static $presenters = array();
protected static $commands = array();

public function setMatchers(array $matchers)
{
self::$matchers[] = $matchers;
}

public function setPresenters(array $presenters)
{
self::$presenters[] = $presenters;
}

public function setCommands(array $commands)
{
self::$commands[] = $commands;
}

public static function getMatchers()
{
return array_shift(self::$matchers);
}

public static function getPresenters()
{
return array_shift(self::$presenters);
}

public static function getCommands()
{
return array_shift(self::$commands);
}
}

0 comments on commit f1d7610

Please sign in to comment.