force plugin factory to only build plugins matching interface.

This commit is contained in:
Steve B 2013-11-28 14:05:26 +00:00 committed by steve.brazier
parent ea2981b45e
commit 9891a4be62
4 changed files with 41 additions and 33 deletions

View file

@ -9,6 +9,8 @@ class Factory {
const TYPE_ARRAY = "array";
const TYPE_CALLABLE = "callable";
const INTERFACE_PHPCI_PLUGIN = '\PHPCI\Plugin';
private $currentPluginOptions;
/**
@ -45,6 +47,7 @@ class Factory {
*
* @param $className
* @param array $options
* @throws \InvalidArgumentException if $className doesn't represent a valid plugin
* @return \PHPCI\Plugin
*/
public function buildPlugin($className, array $options = array())
@ -53,6 +56,12 @@ class Factory {
$reflectedPlugin = new \ReflectionClass($className);
if (!$reflectedPlugin->implementsInterface(self::INTERFACE_PHPCI_PLUGIN)) {
throw new \InvalidArgumentException(
"Requested class must implement " . self:: INTERFACE_PHPCI_PLUGIN
);
}
$constructor = $reflectedPlugin->getConstructor();
if ($constructor) {

View file

@ -5,18 +5,28 @@ use PHPCI\Builder;
use PHPCI\Model\Build;
use PHPCI\Plugin;
class ExamplePluginWithNoConstructorArgs {
class ExamplePluginWithNoConstructorArgs implements Plugin
{
public function execute()
{
}
}
class ExamplePluginWithSingleOptionalArg {
class ExamplePluginWithSingleOptionalArg implements Plugin
{
function __construct($optional = null)
{
}
public function execute()
{
}
}
class ExamplePluginWithSingleRequiredArg {
class ExamplePluginWithSingleRequiredArg implements Plugin
{
public $RequiredArgument;
@ -24,9 +34,15 @@ class ExamplePluginWithSingleRequiredArg {
{
$this->RequiredArgument = $requiredArgument;
}
public function execute()
{
}
}
class ExamplePluginWithSingleTypedRequiredArg {
class ExamplePluginWithSingleTypedRequiredArg implements Plugin
{
public $RequiredArgument;
@ -34,6 +50,11 @@ class ExamplePluginWithSingleTypedRequiredArg {
{
$this->RequiredArgument = $requiredArgument;
}
public function execute()
{
}
}
class ExamplePluginFull implements Plugin {

View file

@ -49,6 +49,12 @@ class FactoryTest extends \PHPUnit_Framework_TestCase {
$this->assertInstanceOf($expectedPluginClass, $plugin);
}
public function testBuildPluginFailsForNonPluginClasses()
{
$this->setExpectedException('InvalidArgumentException', 'Requested class must implement \PHPCI\Plugin');
$plugin = $this->testedFactory->buildPlugin("stdClass");
}
public function testBuildPluginWorksWithSingleOptionalArgConstructor()
{
$namespace = '\\PHPCI\\Plugin\\Tests\\Util\\';

View file

@ -1,14 +1,5 @@
<?php
/**
* PHPCI - Continuous Integration for PHP
*
* @copyright Copyright 2013, Block 8 Limited.
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
* @link http://www.phptesting.org/
*/
// Let PHP take a guess as to the default timezone, if the user hasn't set one:
date_default_timezone_set(@date_default_timezone_get());
// Set up a basic autoloader for PHPCI:
$autoload = function ($class) {
@ -27,27 +18,8 @@ $autoload = function ($class) {
spl_autoload_register($autoload, true, true);
if (!file_exists(dirname(__FILE__) . '/PHPCI/config.yml') && (!defined('PHPCI_IS_CONSOLE') || !PHPCI_IS_CONSOLE)) {
header('Location: install.php');
die;
}
if (!file_exists(dirname(__FILE__) . '/vendor/autoload.php') && defined('PHPCI_IS_CONSOLE') && PHPCI_IS_CONSOLE) {
file_put_contents('php://stderr', 'Please install PHPCI with "composer install" before using console');
exit(1);
}
// Load Composer autoloader:
require_once(dirname(__FILE__) . '/vendor/autoload.php');
// Load configuration if present:
$conf = array();
$conf['b8']['app']['namespace'] = 'PHPCI';
$conf['b8']['app']['default_controller'] = 'Home';
$conf['b8']['view']['path'] = dirname(__FILE__) . '/PHPCI/View/';
$config = new b8\Config($conf);
$config->loadYaml(dirname(__FILE__) . '/PHPCI/config.yml');
require_once(dirname(__FILE__) . '/vars.php');