From 4d1130f53e100bf39efbd4d86652fd3ecafd7e11 Mon Sep 17 00:00:00 2001 From: Marc Garcia Date: Tue, 3 Mar 2015 16:38:22 +0100 Subject: [PATCH] Added testing, refactored a little bit the configuration building services --- src/Psy/Command/DemoCommand.php | 36 ---------- src/Psy/Plugin/AbstractPlugin.php | 26 ++----- src/Psy/Plugin/DemoPlugin.php | 18 ----- src/Psy/Plugin/Manager.php | 32 --------- src/Psy/Plugin/PluginManager.php | 63 +++++++++++++++++ test/Psy/Test/Plugin/ManagerTest.php | 101 +++++++++++++++++++++++++++ test/Psy/Test/Plugin/PluginStub.php | 44 ++++++++++++ 7 files changed, 212 insertions(+), 108 deletions(-) delete mode 100644 src/Psy/Command/DemoCommand.php delete mode 100644 src/Psy/Plugin/DemoPlugin.php delete mode 100644 src/Psy/Plugin/Manager.php create mode 100644 src/Psy/Plugin/PluginManager.php create mode 100644 test/Psy/Test/Plugin/ManagerTest.php create mode 100644 test/Psy/Test/Plugin/PluginStub.php diff --git a/src/Psy/Command/DemoCommand.php b/src/Psy/Command/DemoCommand.php deleted file mode 100644 index c5ceb0cfb..000000000 --- a/src/Psy/Command/DemoCommand.php +++ /dev/null @@ -1,36 +0,0 @@ -setName('demo') - ->setDefinition(array( - new InputOption('message', 'm', InputOption::VALUE_REQUIRED, 'Message to send.'), - )) - ->setDescription('Sample command just for testing.') - ->setHelp( - <<getOption('message'); - $output->writeln(sprintf('Received message "%s". ', $message)); - } -} diff --git a/src/Psy/Plugin/AbstractPlugin.php b/src/Psy/Plugin/AbstractPlugin.php index c3b7fba88..93a8e5c02 100644 --- a/src/Psy/Plugin/AbstractPlugin.php +++ b/src/Psy/Plugin/AbstractPlugin.php @@ -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 ↓ diff --git a/src/Psy/Plugin/DemoPlugin.php b/src/Psy/Plugin/DemoPlugin.php deleted file mode 100644 index d8451685b..000000000 --- a/src/Psy/Plugin/DemoPlugin.php +++ /dev/null @@ -1,18 +0,0 @@ -setAccessible(true); + $prop->setValue('Psy\Plugin\Manager', array()); + } + + public function testRegisterMultiplePlugins() + { + $mockedPlugin = $this->getMock('Psy\Plugin\AbstractPlugin'); + Manager::register($mockedPlugin, 'mock1'); + Manager::register($mockedPlugin, 'mock2'); + + $prop = new \ReflectionProperty('Psy\Plugin\Manager', 'plugins'); + $prop->setAccessible(true); + $plugins = $prop->getValue('Psy\Plugin\Manager'); + $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); + + Manager::register($stub, 'mock'); + + $config = Manager::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); + + Manager::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); + + Manager::register($stub2, 'mock2'); + + $config = Manager::getConfiguration(); + $this->assertArraySubset($commands1, $config['commands']); + $this->assertArraySubset($presenters1, $config['presenters']); + $this->assertArraySubset($matchers1, $config['matchers']); + } +} diff --git a/test/Psy/Test/Plugin/PluginStub.php b/test/Psy/Test/Plugin/PluginStub.php new file mode 100644 index 000000000..3dc5436ec --- /dev/null +++ b/test/Psy/Test/Plugin/PluginStub.php @@ -0,0 +1,44 @@ +