diff --git a/composer.json b/composer.json index 722e67f34..fc1f6c732 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "zendframework/zend-serializer": "^2.6", "zendframework/zend-session": "^2.6.2", "fabpot/php-cs-fixer": "1.7.*", - "phpunit/PHPUnit": "~4.0" + "phpunit/phpunit": "^4.5" }, "suggest": { "zendframework/zend-serializer": "Zend\\Serializer component", @@ -44,6 +44,10 @@ "branch-alias": { "dev-master": "2.6-dev", "dev-develop": "2.7-dev" + }, + "zf": { + "component": "Zend\\Cache", + "config-provider": "Zend\\Cache\\ConfigProvider" } }, "autoload-dev": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 8e443e2b6..15044df0c 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,7 +1,7 @@ diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php new file mode 100644 index 000000000..0ebede7fe --- /dev/null +++ b/src/ConfigProvider.php @@ -0,0 +1,42 @@ + $this->getDependencyConfig(), + ]; + } + + /** + * Return default service mappings for zend-cache. + * + * @return array + */ + public function getDependencyConfig() + { + return [ + 'abstract_factories' => [ + Service\StorageCacheAbstractServiceFactory::class, + ], + 'factories' => [ + PatternPluginManager::class => Service\PatternPluginManagerFactory::class, + Storage\AdapterPluginManager::class => Service\StorageAdapterPluginManagerFactory::class, + Storage\PluginManager::class => Service\StoragePluginManagerFactory::class, + ], + ]; + } +} diff --git a/src/Module.php b/src/Module.php new file mode 100644 index 000000000..bb19f1e18 --- /dev/null +++ b/src/Module.php @@ -0,0 +1,24 @@ + $provider->getDependencyConfig(), + ]; + } +} diff --git a/src/PatternFactory.php b/src/PatternFactory.php index 7d9d82e7e..61ded14cc 100644 --- a/src/PatternFactory.php +++ b/src/PatternFactory.php @@ -32,12 +32,15 @@ abstract class PatternFactory */ public static function factory($patternName, $options = []) { + if ($options instanceof Pattern\PatternOptions) { + $options = $options->toArray(); + } + if ($options instanceof Traversable) { $options = ArrayUtils::iteratorToArray($options); } - if (is_array($options)) { - $options = new Pattern\PatternOptions($options); - } elseif (!$options instanceof Pattern\PatternOptions) { + + if (! is_array($options)) { throw new Exception\InvalidArgumentException(sprintf( '%s expects an array, Traversable object, or %s\Pattern\PatternOptions object; received "%s"', __METHOD__, @@ -47,13 +50,11 @@ public static function factory($patternName, $options = []) } if ($patternName instanceof Pattern\PatternInterface) { - $patternName->setOptions($options); + $patternName->setOptions(new Pattern\PatternOptions($options)); return $patternName; } - $pattern = static::getPluginManager()->get($patternName); - $pattern->setOptions($options); - return $pattern; + return static::getPluginManager()->get($patternName, $options); } /** diff --git a/src/PatternPluginManager.php b/src/PatternPluginManager.php index 5d4fb474a..efaf18b76 100644 --- a/src/PatternPluginManager.php +++ b/src/PatternPluginManager.php @@ -69,6 +69,38 @@ class PatternPluginManager extends AbstractPluginManager */ protected $instanceOf = Pattern\PatternInterface::class; + /** + * Override get to inject options as PatternOptions instance. + * + * {@inheritDoc} + */ + public function get($plugin, array $options = [], $usePeeringServiceManagers = true) + { + if (empty($options)) { + return parent::get($plugin, [], $usePeeringServiceManagers); + } + + $plugin = parent::get($plugin, [], $usePeeringServiceManagers); + $plugin->setOptions(new Pattern\PatternOptions($options)); + return $plugin; + } + + /** + * Override build to inject options as PatternOptions instance. + * + * {@inheritDoc} + */ + public function build($plugin, array $options = null) + { + if (empty($options)) { + return parent::build($plugin); + } + + $plugin = parent::build($plugin); + $plugin->setOptions(new Pattern\PatternOptions($options)); + return $plugin; + } + /** * Validate the plugin is of the expected type (v3). * diff --git a/src/Service/PatternPluginManagerFactory.php b/src/Service/PatternPluginManagerFactory.php new file mode 100644 index 000000000..6bd4898a4 --- /dev/null +++ b/src/Service/PatternPluginManagerFactory.php @@ -0,0 +1,54 @@ +creationOptions); + } + + /** + * zend-servicemanager v2 support for invocation options. + * + * @param array $options + * @return void + */ + public function setCreationOptions(array $options) + { + $this->creationOptions = $options; + } +} diff --git a/src/Service/PluginManagerLookupTrait.php b/src/Service/PluginManagerLookupTrait.php new file mode 100644 index 000000000..c33c35402 --- /dev/null +++ b/src/Service/PluginManagerLookupTrait.php @@ -0,0 +1,62 @@ +lookupStorageAdapterPluginManager($container)); + StorageFactory::setPluginManager($this->lookupStoragePluginManager($container)); + } + + /** + * Lookup the storage adapter plugin manager. + * + * Returns the Zend\Cache\Storage\AdapterPluginManager service if present, + * or creates a new instance otherwise. + * + * @param ContainerInterface $container + * @return AdapterPluginManager + */ + private function lookupStorageAdapterPluginManager(ContainerInterface $container) + { + if ($container->has(AdapterPluginManager::class)) { + return $container->get(AdapterPluginManager::class); + } + return new AdapterPluginManager($container); + } + + /** + * Lookup the storage plugins plugin manager. + * + * Returns the Zend\Cache\Storage\PluginManager service if present, or + * creates a new instance otherwise. + * + * @param ContainerInterface $container + * @return PluginManager + */ + private function lookupStoragePluginManager(ContainerInterface $container) + { + if ($container->has(PluginManager::class)) { + return $container->get(PluginManager::class); + } + return new PluginManager($container); + } +} diff --git a/src/Service/StorageAdapterPluginManagerFactory.php b/src/Service/StorageAdapterPluginManagerFactory.php new file mode 100644 index 000000000..62d766a98 --- /dev/null +++ b/src/Service/StorageAdapterPluginManagerFactory.php @@ -0,0 +1,54 @@ +creationOptions); + } + + /** + * zend-servicemanager v2 support for invocation options. + * + * @param array $options + * @return void + */ + public function setCreationOptions(array $options) + { + $this->creationOptions = $options; + } +} diff --git a/src/Service/StorageCacheAbstractServiceFactory.php b/src/Service/StorageCacheAbstractServiceFactory.php index 9a38d9a5e..fb86e7352 100644 --- a/src/Service/StorageCacheAbstractServiceFactory.php +++ b/src/Service/StorageCacheAbstractServiceFactory.php @@ -9,9 +9,9 @@ namespace Zend\Cache\Service; +use Interop\Container\ContainerInterface; use Zend\Cache\StorageFactory; use Zend\ServiceManager\AbstractFactoryInterface; -use Interop\Container\ContainerInterface; use Zend\ServiceManager\ServiceLocatorInterface; /** @@ -19,6 +19,8 @@ */ class StorageCacheAbstractServiceFactory implements AbstractFactoryInterface { + use PluginManagerLookupTrait; + /** * @var array */ @@ -66,6 +68,8 @@ public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator */ public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { + $this->prepareStorageFactory($container); + $config = $this->getConfig($container); return StorageFactory::factory($config[$requestedName]); } diff --git a/src/Service/StorageCacheFactory.php b/src/Service/StorageCacheFactory.php index 3fe425262..031821558 100644 --- a/src/Service/StorageCacheFactory.php +++ b/src/Service/StorageCacheFactory.php @@ -9,10 +9,10 @@ namespace Zend\Cache\Service; +use Interop\Container\ContainerInterface; use Zend\Cache\Storage\StorageInterface; use Zend\Cache\StorageFactory; use Zend\ServiceManager\FactoryInterface; -use Interop\Container\ContainerInterface; use Zend\ServiceManager\ServiceLocatorInterface; /** @@ -20,8 +20,12 @@ */ class StorageCacheFactory implements FactoryInterface { + use PluginManagerLookupTrait; + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { + $this->prepareStorageFactory($container); + $config = $container->get('config'); $cacheConfig = isset($config['cache']) ? $config['cache'] : []; return StorageFactory::factory($cacheConfig); diff --git a/src/Service/StoragePluginManagerFactory.php b/src/Service/StoragePluginManagerFactory.php new file mode 100644 index 000000000..7dcc4caef --- /dev/null +++ b/src/Service/StoragePluginManagerFactory.php @@ -0,0 +1,54 @@ +creationOptions); + } + + /** + * zend-servicemanager v2 support for invocation options. + * + * @param array $options + * @return void + */ + public function setCreationOptions(array $options) + { + $this->creationOptions = $options; + } +} diff --git a/src/StorageFactory.php b/src/StorageFactory.php index 3d0de8e2c..dda9c322a 100644 --- a/src/StorageFactory.php +++ b/src/StorageFactory.php @@ -203,11 +203,10 @@ public static function pluginFactory($pluginName, $options = []) $plugin = static::getPluginManager()->get($pluginName); } - if (!$options instanceof Storage\Plugin\PluginOptions) { - $options = new Storage\Plugin\PluginOptions($options); - } - if ($options) { + if (!$options instanceof Storage\Plugin\PluginOptions) { + $options = new Storage\Plugin\PluginOptions($options); + } $plugin->setOptions($options); } diff --git a/test/PatternPluginManagerTest.php b/test/PatternPluginManagerTest.php index f619d7c64..a8ca0909a 100644 --- a/test/PatternPluginManagerTest.php +++ b/test/PatternPluginManagerTest.php @@ -13,6 +13,7 @@ use Zend\Cache\Exception\RuntimeException; use Zend\Cache\Pattern\PatternInterface; use Zend\Cache\PatternPluginManager; +use Zend\Cache\Storage\StorageInterface; use Zend\ServiceManager\ServiceManager; use Zend\ServiceManager\Test\CommonPluginManagerTrait; @@ -34,4 +35,35 @@ protected function getInstanceOf() { return PatternInterface::class; } + + public function testGetWillInjectProvidedOptionsAsPatternOptionsInstance() + { + $plugins = $this->getPluginManager(); + $storage = $this->prophesize(StorageInterface::class)->reveal(); + $plugin = $plugins->get('callback', [ + 'cache_output' => false, + 'storage' => $storage, + ]); + $options = $plugin->getOptions(); + $this->assertFalse($options->getCacheOutput()); + $this->assertSame($storage, $options->getStorage()); + } + + public function testBuildWillInjectProvidedOptionsAsPatternOptionsInstance() + { + $plugins = $this->getPluginManager(); + + if (! method_exists($plugins, 'configure')) { + $this->markTestSkipped('Test is only relevant for zend-servicemanager v3'); + } + + $storage = $this->prophesize(StorageInterface::class)->reveal(); + $plugin = $plugins->build('callback', [ + 'cache_output' => false, + 'storage' => $storage, + ]); + $options = $plugin->getOptions(); + $this->assertFalse($options->getCacheOutput()); + $this->assertSame($storage, $options->getStorage()); + } } diff --git a/test/Service/PatternPluginManagerFactoryTest.php b/test/Service/PatternPluginManagerFactoryTest.php new file mode 100644 index 000000000..2d6b7b9bd --- /dev/null +++ b/test/Service/PatternPluginManagerFactoryTest.php @@ -0,0 +1,73 @@ +prophesize(ContainerInterface::class)->reveal(); + $factory = new PatternPluginManagerFactory(); + + $patterns = $factory($container, PatternPluginManager::class); + $this->assertInstanceOf(PatternPluginManager::class, $patterns); + + if (method_exists($patterns, 'configure')) { + // zend-servicemanager v3 + $this->assertAttributeSame($container, 'creationContext', $patterns); + } else { + // zend-servicemanager v2 + $this->assertSame($container, $patterns->getServiceLocator()); + } + } + + /** + * @depends testFactoryReturnsPluginManager + */ + public function testFactoryConfiguresPluginManagerUnderContainerInterop() + { + $container = $this->prophesize(ContainerInterface::class)->reveal(); + $pattern = $this->prophesize(PatternInterface::class)->reveal(); + + $factory = new PatternPluginManagerFactory(); + $patterns = $factory($container, PatternPluginManager::class, [ + 'services' => [ + 'test' => $pattern, + ], + ]); + $this->assertSame($pattern, $patterns->get('test')); + } + + /** + * @depends testFactoryReturnsPluginManager + */ + public function testFactoryConfiguresPluginManagerUnderServiceManagerV2() + { + $container = $this->prophesize(ServiceLocatorInterface::class); + $container->willImplement(ContainerInterface::class); + + $pattern = $this->prophesize(PatternInterface::class)->reveal(); + + $factory = new PatternPluginManagerFactory(); + $factory->setCreationOptions([ + 'services' => [ + 'test' => $pattern, + ], + ]); + + $patterns = $factory->createService($container->reveal()); + $this->assertSame($pattern, $patterns->get('test')); + } +} diff --git a/test/Service/StorageAdapterPluginManagerFactoryTest.php b/test/Service/StorageAdapterPluginManagerFactoryTest.php new file mode 100644 index 000000000..6a6807c06 --- /dev/null +++ b/test/Service/StorageAdapterPluginManagerFactoryTest.php @@ -0,0 +1,73 @@ +prophesize(ContainerInterface::class)->reveal(); + $factory = new StorageAdapterPluginManagerFactory(); + + $adapters = $factory($container, AdapterPluginManager::class); + $this->assertInstanceOf(AdapterPluginManager::class, $adapters); + + if (method_exists($adapters, 'configure')) { + // zend-servicemanager v3 + $this->assertAttributeSame($container, 'creationContext', $adapters); + } else { + // zend-servicemanager v2 + $this->assertSame($container, $adapters->getServiceLocator()); + } + } + + /** + * @depends testFactoryReturnsPluginManager + */ + public function testFactoryConfiguresPluginManagerUnderContainerInterop() + { + $container = $this->prophesize(ContainerInterface::class)->reveal(); + $adapter = $this->prophesize(StorageInterface::class)->reveal(); + + $factory = new StorageAdapterPluginManagerFactory(); + $adapters = $factory($container, AdapterPluginManager::class, [ + 'services' => [ + 'test' => $adapter, + ], + ]); + $this->assertSame($adapter, $adapters->get('test')); + } + + /** + * @depends testFactoryReturnsPluginManager + */ + public function testFactoryConfiguresPluginManagerUnderServiceManagerV2() + { + $container = $this->prophesize(ServiceLocatorInterface::class); + $container->willImplement(ContainerInterface::class); + + $adapter = $this->prophesize(StorageInterface::class)->reveal(); + + $factory = new StorageAdapterPluginManagerFactory(); + $factory->setCreationOptions([ + 'services' => [ + 'test' => $adapter, + ], + ]); + + $adapters = $factory->createService($container->reveal()); + $this->assertSame($adapter, $adapters->get('test')); + } +} diff --git a/test/Service/StorageCacheAbstractServiceFactoryTest.php b/test/Service/StorageCacheAbstractServiceFactoryTest.php index b68e1ba87..98e019193 100644 --- a/test/Service/StorageCacheAbstractServiceFactoryTest.php +++ b/test/Service/StorageCacheAbstractServiceFactoryTest.php @@ -9,12 +9,20 @@ namespace ZendTest\Cache\Service; -use Zend\Cache; +use Interop\Container\ContainerInterface; +use Prophecy\Argument; +use Zend\Cache\Service\StorageCacheAbstractServiceFactory; +use Zend\Cache\StorageFactory; +use Zend\Cache\Storage\AdapterPluginManager; +use Zend\Cache\Storage\Adapter\AbstractAdapter; +use Zend\Cache\Storage\Adapter\Memory; +use Zend\Cache\Storage\PluginManager; +use Zend\Cache\Storage\Plugin\PluginInterface; +use Zend\Cache\Storage\StorageInterface; use Zend\ServiceManager\Config; use Zend\ServiceManager\ServiceManager; /** - * @group Zend_Cache * @covers Zend\Cache\StorageFactory */ class StorageCacheAbstractServiceFactoryTest extends \PHPUnit_Framework_TestCase @@ -23,8 +31,8 @@ class StorageCacheAbstractServiceFactoryTest extends \PHPUnit_Framework_TestCase public function setUp() { - Cache\StorageFactory::resetAdapterPluginManager(); - Cache\StorageFactory::resetPluginManager(); + StorageFactory::resetAdapterPluginManager(); + StorageFactory::resetPluginManager(); $config = [ 'services' => [ 'config' => [ @@ -41,24 +49,17 @@ public function setUp() ], ], 'abstract_factories' => [ - 'Zend\Cache\Service\StorageCacheAbstractServiceFactory' + StorageCacheAbstractServiceFactory::class ] ]; $this->sm = new ServiceManager(); - if (method_exists($this->sm, 'configure')) { - // v3 - $this->sm->configure($config); - } else { - // v2 - $config = new Config($config); - $config->configureServiceManager($this->sm); - } + (new Config($config))->configureServiceManager($this->sm); } public function tearDown() { - Cache\StorageFactory::resetAdapterPluginManager(); - Cache\StorageFactory::resetPluginManager(); + StorageFactory::resetAdapterPluginManager(); + StorageFactory::resetPluginManager(); } public function testCanLookupCacheByName() @@ -70,10 +71,10 @@ public function testCanLookupCacheByName() public function testCanRetrieveCacheByName() { $cacheA = $this->sm->get('Memory'); - $this->assertInstanceOf('Zend\Cache\Storage\Adapter\Memory', $cacheA); + $this->assertInstanceOf(Memory::class, $cacheA); $cacheB = $this->sm->get('Foo'); - $this->assertInstanceOf('Zend\Cache\Storage\Adapter\Memory', $cacheB); + $this->assertInstanceOf(Memory::class, $cacheB); $this->assertNotSame($cacheA, $cacheB); } @@ -82,4 +83,66 @@ public function testInvalidCacheServiceNameWillBeIgnored() { $this->assertFalse($this->sm->has('invalid')); } + + public function testSetsFactoryAdapterPluginManagerInstanceOnInvocation() + { + $adapter = $this->prophesize(AbstractAdapter::class); + $adapter->willImplement(StorageInterface::class); + $adapter->setOptions(Argument::any())->shouldNotBeCalled(); + $adapter->hasPlugin(Argument::any(), Argument::any())->shouldNotBeCalled(); + $adapter->addPlugin(Argument::any(), Argument::any())->shouldNotBeCalled(); + + $adapterPluginManager = $this->prophesize(AdapterPluginManager::class); + $adapterPluginManager->get('Memory')->willReturn($adapter->reveal()); + + $container = $this->prophesize(ContainerInterface::class); + $container->has(AdapterPluginManager::class)->willReturn(true); + $container->get(AdapterPluginManager::class)->willReturn($adapterPluginManager->reveal()); + $container->has(PluginManager::class)->willReturn(false); + + $container->has('config')->willReturn(true); + $container->get('config')->willReturn([ + 'caches' => [ 'Cache' => [ 'adapter' => 'Memory' ]], + ]); + + $factory = new StorageCacheAbstractServiceFactory(); + $this->assertSame($adapter->reveal(), $factory($container->reveal(), 'Cache')); + $this->assertSame($adapterPluginManager->reveal(), StorageFactory::getAdapterPluginManager()); + } + + public function testSetsFactoryPluginManagerInstanceOnInvocation() + { + $plugin = $this->prophesize(PluginInterface::class); + $plugin->setOptions(Argument::any())->shouldNotBeCalled(); + + $pluginManager = $this->prophesize(PluginManager::class); + $pluginManager->get('Serializer')->willReturn($plugin->reveal()); + + $adapter = $this->prophesize(AbstractAdapter::class); + $adapter->willImplement(StorageInterface::class); + $adapter->setOptions(Argument::any())->shouldNotBeCalled(); + $adapter->hasPlugin($plugin->reveal(), Argument::any())->willReturn(false); + $adapter->addPlugin($plugin->reveal(), Argument::any())->shouldBeCalled(); + + $adapterPluginManager = $this->prophesize(AdapterPluginManager::class); + $adapterPluginManager->get('Memory')->willReturn($adapter->reveal()); + + $container = $this->prophesize(ContainerInterface::class); + $container->has(AdapterPluginManager::class)->willReturn(true); + $container->get(AdapterPluginManager::class)->willReturn($adapterPluginManager->reveal()); + $container->has(PluginManager::class)->willReturn(true); + $container->get(PluginManager::class)->willReturn($pluginManager->reveal()); + + $container->has('config')->willReturn(true); + $container->get('config')->willReturn([ + 'caches' => [ 'Cache' => [ + 'adapter' => 'Memory', + 'plugins' => ['Serializer'], + ]], + ]); + + $factory = new StorageCacheAbstractServiceFactory(); + $factory($container->reveal(), 'Cache'); + $this->assertSame($pluginManager->reveal(), StorageFactory::getPluginManager()); + } } diff --git a/test/Service/StorageCacheFactoryTest.php b/test/Service/StorageCacheFactoryTest.php index 9c4bd7967..4baa2d11a 100644 --- a/test/Service/StorageCacheFactoryTest.php +++ b/test/Service/StorageCacheFactoryTest.php @@ -9,22 +9,31 @@ namespace ZendTest\Cache\Service; -use Zend\Cache; +use Interop\Container\ContainerInterface; +use PHPUnit_Framework_TestCase as TestCase; +use Prophecy\Argument; +use Zend\Cache\StorageFactory; +use Zend\Cache\Service\StorageCacheFactory; +use Zend\Cache\Storage\AdapterPluginManager; +use Zend\Cache\Storage\Adapter\AbstractAdapter; +use Zend\Cache\Storage\Adapter\Memory; +use Zend\Cache\Storage\PluginManager; +use Zend\Cache\Storage\Plugin\PluginInterface; +use Zend\Cache\Storage\StorageInterface; use Zend\ServiceManager\Config; use Zend\ServiceManager\ServiceManager; /** - * @group Zend_Cache * @covers Zend\Cache\Service\StorageCacheFactory */ -class StorageCacheFactoryTest extends \PHPUnit_Framework_TestCase +class StorageCacheFactoryTest extends TestCase { protected $sm; public function setUp() { - Cache\StorageFactory::resetAdapterPluginManager(); - Cache\StorageFactory::resetPluginManager(); + StorageFactory::resetAdapterPluginManager(); + StorageFactory::resetPluginManager(); $config = [ 'services' => [ 'config' => [ @@ -35,29 +44,84 @@ public function setUp() ] ], 'factories' => [ - 'CacheFactory' => \Zend\Cache\Service\StorageCacheFactory::class + 'CacheFactory' => StorageCacheFactory::class ] ]; $this->sm = new ServiceManager(); - if (method_exists($this->sm, 'configure')) { - // v3 - $this->sm->configure($config); - } else { - // v2 - $config = new Config($config); - $config->configureServiceManager($this->sm); - } + (new Config($config))->configureServiceManager($this->sm); } public function tearDown() { - Cache\StorageFactory::resetAdapterPluginManager(); - Cache\StorageFactory::resetPluginManager(); + StorageFactory::resetAdapterPluginManager(); + StorageFactory::resetPluginManager(); } public function testCreateServiceCache() { $cache = $this->sm->get('CacheFactory'); - $this->assertEquals('Zend\Cache\Storage\Adapter\Memory', get_class($cache)); + $this->assertEquals(Memory::class, get_class($cache)); + } + + public function testSetsFactoryAdapterPluginManagerInstanceOnInvocation() + { + $adapter = $this->prophesize(AbstractAdapter::class); + $adapter->willImplement(StorageInterface::class); + $adapter->setOptions(Argument::any())->shouldNotBeCalled(); + $adapter->hasPlugin(Argument::any(), Argument::any())->shouldNotBeCalled(); + $adapter->addPlugin(Argument::any(), Argument::any())->shouldNotBeCalled(); + + $adapterPluginManager = $this->prophesize(AdapterPluginManager::class); + $adapterPluginManager->get('Memory')->willReturn($adapter->reveal()); + + $container = $this->prophesize(ContainerInterface::class); + $container->has(AdapterPluginManager::class)->willReturn(true); + $container->get(AdapterPluginManager::class)->willReturn($adapterPluginManager->reveal()); + $container->has(PluginManager::class)->willReturn(false); + + $container->has('config')->willReturn(true); + $container->get('config')->willReturn([ + 'cache' => [ 'adapter' => 'Memory' ], + ]); + + $factory = new StorageCacheFactory(); + $this->assertSame($adapter->reveal(), $factory($container->reveal(), 'Cache')); + $this->assertSame($adapterPluginManager->reveal(), StorageFactory::getAdapterPluginManager()); + } + + public function testSetsFactoryPluginManagerInstanceOnInvocation() + { + $plugin = $this->prophesize(PluginInterface::class); + $plugin->setOptions(Argument::any())->shouldNotBeCalled(); + + $pluginManager = $this->prophesize(PluginManager::class); + $pluginManager->get('Serializer')->willReturn($plugin->reveal()); + + $adapter = $this->prophesize(AbstractAdapter::class); + $adapter->willImplement(StorageInterface::class); + $adapter->setOptions(Argument::any())->shouldNotBeCalled(); + $adapter->hasPlugin($plugin->reveal(), Argument::any())->willReturn(false); + $adapter->addPlugin($plugin->reveal(), Argument::any())->shouldBeCalled(); + + $adapterPluginManager = $this->prophesize(AdapterPluginManager::class); + $adapterPluginManager->get('Memory')->willReturn($adapter->reveal()); + + $container = $this->prophesize(ContainerInterface::class); + $container->has(AdapterPluginManager::class)->willReturn(true); + $container->get(AdapterPluginManager::class)->willReturn($adapterPluginManager->reveal()); + $container->has(PluginManager::class)->willReturn(true); + $container->get(PluginManager::class)->willReturn($pluginManager->reveal()); + + $container->has('config')->willReturn(true); + $container->get('config')->willReturn([ + 'cache' => [ + 'adapter' => 'Memory', + 'plugins' => ['Serializer'], + ], + ]); + + $factory = new StorageCacheFactory(); + $factory($container->reveal(), 'Cache'); + $this->assertSame($pluginManager->reveal(), StorageFactory::getPluginManager()); } } diff --git a/test/Service/StoragePluginManagerFactoryTest.php b/test/Service/StoragePluginManagerFactoryTest.php new file mode 100644 index 000000000..ce4a5b527 --- /dev/null +++ b/test/Service/StoragePluginManagerFactoryTest.php @@ -0,0 +1,73 @@ +prophesize(ContainerInterface::class)->reveal(); + $factory = new StoragePluginManagerFactory(); + + $plugins = $factory($container, PluginManager::class); + $this->assertInstanceOf(PluginManager::class, $plugins); + + if (method_exists($plugins, 'configure')) { + // zend-servicemanager v3 + $this->assertAttributeSame($container, 'creationContext', $plugins); + } else { + // zend-servicemanager v2 + $this->assertSame($container, $plugins->getServiceLocator()); + } + } + + /** + * @depends testFactoryReturnsPluginManager + */ + public function testFactoryConfiguresPluginManagerUnderContainerInterop() + { + $container = $this->prophesize(ContainerInterface::class)->reveal(); + $plugin = $this->prophesize(pluginInterface::class)->reveal(); + + $factory = new StoragePluginManagerFactory(); + $plugins = $factory($container, PluginManager::class, [ + 'services' => [ + 'test' => $plugin, + ], + ]); + $this->assertSame($plugin, $plugins->get('test')); + } + + /** + * @depends testFactoryReturnsPluginManager + */ + public function testFactoryConfiguresPluginManagerUnderServiceManagerV2() + { + $container = $this->prophesize(ServiceLocatorInterface::class); + $container->willImplement(ContainerInterface::class); + + $plugin = $this->prophesize(PluginInterface::class)->reveal(); + + $factory = new StoragePluginManagerFactory(); + $factory->setCreationOptions([ + 'services' => [ + 'test' => $plugin, + ], + ]); + + $plugins = $factory->createService($container->reveal()); + $this->assertSame($plugin, $plugins->get('test')); + } +} diff --git a/test/bootstrap.php b/test/bootstrap.php deleted file mode 100644 index e3a7f90b6..000000000 --- a/test/bootstrap.php +++ /dev/null @@ -1,34 +0,0 @@ -