Create factory for plugins that resources can be registered with.

This commit is contained in:
meadsteve 2013-11-17 17:23:35 +00:00
commit e1d8239e8a
5 changed files with 481 additions and 2 deletions

View file

@ -0,0 +1,57 @@
<?php
namespace PHPCI\Plugin\Tests\Util;
use PHPCI\Builder;
use PHPCI\Model\Build;
use PHPCI\Plugin;
class ExamplePluginWithNoConstructorArgs {
}
class ExamplePluginWithSingleOptionalArg {
function __construct($optional = null)
{
}
}
class ExamplePluginWithSingleRequiredArg {
public $RequiredArgument;
function __construct($requiredArgument)
{
$this->RequiredArgument = $requiredArgument;
}
}
class ExamplePluginWithSingleTypedRequiredArg {
public $RequiredArgument;
function __construct(\stdClass $requiredArgument)
{
$this->RequiredArgument = $requiredArgument;
}
}
class ExamplePluginFull implements Plugin {
public $Options;
public function __construct(
Builder $phpci,
Build $build,
array $options = array()
)
{
$this->Options = $options;
}
public function execute()
{
}
}

View file

@ -0,0 +1,174 @@
<?php
namespace PHPCI\Plugin\Tests\Util;
require_once __DIR__ . "/ExamplePlugins.php";
use PHPCI\Plugin\Util\Factory;
class FactoryTest extends \PHPUnit_Framework_TestCase {
/**
* @var \PHPCI\Plugin\Util\Factory
*/
protected $testedFactory;
protected $expectedResource;
protected $resourceLoader;
protected function setUp()
{
$this->testedFactory = new Factory();
// Setup a resource that can be returned and asserted against
$this->expectedResource = new \stdClass();
$resourceLink = $this->expectedResource;
$this->resourceLoader = function() use (&$resourceLink) {
return $resourceLink;
};
}
protected function tearDown()
{
// Nothing to do.
}
public function testRegisterResourceThrowsExceptionWithoutTypeAndName()
{
$this->setExpectedException("InvalidArgumentException");
$this->testedFactory->registerResource($this->resourceLoader, null, null);
}
public function testBuildPluginWorksWithConstructorlessPlugins()
{
$namespace = '\\PHPCI\\Plugin\\Tests\\Util\\';
$expectedPluginClass = $namespace .'ExamplePluginWithNoConstructorArgs';
$plugin = $this->testedFactory->buildPlugin($expectedPluginClass);
$this->assertInstanceOf($expectedPluginClass, $plugin);
}
public function testBuildPluginWorksWithSingleOptionalArgConstructor()
{
$namespace = '\\PHPCI\\Plugin\\Tests\\Util\\';
$expectedPluginClass = $namespace . 'ExamplePluginWithSingleOptionalArg';
$plugin = $this->testedFactory->buildPlugin($expectedPluginClass);
$this->assertInstanceOf($expectedPluginClass, $plugin);
}
public function testBuildPluginThrowsExceptionIfMissingResourcesForRequiredArg()
{
$this->setExpectedException(
'DomainException',
'Unsatisfied dependency: requiredArgument'
);
$namespace = '\\PHPCI\\Plugin\\Tests\\Util\\';
$expectedPluginClass = $namespace . 'ExamplePluginWithSingleRequiredArg';
$plugin = $this->testedFactory->buildPlugin($expectedPluginClass);
}
public function testBuildPluginLoadsArgumentsBasedOnName()
{
$namespace = '\\PHPCI\\Plugin\\Tests\\Util\\';
$expectedPluginClass = $namespace . 'ExamplePluginWithSingleRequiredArg';
$this->testedFactory->registerResource(
$this->resourceLoader,
"requiredArgument"
);
/** @var ExamplePluginWithSingleRequiredArg $plugin */
$plugin = $this->testedFactory->buildPlugin($expectedPluginClass);
$this->assertEquals($this->expectedResource, $plugin->RequiredArgument);
}
public function testBuildPluginLoadsArgumentsBasedOnType()
{
$namespace = '\\PHPCI\\Plugin\\Tests\\Util\\';
$expectedPluginClass = $namespace . 'ExamplePluginWithSingleTypedRequiredArg';
$this->testedFactory->registerResource(
$this->resourceLoader,
null,
"stdClass"
);
/** @var ExamplePluginWithSingleTypedRequiredArg $plugin */
$plugin = $this->testedFactory->buildPlugin($expectedPluginClass);
$this->assertEquals($this->expectedResource, $plugin->RequiredArgument);
}
public function testBuildPluginLoadsFullExample()
{
$namespace = '\\PHPCI\\Plugin\\Tests\\Util\\';
$expectedPluginClass = $namespace . 'ExamplePluginFull';
$this->registerBuildAndBuilder();
/** @var ExamplePluginFull $plugin */
$plugin = $this->testedFactory->buildPlugin($expectedPluginClass);
$this->assertInstanceOf($expectedPluginClass, $plugin);
}
public function testBuildPluginLoadsFullExampleWithOptions()
{
$namespace = '\\PHPCI\\Plugin\\Tests\\Util\\';
$expectedPluginClass = $namespace . 'ExamplePluginFull';
$expectedArgs = array(
'thing' => "stuff"
);
$this->registerBuildAndBuilder();
/** @var ExamplePluginFull $plugin */
$plugin = $this->testedFactory->buildPlugin(
$expectedPluginClass,
$expectedArgs
);
$this->assertInternalType('array', $plugin->Options);
$this->assertArrayHasKey('thing', $plugin->Options);
}
/**
* Registers mocked Builder and Build classes so that realistic plugins
* can be tested.
*/
private function registerBuildAndBuilder()
{
$this->testedFactory->registerResource(
function () {
return $this->getMock(
'PHPCI\Builder',
array(),
array(),
'',
false
);
},
null,
'PHPCI\\Builder'
);
$this->testedFactory->registerResource(
function () {
return $this->getMock(
'PHPCI\Model\Build',
array(),
array(),
'',
false
);
},
null,
'PHPCI\\Model\Build'
);
}
}