From b2a03e47d3e35484490d19d65d70e7fcd4b82c12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= Date: Mon, 10 Sep 2018 16:20:14 +0200 Subject: [PATCH 01/11] Created `AdapterPluginManager` polyfills for either `zend-servicemanager` v2 and v3 --- autoload/adapterPluginManagerPolyfill.php | 17 +++ composer.json | 3 +- .../AdapterPluginManagerTrait.php | 70 +++++++++++ .../AdapterPluginManagerV2Polyfill.php} | 77 +++++------- .../AdapterPluginManagerV3Polyfill.php | 118 ++++++++++++++++++ 5 files changed, 235 insertions(+), 50 deletions(-) create mode 100644 autoload/adapterPluginManagerPolyfill.php create mode 100644 src/Storage/AdapterPluginManager/AdapterPluginManagerTrait.php rename src/Storage/{AdapterPluginManager.php => AdapterPluginManager/AdapterPluginManagerV2Polyfill.php} (77%) create mode 100644 src/Storage/AdapterPluginManager/AdapterPluginManagerV3Polyfill.php diff --git a/autoload/adapterPluginManagerPolyfill.php b/autoload/adapterPluginManagerPolyfill.php new file mode 100644 index 000000000..dd3da4aaf --- /dev/null +++ b/autoload/adapterPluginManagerPolyfill.php @@ -0,0 +1,17 @@ +setOptions(new Adapter\AdapterOptions($options)); + return $plugin; + } + + /** + * Validate the plugin is of the expected type (v3). + * + * Validates against `$instanceOf`. + * + * @param mixed $instance + * @throws InvalidServiceException + */ + public function validate($instance) + { + if (! $instance instanceof $this->instanceOf) { + throw new InvalidServiceException(sprintf( + '%s can only create instances of %s; %s is invalid', + get_class($this), + $this->instanceOf, + (is_object($instance) ? get_class($instance) : gettype($instance)) + )); + } + } + + /** + * Validate the plugin is of the expected type (v2). + * + * Proxies to `validate()`. + * + * @param mixed $plugin + * @throws Exception\RuntimeException if invalid + */ + public function validatePlugin($plugin) + { + try { + $this->validate($plugin); + } catch (InvalidServiceException $e) { + throw new Exception\RuntimeException($e->getMessage(), $e->getCode(), $e); + } + } +} diff --git a/src/Storage/AdapterPluginManager.php b/src/Storage/AdapterPluginManager/AdapterPluginManagerV2Polyfill.php similarity index 77% rename from src/Storage/AdapterPluginManager.php rename to src/Storage/AdapterPluginManager/AdapterPluginManagerV2Polyfill.php index 98b73f35d..d314de934 100644 --- a/src/Storage/AdapterPluginManager.php +++ b/src/Storage/AdapterPluginManager/AdapterPluginManagerV2Polyfill.php @@ -1,28 +1,23 @@ Adapter\Apc::class, 'Apc' => Adapter\Apc::class, @@ -120,18 +115,18 @@ class AdapterPluginManager extends AbstractPluginManager ]; /** - * Do not share by default (v3) + * Don't share by default (v2) * - * @var array + * @var boolean */ - protected $sharedByDefault = false; + protected $shareByDefault = false; /** - * Don't share by default (v2) + * Don't share by default (v3) * * @var boolean */ - protected $shareByDefault = false; + protected $sharedByDefault = false; /** * @var string @@ -139,39 +134,23 @@ class AdapterPluginManager extends AbstractPluginManager protected $instanceOf = StorageInterface::class; /** - * Validate the plugin is of the expected type (v3). + * Override get to inject options as AdapterOptions instance. * - * Validates against `$instanceOf`. - * - * @param mixed $instance - * @throws InvalidServiceException + * {@inheritDoc} */ - public function validate($instance) + public function get($plugin, $options = [], $usePeeringServiceManagers = true) { - if (! $instance instanceof $this->instanceOf) { - throw new InvalidServiceException(sprintf( - '%s can only create instances of %s; %s is invalid', - get_class($this), - $this->instanceOf, - (is_object($instance) ? get_class($instance) : gettype($instance)) - )); + if (empty($options)) { + return parent::get($plugin, [], $usePeeringServiceManagers); } - } - /** - * Validate the plugin is of the expected type (v2). - * - * Proxies to `validate()`. - * - * @param mixed $instance - * @throws InvalidServiceException - */ - public function validatePlugin($instance) - { - try { - $this->validate($instance); - } catch (InvalidServiceException $e) { - throw new RuntimeException($e->getMessage(), $e->getCode(), $e); - } + $plugins = isset($options['plugins']) ? $options['plugins'] : []; + unset($options['plugins']); + + /** @var StorageInterface $adapter */ + $adapter = parent::get($plugin, [], $usePeeringServiceManagers); + $adapter->setOptions(new Adapter\AdapterOptions($options)); + + return $adapter; } } diff --git a/src/Storage/AdapterPluginManager/AdapterPluginManagerV3Polyfill.php b/src/Storage/AdapterPluginManager/AdapterPluginManagerV3Polyfill.php new file mode 100644 index 000000000..584238d52 --- /dev/null +++ b/src/Storage/AdapterPluginManager/AdapterPluginManagerV3Polyfill.php @@ -0,0 +1,118 @@ + Adapter\Apc::class, + 'Apc' => Adapter\Apc::class, + 'APC' => Adapter\Apc::class, + 'apcu' => Adapter\Apcu::class, + 'ApcU' => Adapter\Apcu::class, + 'Apcu' => Adapter\Apcu::class, + 'APCu' => Adapter\Apcu::class, + 'black_hole' => Adapter\BlackHole::class, + 'blackhole' => Adapter\BlackHole::class, + 'blackHole' => Adapter\BlackHole::class, + 'BlackHole' => Adapter\BlackHole::class, + 'dba' => Adapter\Dba::class, + 'Dba' => Adapter\Dba::class, + 'DBA' => Adapter\Dba::class, + 'ext_mongo_db' => Adapter\ExtMongoDb::class, + 'extmongodb' => Adapter\ExtMongoDb::class, + 'ExtMongoDb' => Adapter\ExtMongoDb::class, + 'ExtMongoDB' => Adapter\ExtMongoDb::class, + 'extMongoDb' => Adapter\ExtMongoDb::class, + 'extMongoDB' => Adapter\ExtMongoDb::class, + 'filesystem' => Adapter\Filesystem::class, + 'Filesystem' => Adapter\Filesystem::class, + 'memcache' => Adapter\Memcache::class, + 'Memcache' => Adapter\Memcache::class, + 'memcached' => Adapter\Memcached::class, + 'Memcached' => Adapter\Memcached::class, + 'memory' => Adapter\Memory::class, + 'Memory' => Adapter\Memory::class, + 'mongo_db' => Adapter\MongoDb::class, + 'mongodb' => Adapter\MongoDb::class, + 'MongoDb' => Adapter\MongoDb::class, + 'MongoDB' => Adapter\MongoDb::class, + 'mongoDb' => Adapter\MongoDb::class, + 'mongoDB' => Adapter\MongoDb::class, + 'redis' => Adapter\Redis::class, + 'Redis' => Adapter\Redis::class, + 'session' => Adapter\Session::class, + 'Session' => Adapter\Session::class, + 'xcache' => Adapter\XCache::class, + 'xCache' => Adapter\XCache::class, + 'Xcache' => Adapter\XCache::class, + 'XCache' => Adapter\XCache::class, + 'win_cache' => Adapter\WinCache::class, + 'wincache' => Adapter\WinCache::class, + 'winCache' => Adapter\WinCache::class, + 'WinCache' => Adapter\WinCache::class, + 'zend_server_disk' => Adapter\ZendServerDisk::class, + 'zendserverdisk' => Adapter\ZendServerDisk::class, + 'zendServerDisk' => Adapter\ZendServerDisk::class, + 'ZendServerDisk' => Adapter\ZendServerDisk::class, + 'zend_server_shm' => Adapter\ZendServerShm::class, + 'zendservershm' => Adapter\ZendServerShm::class, + 'zendServerShm' => Adapter\ZendServerShm::class, + 'zendServerSHM' => Adapter\ZendServerShm::class, + 'ZendServerShm' => Adapter\ZendServerShm::class, + 'ZendServerSHM' => Adapter\ZendServerShm::class, + ]; + + protected $factories = [ + Adapter\Apc::class => InvokableFactory::class, + Adapter\Apcu::class => InvokableFactory::class, + Adapter\BlackHole::class => InvokableFactory::class, + Adapter\Dba::class => InvokableFactory::class, + Adapter\ExtMongoDb::class => InvokableFactory::class, + Adapter\Filesystem::class => InvokableFactory::class, + Adapter\Memcache::class => InvokableFactory::class, + Adapter\Memcached::class => InvokableFactory::class, + Adapter\Memory::class => InvokableFactory::class, + Adapter\MongoDb::class => InvokableFactory::class, + Adapter\Redis::class => InvokableFactory::class, + Adapter\Session::class => InvokableFactory::class, + Adapter\WinCache::class => InvokableFactory::class, + Adapter\XCache::class => InvokableFactory::class, + Adapter\ZendServerDisk::class => InvokableFactory::class, + Adapter\ZendServerShm::class => InvokableFactory::class, + ]; + + /** + * Don't share by default (v2) + * + * @var boolean + */ + protected $shareByDefault = false; + + /** + * Don't share by default (v3) + * + * @var boolean + */ + protected $sharedByDefault = false; + + /** + * @var string + */ + protected $instanceOf = StorageInterface::class; +} From 0186d9ad04cb0545d89cb41e654e0470ff981848 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= Date: Mon, 10 Sep 2018 16:20:54 +0200 Subject: [PATCH 02/11] Added stubs for polyfilled plugin managers --- .gitattributes | 1 + stubs/AdapterPluginManager.php | 10 ++++++++++ stubs/PatternPluginManager.php | 10 ++++++++++ stubs/README.md | 4 ++++ 4 files changed, 25 insertions(+) create mode 100644 stubs/AdapterPluginManager.php create mode 100644 stubs/PatternPluginManager.php create mode 100644 stubs/README.md diff --git a/.gitattributes b/.gitattributes index 84d967a71..b1742eb27 100644 --- a/.gitattributes +++ b/.gitattributes @@ -11,3 +11,4 @@ /phpcs.xml export-ignore /phpunit.xml.dist export-ignore /test/ export-ignore +/stubs/ diff --git a/stubs/AdapterPluginManager.php b/stubs/AdapterPluginManager.php new file mode 100644 index 000000000..d9a664f3b --- /dev/null +++ b/stubs/AdapterPluginManager.php @@ -0,0 +1,10 @@ + Date: Mon, 10 Sep 2018 16:29:23 +0200 Subject: [PATCH 03/11] Linked the polyfill classes in their stubs --- stubs/AdapterPluginManager.php | 6 ++++++ stubs/PatternPluginManager.php | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/stubs/AdapterPluginManager.php b/stubs/AdapterPluginManager.php index d9a664f3b..0e80cdae1 100644 --- a/stubs/AdapterPluginManager.php +++ b/stubs/AdapterPluginManager.php @@ -2,8 +2,14 @@ namespace Zend\Cache\Storage; +use Zend\Cache\Storage\AdapterPluginManager\AdapterPluginManagerV2Polyfill; +use Zend\Cache\Storage\AdapterPluginManager\AdapterPluginManagerV3Polyfill; use Zend\ServiceManager\AbstractPluginManager; +/** + * @see AdapterPluginManagerV2Polyfill + * @see AdapterPluginManagerV3Polyfill + */ class AdapterPluginManager extends AbstractPluginManager { diff --git a/stubs/PatternPluginManager.php b/stubs/PatternPluginManager.php index 10c09a920..9850911d8 100644 --- a/stubs/PatternPluginManager.php +++ b/stubs/PatternPluginManager.php @@ -2,8 +2,14 @@ namespace Zend\Cache; +use Zend\Cache\PatternPluginManager\PatternPluginManagerV2Polyfill; +use Zend\Cache\PatternPluginManager\PatternPluginManagerV3Polyfill; use Zend\ServiceManager\AbstractPluginManager; +/** + * @see PatternPluginManagerV2Polyfill + * @see PatternPluginManagerV3Polyfill + */ class PatternPluginManager extends AbstractPluginManager { From e5e6a6174a22591393504455aa3d0effdfb5a148 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= Date: Mon, 10 Sep 2018 16:35:29 +0200 Subject: [PATCH 04/11] Fixed documentation issue --- src/PatternPluginManager/PatternPluginManagerTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PatternPluginManager/PatternPluginManagerTrait.php b/src/PatternPluginManager/PatternPluginManagerTrait.php index d05160585..1cb5f35b7 100644 --- a/src/PatternPluginManager/PatternPluginManagerTrait.php +++ b/src/PatternPluginManager/PatternPluginManagerTrait.php @@ -12,7 +12,7 @@ use Zend\ServiceManager\Exception\InvalidServiceException; /** - * Trait providing common logic between FormElementManager implementations. + * Trait providing common logic between PatternPluginManager implementations. * * Trait does not define properties, as the properties common between the * two versions are originally defined in their parent class, causing a From 76e132229648c12b014650ee00e8dd0a27566557 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= Date: Mon, 10 Sep 2018 16:36:06 +0200 Subject: [PATCH 05/11] Marked `Zend\Cache\StorageFactory` and `Zend\Cache\PatternFactory` as deprecated - Piped `Zend\Cache\StorageFactory` logic to new `Zend\Cache\Storage\StorageFactory` which uses dependency injection instead of static methods - Fixed unit tests for new PluginManager calls - Trigger `E_USER_DEPRECATED` error on static factory calls --- src/PatternFactory.php | 36 +++- src/Service/StorageFactoryFactory.php | 30 ++++ src/Storage/StorageFactory.php | 157 ++++++++++++++++ src/StorageFactory.php | 167 +++++++----------- test/PatternFactoryTest.php | 14 ++ ...StorageCacheAbstractServiceFactoryTest.php | 15 +- test/Service/StorageCacheFactoryTest.php | 9 +- test/Service/StorageFactoryFactoryTest.php | 66 +++++++ test/Storage/AdapterPluginManagerTest.php | 14 ++ test/StorageFactoryTest.php | 16 ++ 10 files changed, 415 insertions(+), 109 deletions(-) create mode 100644 src/Service/StorageFactoryFactory.php create mode 100644 src/Storage/StorageFactory.php create mode 100644 test/Service/StorageFactoryFactoryTest.php diff --git a/src/PatternFactory.php b/src/PatternFactory.php index 61ded14cc..9242c9ff2 100644 --- a/src/PatternFactory.php +++ b/src/PatternFactory.php @@ -13,8 +13,12 @@ use Zend\Stdlib\ArrayUtils; use Zend\ServiceManager\ServiceManager; +/** + * @deprecated static factories are deprecated as of zendframework 2.9 and may be removed in future major versions. + */ abstract class PatternFactory { + /** * The pattern manager * @@ -25,13 +29,21 @@ abstract class PatternFactory /** * Instantiate a cache pattern * - * @param string|Pattern\PatternInterface $patternName + * @param string|Pattern\PatternInterface $patternName * @param array|Traversable|Pattern\PatternOptions $options + * * @return Pattern\PatternInterface * @throws Exception\InvalidArgumentException + * @deprecated static factories are deprecated as of zendframework 2.9 and may be removed in future major versions. */ public static function factory($patternName, $options = []) { + trigger_error(sprintf( + '%s is deprecated; please use %s::get instead', + __METHOD__, + PatternPluginManager::class + ), E_USER_DEPRECATED); + if ($options instanceof Pattern\PatternOptions) { $options = $options->toArray(); } @@ -40,7 +52,7 @@ public static function factory($patternName, $options = []) $options = ArrayUtils::iteratorToArray($options); } - if (! is_array($options)) { + if (!is_array($options)) { throw new Exception\InvalidArgumentException(sprintf( '%s expects an array, Traversable object, or %s\Pattern\PatternOptions object; received "%s"', __METHOD__, @@ -51,6 +63,7 @@ public static function factory($patternName, $options = []) if ($patternName instanceof Pattern\PatternInterface) { $patternName->setOptions(new Pattern\PatternOptions($options)); + return $patternName; } @@ -61,9 +74,15 @@ public static function factory($patternName, $options = []) * Get the pattern plugin manager * * @return PatternPluginManager + * @deprecated static factories are deprecated as of zendframework 2.9 and may be removed in future major versions. */ public static function getPluginManager() { + trigger_error(sprintf( + '%s is deprecated', + __METHOD__ + ), E_USER_DEPRECATED); + if (static::$plugins === null) { static::$plugins = new PatternPluginManager(new ServiceManager); } @@ -75,10 +94,17 @@ public static function getPluginManager() * Set the pattern plugin manager * * @param PatternPluginManager $plugins + * * @return void + * @deprecated static factories are deprecated as of zendframework 2.9 and may be removed in future major versions. */ public static function setPluginManager(PatternPluginManager $plugins) { + trigger_error(sprintf( + '%s is deprecated', + __METHOD__ + ), E_USER_DEPRECATED); + static::$plugins = $plugins; } @@ -86,9 +112,15 @@ public static function setPluginManager(PatternPluginManager $plugins) * Reset pattern plugin manager to default * * @return void + * @deprecated static factories are deprecated as of zendframework 2.9 and may be removed in future major versions. */ public static function resetPluginManager() { + trigger_error(sprintf( + '%s is deprecated', + __METHOD__ + ), E_USER_DEPRECATED); + static::$plugins = null; } } diff --git a/src/Service/StorageFactoryFactory.php b/src/Service/StorageFactoryFactory.php new file mode 100644 index 000000000..b429c89c3 --- /dev/null +++ b/src/Service/StorageFactoryFactory.php @@ -0,0 +1,30 @@ +get(AdapterPluginManager::class), $container->get(PluginManager::class)); + } +} diff --git a/src/Storage/StorageFactory.php b/src/Storage/StorageFactory.php new file mode 100644 index 000000000..fa444e01f --- /dev/null +++ b/src/Storage/StorageFactory.php @@ -0,0 +1,157 @@ +adapters = $adapters; + $this->plugins = $plugins; + } + + /** + * Creates a storage adapter by parsing the `caches` configuration. + * + * @param array|Traversable $config + * + * @return StorageInterface + * @throws Exception\InvalidArgumentException + */ + public function createFromCachesConfig($config) + { + if ($config instanceof Traversable) { + $config = ArrayUtils::iteratorToArray($config); + } + + if (!is_array($config)) { + throw new Exception\InvalidArgumentException( + 'The factory needs an associative array ' + . 'or a Traversable object as an argument' + ); + } + + // instantiate the adapter + if (!isset($config['adapter'])) { + throw new Exception\InvalidArgumentException('Missing "adapter"'); + } + $adapterName = $config['adapter']; + $adapterOptions = []; + if (is_array($config['adapter'])) { + if (!isset($config['adapter']['name'])) { + throw new Exception\InvalidArgumentException('Missing "adapter.name"'); + } + + $adapterName = $config['adapter']['name']; + $adapterOptions = isset($config['adapter']['options']) ? $config['adapter']['options'] : []; + } + if (isset($config['options'])) { + $adapterOptions = array_merge($adapterOptions, $config['options']); + } + + $pluginConfiguration = []; + + // add plugins + if (isset($config['plugins'])) { + if (!is_array($config['plugins'])) { + throw new Exception\InvalidArgumentException( + 'Plugins needs to be an array' + ); + } + + $pluginConfiguration = $config['plugins']; + } + + return $this->create($adapterName, $adapterOptions, $pluginConfiguration); + } + + /** + * Creates a storage adapter and attaches plugins if needed. + * + * @param string $adapterName + * @param array $options + * @param array $pluginConfiguration + * + * @return StorageInterface + */ + public function create($adapterName, array $options = [], array $pluginConfiguration = []) + { + $adapter = $this->adapters->get($adapterName, $options); + + if (empty($pluginConfiguration)) { + return $adapter; + } + + if (!$adapter instanceof EventsCapableInterface) { + throw new Exception\RuntimeException(sprintf( + "The adapter '%s' doesn't implement '%s' and therefore can't handle plugins", + get_class($adapter), + EventsCapableInterface::class + )); + } + + foreach ($pluginConfiguration as $k => $v) { + $pluginPrio = 1; // default priority + + if (is_string($k)) { + if (!is_array($v)) { + throw new Exception\InvalidArgumentException( + "'plugins.{$k}' needs to be an array" + ); + } + $pluginName = $k; + $pluginOptions = $v; + } elseif (is_array($v)) { + if (!isset($v['name'])) { + throw new Exception\InvalidArgumentException( + "Invalid plugins[{$k}] or missing plugins[{$k}].name" + ); + } + $pluginName = (string) $v['name']; + + if (isset($v['options'])) { + $pluginOptions = $v['options']; + } else { + $pluginOptions = []; + } + + if (isset($v['priority'])) { + $pluginPrio = $v['priority']; + } + } else { + $pluginName = $v; + $pluginOptions = []; + } + + $plugin = $this->plugins->get($pluginName, $pluginOptions); + if (!$adapter->hasPlugin($plugin)) { + $adapter->addPlugin($plugin, $pluginPrio); + } + + return $adapter; + } + } +} diff --git a/src/StorageFactory.php b/src/StorageFactory.php index 64fa6c268..c26ac517d 100644 --- a/src/StorageFactory.php +++ b/src/StorageFactory.php @@ -10,10 +10,11 @@ namespace Zend\Cache; use Traversable; -use Zend\EventManager\EventsCapableInterface; -use Zend\Stdlib\ArrayUtils; use Zend\ServiceManager\ServiceManager; +/** + * @deprecated static factories are deprecated as of zendframework 2.9 and may be removed in future major versions. + */ abstract class StorageFactory { /** @@ -37,97 +38,18 @@ abstract class StorageFactory * @param array|Traversable $cfg * @return Storage\StorageInterface * @throws Exception\InvalidArgumentException + * @deprecated static factories are deprecated as of zendframework 2.9 and may be removed in future major versions. */ public static function factory($cfg) { - if ($cfg instanceof Traversable) { - $cfg = ArrayUtils::iteratorToArray($cfg); - } - - if (! is_array($cfg)) { - throw new Exception\InvalidArgumentException( - 'The factory needs an associative array ' - . 'or a Traversable object as an argument' - ); - } - - // instantiate the adapter - if (! isset($cfg['adapter'])) { - throw new Exception\InvalidArgumentException('Missing "adapter"'); - } - $adapterName = $cfg['adapter']; - $adapterOptions = []; - if (is_array($cfg['adapter'])) { - if (! isset($cfg['adapter']['name'])) { - throw new Exception\InvalidArgumentException('Missing "adapter.name"'); - } - - $adapterName = $cfg['adapter']['name']; - $adapterOptions = isset($cfg['adapter']['options']) ? $cfg['adapter']['options'] : []; - } - if (isset($cfg['options'])) { - $adapterOptions = array_merge($adapterOptions, $cfg['options']); - } - - $adapter = static::adapterFactory((string) $adapterName, $adapterOptions); - - // add plugins - if (isset($cfg['plugins'])) { - if (! $adapter instanceof EventsCapableInterface) { - throw new Exception\RuntimeException(sprintf( - "The adapter '%s' doesn't implement '%s' and therefore can't handle plugins", - get_class($adapter), - 'Zend\EventManager\EventsCapableInterface' - )); - } - - if (! is_array($cfg['plugins'])) { - throw new Exception\InvalidArgumentException( - 'Plugins needs to be an array' - ); - } - - foreach ($cfg['plugins'] as $k => $v) { - $pluginPrio = 1; // default priority - - if (is_string($k)) { - if (! is_array($v)) { - throw new Exception\InvalidArgumentException( - "'plugins.{$k}' needs to be an array" - ); - } - $pluginName = $k; - $pluginOptions = $v; - } elseif (is_array($v)) { - if (! isset($v['name'])) { - throw new Exception\InvalidArgumentException( - "Invalid plugins[{$k}] or missing plugins[{$k}].name" - ); - } - $pluginName = (string) $v['name']; - - if (isset($v['options'])) { - $pluginOptions = $v['options']; - } else { - $pluginOptions = []; - } - - if (isset($v['priority'])) { - $pluginPrio = $v['priority']; - } - } else { - $pluginName = $v; - $pluginOptions = []; - } - - $plugin = static::pluginFactory($pluginName, $pluginOptions); - if (! $adapter->hasPlugin($plugin)) { - $adapter->addPlugin($plugin, $pluginPrio); - } - } - } - - return $adapter; + trigger_error(sprintf( + '%s is deprecated; please use %s::createFromCachesConfig instead', + __METHOD__, + Storage\StorageFactory::class + ), E_USER_DEPRECATED); + + return (new Storage\StorageFactory(static::getAdapterPluginManager(), static::getPluginManager())) + ->createFromCachesConfig($cfg); } /** @@ -137,13 +59,19 @@ public static function factory($cfg) * @param array|Traversable|Storage\Adapter\AdapterOptions $options * @return Storage\StorageInterface * @throws Exception\RuntimeException + * @deprecated static factories are deprecated as of zendframework 2.9 and may be removed in future major versions. */ public static function adapterFactory($adapterName, $options = []) { - if ($adapterName instanceof Storage\StorageInterface) { - // $adapterName is already an adapter object - $adapter = $adapterName; - } else { + trigger_error(sprintf( + '%s is deprecated; please use %s::get instead', + __METHOD__, + Storage\AdapterPluginManager::class + ), E_USER_DEPRECATED); + + $adapter = $adapterName; + + if (!$adapterName instanceof Storage\StorageInterface) { $adapter = static::getAdapterPluginManager()->get($adapterName); } @@ -158,9 +86,15 @@ public static function adapterFactory($adapterName, $options = []) * Get the adapter plugin manager * * @return Storage\AdapterPluginManager + * @deprecated static factories are deprecated as of zendframework 2.9 and may be removed in future major versions. */ public static function getAdapterPluginManager() { + trigger_error(sprintf( + '%s is deprecated', + __METHOD__ + ), E_USER_DEPRECATED); + if (static::$adapters === null) { static::$adapters = new Storage\AdapterPluginManager(new ServiceManager); } @@ -172,9 +106,15 @@ public static function getAdapterPluginManager() * * @param Storage\AdapterPluginManager $adapters * @return void + * @deprecated static factories are deprecated as of zendframework 2.9 and may be removed in future major versions. */ public static function setAdapterPluginManager(Storage\AdapterPluginManager $adapters) { + trigger_error(sprintf( + '%s is deprecated', + __METHOD__ + ), E_USER_DEPRECATED); + static::$adapters = $adapters; } @@ -182,9 +122,15 @@ public static function setAdapterPluginManager(Storage\AdapterPluginManager $ada * Resets the internal adapter plugin manager * * @return void + * @deprecated static factories are deprecated as of zendframework 2.9 and may be removed in future major versions. */ public static function resetAdapterPluginManager() { + trigger_error(sprintf( + '%s is deprecated', + __METHOD__ + ), E_USER_DEPRECATED); + static::$adapters = null; } @@ -195,14 +141,19 @@ public static function resetAdapterPluginManager() * @param array|Traversable|Storage\Plugin\PluginOptions $options * @return Storage\Plugin\PluginInterface * @throws Exception\RuntimeException + * @deprecated static factories are deprecated as of zendframework 2.9 and may be removed in future major versions. */ public static function pluginFactory($pluginName, $options = []) { - if ($pluginName instanceof Storage\Plugin\PluginInterface) { - // $pluginName is already a plugin object - $plugin = $pluginName; - } else { - $plugin = static::getPluginManager()->get($pluginName); + trigger_error(sprintf( + '%s is deprecated; please use %s::get instead', + __METHOD__, + Storage\PluginManager::class + ), E_USER_DEPRECATED); + + $plugin = $pluginName; + if (!$pluginName instanceof Storage\Plugin\PluginInterface) { + $plugin = self::getPluginManager()->get($pluginName); } if ($options) { @@ -219,9 +170,15 @@ public static function pluginFactory($pluginName, $options = []) * Get the plugin manager * * @return Storage\PluginManager + * @deprecated static factories are deprecated as of zendframework 2.9 and may be removed in future major versions. */ public static function getPluginManager() { + trigger_error(sprintf( + '%s is deprecated', + __METHOD__ + ), E_USER_DEPRECATED); + if (static::$plugins === null) { static::$plugins = new Storage\PluginManager(new ServiceManager); } @@ -233,9 +190,15 @@ public static function getPluginManager() * * @param Storage\PluginManager $plugins * @return void + * @deprecated static factories are deprecated as of zendframework 2.9 and may be removed in future major versions. */ public static function setPluginManager(Storage\PluginManager $plugins) { + trigger_error(sprintf( + '%s is deprecated', + __METHOD__ + ), E_USER_DEPRECATED); + static::$plugins = $plugins; } @@ -243,9 +206,15 @@ public static function setPluginManager(Storage\PluginManager $plugins) * Resets the internal plugin manager * * @return void + * @deprecated static factories are deprecated as of zendframework 2.9 and may be removed in future major versions. */ public static function resetPluginManager() { + trigger_error(sprintf( + '%s is deprecated', + __METHOD__ + ), E_USER_DEPRECATED); + static::$plugins = null; } } diff --git a/test/PatternFactoryTest.php b/test/PatternFactoryTest.php index bce920520..d6e962304 100644 --- a/test/PatternFactoryTest.php +++ b/test/PatternFactoryTest.php @@ -9,8 +9,11 @@ namespace ZendTest\Cache; +use const E_USER_DEPRECATED; +use ErrorException; use PHPUnit\Framework\TestCase; use Zend\Cache; +use Zend\Stdlib\ErrorHandler; /** * @group Zend_Cache @@ -20,12 +23,14 @@ class PatternFactoryTest extends TestCase { public function setUp() { + ErrorHandler::start(E_USER_DEPRECATED); Cache\PatternFactory::resetPluginManager(); } public function tearDown() { Cache\PatternFactory::resetPluginManager(); + ErrorHandler::clean(); } public function testDefaultPluginManager() @@ -53,4 +58,13 @@ public function testFactory() $this->assertNotSame($pattern1, $pattern2); } + + public function testWillTriggerDeprecationError() + { + Cache\PatternFactory::factory('capture'); + $error = ErrorHandler::stop(); + + $this->assertInstanceOf(ErrorException::class, $error); + $this->assertSame(E_USER_DEPRECATED, $error->getSeverity()); + } } diff --git a/test/Service/StorageCacheAbstractServiceFactoryTest.php b/test/Service/StorageCacheAbstractServiceFactoryTest.php index 923bd5c25..9e4487bc8 100644 --- a/test/Service/StorageCacheAbstractServiceFactoryTest.php +++ b/test/Service/StorageCacheAbstractServiceFactoryTest.php @@ -13,6 +13,7 @@ use PHPUnit\Framework\TestCase; use Prophecy\Argument; use Zend\Cache\Service\StorageCacheAbstractServiceFactory; +use Zend\Cache\Storage\Plugin\Serializer; use Zend\Cache\StorageFactory; use Zend\Cache\Storage\AdapterPluginManager; use Zend\Cache\Storage\Adapter\AbstractAdapter; @@ -22,6 +23,7 @@ use Zend\Cache\Storage\StorageInterface; use Zend\ServiceManager\Config; use Zend\ServiceManager\ServiceManager; +use Zend\Stdlib\ErrorHandler; /** * @covers Zend\Cache\StorageFactory @@ -32,8 +34,10 @@ class StorageCacheAbstractServiceFactoryTest extends TestCase public function setUp() { + ErrorHandler::start(E_USER_DEPRECATED); StorageFactory::resetAdapterPluginManager(); StorageFactory::resetPluginManager(); + $this->sm = new ServiceManager(); $config = [ 'services' => [ 'config' => [ @@ -51,9 +55,8 @@ public function setUp() ], 'abstract_factories' => [ StorageCacheAbstractServiceFactory::class - ] + ], ]; - $this->sm = new ServiceManager(); (new Config($config))->configureServiceManager($this->sm); } @@ -61,6 +64,7 @@ public function tearDown() { StorageFactory::resetAdapterPluginManager(); StorageFactory::resetPluginManager(); + ErrorHandler::clean(); } public function testCanLookupCacheByName() @@ -94,7 +98,7 @@ public function testSetsFactoryAdapterPluginManagerInstanceOnInvocation() $adapter->addPlugin(Argument::any(), Argument::any())->shouldNotBeCalled(); $adapterPluginManager = $this->prophesize(AdapterPluginManager::class); - $adapterPluginManager->get('Memory')->willReturn($adapter->reveal()); + $adapterPluginManager->get('Memory', [])->willReturn($adapter->reveal()); $container = $this->prophesize(ContainerInterface::class); $container->has(AdapterPluginManager::class)->willReturn(true); @@ -117,7 +121,7 @@ public function testSetsFactoryPluginManagerInstanceOnInvocation() $plugin->setOptions(Argument::any())->shouldNotBeCalled(); $pluginManager = $this->prophesize(PluginManager::class); - $pluginManager->get('Serializer')->willReturn($plugin->reveal()); + $pluginManager->get('Serializer', [])->willReturn($plugin->reveal()); $adapter = $this->prophesize(AbstractAdapter::class); $adapter->willImplement(StorageInterface::class); @@ -126,7 +130,7 @@ public function testSetsFactoryPluginManagerInstanceOnInvocation() $adapter->addPlugin($plugin->reveal(), Argument::any())->shouldBeCalled(); $adapterPluginManager = $this->prophesize(AdapterPluginManager::class); - $adapterPluginManager->get('Memory')->willReturn($adapter->reveal()); + $adapterPluginManager->get('Memory', [])->willReturn($adapter->reveal()); $container = $this->prophesize(ContainerInterface::class); $container->has(AdapterPluginManager::class)->willReturn(true); @@ -143,6 +147,7 @@ public function testSetsFactoryPluginManagerInstanceOnInvocation() ]); $factory = new StorageCacheAbstractServiceFactory(); + /** @var Memory $storage */ $factory($container->reveal(), 'Cache'); $this->assertSame($pluginManager->reveal(), StorageFactory::getPluginManager()); } diff --git a/test/Service/StorageCacheFactoryTest.php b/test/Service/StorageCacheFactoryTest.php index 4d489238b..ddaaa67ba 100644 --- a/test/Service/StorageCacheFactoryTest.php +++ b/test/Service/StorageCacheFactoryTest.php @@ -22,6 +22,7 @@ use Zend\Cache\Storage\StorageInterface; use Zend\ServiceManager\Config; use Zend\ServiceManager\ServiceManager; +use Zend\Stdlib\ErrorHandler; /** * @covers Zend\Cache\Service\StorageCacheFactory @@ -32,6 +33,7 @@ class StorageCacheFactoryTest extends TestCase public function setUp() { + ErrorHandler::start(E_USER_DEPRECATED); StorageFactory::resetAdapterPluginManager(); StorageFactory::resetPluginManager(); $config = [ @@ -55,6 +57,7 @@ public function tearDown() { StorageFactory::resetAdapterPluginManager(); StorageFactory::resetPluginManager(); + ErrorHandler::clean(); } public function testCreateServiceCache() @@ -72,7 +75,7 @@ public function testSetsFactoryAdapterPluginManagerInstanceOnInvocation() $adapter->addPlugin(Argument::any(), Argument::any())->shouldNotBeCalled(); $adapterPluginManager = $this->prophesize(AdapterPluginManager::class); - $adapterPluginManager->get('Memory')->willReturn($adapter->reveal()); + $adapterPluginManager->get('Memory', [])->willReturn($adapter->reveal()); $container = $this->prophesize(ContainerInterface::class); $container->has(AdapterPluginManager::class)->willReturn(true); @@ -95,7 +98,7 @@ public function testSetsFactoryPluginManagerInstanceOnInvocation() $plugin->setOptions(Argument::any())->shouldNotBeCalled(); $pluginManager = $this->prophesize(PluginManager::class); - $pluginManager->get('Serializer')->willReturn($plugin->reveal()); + $pluginManager->get('Serializer', [])->willReturn($plugin->reveal()); $adapter = $this->prophesize(AbstractAdapter::class); $adapter->willImplement(StorageInterface::class); @@ -104,7 +107,7 @@ public function testSetsFactoryPluginManagerInstanceOnInvocation() $adapter->addPlugin($plugin->reveal(), Argument::any())->shouldBeCalled(); $adapterPluginManager = $this->prophesize(AdapterPluginManager::class); - $adapterPluginManager->get('Memory')->willReturn($adapter->reveal()); + $adapterPluginManager->get('Memory', [])->willReturn($adapter->reveal()); $container = $this->prophesize(ContainerInterface::class); $container->has(AdapterPluginManager::class)->willReturn(true); diff --git a/test/Service/StorageFactoryFactoryTest.php b/test/Service/StorageFactoryFactoryTest.php new file mode 100644 index 000000000..92cc77578 --- /dev/null +++ b/test/Service/StorageFactoryFactoryTest.php @@ -0,0 +1,66 @@ +factory = new StorageFactoryFactory(); + } + + + public function testFactoryCreatesServiceV2() + { + if (method_exists(new ServiceManager(), 'configure')) { + $this->markTestSkipped('Test is only needed in ZF2'); + } + + $adapterPluginManager = $this->prophesize(AdapterPluginManager::class); + $pluginManager = $this->prophesize(PluginManager::class); + + $container = $this->prophesize(ServiceLocatorInterface::class); + $container->get(AdapterPluginManager::class)->willReturn($adapterPluginManager); + + $container->get(PluginManager::class)->willReturn($pluginManager); + + $instance = $this->factory->createService($container->reveal()); + $this->assertInstanceOf(StorageFactory::class, $instance); + } + + public function testFactoryCreatesServiceV3() + { + if (!method_exists(new ServiceManager(), 'configure')) { + $this->markTestSkipped('Test is only needed in ZF3'); + } + + $adapterPluginManager = $this->prophesize(AdapterPluginManager::class); + $pluginManager = $this->prophesize(PluginManager::class); + + $container = $this->prophesize(ContainerInterface::class); + $container->get(AdapterPluginManager::class)->willReturn($adapterPluginManager); + + $container->get(PluginManager::class)->willReturn($pluginManager); + + $instance = $this->factory->__invoke($container->reveal(), StorageFactory::class); + + $this->assertInstanceOf(StorageFactory::class, $instance); + } +} diff --git a/test/Storage/AdapterPluginManagerTest.php b/test/Storage/AdapterPluginManagerTest.php index 02da8e2b3..08310963e 100644 --- a/test/Storage/AdapterPluginManagerTest.php +++ b/test/Storage/AdapterPluginManagerTest.php @@ -54,4 +54,18 @@ protected function getInstanceOf() { return StorageInterface::class; } + + public function testOptionsWillBeSet() + { + $options = [ + 'readable' => false, + 'ttl' => 9999, + 'namespace' => 'test', + ]; + + $storage = $this->getPluginManager()->get('Memory', $options); + + $adapterOptions = $storage->getOptions(); + $this->assertArraySubset($options, $adapterOptions->toArray()); + } } diff --git a/test/StorageFactoryTest.php b/test/StorageFactoryTest.php index 95d7a5240..e89a964cb 100644 --- a/test/StorageFactoryTest.php +++ b/test/StorageFactoryTest.php @@ -9,18 +9,23 @@ namespace ZendTest\Cache; +use const E_USER_DEPRECATED; +use ErrorException; use PHPUnit\Framework\TestCase; use Zend\Cache; use Zend\ServiceManager\ServiceManager; +use Zend\Stdlib\ErrorHandler; /** * @group Zend_Cache * @covers Zend\Cache\StorageFactory + * @covers Zend\Cache\Storage\StorageFactory */ class StorageFactoryTest extends TestCase { public function setUp() { + ErrorHandler::start(E_USER_DEPRECATED); Cache\StorageFactory::resetAdapterPluginManager(); Cache\StorageFactory::resetPluginManager(); } @@ -29,6 +34,7 @@ public function tearDown() { Cache\StorageFactory::resetAdapterPluginManager(); Cache\StorageFactory::resetPluginManager(); + ErrorHandler::clean(); } public function testDefaultAdapterPluginManager() @@ -206,4 +212,14 @@ public function testFactoryWithPluginsAndOptionsArray() } } } + + public function testWillTriggerUserDeprecatedOnUsage() + { + Cache\StorageFactory::factory([ + 'adapter' => 'Memory', + ]); + $error = ErrorHandler::stop(); + $this->assertInstanceOf(ErrorException::class, $error); + $this->assertSame(E_USER_DEPRECATED, $error->getSeverity()); + } } From 0ddade2b8e503ae6332af20dd77e282312d9d4ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= Date: Mon, 10 Sep 2018 16:41:08 +0200 Subject: [PATCH 06/11] Added missing file headers --- .../AdapterPluginManager/AdapterPluginManagerTrait.php | 8 +++++++- .../AdapterPluginManagerV2Polyfill.php | 8 +++++++- .../AdapterPluginManagerV3Polyfill.php | 8 +++++++- stubs/AdapterPluginManager.php | 8 +++++++- stubs/PatternPluginManager.php | 8 +++++++- 5 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/Storage/AdapterPluginManager/AdapterPluginManagerTrait.php b/src/Storage/AdapterPluginManager/AdapterPluginManagerTrait.php index c9f921fcf..830e91709 100644 --- a/src/Storage/AdapterPluginManager/AdapterPluginManagerTrait.php +++ b/src/Storage/AdapterPluginManager/AdapterPluginManagerTrait.php @@ -1,5 +1,11 @@ Date: Mon, 10 Sep 2018 17:20:33 +0200 Subject: [PATCH 07/11] Fixed unit test after added `E_USER_DEPRECATED` error --- benchmark/FilesystemStorageAdapterBench.php | 3 +++ benchmark/MemoryStorageAdapterBench.php | 3 +++ phpunit.xml.dist | 2 ++ test/PatternFactoryTest.php | 10 ---------- test/Psr/CacheItemPool/ApcIntegrationTest.php | 8 ++++++-- test/Psr/CacheItemPool/ApcuIntegrationTest.php | 8 ++++++-- .../Psr/CacheItemPool/BlackHoleIntegrationTest.php | 13 +++++++++++++ test/Psr/CacheItemPool/CacheItemTest.php | 4 ++-- test/Psr/CacheItemPool/DbaIntegrationTest.php | 13 +++++++++++++ .../CacheItemPool/ExtMongoDbIntegrationTest.php | 9 ++++++--- .../CacheItemPool/FilesystemIntegrationTest.php | 13 +++++++++++++ test/Psr/CacheItemPool/MemcacheIntegrationTest.php | 9 +++++++-- .../Psr/CacheItemPool/MemcachedIntegrationTest.php | 8 ++++++-- test/Psr/CacheItemPool/MemoryIntegrationTest.php | 14 ++++++++++++++ test/Psr/CacheItemPool/MongoDbIntegrationTest.php | 9 +++++++-- test/Psr/CacheItemPool/RedisIntegrationTest.php | 9 +++++++-- test/Psr/CacheItemPool/SessionIntegrationTest.php | 13 +++++++++++++ test/Psr/CacheItemPool/WinCacheIntegrationTest.php | 9 +++++++-- test/Psr/CacheItemPool/XCacheIntegrationTest.php | 12 ++++++++++++ .../ZendServerDiskIntegrationTest.php | 9 +++++++-- .../CacheItemPool/ZendServerShmIntegrationTest.php | 9 +++++++-- test/Psr/SimpleCache/ApcIntegrationTest.php | 9 +++++++-- test/Psr/SimpleCache/ApcuIntegrationTest.php | 8 ++++++-- test/Psr/SimpleCache/ExtMongoDbIntegrationTest.php | 9 +++++++-- test/Psr/SimpleCache/MemcacheIntegrationTest.php | 9 +++++++-- test/Psr/SimpleCache/MemcachedIntegrationTest.php | 9 +++++++-- test/Psr/SimpleCache/MemoryIntegrationTest.php | 10 ++++++++++ test/Psr/SimpleCache/MongoDbIntegrationTest.php | 9 +++++++-- test/Psr/SimpleCache/RedisIntegrationTest.php | 9 +++++++-- test/Psr/SimpleCache/SessionIntegrationTest.php | 5 +++++ test/Psr/SimpleCache/WinCacheIntegrationTest.php | 9 +++++++-- test/Psr/SimpleCache/XCacheIntegrationTest.php | 9 +++++++++ .../SimpleCache/ZendServerDiskIntegrationTest.php | 8 ++++++-- .../SimpleCache/ZendServerShmIntegrationTest.php | 7 +++++-- test/Storage/Adapter/BlackHoleTest.php | 10 +++++++++- test/StorageFactoryTest.php | 11 ----------- 36 files changed, 253 insertions(+), 65 deletions(-) diff --git a/benchmark/FilesystemStorageAdapterBench.php b/benchmark/FilesystemStorageAdapterBench.php index 6eb5f786f..5ef6c2913 100644 --- a/benchmark/FilesystemStorageAdapterBench.php +++ b/benchmark/FilesystemStorageAdapterBench.php @@ -3,6 +3,7 @@ namespace ZendBench\Cache; use Zend\Cache\StorageFactory; +use Zend\Stdlib\ErrorHandler; /** * @Revs(100) @@ -27,9 +28,11 @@ public function __construct() $this->fail("Can't create temporary cache directory: {$err['message']}"); } + ErrorHandler::start(E_USER_DEPRECATED); $this->storage = StorageFactory::adapterFactory('filesystem', [ 'cache_dir' => $this->tmpCacheDir, ]); + ErrorHandler::clean(); parent::__construct(); } diff --git a/benchmark/MemoryStorageAdapterBench.php b/benchmark/MemoryStorageAdapterBench.php index 957e9369f..eefcdc958 100644 --- a/benchmark/MemoryStorageAdapterBench.php +++ b/benchmark/MemoryStorageAdapterBench.php @@ -3,6 +3,7 @@ namespace ZendBench\Cache; use Zend\Cache\StorageFactory; +use Zend\Stdlib\ErrorHandler; /** * @Revs(100) @@ -13,8 +14,10 @@ class MemoryStorageAdapterBench extends CommonStorageAdapterBench { public function __construct() { + ErrorHandler::start(E_USER_DEPRECATED); // instantiate the storage adapter $this->storage = StorageFactory::adapterFactory('memory'); + ErrorHandler::clean(); parent::__construct(); } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 98938de96..2d6949788 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -55,5 +55,7 @@ + + diff --git a/test/PatternFactoryTest.php b/test/PatternFactoryTest.php index d6e962304..5aeff09c5 100644 --- a/test/PatternFactoryTest.php +++ b/test/PatternFactoryTest.php @@ -10,7 +10,6 @@ namespace ZendTest\Cache; use const E_USER_DEPRECATED; -use ErrorException; use PHPUnit\Framework\TestCase; use Zend\Cache; use Zend\Stdlib\ErrorHandler; @@ -58,13 +57,4 @@ public function testFactory() $this->assertNotSame($pattern1, $pattern2); } - - public function testWillTriggerDeprecationError() - { - Cache\PatternFactory::factory('capture'); - $error = ErrorHandler::stop(); - - $this->assertInstanceOf(ErrorException::class, $error); - $this->assertSame(E_USER_DEPRECATED, $error->getSeverity()); - } } diff --git a/test/Psr/CacheItemPool/ApcIntegrationTest.php b/test/Psr/CacheItemPool/ApcIntegrationTest.php index a5b384fd7..adba9c1f4 100644 --- a/test/Psr/CacheItemPool/ApcIntegrationTest.php +++ b/test/Psr/CacheItemPool/ApcIntegrationTest.php @@ -12,6 +12,7 @@ use Zend\Cache\StorageFactory; use Zend\Cache\Exception; use Zend\ServiceManager\Exception\ServiceNotCreatedException; +use Zend\Stdlib\ErrorHandler; /** * @requires extension apcu @@ -22,7 +23,7 @@ class ApcIntegrationTest extends CachePoolTest * Backup default timezone * @var string */ - private $tz; + private $tz = 'UTC'; /** * Restore 'apc.use_request_time' @@ -33,12 +34,13 @@ class ApcIntegrationTest extends CachePoolTest protected function setUp() { + ErrorHandler::start(E_USER_DEPRECATED); if (! getenv('TESTS_ZEND_CACHE_APC_ENABLED')) { $this->markTestSkipped('Enable TESTS_ZEND_CACHE_APC_ENABLED to run this test'); } // set non-UTC timezone - $this->tz = date_default_timezone_get(); + $this->tz = date_default_timezone_get() ?: 'UTC'; date_default_timezone_set('America/Vancouver'); // needed on test expirations @@ -59,6 +61,8 @@ protected function tearDown() // reset ini configurations ini_set('apc.use_request_time', $this->iniUseRequestTime); + + ErrorHandler::clean(); parent::tearDown(); } diff --git a/test/Psr/CacheItemPool/ApcuIntegrationTest.php b/test/Psr/CacheItemPool/ApcuIntegrationTest.php index def88d1ef..e6ec76bdb 100644 --- a/test/Psr/CacheItemPool/ApcuIntegrationTest.php +++ b/test/Psr/CacheItemPool/ApcuIntegrationTest.php @@ -12,6 +12,7 @@ use Zend\Cache\StorageFactory; use Zend\Cache\Exception; use Zend\ServiceManager\Exception\ServiceNotCreatedException; +use Zend\Stdlib\ErrorHandler; /** * @requires extension apcu @@ -22,7 +23,7 @@ class ApcuIntegrationTest extends CachePoolTest * Backup default timezone * @var string */ - private $tz; + private $tz = 'UTC'; /** * Restore 'apc.use_request_time' @@ -38,13 +39,14 @@ protected function setUp() } // set non-UTC timezone - $this->tz = date_default_timezone_get(); + $this->tz = date_default_timezone_get() ?: 'UTC'; date_default_timezone_set('America/Vancouver'); // needed on test expirations $this->iniUseRequestTime = ini_get('apc.use_request_time'); ini_set('apc.use_request_time', 0); + ErrorHandler::start(E_USER_DEPRECATED); parent::setUp(); } @@ -59,6 +61,8 @@ protected function tearDown() // reset ini configurations ini_set('apc.use_request_time', $this->iniUseRequestTime); + ErrorHandler::clean(); + parent::tearDown(); } diff --git a/test/Psr/CacheItemPool/BlackHoleIntegrationTest.php b/test/Psr/CacheItemPool/BlackHoleIntegrationTest.php index 18b1bbc7f..df6eb1a86 100644 --- a/test/Psr/CacheItemPool/BlackHoleIntegrationTest.php +++ b/test/Psr/CacheItemPool/BlackHoleIntegrationTest.php @@ -10,9 +10,22 @@ use PHPUnit\Framework\TestCase; use Zend\Cache\Psr\CacheItemPool\CacheItemPoolDecorator; use Zend\Cache\StorageFactory; +use Zend\Stdlib\ErrorHandler; class BlackHoleIntegrationTest extends TestCase { + protected function setUp() + { + ErrorHandler::start(E_USER_DEPRECATED); + parent::setUp(); + } + + protected function tearDown() + { + ErrorHandler::clean(); + parent::tearDown(); + } + /** * @expectedException \Zend\Cache\Psr\CacheItemPool\CacheException */ diff --git a/test/Psr/CacheItemPool/CacheItemTest.php b/test/Psr/CacheItemPool/CacheItemTest.php index 857c4afcb..44a09a833 100644 --- a/test/Psr/CacheItemPool/CacheItemTest.php +++ b/test/Psr/CacheItemPool/CacheItemTest.php @@ -14,12 +14,12 @@ class CacheItemTest extends TestCase { - private $tz; + private $tz = 'UTC'; protected function setUp() { // set non-UTC timezone - $this->tz = date_default_timezone_get(); + $this->tz = date_default_timezone_get() ?: 'UTC'; date_default_timezone_set('America/Vancouver'); } diff --git a/test/Psr/CacheItemPool/DbaIntegrationTest.php b/test/Psr/CacheItemPool/DbaIntegrationTest.php index fc529e1cb..41957b326 100644 --- a/test/Psr/CacheItemPool/DbaIntegrationTest.php +++ b/test/Psr/CacheItemPool/DbaIntegrationTest.php @@ -12,9 +12,22 @@ use Zend\Cache\StorageFactory; use Zend\Cache\Exception; use Zend\ServiceManager\Exception\ServiceNotCreatedException; +use Zend\Stdlib\ErrorHandler; class DbaIntegrationTest extends TestCase { + protected function setUp() + { + ErrorHandler::start(E_USER_DEPRECATED); + parent::setUp(); + } + + protected function tearDown() + { + ErrorHandler::clean(); + parent::tearDown(); + } + /** * The DBA adapter doesn't support TTL * diff --git a/test/Psr/CacheItemPool/ExtMongoDbIntegrationTest.php b/test/Psr/CacheItemPool/ExtMongoDbIntegrationTest.php index 3df4d81a1..5867c8cd2 100644 --- a/test/Psr/CacheItemPool/ExtMongoDbIntegrationTest.php +++ b/test/Psr/CacheItemPool/ExtMongoDbIntegrationTest.php @@ -14,6 +14,7 @@ use Zend\Cache\StorageFactory; use Zend\Cache\Exception; use Zend\ServiceManager\Exception\ServiceNotCreatedException; +use Zend\Stdlib\ErrorHandler; class ExtMongoDbIntegrationTest extends CachePoolTest { @@ -21,7 +22,7 @@ class ExtMongoDbIntegrationTest extends CachePoolTest * Backup default timezone * @var string */ - private $tz; + private $tz = 'UTC'; /** * @var ExtMongoDb @@ -30,6 +31,8 @@ class ExtMongoDbIntegrationTest extends CachePoolTest protected function setUp() { + ErrorHandler::start(E_USER_DEPRECATED); + if (! getenv('TESTS_ZEND_CACHE_EXTMONGODB_ENABLED')) { $this->markTestSkipped('Enable TESTS_ZEND_CACHE_EXTMONGODB_ENABLED to run this test'); } @@ -39,7 +42,7 @@ protected function setUp() } // set non-UTC timezone - $this->tz = date_default_timezone_get(); + $this->tz = date_default_timezone_get() ?: 'UTC'; date_default_timezone_set('America/Vancouver'); parent::setUp(); @@ -52,7 +55,7 @@ protected function tearDown() if ($this->storage) { $this->storage->flush(); } - + ErrorHandler::clean(); parent::tearDown(); } diff --git a/test/Psr/CacheItemPool/FilesystemIntegrationTest.php b/test/Psr/CacheItemPool/FilesystemIntegrationTest.php index ea641b71b..ab4500f70 100644 --- a/test/Psr/CacheItemPool/FilesystemIntegrationTest.php +++ b/test/Psr/CacheItemPool/FilesystemIntegrationTest.php @@ -11,9 +11,22 @@ use Zend\Cache\Psr\CacheItemPool\CacheItemPoolDecorator; use Zend\Cache\Storage\Plugin\Serializer; use Zend\Cache\StorageFactory; +use Zend\Stdlib\ErrorHandler; class FilesystemIntegrationTest extends TestCase { + protected function setUp() + { + ErrorHandler::start(E_USER_DEPRECATED); + parent::setUp(); + } + + protected function tearDown() + { + ErrorHandler::clean(); + parent::tearDown(); + } + /** * @expectedException \Zend\Cache\Psr\CacheItemPool\CacheException */ diff --git a/test/Psr/CacheItemPool/MemcacheIntegrationTest.php b/test/Psr/CacheItemPool/MemcacheIntegrationTest.php index ed54bf675..0807c6529 100644 --- a/test/Psr/CacheItemPool/MemcacheIntegrationTest.php +++ b/test/Psr/CacheItemPool/MemcacheIntegrationTest.php @@ -14,6 +14,7 @@ use Zend\Cache\StorageFactory; use Zend\Cache\Exception; use Zend\ServiceManager\Exception\ServiceNotCreatedException; +use Zend\Stdlib\ErrorHandler; /** * @require extension memcache @@ -24,7 +25,7 @@ class MemcacheIntegrationTest extends CachePoolTest * Backup default timezone * @var string */ - private $tz; + private $tz = 'UTC'; /** * @var Memcache @@ -38,9 +39,11 @@ protected function setUp() } // set non-UTC timezone - $this->tz = date_default_timezone_get(); + $this->tz = date_default_timezone_get() ?: 'UTC'; date_default_timezone_set('America/Vancouver'); + ErrorHandler::start(E_USER_DEPRECATED); + parent::setUp(); } @@ -52,6 +55,8 @@ protected function tearDown() $this->storage->flush(); } + ErrorHandler::clean(); + parent::tearDown(); } diff --git a/test/Psr/CacheItemPool/MemcachedIntegrationTest.php b/test/Psr/CacheItemPool/MemcachedIntegrationTest.php index d58264cb0..fad4d3634 100644 --- a/test/Psr/CacheItemPool/MemcachedIntegrationTest.php +++ b/test/Psr/CacheItemPool/MemcachedIntegrationTest.php @@ -13,6 +13,7 @@ use Zend\Cache\StorageFactory; use Zend\Cache\Exception; use Zend\ServiceManager\Exception\ServiceNotCreatedException; +use Zend\Stdlib\ErrorHandler; /** * @require extension memcached @@ -23,7 +24,7 @@ class MemcachedIntegrationTest extends CachePoolTest * Backup default timezone * @var string */ - private $tz; + private $tz = 'UTC'; /** * @var Memcached @@ -37,9 +38,10 @@ protected function setUp() } // set non-UTC timezone - $this->tz = date_default_timezone_get(); + $this->tz = date_default_timezone_get() ?: 'UTC'; date_default_timezone_set('America/Vancouver'); + ErrorHandler::start(E_USER_DEPRECATED); parent::setUp(); } @@ -51,6 +53,8 @@ protected function tearDown() $this->storage->flush(); } + ErrorHandler::clean(); + parent::tearDown(); } diff --git a/test/Psr/CacheItemPool/MemoryIntegrationTest.php b/test/Psr/CacheItemPool/MemoryIntegrationTest.php index 7c39b04c9..7c4ebea0e 100644 --- a/test/Psr/CacheItemPool/MemoryIntegrationTest.php +++ b/test/Psr/CacheItemPool/MemoryIntegrationTest.php @@ -10,9 +10,23 @@ use PHPUnit\Framework\TestCase; use Zend\Cache\Psr\CacheItemPool\CacheItemPoolDecorator; use Zend\Cache\StorageFactory; +use Zend\Stdlib\ErrorHandler; class MemoryIntegrationTest extends TestCase { + protected function setUp() + { + ErrorHandler::start(E_USER_DEPRECATED); + + parent::setUp(); + } + + protected function tearDown() + { + ErrorHandler::clean(); + parent::tearDown(); + } + /** * The memory adapter calculates the TTL on reading which violates PSR-6 * diff --git a/test/Psr/CacheItemPool/MongoDbIntegrationTest.php b/test/Psr/CacheItemPool/MongoDbIntegrationTest.php index fe7935722..6a87b39b7 100644 --- a/test/Psr/CacheItemPool/MongoDbIntegrationTest.php +++ b/test/Psr/CacheItemPool/MongoDbIntegrationTest.php @@ -14,6 +14,7 @@ use Zend\Cache\StorageFactory; use Zend\Cache\Exception; use Zend\ServiceManager\Exception\ServiceNotCreatedException; +use Zend\Stdlib\ErrorHandler; class MongoDbIntegrationTest extends CachePoolTest { @@ -21,7 +22,7 @@ class MongoDbIntegrationTest extends CachePoolTest * Backup default timezone * @var string */ - private $tz; + private $tz = 'UTC'; /** * @var MongoDb @@ -35,9 +36,11 @@ protected function setUp() } // set non-UTC timezone - $this->tz = date_default_timezone_get(); + $this->tz = date_default_timezone_get() ?: 'UTC'; date_default_timezone_set('America/Vancouver'); + ErrorHandler::start(E_USER_DEPRECATED); + parent::setUp(); } @@ -49,6 +52,8 @@ protected function tearDown() $this->storage->flush(); } + ErrorHandler::clean(); + parent::tearDown(); } diff --git a/test/Psr/CacheItemPool/RedisIntegrationTest.php b/test/Psr/CacheItemPool/RedisIntegrationTest.php index 38b5d3bd8..3766fb716 100644 --- a/test/Psr/CacheItemPool/RedisIntegrationTest.php +++ b/test/Psr/CacheItemPool/RedisIntegrationTest.php @@ -14,6 +14,7 @@ use Zend\Cache\StorageFactory; use Zend\Cache\Exception; use Zend\ServiceManager\Exception\ServiceNotCreatedException; +use Zend\Stdlib\ErrorHandler; class RedisIntegrationTest extends CachePoolTest { @@ -21,7 +22,7 @@ class RedisIntegrationTest extends CachePoolTest * Backup default timezone * @var string */ - private $tz; + private $tz = 'UTC'; /** * @var Redis @@ -35,9 +36,11 @@ protected function setUp() } // set non-UTC timezone - $this->tz = date_default_timezone_get(); + $this->tz = date_default_timezone_get() ?: 'UTC'; date_default_timezone_set('America/Vancouver'); + ErrorHandler::start(E_USER_DEPRECATED); + parent::setUp(); } @@ -49,6 +52,8 @@ protected function tearDown() $this->storage->flush(); } + ErrorHandler::clean(); + parent::tearDown(); } diff --git a/test/Psr/CacheItemPool/SessionIntegrationTest.php b/test/Psr/CacheItemPool/SessionIntegrationTest.php index f4c082897..108be7c87 100644 --- a/test/Psr/CacheItemPool/SessionIntegrationTest.php +++ b/test/Psr/CacheItemPool/SessionIntegrationTest.php @@ -10,9 +10,22 @@ use PHPUnit\Framework\TestCase; use Zend\Cache\Psr\CacheItemPool\CacheItemPoolDecorator; use Zend\Cache\StorageFactory; +use Zend\Stdlib\ErrorHandler; class SessionIntegrationTest extends TestCase { + protected function setUp() + { + ErrorHandler::start(E_USER_DEPRECATED); + parent::setUp(); + } + + protected function tearDown() + { + ErrorHandler::clean(); + parent::tearDown(); + } + /** * The session adapter doesn't support TTL * diff --git a/test/Psr/CacheItemPool/WinCacheIntegrationTest.php b/test/Psr/CacheItemPool/WinCacheIntegrationTest.php index b745ceeb2..b232c4bee 100644 --- a/test/Psr/CacheItemPool/WinCacheIntegrationTest.php +++ b/test/Psr/CacheItemPool/WinCacheIntegrationTest.php @@ -13,6 +13,7 @@ use Zend\Cache\StorageFactory; use Zend\Cache\Exception; use Zend\ServiceManager\Exception\ServiceNotCreatedException; +use Zend\Stdlib\ErrorHandler; /** * @requires extension wincache @@ -23,7 +24,7 @@ class WinCacheIntegrationTest extends CachePoolTest * Backup default timezone * @var string */ - private $tz; + private $tz = 'UTC'; /** * @var WinCache @@ -37,9 +38,11 @@ protected function setUp() } // set non-UTC timezone - $this->tz = date_default_timezone_get(); + $this->tz = date_default_timezone_get() ?: 'UTC'; date_default_timezone_set('America/Vancouver'); + ErrorHandler::start(E_USER_DEPRECATED); + parent::setUp(); } @@ -51,6 +54,8 @@ protected function tearDown() $this->storage->flush(); } + ErrorHandler::clean(); + parent::tearDown(); } diff --git a/test/Psr/CacheItemPool/XCacheIntegrationTest.php b/test/Psr/CacheItemPool/XCacheIntegrationTest.php index 7f3b79487..58dd2eafe 100644 --- a/test/Psr/CacheItemPool/XCacheIntegrationTest.php +++ b/test/Psr/CacheItemPool/XCacheIntegrationTest.php @@ -13,12 +13,24 @@ use Zend\Cache\StorageFactory; use Zend\Cache\Exception; use Zend\ServiceManager\Exception\ServiceNotCreatedException; +use Zend\Stdlib\ErrorHandler; /** * @requires extension xcache */ class XCacheIntegrationTest extends TestCase { + protected function setUp() + { + ErrorHandler::start(E_USER_DEPRECATED); + parent::setUp(); + } + + protected function tearDown() + { + ErrorHandler::clean(); + parent::tearDown(); + } /** * XCache is using request time based TTL handling which violates PSR-6 diff --git a/test/Psr/CacheItemPool/ZendServerDiskIntegrationTest.php b/test/Psr/CacheItemPool/ZendServerDiskIntegrationTest.php index 62198e92b..b8506a7a0 100644 --- a/test/Psr/CacheItemPool/ZendServerDiskIntegrationTest.php +++ b/test/Psr/CacheItemPool/ZendServerDiskIntegrationTest.php @@ -12,6 +12,7 @@ use Zend\Cache\StorageFactory; use Zend\Cache\Exception; use Zend\ServiceManager\Exception\ServiceNotCreatedException; +use Zend\Stdlib\ErrorHandler; class ZendServerDiskIntegrationTest extends CachePoolTest { @@ -19,7 +20,7 @@ class ZendServerDiskIntegrationTest extends CachePoolTest * Backup default timezone * @var string */ - private $tz; + private $tz = 'UTC'; protected function setUp() { @@ -28,9 +29,11 @@ protected function setUp() } // set non-UTC timezone - $this->tz = date_default_timezone_get(); + $this->tz = date_default_timezone_get() ?: 'UTC'; date_default_timezone_set('America/Vancouver'); + ErrorHandler::start(E_USER_DEPRECATED); + parent::setUp(); } @@ -42,6 +45,8 @@ protected function tearDown() zend_disk_cache_clear(); } + ErrorHandler::clean(); + parent::tearDown(); } diff --git a/test/Psr/CacheItemPool/ZendServerShmIntegrationTest.php b/test/Psr/CacheItemPool/ZendServerShmIntegrationTest.php index 571bc43aa..39036d22d 100644 --- a/test/Psr/CacheItemPool/ZendServerShmIntegrationTest.php +++ b/test/Psr/CacheItemPool/ZendServerShmIntegrationTest.php @@ -12,6 +12,7 @@ use Zend\Cache\StorageFactory; use Zend\Cache\Exception; use Zend\ServiceManager\Exception\ServiceNotCreatedException; +use Zend\Stdlib\ErrorHandler; class ZendServerShmIntegrationTest extends CachePoolTest { @@ -19,7 +20,7 @@ class ZendServerShmIntegrationTest extends CachePoolTest * Backup default timezone * @var string */ - private $tz; + private $tz = 'UTC'; protected function setUp() { @@ -32,9 +33,11 @@ protected function setUp() } // set non-UTC timezone - $this->tz = date_default_timezone_get(); + $this->tz = date_default_timezone_get() ?: 'UTC'; date_default_timezone_set('America/Vancouver'); + ErrorHandler::start(E_USER_DEPRECATED); + parent::setUp(); } @@ -46,6 +49,8 @@ protected function tearDown() zend_disk_cache_clear(); } + ErrorHandler::clean(); + parent::tearDown(); } diff --git a/test/Psr/SimpleCache/ApcIntegrationTest.php b/test/Psr/SimpleCache/ApcIntegrationTest.php index 89d846f83..f26c014c1 100644 --- a/test/Psr/SimpleCache/ApcIntegrationTest.php +++ b/test/Psr/SimpleCache/ApcIntegrationTest.php @@ -12,6 +12,7 @@ use Zend\Cache\Psr\SimpleCache\SimpleCacheDecorator; use Zend\Cache\StorageFactory; use Zend\ServiceManager\Exception\ServiceNotCreatedException; +use Zend\Stdlib\ErrorHandler; class ApcIntegrationTest extends SimpleCacheTest { @@ -19,7 +20,7 @@ class ApcIntegrationTest extends SimpleCacheTest * Backup default timezone * @var string */ - private $tz; + private $tz = 'UTC'; /** * Restore 'apc.use_request_time' @@ -35,13 +36,15 @@ protected function setUp() } // set non-UTC timezone - $this->tz = date_default_timezone_get(); + $this->tz = date_default_timezone_get() ?: 'UTC'; date_default_timezone_set('America/Vancouver'); // needed on test expirations $this->iniUseRequestTime = ini_get('apc.use_request_time'); ini_set('apc.use_request_time', 0); + ErrorHandler::start(E_USER_DEPRECATED); + parent::setUp(); } @@ -56,6 +59,8 @@ protected function tearDown() // reset ini configurations ini_set('apc.use_request_time', $this->iniUseRequestTime); + + ErrorHandler::clean(); parent::tearDown(); } diff --git a/test/Psr/SimpleCache/ApcuIntegrationTest.php b/test/Psr/SimpleCache/ApcuIntegrationTest.php index 47d868cab..9e5fef9e1 100644 --- a/test/Psr/SimpleCache/ApcuIntegrationTest.php +++ b/test/Psr/SimpleCache/ApcuIntegrationTest.php @@ -12,6 +12,7 @@ use Zend\Cache\Psr\SimpleCache\SimpleCacheDecorator; use Zend\Cache\StorageFactory; use Zend\ServiceManager\Exception\ServiceNotCreatedException; +use Zend\Stdlib\ErrorHandler; class ApcuIntegrationTest extends SimpleCacheTest { @@ -19,7 +20,7 @@ class ApcuIntegrationTest extends SimpleCacheTest * Backup default timezone * @var string */ - private $tz; + private $tz = 'UTC'; /** * Restore 'apc.use_request_time' @@ -35,13 +36,15 @@ protected function setUp() } // set non-UTC timezone - $this->tz = date_default_timezone_get(); + $this->tz = date_default_timezone_get() ?: 'UTC'; date_default_timezone_set('America/Vancouver'); // needed on test expirations $this->iniUseRequestTime = ini_get('apc.use_request_time'); ini_set('apc.use_request_time', 0); + ErrorHandler::start(E_USER_DEPRECATED); + parent::setUp(); } @@ -56,6 +59,7 @@ protected function tearDown() // reset ini configurations ini_set('apc.use_request_time', $this->iniUseRequestTime); + ErrorHandler::clean(); parent::tearDown(); } diff --git a/test/Psr/SimpleCache/ExtMongoDbIntegrationTest.php b/test/Psr/SimpleCache/ExtMongoDbIntegrationTest.php index a157bb387..3cfbfe435 100644 --- a/test/Psr/SimpleCache/ExtMongoDbIntegrationTest.php +++ b/test/Psr/SimpleCache/ExtMongoDbIntegrationTest.php @@ -14,6 +14,7 @@ use Zend\Cache\StorageFactory; use Zend\Cache\Exception; use Zend\ServiceManager\Exception\ServiceNotCreatedException; +use Zend\Stdlib\ErrorHandler; class ExtMongoDbIntegrationTest extends SimpleCacheTest { @@ -21,7 +22,7 @@ class ExtMongoDbIntegrationTest extends SimpleCacheTest * Backup default timezone * @var string */ - private $tz; + private $tz = 'UTC'; /** * @var ExtMongoDb @@ -39,9 +40,11 @@ protected function setUp() } // set non-UTC timezone - $this->tz = date_default_timezone_get(); + $this->tz = date_default_timezone_get() ?: 'UTC'; date_default_timezone_set('America/Vancouver'); + ErrorHandler::start(E_USER_DEPRECATED); + parent::setUp(); } @@ -53,6 +56,8 @@ protected function tearDown() $this->storage->flush(); } + ErrorHandler::clean(); + parent::tearDown(); } diff --git a/test/Psr/SimpleCache/MemcacheIntegrationTest.php b/test/Psr/SimpleCache/MemcacheIntegrationTest.php index 270352cdf..d995bff4f 100644 --- a/test/Psr/SimpleCache/MemcacheIntegrationTest.php +++ b/test/Psr/SimpleCache/MemcacheIntegrationTest.php @@ -14,6 +14,7 @@ use Zend\Cache\StorageFactory; use Zend\Cache\Exception; use Zend\ServiceManager\Exception\ServiceNotCreatedException; +use Zend\Stdlib\ErrorHandler; /** * @require extension memcache @@ -24,7 +25,7 @@ class MemcacheIntegrationTest extends SimpleCacheTest * Backup default timezone * @var string */ - private $tz; + private $tz = 'UTC'; /** * @var Memcache @@ -38,9 +39,11 @@ protected function setUp() } // set non-UTC timezone - $this->tz = date_default_timezone_get(); + $this->tz = date_default_timezone_get() ?: 'UTC'; date_default_timezone_set('America/Vancouver'); + ErrorHandler::start(E_USER_DEPRECATED); + parent::setUp(); } @@ -52,6 +55,8 @@ protected function tearDown() $this->storage->flush(); } + ErrorHandler::clean(); + parent::tearDown(); } diff --git a/test/Psr/SimpleCache/MemcachedIntegrationTest.php b/test/Psr/SimpleCache/MemcachedIntegrationTest.php index 19e874dc2..e36c03182 100644 --- a/test/Psr/SimpleCache/MemcachedIntegrationTest.php +++ b/test/Psr/SimpleCache/MemcachedIntegrationTest.php @@ -13,6 +13,7 @@ use Zend\Cache\StorageFactory; use Zend\Cache\Exception; use Zend\ServiceManager\Exception\ServiceNotCreatedException; +use Zend\Stdlib\ErrorHandler; /** * @require extension memcached @@ -23,7 +24,7 @@ class MemcachedIntegrationTest extends SimpleCacheTest * Backup default timezone * @var string */ - private $tz; + private $tz = 'UTC'; /** * @var Memcached @@ -37,9 +38,11 @@ protected function setUp() } // set non-UTC timezone - $this->tz = date_default_timezone_get(); + $this->tz = date_default_timezone_get() ?: 'UTC'; date_default_timezone_set('America/Vancouver'); + ErrorHandler::start(E_USER_DEPRECATED); + parent::setUp(); } @@ -51,6 +54,8 @@ protected function tearDown() $this->storage->flush(); } + ErrorHandler::clean(); + parent::tearDown(); } diff --git a/test/Psr/SimpleCache/MemoryIntegrationTest.php b/test/Psr/SimpleCache/MemoryIntegrationTest.php index 5c09c7982..e10163c2b 100644 --- a/test/Psr/SimpleCache/MemoryIntegrationTest.php +++ b/test/Psr/SimpleCache/MemoryIntegrationTest.php @@ -10,6 +10,7 @@ use Cache\IntegrationTests\SimpleCacheTest; use Zend\Cache\Psr\SimpleCache\SimpleCacheDecorator; use Zend\Cache\StorageFactory; +use Zend\Stdlib\ErrorHandler; class MemoryIntegrationTest extends SimpleCacheTest { @@ -21,9 +22,18 @@ public function setUp() $this->skippedTests['testObjectDoesNotChangeInCache'] = 'Memory adapter stores objects in memory; so change in references is possible'; + ErrorHandler::start(E_USER_DEPRECATED); + parent::setUp(); } + protected function tearDown() + { + ErrorHandler::clean(); + + parent::tearDown(); + } + public function createSimpleCache() { $storage = StorageFactory::adapterFactory('memory'); diff --git a/test/Psr/SimpleCache/MongoDbIntegrationTest.php b/test/Psr/SimpleCache/MongoDbIntegrationTest.php index 36bdbdfd8..d0ab3e49b 100644 --- a/test/Psr/SimpleCache/MongoDbIntegrationTest.php +++ b/test/Psr/SimpleCache/MongoDbIntegrationTest.php @@ -14,6 +14,7 @@ use Zend\Cache\StorageFactory; use Zend\Cache\Exception; use Zend\ServiceManager\Exception\ServiceNotCreatedException; +use Zend\Stdlib\ErrorHandler; class MongoDbIntegrationTest extends SimpleCacheTest { @@ -21,7 +22,7 @@ class MongoDbIntegrationTest extends SimpleCacheTest * Backup default timezone * @var string */ - private $tz; + private $tz = 'UTC'; /** * @var MongoDb @@ -35,9 +36,11 @@ protected function setUp() } // set non-UTC timezone - $this->tz = date_default_timezone_get(); + $this->tz = date_default_timezone_get() ?: 'UTC'; date_default_timezone_set('America/Vancouver'); + ErrorHandler::start(E_USER_DEPRECATED); + parent::setUp(); } @@ -49,6 +52,8 @@ protected function tearDown() $this->storage->flush(); } + ErrorHandler::clean(); + parent::tearDown(); } diff --git a/test/Psr/SimpleCache/RedisIntegrationTest.php b/test/Psr/SimpleCache/RedisIntegrationTest.php index 1853782cf..ba68681b9 100644 --- a/test/Psr/SimpleCache/RedisIntegrationTest.php +++ b/test/Psr/SimpleCache/RedisIntegrationTest.php @@ -14,6 +14,7 @@ use Zend\Cache\StorageFactory; use Zend\Cache\Exception; use Zend\ServiceManager\Exception\ServiceNotCreatedException; +use Zend\Stdlib\ErrorHandler; class RedisIntegrationTest extends SimpleCacheTest { @@ -21,7 +22,7 @@ class RedisIntegrationTest extends SimpleCacheTest * Backup default timezone * @var string */ - private $tz; + private $tz = 'UTC'; /** * @var Redis @@ -35,9 +36,11 @@ protected function setUp() } // set non-UTC timezone - $this->tz = date_default_timezone_get(); + $this->tz = date_default_timezone_get() ?: 'UTC'; date_default_timezone_set('America/Vancouver'); + ErrorHandler::start(E_USER_DEPRECATED); + parent::setUp(); } @@ -49,6 +52,8 @@ protected function tearDown() $this->storage->flush(); } + ErrorHandler::clean(); + parent::tearDown(); } diff --git a/test/Psr/SimpleCache/SessionIntegrationTest.php b/test/Psr/SimpleCache/SessionIntegrationTest.php index d97247340..fea2fdc88 100644 --- a/test/Psr/SimpleCache/SessionIntegrationTest.php +++ b/test/Psr/SimpleCache/SessionIntegrationTest.php @@ -11,6 +11,7 @@ use Zend\Cache\Psr\SimpleCache\SimpleCacheDecorator; use Zend\Cache\StorageFactory; use Zend\Session\Container as SessionContainer; +use Zend\Stdlib\ErrorHandler; class SessionIntegrationTest extends SimpleCacheTest { @@ -28,6 +29,8 @@ public function setUp() $this->skippedTests['testObjectDoesNotChangeInCache'] = 'Session adapter stores objects in memory; so change in references is possible'; + ErrorHandler::start(E_USER_DEPRECATED); + parent::setUp(); } @@ -40,6 +43,8 @@ public function tearDown() $_SESSION = []; SessionContainer::setDefaultManager(null); + ErrorHandler::clean(); + parent::tearDown(); } diff --git a/test/Psr/SimpleCache/WinCacheIntegrationTest.php b/test/Psr/SimpleCache/WinCacheIntegrationTest.php index 9c2464d30..b51a29f0b 100644 --- a/test/Psr/SimpleCache/WinCacheIntegrationTest.php +++ b/test/Psr/SimpleCache/WinCacheIntegrationTest.php @@ -13,6 +13,7 @@ use Zend\Cache\StorageFactory; use Zend\Cache\Exception; use Zend\ServiceManager\Exception\ServiceNotCreatedException; +use Zend\Stdlib\ErrorHandler; /** * @requires extension wincache @@ -23,7 +24,7 @@ class WinCacheIntegrationTest extends SimpleCacheTest * Backup default timezone * @var string */ - private $tz; + private $tz = 'UTC'; /** * @var WinCache @@ -37,9 +38,11 @@ protected function setUp() } // set non-UTC timezone - $this->tz = date_default_timezone_get(); + $this->tz = date_default_timezone_get() ?: 'UTC'; date_default_timezone_set('America/Vancouver'); + ErrorHandler::start(E_USER_DEPRECATED); + parent::setUp(); } @@ -51,6 +54,8 @@ protected function tearDown() $this->storage->flush(); } + ErrorHandler::clean(); + parent::tearDown(); } diff --git a/test/Psr/SimpleCache/XCacheIntegrationTest.php b/test/Psr/SimpleCache/XCacheIntegrationTest.php index 134e8101c..ca5c3830d 100644 --- a/test/Psr/SimpleCache/XCacheIntegrationTest.php +++ b/test/Psr/SimpleCache/XCacheIntegrationTest.php @@ -13,6 +13,7 @@ use Zend\Cache\StorageFactory; use Zend\Cache\Exception; use Zend\ServiceManager\Exception\ServiceNotCreatedException; +use Zend\Stdlib\ErrorHandler; /** * @requires extension xcache @@ -55,9 +56,17 @@ public function setUp() $this->skippedTests['testSetTtl'] = 'XCache adapter does not honor TTL'; $this->skippedTests['testSetMultipleTtl'] = 'XCache adapter does not honor TTL'; + ErrorHandler::start(E_USER_DEPRECATED); + parent::setUp(); } + protected function tearDown() + { + ErrorHandler::clean(); + parent::tearDown(); + } + public function createSimpleCache() { try { diff --git a/test/Psr/SimpleCache/ZendServerDiskIntegrationTest.php b/test/Psr/SimpleCache/ZendServerDiskIntegrationTest.php index 2dfeb140e..90b3d8965 100644 --- a/test/Psr/SimpleCache/ZendServerDiskIntegrationTest.php +++ b/test/Psr/SimpleCache/ZendServerDiskIntegrationTest.php @@ -12,6 +12,7 @@ use Zend\Cache\StorageFactory; use Zend\Cache\Exception; use Zend\ServiceManager\Exception\ServiceNotCreatedException; +use Zend\Stdlib\ErrorHandler; class ZendServerDiskIntegrationTest extends SimpleCacheTest { @@ -19,7 +20,7 @@ class ZendServerDiskIntegrationTest extends SimpleCacheTest * Backup default timezone * @var string */ - private $tz; + private $tz = 'UTC'; protected function setUp() { @@ -28,9 +29,11 @@ protected function setUp() } // set non-UTC timezone - $this->tz = date_default_timezone_get(); + $this->tz = date_default_timezone_get() ?: 'UTC'; date_default_timezone_set('America/Vancouver'); + ErrorHandler::start(E_USER_DEPRECATED); + parent::setUp(); } @@ -42,6 +45,7 @@ protected function tearDown() zend_disk_cache_clear(); } + ErrorHandler::clean(); parent::tearDown(); } diff --git a/test/Psr/SimpleCache/ZendServerShmIntegrationTest.php b/test/Psr/SimpleCache/ZendServerShmIntegrationTest.php index e8c44bbc0..59e0ccaa2 100644 --- a/test/Psr/SimpleCache/ZendServerShmIntegrationTest.php +++ b/test/Psr/SimpleCache/ZendServerShmIntegrationTest.php @@ -12,6 +12,7 @@ use Zend\Cache\StorageFactory; use Zend\Cache\Exception; use Zend\ServiceManager\Exception\ServiceNotCreatedException; +use Zend\Stdlib\ErrorHandler; class ZendServerShmIntegrationTest extends SimpleCacheTest { @@ -19,7 +20,7 @@ class ZendServerShmIntegrationTest extends SimpleCacheTest * Backup default timezone * @var string */ - private $tz; + private $tz = 'UTC'; protected function setUp() { @@ -31,8 +32,9 @@ protected function setUp() $this->markTestSkipped("Missing 'zend_shm_cache_*' functions or running from SAPI 'cli'"); } + ErrorHandler::start(E_USER_DEPRECATED); // set non-UTC timezone - $this->tz = date_default_timezone_get(); + $this->tz = date_default_timezone_get() ?: 'UTC'; date_default_timezone_set('America/Vancouver'); parent::setUp(); @@ -46,6 +48,7 @@ protected function tearDown() zend_disk_cache_clear(); } + ErrorHandler::clean(); parent::tearDown(); } diff --git a/test/Storage/Adapter/BlackHoleTest.php b/test/Storage/Adapter/BlackHoleTest.php index f8d2f46b7..9849475d7 100644 --- a/test/Storage/Adapter/BlackHoleTest.php +++ b/test/Storage/Adapter/BlackHoleTest.php @@ -15,6 +15,7 @@ use Zend\Cache\Storage\Adapter\BlackHole; use Zend\Cache\StorageFactory; use Zend\ServiceManager\ServiceManager; +use Zend\Stdlib\ErrorHandler; /** * PHPUnit test case @@ -36,11 +37,18 @@ class BlackHoleTest extends TestCase */ protected $storage; - public function setUp() + protected function setUp() { + ErrorHandler::start(E_USER_DEPRECATED); $this->storage = StorageFactory::adapterFactory('BlackHole'); } + protected function tearDown() + { + ErrorHandler::clean(); + parent::tearDown(); + } + /** * A data provider for common storage adapter names */ diff --git a/test/StorageFactoryTest.php b/test/StorageFactoryTest.php index e89a964cb..fbfdd8f12 100644 --- a/test/StorageFactoryTest.php +++ b/test/StorageFactoryTest.php @@ -10,7 +10,6 @@ namespace ZendTest\Cache; use const E_USER_DEPRECATED; -use ErrorException; use PHPUnit\Framework\TestCase; use Zend\Cache; use Zend\ServiceManager\ServiceManager; @@ -212,14 +211,4 @@ public function testFactoryWithPluginsAndOptionsArray() } } } - - public function testWillTriggerUserDeprecatedOnUsage() - { - Cache\StorageFactory::factory([ - 'adapter' => 'Memory', - ]); - $error = ErrorHandler::stop(); - $this->assertInstanceOf(ErrorException::class, $error); - $this->assertSame(E_USER_DEPRECATED, $error->getSeverity()); - } } From ae76189cfd2afc3f24b2221d751137bd479e7c22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= Date: Tue, 11 Sep 2018 09:08:11 +0200 Subject: [PATCH 08/11] Fixed codestyle --- src/PatternFactory.php | 2 +- src/Storage/StorageFactory.php | 16 ++++++++-------- src/StorageFactory.php | 4 ++-- test/Service/StorageFactoryFactoryTest.php | 2 +- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/PatternFactory.php b/src/PatternFactory.php index 9242c9ff2..a1b5a3f5e 100644 --- a/src/PatternFactory.php +++ b/src/PatternFactory.php @@ -52,7 +52,7 @@ public static function factory($patternName, $options = []) $options = ArrayUtils::iteratorToArray($options); } - if (!is_array($options)) { + if (! is_array($options)) { throw new Exception\InvalidArgumentException(sprintf( '%s expects an array, Traversable object, or %s\Pattern\PatternOptions object; received "%s"', __METHOD__, diff --git a/src/Storage/StorageFactory.php b/src/Storage/StorageFactory.php index fa444e01f..da158783b 100644 --- a/src/Storage/StorageFactory.php +++ b/src/Storage/StorageFactory.php @@ -47,7 +47,7 @@ public function createFromCachesConfig($config) $config = ArrayUtils::iteratorToArray($config); } - if (!is_array($config)) { + if (! is_array($config)) { throw new Exception\InvalidArgumentException( 'The factory needs an associative array ' . 'or a Traversable object as an argument' @@ -55,13 +55,13 @@ public function createFromCachesConfig($config) } // instantiate the adapter - if (!isset($config['adapter'])) { + if (! isset($config['adapter'])) { throw new Exception\InvalidArgumentException('Missing "adapter"'); } $adapterName = $config['adapter']; $adapterOptions = []; if (is_array($config['adapter'])) { - if (!isset($config['adapter']['name'])) { + if (! isset($config['adapter']['name'])) { throw new Exception\InvalidArgumentException('Missing "adapter.name"'); } @@ -76,7 +76,7 @@ public function createFromCachesConfig($config) // add plugins if (isset($config['plugins'])) { - if (!is_array($config['plugins'])) { + if (! is_array($config['plugins'])) { throw new Exception\InvalidArgumentException( 'Plugins needs to be an array' ); @@ -105,7 +105,7 @@ public function create($adapterName, array $options = [], array $pluginConfigura return $adapter; } - if (!$adapter instanceof EventsCapableInterface) { + if (! $adapter instanceof EventsCapableInterface) { throw new Exception\RuntimeException(sprintf( "The adapter '%s' doesn't implement '%s' and therefore can't handle plugins", get_class($adapter), @@ -117,7 +117,7 @@ public function create($adapterName, array $options = [], array $pluginConfigura $pluginPrio = 1; // default priority if (is_string($k)) { - if (!is_array($v)) { + if (! is_array($v)) { throw new Exception\InvalidArgumentException( "'plugins.{$k}' needs to be an array" ); @@ -125,7 +125,7 @@ public function create($adapterName, array $options = [], array $pluginConfigura $pluginName = $k; $pluginOptions = $v; } elseif (is_array($v)) { - if (!isset($v['name'])) { + if (! isset($v['name'])) { throw new Exception\InvalidArgumentException( "Invalid plugins[{$k}] or missing plugins[{$k}].name" ); @@ -147,7 +147,7 @@ public function create($adapterName, array $options = [], array $pluginConfigura } $plugin = $this->plugins->get($pluginName, $pluginOptions); - if (!$adapter->hasPlugin($plugin)) { + if (! $adapter->hasPlugin($plugin)) { $adapter->addPlugin($plugin, $pluginPrio); } diff --git a/src/StorageFactory.php b/src/StorageFactory.php index c26ac517d..b6a030f69 100644 --- a/src/StorageFactory.php +++ b/src/StorageFactory.php @@ -71,7 +71,7 @@ public static function adapterFactory($adapterName, $options = []) $adapter = $adapterName; - if (!$adapterName instanceof Storage\StorageInterface) { + if (! $adapterName instanceof Storage\StorageInterface) { $adapter = static::getAdapterPluginManager()->get($adapterName); } @@ -152,7 +152,7 @@ public static function pluginFactory($pluginName, $options = []) ), E_USER_DEPRECATED); $plugin = $pluginName; - if (!$pluginName instanceof Storage\Plugin\PluginInterface) { + if (! $pluginName instanceof Storage\Plugin\PluginInterface) { $plugin = self::getPluginManager()->get($pluginName); } diff --git a/test/Service/StorageFactoryFactoryTest.php b/test/Service/StorageFactoryFactoryTest.php index 92cc77578..ee706d8fd 100644 --- a/test/Service/StorageFactoryFactoryTest.php +++ b/test/Service/StorageFactoryFactoryTest.php @@ -47,7 +47,7 @@ public function testFactoryCreatesServiceV2() public function testFactoryCreatesServiceV3() { - if (!method_exists(new ServiceManager(), 'configure')) { + if (! method_exists(new ServiceManager(), 'configure')) { $this->markTestSkipped('Test is only needed in ZF3'); } From fb71856bb776ac66cf62215c86cffcbf1b0ff245 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= Date: Tue, 11 Sep 2018 09:09:42 +0200 Subject: [PATCH 09/11] Added new storage factory to `ConfigProvider` --- src/ConfigProvider.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php index 0ebede7fe..e6fad1183 100644 --- a/src/ConfigProvider.php +++ b/src/ConfigProvider.php @@ -36,6 +36,7 @@ public function getDependencyConfig() PatternPluginManager::class => Service\PatternPluginManagerFactory::class, Storage\AdapterPluginManager::class => Service\StorageAdapterPluginManagerFactory::class, Storage\PluginManager::class => Service\StoragePluginManagerFactory::class, + Storage\StorageFactory::class => Service\StorageFactoryFactory::class, ], ]; } From acd9edcae6ea1c6d867d8d5c2e66a8dca2e7aaab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= Date: Tue, 11 Sep 2018 13:54:39 +0200 Subject: [PATCH 10/11] Removed new `StorageFactory` - Moved plugin attachment logic into the `AdapterPluginManager` - Restored most of the old static `StorageFactory` with minor enhancements - Removed unit tests for new `StorageFactory` - Added unit test for extended options for `AdapterPluginManager` --- src/ConfigProvider.php | 1 - src/Service/StorageFactoryFactory.php | 30 ---- .../AdapterPluginManagerTrait.php | 103 ++++++++++-- .../AdapterPluginManagerV2Polyfill.php | 15 +- .../AdapterPluginManagerV3Polyfill.php | 26 +++ src/Storage/StorageFactory.php | 157 ------------------ src/StorageFactory.php | 101 ++++++++++- ...StorageCacheAbstractServiceFactoryTest.php | 6 +- test/Service/StorageCacheFactoryTest.php | 6 +- test/Service/StorageFactoryFactoryTest.php | 66 -------- test/Storage/AdapterPluginManagerTest.php | 55 ++++++ test/StorageFactoryTest.php | 1 - 12 files changed, 279 insertions(+), 288 deletions(-) delete mode 100644 src/Service/StorageFactoryFactory.php delete mode 100644 src/Storage/StorageFactory.php delete mode 100644 test/Service/StorageFactoryFactoryTest.php diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php index e6fad1183..0ebede7fe 100644 --- a/src/ConfigProvider.php +++ b/src/ConfigProvider.php @@ -36,7 +36,6 @@ public function getDependencyConfig() PatternPluginManager::class => Service\PatternPluginManagerFactory::class, Storage\AdapterPluginManager::class => Service\StorageAdapterPluginManagerFactory::class, Storage\PluginManager::class => Service\StoragePluginManagerFactory::class, - Storage\StorageFactory::class => Service\StorageFactoryFactory::class, ], ]; } diff --git a/src/Service/StorageFactoryFactory.php b/src/Service/StorageFactoryFactory.php deleted file mode 100644 index b429c89c3..000000000 --- a/src/Service/StorageFactoryFactory.php +++ /dev/null @@ -1,30 +0,0 @@ -get(AdapterPluginManager::class), $container->get(PluginManager::class)); - } -} diff --git a/src/Storage/AdapterPluginManager/AdapterPluginManagerTrait.php b/src/Storage/AdapterPluginManager/AdapterPluginManagerTrait.php index 830e91709..ea5b37103 100644 --- a/src/Storage/AdapterPluginManager/AdapterPluginManagerTrait.php +++ b/src/Storage/AdapterPluginManager/AdapterPluginManagerTrait.php @@ -9,8 +9,11 @@ namespace Zend\Cache\Storage\AdapterPluginManager; use Zend\Cache\Exception; -use Zend\Cache\Storage\Adapter; +use Zend\Cache\Storage\PluginManager; +use Zend\Cache\Storage\StorageInterface; +use Zend\EventManager\EventsCapableInterface; use Zend\ServiceManager\Exception\InvalidServiceException; +use Zend\Stdlib\ArrayUtils; /** * Trait providing common logic between AdapterPluginManager implementations. @@ -21,22 +24,6 @@ */ trait AdapterPluginManagerTrait { - /** - * 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 Adapter\AdapterOptions($options)); - return $plugin; - } - /** * Validate the plugin is of the expected type (v3). * @@ -73,4 +60,86 @@ public function validatePlugin($plugin) throw new Exception\RuntimeException($e->getMessage(), $e->getCode(), $e); } } + + private function parseOptions(array $options) + { + $plugins = []; + if (array_key_exists('plugins', $options)) { + $plugins = $options['plugins'] ?: []; + unset($options['plugins']); + } + + $adapter = $options; + + if (isset($options['options'])) { + $adapter = $options['options']; + } + + if (isset($options['adapter']['options']) && is_array($options['adapter']['options'])) { + $adapter = ArrayUtils::merge($adapter, $options['adapter']['options']); + } + + return [$adapter, $plugins]; + } + + /** + * Attaches plugins by using the provided plugin manager. + * + * @param StorageInterface $adapter + * @param PluginManager $pluginManager + * @param array $plugins + * + * @return void + * @throws Exception\RuntimeException if adapter does not implement `EventsCapableInterface` + * @throws Exception\InvalidArgumentException if the plugin configuration does not fit specification. + */ + private function attachPlugins(StorageInterface $adapter, PluginManager $pluginManager, array $plugins) + { + if (! $adapter instanceof EventsCapableInterface) { + throw new Exception\RuntimeException(sprintf( + "The adapter '%s' doesn't implement '%s' and therefore can't handle plugins", + get_class($adapter), + EventsCapableInterface::class + )); + } + + foreach ($plugins as $k => $v) { + $pluginPrio = 1; // default priority + + if (is_string($k)) { + if (! is_array($v)) { + throw new Exception\InvalidArgumentException( + "'plugins.{$k}' needs to be an array" + ); + } + $pluginName = $k; + $pluginOptions = $v; + } elseif (is_array($v)) { + if (! isset($v['name'])) { + throw new Exception\InvalidArgumentException( + "Invalid plugins[{$k}] or missing plugins[{$k}].name" + ); + } + $pluginName = (string) $v['name']; + + if (isset($v['options'])) { + $pluginOptions = $v['options']; + } else { + $pluginOptions = []; + } + + if (isset($v['priority'])) { + $pluginPrio = $v['priority']; + } + } else { + $pluginName = $v; + $pluginOptions = []; + } + + $plugin = $pluginManager->get($pluginName, $pluginOptions); + if (! $adapter->hasPlugin($plugin)) { + $adapter->addPlugin($plugin, $pluginPrio); + } + } + } } diff --git a/src/Storage/AdapterPluginManager/AdapterPluginManagerV2Polyfill.php b/src/Storage/AdapterPluginManager/AdapterPluginManagerV2Polyfill.php index 0364cf224..272bdbac1 100644 --- a/src/Storage/AdapterPluginManager/AdapterPluginManagerV2Polyfill.php +++ b/src/Storage/AdapterPluginManager/AdapterPluginManagerV2Polyfill.php @@ -8,6 +8,7 @@ */ namespace Zend\Cache\Storage\AdapterPluginManager; +use Zend\Cache\Storage\PluginManager; use Zend\Cache\Storage\StorageInterface; use Zend\ServiceManager\AbstractPluginManager; use Zend\Cache\Storage\Adapter; @@ -146,15 +147,21 @@ class AdapterPluginManagerV2Polyfill extends AbstractPluginManager */ public function get($plugin, $options = [], $usePeeringServiceManagers = true) { + $adapter = parent::get($plugin, [], $usePeeringServiceManagers); + if (empty($options)) { - return parent::get($plugin, [], $usePeeringServiceManagers); + return $adapter; } - $plugins = isset($options['plugins']) ? $options['plugins'] : []; - unset($options['plugins']); + list($options, $pluginConfiguration) = $this->parseOptions($options); + + if (! empty($pluginConfiguration)) { + $plugins = $this->serviceLocator->has(PluginManager::class) ? + $this->serviceLocator->get(PluginManager::class) : new PluginManager($this->serviceLocator); + $this->attachPlugins($adapter, $plugins, $pluginConfiguration); + } /** @var StorageInterface $adapter */ - $adapter = parent::get($plugin, [], $usePeeringServiceManagers); $adapter->setOptions(new Adapter\AdapterOptions($options)); return $adapter; diff --git a/src/Storage/AdapterPluginManager/AdapterPluginManagerV3Polyfill.php b/src/Storage/AdapterPluginManager/AdapterPluginManagerV3Polyfill.php index 31631a958..9f5bf6308 100644 --- a/src/Storage/AdapterPluginManager/AdapterPluginManagerV3Polyfill.php +++ b/src/Storage/AdapterPluginManager/AdapterPluginManagerV3Polyfill.php @@ -9,6 +9,7 @@ namespace Zend\Cache\Storage\AdapterPluginManager; use Zend\Cache\Storage\Adapter; +use Zend\Cache\Storage\PluginManager; use Zend\Cache\Storage\StorageInterface; use Zend\ServiceManager\AbstractPluginManager; use Zend\ServiceManager\Factory\InvokableFactory; @@ -121,4 +122,29 @@ class AdapterPluginManagerV3Polyfill extends AbstractPluginManager * @var string */ protected $instanceOf = StorageInterface::class; + + /** + * Override build to inject options as PatternOptions instance. + * + * {@inheritDoc} + */ + public function build($plugin, array $options = null) + { + $adapter = parent::build($plugin); + + if (empty($options)) { + return $adapter; + } + + list ($options, $pluginConfiguration) = $this->parseOptions($options); + + if (! empty($pluginConfiguration)) { + $plugins = $this->creationContext->has(PluginManager::class) ? + $this->creationContext->get(PluginManager::class) : new PluginManager($this->creationContext); + $this->attachPlugins($adapter, $plugins, $pluginConfiguration); + } + + $adapter->setOptions(new Adapter\AdapterOptions($options)); + return $adapter; + } } diff --git a/src/Storage/StorageFactory.php b/src/Storage/StorageFactory.php deleted file mode 100644 index da158783b..000000000 --- a/src/Storage/StorageFactory.php +++ /dev/null @@ -1,157 +0,0 @@ -adapters = $adapters; - $this->plugins = $plugins; - } - - /** - * Creates a storage adapter by parsing the `caches` configuration. - * - * @param array|Traversable $config - * - * @return StorageInterface - * @throws Exception\InvalidArgumentException - */ - public function createFromCachesConfig($config) - { - if ($config instanceof Traversable) { - $config = ArrayUtils::iteratorToArray($config); - } - - if (! is_array($config)) { - throw new Exception\InvalidArgumentException( - 'The factory needs an associative array ' - . 'or a Traversable object as an argument' - ); - } - - // instantiate the adapter - if (! isset($config['adapter'])) { - throw new Exception\InvalidArgumentException('Missing "adapter"'); - } - $adapterName = $config['adapter']; - $adapterOptions = []; - if (is_array($config['adapter'])) { - if (! isset($config['adapter']['name'])) { - throw new Exception\InvalidArgumentException('Missing "adapter.name"'); - } - - $adapterName = $config['adapter']['name']; - $adapterOptions = isset($config['adapter']['options']) ? $config['adapter']['options'] : []; - } - if (isset($config['options'])) { - $adapterOptions = array_merge($adapterOptions, $config['options']); - } - - $pluginConfiguration = []; - - // add plugins - if (isset($config['plugins'])) { - if (! is_array($config['plugins'])) { - throw new Exception\InvalidArgumentException( - 'Plugins needs to be an array' - ); - } - - $pluginConfiguration = $config['plugins']; - } - - return $this->create($adapterName, $adapterOptions, $pluginConfiguration); - } - - /** - * Creates a storage adapter and attaches plugins if needed. - * - * @param string $adapterName - * @param array $options - * @param array $pluginConfiguration - * - * @return StorageInterface - */ - public function create($adapterName, array $options = [], array $pluginConfiguration = []) - { - $adapter = $this->adapters->get($adapterName, $options); - - if (empty($pluginConfiguration)) { - return $adapter; - } - - if (! $adapter instanceof EventsCapableInterface) { - throw new Exception\RuntimeException(sprintf( - "The adapter '%s' doesn't implement '%s' and therefore can't handle plugins", - get_class($adapter), - EventsCapableInterface::class - )); - } - - foreach ($pluginConfiguration as $k => $v) { - $pluginPrio = 1; // default priority - - if (is_string($k)) { - if (! is_array($v)) { - throw new Exception\InvalidArgumentException( - "'plugins.{$k}' needs to be an array" - ); - } - $pluginName = $k; - $pluginOptions = $v; - } elseif (is_array($v)) { - if (! isset($v['name'])) { - throw new Exception\InvalidArgumentException( - "Invalid plugins[{$k}] or missing plugins[{$k}].name" - ); - } - $pluginName = (string) $v['name']; - - if (isset($v['options'])) { - $pluginOptions = $v['options']; - } else { - $pluginOptions = []; - } - - if (isset($v['priority'])) { - $pluginPrio = $v['priority']; - } - } else { - $pluginName = $v; - $pluginOptions = []; - } - - $plugin = $this->plugins->get($pluginName, $pluginOptions); - if (! $adapter->hasPlugin($plugin)) { - $adapter->addPlugin($plugin, $pluginPrio); - } - - return $adapter; - } - } -} diff --git a/src/StorageFactory.php b/src/StorageFactory.php index b6a030f69..ad0d6f5c4 100644 --- a/src/StorageFactory.php +++ b/src/StorageFactory.php @@ -10,7 +10,9 @@ namespace Zend\Cache; use Traversable; +use Zend\EventManager\EventsCapableInterface; use Zend\ServiceManager\ServiceManager; +use Zend\Stdlib\ArrayUtils; /** * @deprecated static factories are deprecated as of zendframework 2.9 and may be removed in future major versions. @@ -35,21 +37,108 @@ abstract class StorageFactory * The storage factory * This can instantiate storage adapters and plugins. * - * @param array|Traversable $cfg + * @param array|Traversable $config * @return Storage\StorageInterface * @throws Exception\InvalidArgumentException * @deprecated static factories are deprecated as of zendframework 2.9 and may be removed in future major versions. */ - public static function factory($cfg) + public static function factory($config) { trigger_error(sprintf( - '%s is deprecated; please use %s::createFromCachesConfig instead', + '%s is deprecated; please use %s::get instead', __METHOD__, - Storage\StorageFactory::class + Storage\AdapterPluginManager::class ), E_USER_DEPRECATED); - return (new Storage\StorageFactory(static::getAdapterPluginManager(), static::getPluginManager())) - ->createFromCachesConfig($cfg); + if ($config instanceof Traversable) { + $config = ArrayUtils::iteratorToArray($config); + } + + if (! is_array($config)) { + throw new Exception\InvalidArgumentException( + 'The factory needs an associative array ' + . 'or a Traversable object as an argument' + ); + } + + // instantiate the adapter + if (! isset($config['adapter'])) { + throw new Exception\InvalidArgumentException('Missing "adapter"'); + } + + $adapterName = $config['adapter']; + $adapterOptions = []; + if (is_array($config['adapter'])) { + if (! isset($config['adapter']['name'])) { + throw new Exception\InvalidArgumentException('Missing "adapter.name"'); + } + + $adapterName = $config['adapter']['name']; + $adapterOptions = isset($config['adapter']['options']) ? $config['adapter']['options'] : []; + } + if (isset($config['options'])) { + $adapterOptions = ArrayUtils::merge($adapterOptions, $config['options']); + } + + $adapter = self::adapterFactory((string) $adapterName, $adapterOptions); + + // add plugins + if (isset($config['plugins'])) { + if (! $adapter instanceof EventsCapableInterface) { + throw new Exception\RuntimeException(sprintf( + "The adapter '%s' doesn't implement '%s' and therefore can't handle plugins", + get_class($adapter), + EventsCapableInterface::class + )); + } + + if (! is_array($config['plugins'])) { + throw new Exception\InvalidArgumentException( + 'Plugins needs to be an array' + ); + } + + foreach ($config['plugins'] as $k => $v) { + $pluginPrio = 1; // default priority + + if (is_string($k)) { + if (! is_array($v)) { + throw new Exception\InvalidArgumentException( + "'plugins.{$k}' needs to be an array" + ); + } + $pluginName = $k; + $pluginOptions = $v; + } elseif (is_array($v)) { + if (! isset($v['name'])) { + throw new Exception\InvalidArgumentException( + "Invalid plugins[{$k}] or missing plugins[{$k}].name" + ); + } + $pluginName = (string) $v['name']; + + if (isset($v['options'])) { + $pluginOptions = $v['options']; + } else { + $pluginOptions = []; + } + + if (isset($v['priority'])) { + $pluginPrio = $v['priority']; + } + } else { + $pluginName = $v; + $pluginOptions = []; + } + + $plugin = static::pluginFactory($pluginName, $pluginOptions); + if (! $adapter->hasPlugin($plugin)) { + $adapter->addPlugin($plugin, $pluginPrio); + } + } + } + + return $adapter; } /** diff --git a/test/Service/StorageCacheAbstractServiceFactoryTest.php b/test/Service/StorageCacheAbstractServiceFactoryTest.php index 9e4487bc8..1f0e73cd2 100644 --- a/test/Service/StorageCacheAbstractServiceFactoryTest.php +++ b/test/Service/StorageCacheAbstractServiceFactoryTest.php @@ -98,7 +98,7 @@ public function testSetsFactoryAdapterPluginManagerInstanceOnInvocation() $adapter->addPlugin(Argument::any(), Argument::any())->shouldNotBeCalled(); $adapterPluginManager = $this->prophesize(AdapterPluginManager::class); - $adapterPluginManager->get('Memory', [])->willReturn($adapter->reveal()); + $adapterPluginManager->get('Memory')->willReturn($adapter->reveal()); $container = $this->prophesize(ContainerInterface::class); $container->has(AdapterPluginManager::class)->willReturn(true); @@ -121,7 +121,7 @@ public function testSetsFactoryPluginManagerInstanceOnInvocation() $plugin->setOptions(Argument::any())->shouldNotBeCalled(); $pluginManager = $this->prophesize(PluginManager::class); - $pluginManager->get('Serializer', [])->willReturn($plugin->reveal()); + $pluginManager->get('Serializer')->willReturn($plugin->reveal()); $adapter = $this->prophesize(AbstractAdapter::class); $adapter->willImplement(StorageInterface::class); @@ -130,7 +130,7 @@ public function testSetsFactoryPluginManagerInstanceOnInvocation() $adapter->addPlugin($plugin->reveal(), Argument::any())->shouldBeCalled(); $adapterPluginManager = $this->prophesize(AdapterPluginManager::class); - $adapterPluginManager->get('Memory', [])->willReturn($adapter->reveal()); + $adapterPluginManager->get('Memory')->willReturn($adapter->reveal()); $container = $this->prophesize(ContainerInterface::class); $container->has(AdapterPluginManager::class)->willReturn(true); diff --git a/test/Service/StorageCacheFactoryTest.php b/test/Service/StorageCacheFactoryTest.php index ddaaa67ba..dc7d3ebd0 100644 --- a/test/Service/StorageCacheFactoryTest.php +++ b/test/Service/StorageCacheFactoryTest.php @@ -75,7 +75,7 @@ public function testSetsFactoryAdapterPluginManagerInstanceOnInvocation() $adapter->addPlugin(Argument::any(), Argument::any())->shouldNotBeCalled(); $adapterPluginManager = $this->prophesize(AdapterPluginManager::class); - $adapterPluginManager->get('Memory', [])->willReturn($adapter->reveal()); + $adapterPluginManager->get('Memory')->willReturn($adapter->reveal()); $container = $this->prophesize(ContainerInterface::class); $container->has(AdapterPluginManager::class)->willReturn(true); @@ -98,7 +98,7 @@ public function testSetsFactoryPluginManagerInstanceOnInvocation() $plugin->setOptions(Argument::any())->shouldNotBeCalled(); $pluginManager = $this->prophesize(PluginManager::class); - $pluginManager->get('Serializer', [])->willReturn($plugin->reveal()); + $pluginManager->get('Serializer')->willReturn($plugin->reveal()); $adapter = $this->prophesize(AbstractAdapter::class); $adapter->willImplement(StorageInterface::class); @@ -107,7 +107,7 @@ public function testSetsFactoryPluginManagerInstanceOnInvocation() $adapter->addPlugin($plugin->reveal(), Argument::any())->shouldBeCalled(); $adapterPluginManager = $this->prophesize(AdapterPluginManager::class); - $adapterPluginManager->get('Memory', [])->willReturn($adapter->reveal()); + $adapterPluginManager->get('Memory')->willReturn($adapter->reveal()); $container = $this->prophesize(ContainerInterface::class); $container->has(AdapterPluginManager::class)->willReturn(true); diff --git a/test/Service/StorageFactoryFactoryTest.php b/test/Service/StorageFactoryFactoryTest.php deleted file mode 100644 index ee706d8fd..000000000 --- a/test/Service/StorageFactoryFactoryTest.php +++ /dev/null @@ -1,66 +0,0 @@ -factory = new StorageFactoryFactory(); - } - - - public function testFactoryCreatesServiceV2() - { - if (method_exists(new ServiceManager(), 'configure')) { - $this->markTestSkipped('Test is only needed in ZF2'); - } - - $adapterPluginManager = $this->prophesize(AdapterPluginManager::class); - $pluginManager = $this->prophesize(PluginManager::class); - - $container = $this->prophesize(ServiceLocatorInterface::class); - $container->get(AdapterPluginManager::class)->willReturn($adapterPluginManager); - - $container->get(PluginManager::class)->willReturn($pluginManager); - - $instance = $this->factory->createService($container->reveal()); - $this->assertInstanceOf(StorageFactory::class, $instance); - } - - public function testFactoryCreatesServiceV3() - { - if (! method_exists(new ServiceManager(), 'configure')) { - $this->markTestSkipped('Test is only needed in ZF3'); - } - - $adapterPluginManager = $this->prophesize(AdapterPluginManager::class); - $pluginManager = $this->prophesize(PluginManager::class); - - $container = $this->prophesize(ContainerInterface::class); - $container->get(AdapterPluginManager::class)->willReturn($adapterPluginManager); - - $container->get(PluginManager::class)->willReturn($pluginManager); - - $instance = $this->factory->__invoke($container->reveal(), StorageFactory::class); - - $this->assertInstanceOf(StorageFactory::class, $instance); - } -} diff --git a/test/Storage/AdapterPluginManagerTest.php b/test/Storage/AdapterPluginManagerTest.php index 08310963e..df9b5b4a7 100644 --- a/test/Storage/AdapterPluginManagerTest.php +++ b/test/Storage/AdapterPluginManagerTest.php @@ -68,4 +68,59 @@ public function testOptionsWillBeSet() $adapterOptions = $storage->getOptions(); $this->assertArraySubset($options, $adapterOptions->toArray()); } + + /** + * @dataProvider complexConfigurationProvider + */ + public function testComplexConfigurationIsBeingParsed(array $options, array $adapter, array $plugins) + { + /** @var StorageInterface $storage */ + $storage = $this->getPluginManager()->get('Memory', $options); + + $this->assertArraySubset($adapter, $storage->getOptions()->toArray()); + $this->assertCount(count($plugins), $storage->getPluginRegistry()); + } + + public function complexConfigurationProvider() + { + $adapterOptions = [ + 'readable' => false, + 'ttl' => 9999, + 'namespace' => 'test', + ]; + + $pluginOptions = [ + 'Serializer' => [ + 'options' => [], + 'priority' => 1, + ], + 'ClearExpiredByFactor' => [ + 'options' => [], + 'priority' => 2, + ], + ]; + + return [ + 'default_options' => [ + 'options_provided_to_pluginmanager' => $adapterOptions, + 'adapter_options_for_comparison' => $adapterOptions, + 'plugin_configuration_for_comparison' => [], + ], + 'options_with_plugins' => [ + 'options_provided_to_pluginmanager' => ['options' => $adapterOptions, 'plugins' => array_keys($pluginOptions)], + 'adapter_options_for_comparison' => $adapterOptions, + 'plugin_configuration_for_comparison' => array_keys($pluginOptions), + ], + 'options_with_complex_plugins' => [ + 'options_provided_to_pluginmanager' => ['options' => $adapterOptions, 'plugins' => $pluginOptions], + 'adapter_options_for_comparison' => $adapterOptions, + 'plugin_configuration_for_comparison' => $pluginOptions, + ], + 'options_with_added_plugins' => [ + 'options_provided_to_pluginmanager' => array_merge($adapterOptions, ['plugins' => $pluginOptions]), + 'adapter_options_for_comparison' => $adapterOptions, + 'plugin_configuration_for_comparison' => $pluginOptions, + ], + ]; + } } diff --git a/test/StorageFactoryTest.php b/test/StorageFactoryTest.php index fbfdd8f12..5785e90df 100644 --- a/test/StorageFactoryTest.php +++ b/test/StorageFactoryTest.php @@ -18,7 +18,6 @@ /** * @group Zend_Cache * @covers Zend\Cache\StorageFactory - * @covers Zend\Cache\Storage\StorageFactory */ class StorageFactoryTest extends TestCase { From c29ee81cc31983cee383e3d2e3678a42697ce4e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= Date: Tue, 11 Sep 2018 14:46:57 +0200 Subject: [PATCH 11/11] Fix unit test has more than 120 chars in line warning... --- test/Storage/AdapterPluginManagerTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/Storage/AdapterPluginManagerTest.php b/test/Storage/AdapterPluginManagerTest.php index df9b5b4a7..94514a8ad 100644 --- a/test/Storage/AdapterPluginManagerTest.php +++ b/test/Storage/AdapterPluginManagerTest.php @@ -107,7 +107,10 @@ public function complexConfigurationProvider() 'plugin_configuration_for_comparison' => [], ], 'options_with_plugins' => [ - 'options_provided_to_pluginmanager' => ['options' => $adapterOptions, 'plugins' => array_keys($pluginOptions)], + 'options_provided_to_pluginmanager' => [ + 'options' => $adapterOptions, + 'plugins' => array_keys($pluginOptions) + ], 'adapter_options_for_comparison' => $adapterOptions, 'plugin_configuration_for_comparison' => array_keys($pluginOptions), ],