diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 21d57da..0000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "tools/phptools"] - path = tools/phptools - url = git://github.com/ralphschindler/PHPTools.git diff --git a/composer.json b/composer.json index 1d519ca..0545920 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "zendframework/zend-loader", - "description": "Zend\\Loader component", + "description": " ", "license": "BSD-3-Clause", "keywords": [ "zf2", @@ -9,11 +9,11 @@ "homepage": "https://github.com/zendframework/zend-loader", "autoload": { "psr-4": { - "Zend\\Loader\\": "src/" + "Zend\\Loader": "src/" } }, "require": { - "php": ">=5.3.23" + "php": ">=5.3.3" }, "extra": { "branch-alias": { @@ -21,14 +21,17 @@ "dev-develop": "2.5-dev" } }, - "require-dev": { - "fabpot/php-cs-fixer": "1.7.*", - "satooshi/php-coveralls": "dev-master", - "phpunit/PHPUnit": "~4.0" + "suggest": { + "zendframework/zend-stdlib": "Zend\\Stdlib component" }, "autoload-dev": { "psr-4": { "ZendTest\\Loader\\": "test/" } + }, + "require-dev": { + "fabpot/php-cs-fixer": "1.7.*", + "satooshi/php-coveralls": "dev-master", + "phpunit/PHPUnit": "~4.0" } } \ No newline at end of file diff --git a/src/AutoloaderFactory.php b/src/AutoloaderFactory.php index ab32bce..5f44b82 100644 --- a/src/AutoloaderFactory.php +++ b/src/AutoloaderFactory.php @@ -1,24 +1,13 @@ setOptions($options); - } - } - - /** - * Configure plugin broker - * - * @param array|\Traversable $options - * @return PluginBroker - */ - public function setOptions($options) - { - if (!is_array($options) && !$options instanceof \Traversable) { - throw new Exception\InvalidArgumentException(sprintf( - 'Expected an array or Traversable; received "%s"', - (is_object($options) ? get_class($options) : gettype($options)) - )); - } - - // Cache plugins until after a validator has been registered - $plugins = array(); - - foreach ($options as $key => $value) { - switch (strtolower($key)) { - case 'class_loader': - if (is_string($value)) { - if (!class_exists($value)) { - throw new Exception\RuntimeException(sprintf( - 'Unknown class "%s" provided as class loader option', - $value - )); - } - $value = new $value; - } - if ($value instanceof ShortNameLocator) { - $this->setClassLoader($value); - break; - } - - if (!is_array($value) && !$value instanceof \Traversable) { - throw new Exception\RuntimeException(sprintf( - 'Option passed for class loader (%s) is of an unknown type', - (is_object($value) ? get_class($value) : gettype($value)) - )); - } - - $class = false; - $options = null; - foreach ($value as $k => $v) { - switch (strtolower($k)) { - case 'class': - $class = $v; - break; - case 'options': - $options = $v; - break; - default: - break; - } - } - if ($class) { - $loader = new $class($options); - $this->setClassLoader($loader); - } - break; - case 'plugins': - if (!is_array($value) && !$value instanceof \Traversable) { - throw new Exception\RuntimeException(sprintf( - 'Plugins option must be an array or Traversable; received "%s"', - (is_object($value) ? get_class($value) : gettype($value)) - )); - } - - // Aggregate plugins; register only after a validator has - // been registered - $plugins = $value; - break; - case 'register_plugins_on_load': - $this->setRegisterPluginsOnLoad($value); - break; - case 'validator': - $this->setValidator($value); - break; - default: - // ignore unknown options - break; - } - } - - // Register any plugins discovered - foreach ($plugins as $name => $plugin) { - $this->register($name, $plugin); - } - - return $this; - } - - /** - * Load and return a plugin instance - * - * @param string $plugin - * @param array $options Options to pass to the plugin constructor - * @return object - * @throws Exception if plugin not found - */ - public function load($plugin, array $options = null) - { - $pluginName = strtolower($plugin); - if (isset($this->plugins[$pluginName])) { - return $this->plugins[$pluginName]; - } - - $locator = $this->getServiceLocator(); - // Pulling by alias - if ($locator) { - try { - $instance = $locator->get($plugin); - } catch (ServiceManagerException $e) { - // not returning an instance is okay; there are other ways to - // retrieve the plugin later - $instance = false; - } - - if ($instance) { - if ($this->getRegisterPluginsOnLoad()) { - $this->register($pluginName, $instance); - } - return $instance; - } - } - - $class = $this->getClassLoader()->load($plugin); - if (empty($class) && !class_exists($plugin)) { - throw new Exception\RuntimeException('Unable to locate class associated with "' . $pluginName . '"'); - } - - if (empty($class) && class_exists($plugin)) { - $class = $plugin; - } - - // Pulling by resolved class name - if ($locator) { - try { - $instance = $locator->get($class); - } catch (ServiceManagerException $e) { - // not returning an instance is okay; there are other ways to - // retrieve the plugin later - $instance = false; - } - - if ($instance) { - if ($this->getRegisterPluginsOnLoad()) { - $this->register($pluginName, $instance); - } - return $instance; - } - } - - if (!class_exists($class)) { - throw new Exception\RuntimeException('Unable to locate class associated with "' . $pluginName . '"'); - } - - // Did not find in the locator, so instantiate directly - if (empty($options)) { - $instance = new $class(); - } elseif ($this->isAssocArray($options)) { - $instance = new $class($options); - } else { - $r = new ReflectionClass($class); - $instance = $r->newInstanceArgs($options); - } - - if ($this->getRegisterPluginsOnLoad()) { - $this->register($pluginName, $instance); - } - - return $instance; - } - - /** - * Get list of all loaded plugins - * - * @return array - */ - public function getPlugins() - { - return $this->plugins; - } - - /** - * Whether or not a given plugin has been loaded - * - * @param string $name - * @return bool - */ - public function isLoaded($name) - { - return isset($this->plugins[$name]); - } - - /** - * Register a plugin object by name - * - * @param string $name - * @param mixed $plugin - * @return PluginBroker - */ - public function register($name, $plugin) - { - if (!$this->validatePlugin($plugin)) { - throw new Exception\RuntimeException(); - } - - $name = strtolower($name); - - $this->plugins[$name] = $plugin; - return $this; - } - - /** - * Unregister a named plugin - * - * Removes the plugin instance from the registry, if found. - * - * @param string $name - * @return bool - */ - public function unregister($name) - { - $name = strtolower($name); - if (isset($this->plugins[$name])) { - unset($this->plugins[$name]); - return true; - } - return false; - } - - /** - * Set class loader to use when resolving plugin names to class names - * - * @param ShortNameLocator $loader - * @return PluginBroker - */ - public function setClassLoader(ShortNameLocator $loader) - { - $this->classLoader = $loader; - return $this; - } - - /** - * Retrieve the class loader - * - * Lazy-loads an instance of PluginClassLocator if no loader is registered. - * - * @return ShortNameLocator - */ - public function getClassLoader() - { - if (null === $this->classLoader) { - $loaderClass = $this->defaultClassLoader; - $this->setClassLoader(new $loaderClass()); - } - return $this->classLoader; - } - - /** - * Set if plugins should be registered on load. - * - * @param boolean $flag - * @return PluginBroker - */ - public function setRegisterPluginsOnLoad($flag) - { - $this->registerPluginsOnLoad = (bool) $flag; - return $this; - } - - /** - * Retrieve if plugins are registered on load. - * - * @return boolean - */ - public function getRegisterPluginsOnLoad() - { - return $this->registerPluginsOnLoad; - } - - /** - * Set plugin validator callback - * - * @param callback $callback - * @return PluginBroker - */ - public function setValidator($callback) - { - if (!is_callable($callback)) { - throw new Exception\InvalidArgumentException(sprintf('Validator must be a valid PHP callback; %s provided', gettype($callback))); - } - $this->validator = $callback; - return $this; - } - - /** - * Retrieve plugin validator callback - * - * @return null|callback - */ - public function getValidator() - { - return $this->validator; - } - - /** - * Determine whether we have a valid plugin - * - * Override this method to implement custom validation logic. Typically, - * throw a custom exception for invalid plugins. - * - * @param mixed $plugin - * @return bool - */ - protected function validatePlugin($plugin) - { - if (null !== ($validator = $this->getValidator())) { - return call_user_func($validator, $plugin); - } - return true; - } - - /** - * Is a value an associative array? - * - * @param mixed $value - * @return bool - */ - protected function isAssocArray($value) - { - if (!is_array($value)) { - return false; - } - if (array_keys($value) === range(0, count($value) - 1)) { - return false; - } - return true; - } - - /** - * Get locator. - * - * @return ServiceLocatorInterface - */ - public function getServiceLocator() - { - return $this->locator; - } - - /** - * Set locator. - * - * @param ServiceLocatorInterface $locator - */ - public function setServiceLocator(ServiceLocatorInterface $locator) - { - $this->locator = $locator; - return $this; - } -} diff --git a/src/PluginClassLoader.php b/src/PluginClassLoader.php index c479c04..1090d89 100644 --- a/src/PluginClassLoader.php +++ b/src/PluginClassLoader.php @@ -1,22 +1,11 @@ $value) { - switch(strtolower($key)) { - case 'specs': - if (!is_array($value) && !$value instanceof \Traversable) { - throw new Exception\RuntimeException(sprintf( - 'Expected array or Traversable for specs option; received "%s"', - (is_object($value) ? get_class($value) : gettype($value)) - )); - } - foreach ($value as $name => $pluginOptions) { - $this->registerSpec($name, $pluginOptions); - } - break; - default: - // ignore unknown options - break; - } - } - - return $this; - } - - /** - * Register a plugin specification - * - * Registers a plugin "specification". Implementations should allow - * aggregating such specifications in order to retrieve "registered" - * plugins later. The specification will be the argument passed to - * load() when the plugin is requested later. - * - * @param string $name - * @param array $spec - * @return PluginSpecBroker - */ - public function registerSpec($name, array $spec = null) - { - $name = strtolower($name); - $this->specs[$name] = $spec; - return $this; - } - - /** - * Register a plugin - * - * Overrides parent method to allow passing array or null value for - * $plugin; if so, it proxies to {@link registerSpec()}. - * - * @param string $name - * @param mixed $plugin - * @return PluginSpecBroker - */ - public function register($name, $plugin) - { - if (null === $plugin || is_array($plugin)) { - $this->registerSpec($name, $plugin); - return $this; - } - return parent::register($name, $plugin); - } - - /** - * Register many plugin specifications at once - * - * Implementations should allow both array and Traversable arguments, and - * loop through the argument assuming key/value pairs of name/specs. - * - * @param array|Traversable $specs - * @return PluginSpecBroker - */ - public function registerSpecs($specs) - { - if (!is_array($specs) && !$specs instanceof \Traversable) { - throw new Exception\InvalidArgumentException(sprintf( - 'Expected array or Traversable object; received "%s"', - (is_object($specs) ? get_class($specs) : gettype($specs)) - )); - } - foreach ($specs as $name => $spec) { - $this->registerSpec($name, $spec); - } - return $this; - } - - /** - * Unregister a plugin specification - * - * @param string $name - * @return void - */ - public function unregisterSpec($name) - { - $name = strtolower($name); - if (array_key_exists($name, $this->specs)) { - unset($this->specs[$name]); - } - } - - /** - * Load and return a plugin instance - * - * If the plugin was previously loaded, returns that instance. - * - * If no options were passed, and we have no specification, load normally. - * - * If no options were passed, and we have a specification, use the - * specification to load an instance. - * - * Otherwise, simply try and load the plugin. - * - * @param string $plugin - * @param array|null $options - * @return object - * @throws Exception if plugin not found - */ - public function load($plugin, array $options = null) - { - $pluginName = strtolower($plugin); - if (isset($this->plugins[$pluginName])) { - // If we've loaded it already, just return it - return $this->plugins[$pluginName]; - } - - if ((null !== $options) || !isset($this->specs[$pluginName])) { - // If we got options, or if there are no specs for this plugin, - // simply proxy to the parent method - return parent::load($plugin, $options); - } - - if (isset($this->specs[$pluginName])) { - // If we have a spec, pass the spec to the parent method - return parent::load($plugin, $this->specs[$pluginName]); - } - - // Return control to the parent method with original arguments - return parent::load($plugin, $options); - } - - /** - * Retrieve a list of plugins and/or specs registered - * - * Differs from getPlugins() in that this will return true for both a - * plugin that has been loaded, as well as a plugin for which only a spec - * is available. - * - * @return array - */ - public function getRegisteredPlugins() - { - $specNames = array_keys($this->specs); - $pluginNames = array_keys($this->plugins); - return array_merge($specNames, $pluginNames); - } - - /** - * Whether or not a plugin exists - * - * Should be used to indicate either whether a given plugin has been - * previously loaded, or whether a specification has been registered. - * As such, it differs from isLoaded(), which should report only if the - * plugin has already been loaded. - * - * @param string $name - * @return bool - */ - public function hasPlugin($name) - { - $name = strtolower($name); - return (isset($this->plugins[$name]) || array_key_exists($name, $this->specs)); - } -} diff --git a/src/PrefixPathLoader.php b/src/PrefixPathLoader.php index f6fd662..5b1433f 100644 --- a/src/PrefixPathLoader.php +++ b/src/PrefixPathLoader.php @@ -1,37 +1,25 @@ broker = new PluginBroker(); - } - - public function testUsesEmptyPluginClassLoaderByDefault() - { - $loader = $this->broker->getClassLoader(); - $this->assertInstanceOf('Zend\Loader\ShortNameLocator', $loader); - $this->assertInstanceOf('Zend\Loader\PluginClassLoader', $loader); - $this->assertEquals(array(), $loader->getRegisteredPlugins()); - } - - public function testCanSpecifyPluginClassLoader() - { - $loader = new PluginClassLoader(); - $this->broker->setClassLoader($loader); - $this->assertSame($loader, $this->broker->getClassLoader()); - } - - public function testLoadThrowsExceptionIfPluginNotFound() - { - $this->setExpectedException('Zend\Loader\Exception\RuntimeException'); - $this->broker->load('_foo_'); - } - - public function testLoadInstantiatesAndReturnsPluginWhenFound() - { - $loader = $this->broker->getClassLoader(); - $loader->registerPlugin('sample', 'ZendTest\Loader\TestAsset\SamplePlugin'); - $plugin = $this->broker->load('sample'); - $this->assertInstanceOf('ZendTest\Loader\TestAsset\SamplePlugin', $plugin); - } - - public function testLoadInstantiatesPluginWithPassedOptionsWhenFound() - { - $loader = $this->broker->getClassLoader(); - $loader->registerPlugin('sample', 'ZendTest\Loader\TestAsset\SamplePlugin'); - $plugin = $this->broker->load('sample', array('foo')); - $this->assertInstanceOf('ZendTest\Loader\TestAsset\SamplePlugin', $plugin); - $this->assertEquals('foo', $plugin->options); - } - - public function testCanRegisterPluginInstanceByNameExplicitly() - { - $sample = new TestAsset\SamplePlugin; - $this->broker->register('sample', $sample); - $test = $this->broker->load('sample'); - $this->assertSame($sample, $test); - } - - public function testLoadingSamePluginMultipleTimesReturnsSameInstance() - { - $sample = new TestAsset\SamplePlugin; - $this->broker->register('sample', $sample); - $test1 = $this->broker->load('sample'); - $test2 = $this->broker->load('sample'); - $this->assertSame($test1, $test2); - } - - public function testUnregisteringPluginInstanceForcesNewInstanceOnNextLoad() - { - $loader = $this->broker->getClassLoader(); - $loader->registerPlugin('sample', 'ZendTest\Loader\TestAsset\SamplePlugin'); - $plugin1 = $this->broker->load('sample'); - $this->broker->unregister('sample'); - $plugin2 = $this->broker->load('sample'); - $this->assertNotSame($plugin1, $plugin2); - } - - public function testSettingValidatorRaisesExceptionForInvalidCallback() - { - $this->setExpectedException('Zend\Loader\Exception\InvalidArgumentException'); - $this->broker->setValidator('__bogus__'); - } - - public function testAllowsRegisteringPluginValidatorCallback() - { - $this->broker->setValidator('array_key_exists'); - $this->assertEquals('array_key_exists', $this->broker->getValidator()); - } - - public function testRegisteringPluginThatFailsValidatorRaisesException() - { - $this->broker->setValidator(function($plugin) { - return ($plugin instanceof PluginBrokerTest); - }); - $this->setExpectedException('Zend\Loader\Exception\RuntimeException'); - $this->broker->register('sample', new TestAsset\SamplePlugin()); - } - - public function testLoadingPluginThatFailsValidatorRaisesException() - { - $loader = $this->broker->getClassLoader(); - $loader->registerPlugin('sample', 'ZendTest\Loader\TestAsset\SamplePlugin'); - $this->broker->setValidator(function($plugin) { - return ($plugin instanceof PluginBrokerTest); - }); - $this->setExpectedException('Zend\Loader\Exception\RuntimeException'); - $this->broker->load('sample'); - } - - public function testRegisteringPluginSucceedsWhenPassesValidatorCriteria() - { - $this->broker->setValidator(function($plugin) { - return ($plugin instanceof TestAsset\SamplePlugin); - }); - $test = $this->broker->register('sample', new TestAsset\SamplePlugin()); - $this->assertInstanceOf('Zend\Loader\PluginBroker', $test); - } - - public function testLoadingPluginSucceedsWhenPassesValidatorCriteria() - { - $loader = $this->broker->getClassLoader(); - $loader->registerPlugin('sample', 'ZendTest\Loader\TestAsset\SamplePlugin'); - $this->broker->setValidator(function($plugin) { - return ($plugin instanceof TestAsset\SamplePlugin); - }); - $test = $this->broker->load('sample'); - $this->assertInstanceOf('ZendTest\Loader\TestAsset\SamplePlugin', $test); - } - - public function testPluginNamesAreCaseInsensitive() - { - $loader = $this->broker->getClassLoader(); - $loader->registerPlugin('sample', 'ZendTest\Loader\TestAsset\SamplePlugin'); - $plugin1 = $this->broker->load('sample'); - $plugin2 = $this->broker->load('Sample'); - $this->assertSame($plugin1, $plugin2); - } - - public function testExplicitlyRegisteredPluginNamesAreCaseInsensitive() - { - $test = $this->broker->register('sample', new TestAsset\SamplePlugin()); - $plugin1 = $this->broker->load('sample'); - $plugin2 = $this->broker->load('Sample'); - $this->assertSame($plugin1, $plugin2); - } - - public function testCanRetrieveAllLoadedPlugins() - { - $this->broker->register('sample', new TestAsset\SamplePlugin()); - $this->broker->load('sample'); - $plugins = $this->broker->getPlugins(); - $this->assertInternalType('array', $plugins); - $this->assertEquals(1, count($plugins)); - } - - public function testIsLoadedReturnsFalseForUnknownPluginNames() - { - $this->assertFalse($this->broker->isLoaded('__foo__')); - } - - public function testIsLoadedReturnsFalseForUnloadedPlugin() - { - $this->broker->getClassLoader()->registerPlugin('sample', 'ZendTest\Loader\TestAsset\SamplePlugin'); - $this->assertFalse($this->broker->isLoaded('sample')); - } - - public function testIsLoadedReturnsTrueForLoadedPlugin() - { - $this->broker->getClassLoader()->registerPlugin('sample', 'ZendTest\Loader\TestAsset\SamplePlugin'); - $this->broker->load('sample'); - $this->assertTrue($this->broker->isLoaded('sample')); - } - - public function testIsLoadedReturnsTrueForRegisteredPlugin() - { - $this->broker->register('sample', new TestAsset\SamplePlugin()); - $this->assertTrue($this->broker->isLoaded('sample')); - } - - public function testRegisterPluginsOnLoadDisabled() - { - $this->broker->setRegisterPluginsOnLoad(false); - - $loader = $this->broker->getClassLoader(); - $loader->registerPlugin('sample', 'ZendTest\Loader\TestAsset\SamplePlugin'); - - $plugin1 = $this->broker->load('sample'); - $plugin2 = $this->broker->load('sample'); - - $this->assertNotSame($plugin1, $plugin2); - } - - /** - * Unsure if this is functionality we want to support; requires some form - * of efficient options hashing, which may negatively impact performance. - * - * @group disable - */ - public function testLoadTakesIntoAccountPassedOptions() - { - $loader = $this->broker->getClassLoader(); - $loader->registerPlugin('sample', 'ZendTest\Loader\TestAsset\SamplePlugin'); - - $sample1 = $this->broker->load('sample'); - $sample2 = $this->broker->load('sample', 'foo'); - $sample3 = $this->broker->load('sample', array('foo' => 'bar')); - - $this->assertNotSame($sample1, $sample2); - $this->assertNotSame($sample1, $sample3); - $this->assertNotSame($sample2, $sample3); - - $this->assertNull($sample1->options); - $this->assertEquals('foo', $sample2->options); - $this->assertEquals(array('foo' => 'bar'), $sample3->options); - } - - public function testAllowsConfigurationViaConstructor() - { - $validator = function($plugin) { - return true; - }; - $broker = new PluginBroker(array( - 'class_loader' => array( - 'class' => 'Zend\Loader\PrefixPathLoader', - 'options' => array( - 'ZendTest\UnusualNamespace' => __DIR__ . '/TestAsset', - ) - ), - 'plugins' => array( - 'test' => $this, - ), - 'validator' => $validator, - )); - - $loader = $broker->getClassLoader(); - $this->assertInstanceOf('Zend\Loader\PrefixPathLoader', $loader); - $this->assertEquals('ZendTest\UnusualNamespace\ClassMappedClass', $loader->load('ClassMappedClass')); - - $this->assertTrue($broker->isLoaded('test')); - $this->assertSame($validator, $broker->getValidator()); - - $broker = new PluginBroker(array( - 'class_loader' => 'ZendTest\Loader\TestAsset\CustomClassLoader', - )); - $loader = $broker->getClassLoader(); - $this->assertInstanceOf('ZendTest\Loader\TestAsset\CustomClassLoader', $loader); - } - - public function testWillPullFromLocatorIfAttached() - { - $plugin = new stdClass; - - $locator = new TestAsset\ServiceLocator(); - $locator->set('ZendTest\Loader\TestAsset\Foo', $plugin); - $this->broker->setServiceLocator($locator); - - $loader = $this->broker->getClassLoader(); - $loader->registerPlugin('foo', 'ZendTest\Loader\TestAsset\Foo'); - - $test = $this->broker->load('foo'); - $this->assertSame($plugin, $test); - } -} diff --git a/test/PluginClassLoaderTest.php b/test/PluginClassLoaderTest.php index 45e0053..2fdcdea 100644 --- a/test/PluginClassLoaderTest.php +++ b/test/PluginClassLoaderTest.php @@ -1,22 +1,11 @@ broker = new PluginSpecBroker(); - } - - public function testRegisteringSpecsReflectedWhenRetrievingRegisteredPlugins() - { - $this->broker->registerSpec('test', array('foo', 'bar')); - $plugins = $this->broker->getRegisteredPlugins(); - $this->assertContains('test', $plugins); - } - - public function testRegisteredPluginsIncludeBothLoadedPluginsAndSpecsForUnloadedPlugins() - { - $this->broker->getClassLoader()->registerPlugin('sample', 'ZendTest\Loader\TestAsset\SamplePlugin'); - $this->broker->registerSpec('test', array('foo', 'bar')); - $this->broker->load('sample'); - $plugins = $this->broker->getRegisteredPlugins(); - $this->assertContains('sample', $plugins); - $this->assertContains('test', $plugins); - } - - public function testHasPluginReturnsFalseForUnloadedPlugin() - { - $this->broker->getClassLoader()->registerPlugin('sample', 'ZendTest\Loader\TestAsset\SamplePlugin'); - $this->assertFalse($this->broker->hasPlugin('sample')); - } - - public function testHasPluginReturnsTrueForLoadedPlugins() - { - $this->broker->getClassLoader()->registerPlugin('sample', 'ZendTest\Loader\TestAsset\SamplePlugin'); - $this->broker->load('sample'); - $this->assertTrue($this->broker->hasPlugin('sample')); - } - - public function testHasPluginReturnsTrueForRegisteredSpec() - { - $this->broker->registerSpec('test', array('foo', 'bar')); - $this->assertTrue($this->broker->hasPlugin('test')); - } - - public function testHasPluginReturnsFalseForUnknownPlugin() - { - $this->assertFalse($this->broker->hasPlugin('sample')); - } - - public function testPassingNullValueToPluginArgumentOfRegisterRegistersSpec() - { - $this->broker->getClassLoader()->registerPlugin('sample', 'ZendTest\Loader\TestAsset\SamplePlugin'); - $this->broker->register('sample', null); - $this->assertTrue($this->broker->hasPlugin('sample')); - $this->assertFalse($this->broker->isLoaded('sample')); - } - - public function invalidSpecs() - { - return array( - array(null), - array(true), - array(false), - array(1), - array(1.0), - array('string'), - array(new \stdClass), - ); - } - - /** - * @dataProvider invalidSpecs - */ - public function testPassingInvalidArgumentToRegisterSpecsRaisesException($specs) - { - $this->setExpectedException('Zend\Loader\Exception\InvalidArgumentException'); - $this->broker->registerSpecs($specs); - } - - public function testPassingArrayOfSpecsRegistersEachSpecification() - { - $specs = array( - 'test' => null, - 'sample' => array('foo', 'bar'), - ); - - $this->broker->registerSpecs($specs); - $this->assertTrue($this->broker->hasPlugin('test')); - $this->assertTrue($this->broker->hasPlugin('sample')); - } - - public function testPassingTraversableObjectOfSpecsRegistersEachSpecification() - { - $specs = array( - 'test' => null, - 'sample' => array('foo', 'bar'), - ); - - $this->broker->registerSpecs(new \ArrayIterator($specs)); - $this->assertTrue($this->broker->hasPlugin('test')); - $this->assertTrue($this->broker->hasPlugin('sample')); - } - - public function testLoadUsesRegisteredSpecFirstTimePluginIsLoaded() - { - $this->broker->getClassLoader()->registerPlugin('sample', 'ZendTest\Loader\TestAsset\SamplePlugin'); - $this->broker->registerSpec('sample', array(array('foo' => 'bar'))); - $plugin = $this->broker->load('sample'); - $this->assertInstanceOf('ZendTest\Loader\TestAsset\SamplePlugin', $plugin); - $this->assertEquals(array('foo' => 'bar'), $plugin->options); - } - - public function testLoadUsesPreviousInstanceOnSubsequentRequestsForPluginEvenWithDifferentOptions() - { - $this->broker->getClassLoader()->registerPlugin('sample', 'ZendTest\Loader\TestAsset\SamplePlugin'); - $this->broker->registerSpec('sample', array(array('foo' => 'bar'))); - $plugin = $this->broker->load('sample'); - $plugin2 = $this->broker->load('sample', array('bar' => 'baz')); - $this->assertSame($plugin, $plugin2); - } - - public function testRegisteringPluginTakesPrecedenceOverSpecifications() - { - $this->broker->getClassLoader()->registerPlugin('sample', 'ZendTest\Loader\TestAsset\SamplePlugin'); - $this->broker->registerSpec('sample', array(array('foo' => 'bar'))); - $plugin = new TestAsset\SamplePlugin(array('bar' => 'baz')); - $this->broker->register('sample', $plugin); - $plugin2 = $this->broker->load('sample'); - $this->assertSame($plugin, $plugin2); - } - - public function testPassingOptionsOverridesSpecIfPluginNotPreviouslyLoaded() - { - $this->broker->getClassLoader()->registerPlugin('sample', 'ZendTest\Loader\TestAsset\SamplePlugin'); - $this->broker->registerSpec('sample', array(array('foo' => 'bar'))); - $plugin = $this->broker->load('sample', array(array('bar' => 'baz'))); - $this->assertEquals(array('bar' => 'baz'), $plugin->options); - } - - public function testAllowsUnregisteringSpecificationsIndividually() - { - $this->broker->registerSpec('sample', array('foo')); - $this->broker->unregisterSpec('sample'); - $this->assertFalse($this->broker->hasPlugin('sample')); - } - - public function testAllowsConfigurationViaConstructor() - { - $validator = function($plugin) { - return true; - }; - $broker = new PluginSpecBroker(array( - 'class_loader' => array( - 'class' => 'Zend\Loader\PrefixPathLoader', - 'options' => array( - 'ZendTest\UnusualNamespace' => __DIR__ . '/TestAsset', - ) - ), - 'specs' => array( - 'ClassMappedClass' => array(array('foo' => 'bar')), - ), - 'plugins' => array( - 'test' => $this, - ), - 'validator' => $validator, - )); - - $loader = $broker->getClassLoader(); - $this->assertInstanceOf('Zend\Loader\PrefixPathLoader', $loader); - $this->assertEquals('ZendTest\UnusualNamespace\ClassMappedClass', $loader->load('ClassMappedClass')); - - $this->assertTrue($broker->isLoaded('test')); - $this->assertSame($validator, $broker->getValidator()); - - $plugin = $broker->load('ClassMappedClass'); - $this->assertInstanceOf('ZendTest\UnusualNamespace\ClassMappedClass', $plugin); - $this->assertEquals(array('foo' => 'bar'), $plugin->options); - - $broker = new PluginSpecBroker(array( - 'class_loader' => 'ZendTest\Loader\TestAsset\CustomClassLoader', - )); - $loader = $broker->getClassLoader(); - $this->assertInstanceOf('ZendTest\Loader\TestAsset\CustomClassLoader', $loader); - } -} diff --git a/test/PrefixPathLoaderTest.php b/test/PrefixPathLoaderTest.php index 630d03b..ddf88f0 100644 --- a/test/PrefixPathLoaderTest.php +++ b/test/PrefixPathLoaderTest.php @@ -1,22 +1,11 @@