Skip to content

Commit f8c127e

Browse files
committed
Added testing, refactored a little bit the configuration building services
1 parent d44d773 commit f8c127e

File tree

6 files changed

+167
-73
lines changed

6 files changed

+167
-73
lines changed

src/Psy/Command/DemoCommand.php

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/Psy/Plugin/AbstractPlugin.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,6 @@ final public static function getName()
1919
return preg_replace('#Plugin$#', '', $class->getShortName());
2020
}
2121

22-
/**
23-
* @param array $configuration
24-
*
25-
* @return array
26-
*/
27-
final public static function getConfiguration($configuration = array())
28-
{
29-
return array_merge_recursive(
30-
$configuration,
31-
array(
32-
'commands' => static::getCommands(),
33-
'presenters' => static::getPresenters(),
34-
'matchers' => static::getMatchers(),
35-
// if any more parts of the config are exposed publicly, remember to add here with the static ref.
36-
)
37-
);
38-
}
39-
4022
// any publicly exposed configuration piece below here ↓
4123

4224
/**

src/Psy/Plugin/DemoPlugin.php

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/Psy/Plugin/Manager.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
class Manager
66
{
7+
protected static $exposedConfigurationItems = array(
8+
'commands', 'matchers', 'presenters',
9+
);
10+
711
/** @var AbstractPlugin[] */
812
protected static $plugins = array();
913

@@ -24,7 +28,24 @@ public static function register(AbstractPlugin $plugin, $name)
2428
public static function getConfiguration($configuration = array())
2529
{
2630
foreach (self::$plugins as $plugin) {
27-
$configuration = $plugin::getConfiguration($configuration);
31+
foreach (self::$exposedConfigurationItems as $cfgBlock) {
32+
$getter = sprintf('get%s', ucfirst($cfgBlock));
33+
$cfgData = call_user_func(array($plugin, $getter));
34+
if (array_key_exists($cfgBlock, $configuration)) {
35+
if (is_array($configuration[$cfgBlock])) {
36+
// is array, let's merge
37+
$configuration[$cfgBlock] = array_merge(
38+
$configuration[$cfgBlock],
39+
$cfgData
40+
);
41+
} else {
42+
// not an array, it will be overwritten
43+
$configuration[$cfgBlock] = $cfgData;
44+
}
45+
} else {
46+
$configuration[$cfgBlock] = $cfgData;
47+
}
48+
}
2849
}
2950

3051
return $configuration;
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
namespace Psy\Test\Plugin;
4+
5+
use Psy\Plugin\Manager;
6+
7+
class ManagerTest extends \PHPUnit_Framework_TestCase
8+
{
9+
public function setup()
10+
{
11+
$prop = new \ReflectionProperty('Psy\Plugin\Manager', 'plugins');
12+
$prop->setAccessible(true);
13+
$prop->setValue('Psy\Plugin\Manager', array());
14+
}
15+
16+
public function testRegisterMultiplePlugins()
17+
{
18+
$mockedPlugin = $this->getMock('Psy\Plugin\AbstractPlugin');
19+
Manager::register($mockedPlugin, 'mock1');
20+
Manager::register($mockedPlugin, 'mock2');
21+
22+
$prop = new \ReflectionProperty('Psy\Plugin\Manager', 'plugins');
23+
$prop->setAccessible(true);
24+
$plugins = $prop->getValue('Psy\Plugin\Manager');
25+
$this->assertArrayHasKey('mock1', $plugins);
26+
$this->assertArrayHasKey('mock2', $plugins);
27+
}
28+
29+
public function testConfigurationWithSinglePlugin()
30+
{
31+
$commands = array(
32+
'cmd1', 'cmd2',
33+
);
34+
35+
$presenters = array(
36+
'presenter1', 'presenter2',
37+
);
38+
39+
$matchers = array(
40+
'matcher1', 'matcher2',
41+
);
42+
43+
$stub = new PluginStub();
44+
$stub->setCommands($commands);
45+
$stub->setPresenters($presenters);
46+
$stub->setMatchers($matchers);
47+
48+
Manager::register($stub, 'mock');
49+
50+
$config = Manager::getConfiguration();
51+
$this->assertArraySubset($commands, $config['commands']);
52+
$this->assertArraySubset($presenters, $config['presenters']);
53+
$this->assertArraySubset($matchers, $config['matchers']);
54+
}
55+
56+
public function testConfigurationWithMultiplePlugins()
57+
{
58+
$commands1 = array(
59+
'cmd1', 'cmd2',
60+
);
61+
62+
$presenters1 = array(
63+
'presenter1', 'presenter2',
64+
);
65+
66+
$matchers1 = array(
67+
'matcher1', 'matcher2',
68+
);
69+
70+
$stub1 = new PluginStub();
71+
$stub1->setCommands($commands1);
72+
$stub1->setPresenters($presenters1);
73+
$stub1->setMatchers($matchers1);
74+
75+
Manager::register($stub1, 'mock1');
76+
77+
$commands2 = array(
78+
'cmd3', 'cmd4',
79+
);
80+
81+
$presenters2 = array(
82+
'presenter3', 'presenter4',
83+
);
84+
85+
$matchers2 = array(
86+
'matcher3', 'matcher4',
87+
);
88+
89+
$stub2 = new PluginStub();
90+
$stub2->setCommands($commands2);
91+
$stub2->setPresenters($presenters2);
92+
$stub2->setMatchers($matchers2);
93+
94+
Manager::register($stub2, 'mock2');
95+
96+
$config = Manager::getConfiguration();
97+
$this->assertArraySubset($commands1, $config['commands']);
98+
$this->assertArraySubset($presenters1, $config['presenters']);
99+
$this->assertArraySubset($matchers1, $config['matchers']);
100+
}
101+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Psy\Test\Plugin;
4+
5+
use Psy\Plugin\AbstractPlugin;
6+
7+
class PluginStub extends AbstractPlugin
8+
{
9+
// due to the static nature, and the negative of Mr bergmann to audit static code
10+
// the data here is treated as a FIFO pile.
11+
protected static $matchers = array();
12+
protected static $presenters = array();
13+
protected static $commands = array();
14+
15+
public function setMatchers(array $matchers)
16+
{
17+
self::$matchers[] = $matchers;
18+
}
19+
20+
public function setPresenters(array $presenters)
21+
{
22+
self::$presenters[] = $presenters;
23+
}
24+
25+
public function setCommands(array $commands)
26+
{
27+
self::$commands[] = $commands;
28+
}
29+
30+
public static function getMatchers()
31+
{
32+
return array_shift(self::$matchers);
33+
}
34+
35+
public static function getPresenters()
36+
{
37+
return array_shift(self::$presenters);
38+
}
39+
40+
public static function getCommands()
41+
{
42+
return array_shift(self::$commands);
43+
}
44+
}

0 commit comments

Comments
 (0)