From 93e6cabe0428095673dea4cde3b1506cab381d64 Mon Sep 17 00:00:00 2001 From: jwage Date: Thu, 22 Oct 2009 22:19:17 +0000 Subject: [PATCH] [2.0][DDC-48][DDC-47] Refactored cache drivers to allow more control over deleting, added namespacing to cache drivers and implemented clear-cache task --- lib/Doctrine/Common/Cache/AbstractCache.php | 280 ++++++++++++++++++ lib/Doctrine/Common/Cache/ApcCache.php | 10 +- lib/Doctrine/Common/Cache/ArrayCache.php | 30 +- lib/Doctrine/Common/Cache/MemcacheCache.php | 10 +- lib/Doctrine/Common/Cache/XcacheCache.php | 12 +- lib/Doctrine/Common/DoctrineException.php | 6 +- lib/Doctrine/ORM/Configuration.php | 2 + lib/Doctrine/ORM/Tools/Cli.php | 22 +- .../ORM/Tools/Cli/Tasks/ClearCacheTask.php | 215 ++++++++++++++ .../Tools/Cli/Tasks/ConvertMappingTask.php | 2 +- .../Tasks/EnsureProductionSettingsTask.php | 69 +++++ .../Doctrine/Tests/Common/Cache/AllTests.php | 3 +- .../Doctrine/Tests/Common/Cache/CacheTest.php | 79 +++++ 13 files changed, 687 insertions(+), 53 deletions(-) create mode 100644 lib/Doctrine/Common/Cache/AbstractCache.php create mode 100644 lib/Doctrine/ORM/Tools/Cli/Tasks/ClearCacheTask.php create mode 100644 lib/Doctrine/ORM/Tools/Cli/Tasks/EnsureProductionSettingsTask.php create mode 100644 tests/Doctrine/Tests/Common/Cache/CacheTest.php diff --git a/lib/Doctrine/Common/Cache/AbstractCache.php b/lib/Doctrine/Common/Cache/AbstractCache.php new file mode 100644 index 000000000..f761dda0b --- /dev/null +++ b/lib/Doctrine/Common/Cache/AbstractCache.php @@ -0,0 +1,280 @@ +. + */ + +namespace Doctrine\Common\Cache; + +/** + * Abstract cache driver class + * + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.doctrine-project.org + * @since 2.0 + * @version $Revision: 3938 $ + * @author Guilherme Blanco + * @author Jonathan Wage + * @author Roman Borschel + */ +abstract class AbstractCache implements Cache +{ + /* @var string $cacheIdsIndexId The cache id to store the index of cache ids under */ + private $_cacheIdsIndexId = 'doctrine_cache_ids'; + + /* @var string $namespace The namespace to prefix all cache ids with */ + private $_namespace = null; + + /** + * Set the namespace to prefix all cache ids with + * + * @param string $namespace + * @return void + */ + public function setNamespace($namespace) + { + $this->_namespace = $namespace; + } + + /** + * {@inheritdoc} + */ + public function fetch($id) + { + $id = $this->_getNamespacedId($id); + return $this->_doFetch($this->_getNamespacedId($id)); + } + + /** + * {@inheritdoc} + */ + public function contains($id) + { + $id = $this->_getNamespacedId($id); + return $this->_doContains($id); + } + + /** + * {@inheritdoc} + */ + public function save($id, $data, $lifeTime = false) + { + $id = $this->_getNamespacedId($id); + if ($this->_doSave($id, $data, $lifeTime)) { + $this->_saveId($id); + + return true; + } + return false; + } + + /** + * {@inheritdoc} + */ + public function delete($id) + { + $id = $this->_getNamespacedId($id); + + if (strpos($id, '*') !== false) { + return $this->deleteByRegex('/' . str_replace('*', '.*', $id) . '/'); + } + + if ($this->_doDelete($id)) { + $this->_deleteId($id); + + return true; + } + return false; + } + + /** + * Delete all cache entries. + * + * @return array $deleted Array of the deleted cache ids + */ + public function deleteAll() + { + $ids = $this->getIds(); + foreach ($ids as $id) { + $this->delete($id); + } + return $ids; + } + + /** + * Delete cache entries where the id matches a PHP regular expressions + * + * @param string $regex + * @return array $deleted Array of the deleted cache ids + */ + public function deleteByRegex($regex) + { + $deleted = array(); + $ids = $this->getIds(); + foreach ($ids as $id) { + if (preg_match($regex, $id)) { + $this->delete($id); + $deleted[] = $id; + } + } + return $deleted; + } + + /** + * Delete cache entries where the id has the passed prefix + * + * @param string $prefix + * @return array $deleted Array of the deleted cache ids + */ + public function deleteByPrefix($prefix) + { + $deleted = array(); + $ids = $this->getIds(); + foreach ($ids as $id) { + if (strpos($id, $prefix) == 0) { + $this->delete($id); + $deleted[] = $id; + } + } + return $deleted; + } + + /** + * Delete cache entries where the id has the passed suffix + * + * @param string $suffix + * @return array $deleted Array of the deleted cache ids + */ + public function deleteBySuffix($suffix) + { + $deleted = array(); + $ids = $this->getIds(); + foreach ($ids as $id) { + if (substr($id, -1 * strlen($suffix)) == $suffix) { + $this->delete($id); + $deleted[] = $id; + } + } + return $deleted; + } + + /** + * Count and return the number of cache entries. + * + * @return integer $count + */ + public function count() + { + $ids = $this->getIds(); + return $ids ? count($ids) : 0; + } + + /** + * Get an array of all the cache ids stored + * + * @return array $ids + */ + public function getIds() + { + $ids = $this->fetch($this->_cacheIdsIndexId); + return $ids ? $ids : array(); + } + + /** + * Prefix the passed id with the configured namespace value + * + * @param string $id The id to namespace + * @return string $id The namespaced id + */ + private function _getNamespacedId($id) + { + if ( ! $this->_namespace || strpos($id, $this->_namespace) === 0) { + return $id; + } else { + return $this->_namespace . $id; + } + } + + /** + * Save a cache id to the index of cache ids + * + * @param string $id + * @return boolean TRUE if the id was successfully stored in the cache, FALSE otherwise. + */ + private function _saveId($id) + { + $ids = $this->getIds(); + $ids[] = $id; + + $cacheIdsIndexId = $this->_getNamespacedId($this->_cacheIdsIndexId); + return $this->_doSave($cacheIdsIndexId, $ids, null); + } + + /** + * Delete a cache id from the index of cache ids + * + * @param string $id + * @return boolean TRUE if the entry was successfully removed from the cache, FALSE otherwise. + */ + private function _deleteId($id) + { + $ids = $this->getIds(); + $key = array_search($id, $ids); + if ($key !== false) { + unset($ids[$key]); + + $cacheIdsIndexId = $this->_getNamespacedId($this->_cacheIdsIndexId); + return $this->_doSave($cacheIdsIndexId, $ids, null); + } + return false; + } + + /** + * Fetches an entry from the cache. + * + * @param string $id cache id The id of the cache entry to fetch. + * @return string The cached data or FALSE, if no cache entry exists for the given id. + */ + abstract protected function _doFetch($id); + + /** + * Test if an entry exists in the cache. + * + * @param string $id cache id The cache id of the entry to check for. + * @return boolean TRUE if a cache entry exists for the given cache id, FALSE otherwise. + */ + abstract protected function _doContains($id); + + /** + * Puts data into the cache. + * + * @param string $id The cache id. + * @param string $data The cache entry/data. + * @param int $lifeTime The lifetime. If != false, sets a specific lifetime for this cache entry (null => infinite lifeTime). + * @return boolean TRUE if the entry was successfully stored in the cache, FALSE otherwise. + */ + abstract protected function _doSave($id, $data, $lifeTime = false); + + /** + * Deletes a cache entry. + * + * @param string $id cache id + * @return boolean TRUE if the cache entry was successfully deleted, FALSE otherwise. + */ + abstract protected function _doDelete($id); +} \ No newline at end of file diff --git a/lib/Doctrine/Common/Cache/ApcCache.php b/lib/Doctrine/Common/Cache/ApcCache.php index 73268aa54..d2a0f4b14 100644 --- a/lib/Doctrine/Common/Cache/ApcCache.php +++ b/lib/Doctrine/Common/Cache/ApcCache.php @@ -32,12 +32,12 @@ namespace Doctrine\Common\Cache; * @author Jonathan Wage * @author Roman Borschel */ -class ApcCache implements Cache +class ApcCache extends AbstractCache { /** * {@inheritdoc} */ - public function fetch($id) + protected function _doFetch($id) { return apc_fetch($id); } @@ -45,7 +45,7 @@ class ApcCache implements Cache /** * {@inheritdoc} */ - public function contains($id) + protected function _doContains($id) { return apc_fetch($id) === false ? false : true; } @@ -53,7 +53,7 @@ class ApcCache implements Cache /** * {@inheritdoc} */ - public function save($id, $data, $lifeTime = false) + protected function _doSave($id, $data, $lifeTime = false) { return (bool) apc_store($id, $data, $lifeTime); } @@ -61,7 +61,7 @@ class ApcCache implements Cache /** * {@inheritdoc} */ - public function delete($id) + protected function _doDelete($id) { return apc_delete($id); } diff --git a/lib/Doctrine/Common/Cache/ArrayCache.php b/lib/Doctrine/Common/Cache/ArrayCache.php index c00d71112..636379fb6 100644 --- a/lib/Doctrine/Common/Cache/ArrayCache.php +++ b/lib/Doctrine/Common/Cache/ArrayCache.php @@ -32,7 +32,7 @@ namespace Doctrine\Common\Cache; * @author Jonathan Wage * @author Roman Borschel */ -class ArrayCache implements Cache +class ArrayCache extends AbstractCache { /** * @var array $data @@ -42,7 +42,7 @@ class ArrayCache implements Cache /** * {@inheritdoc} */ - public function fetch($id) + protected function _doFetch($id) { if (isset($this->data[$id])) { return $this->data[$id]; @@ -53,7 +53,7 @@ class ArrayCache implements Cache /** * {@inheritdoc} */ - public function contains($id) + protected function _doContains($id) { return isset($this->data[$id]); } @@ -61,34 +61,18 @@ class ArrayCache implements Cache /** * {@inheritdoc} */ - public function save($id, $data, $lifeTime = false) + protected function _doSave($id, $data, $lifeTime = false) { $this->data[$id] = $data; + return true; } /** * {@inheritdoc} */ - public function delete($id) + protected function _doDelete($id) { unset($this->data[$id]); - } - - /** - * {@inheritdoc} - */ - public function deleteAll() - { - $this->data = array(); - } - - /** - * count - * - * @return integer - */ - public function count() - { - return count($this->data); + return true; } } \ No newline at end of file diff --git a/lib/Doctrine/Common/Cache/MemcacheCache.php b/lib/Doctrine/Common/Cache/MemcacheCache.php index f617bf2e6..3ccb3e7a5 100644 --- a/lib/Doctrine/Common/Cache/MemcacheCache.php +++ b/lib/Doctrine/Common/Cache/MemcacheCache.php @@ -34,7 +34,7 @@ use \Memcache; * @author Jonathan Wage * @author Roman Borschel */ -class MemcacheCache implements Cache +class MemcacheCache extends AbstractCache { /** * @var Memcache @@ -64,7 +64,7 @@ class MemcacheCache implements Cache /** * {@inheritdoc} */ - public function fetch($id) + protected function _doFetch($id) { return $this->_memcache->get($id); } @@ -72,7 +72,7 @@ class MemcacheCache implements Cache /** * {@inheritdoc} */ - public function contains($id) + protected function _doContains($id) { return (bool) $this->_memcache->get($id); } @@ -80,7 +80,7 @@ class MemcacheCache implements Cache /** * {@inheritdoc} */ - public function save($id, $data, $lifeTime = false) + protected function _doSave($id, $data, $lifeTime = false) { return $this->_memcache->set($id, $data, 0, $lifeTime); } @@ -88,7 +88,7 @@ class MemcacheCache implements Cache /** * {@inheritdoc} */ - public function delete($id) + protected function _doDelete($id) { return $this->_memcache->delete($id); } diff --git a/lib/Doctrine/Common/Cache/XcacheCache.php b/lib/Doctrine/Common/Cache/XcacheCache.php index 0058c6fb7..dcba81210 100644 --- a/lib/Doctrine/Common/Cache/XcacheCache.php +++ b/lib/Doctrine/Common/Cache/XcacheCache.php @@ -32,20 +32,20 @@ namespace Doctrine\Common\Cache; * @author Jonathan Wage * @author Roman Borschel */ -class XcacheCache implements Cache +class XcacheCache extends AbstractCache { /** * {@inheritdoc} */ - public function fetch($id) + protected function _doFetch($id) { - return $this->contains($id) ? xcache_get($id) : false; + return $this->_doContains($id) ? xcache_get($id) : false; } /** * {@inheritdoc} */ - public function contains($id) + protected function _doContains($id) { return xcache_isset($id); } @@ -53,7 +53,7 @@ class XcacheCache implements Cache /** * {@inheritdoc} */ - public function save($id, $data, $lifeTime = false) + protected function _doSave($id, $data, $lifeTime = false) { return xcache_set($id, $data, $lifeTime); } @@ -61,7 +61,7 @@ class XcacheCache implements Cache /** * {@inheritdoc} */ - public function delete($id) + protected function _doDelete($id) { return xcache_unset($id); } diff --git a/lib/Doctrine/Common/DoctrineException.php b/lib/Doctrine/Common/DoctrineException.php index b3de944de..9102a6213 100644 --- a/lib/Doctrine/Common/DoctrineException.php +++ b/lib/Doctrine/Common/DoctrineException.php @@ -102,8 +102,10 @@ class DoctrineException extends \Exception } else { $dumper = function ($value) { return var_export($value, true); }; $message = strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $method)); - $message = ucfirst(str_replace('_', ' ', $message)) - . ' (' . implode(', ', array_map($dumper, $arguments)) . ')'; + $message = ucfirst(str_replace('_', ' ', $message)); + if ($arguments) { + $message .= ' (' . implode(', ', array_map($dumper, $arguments)) . ')'; + } } return new $class($message, $innerException); diff --git a/lib/Doctrine/ORM/Configuration.php b/lib/Doctrine/ORM/Configuration.php index 8c946b655..e6ef4a67c 100644 --- a/lib/Doctrine/ORM/Configuration.php +++ b/lib/Doctrine/ORM/Configuration.php @@ -21,6 +21,8 @@ namespace Doctrine\ORM; +use Doctrine\Common\DoctrineException; + /** * Configuration container for all configuration options of Doctrine. * It combines all configuration options from DBAL & ORM. diff --git a/lib/Doctrine/ORM/Tools/Cli.php b/lib/Doctrine/ORM/Tools/Cli.php index 17f75f9cd..07055a7e8 100644 --- a/lib/Doctrine/ORM/Tools/Cli.php +++ b/lib/Doctrine/ORM/Tools/Cli.php @@ -32,7 +32,8 @@ use Doctrine\Common\Util\Inflector, * To include a new Task support, create a task: * * [php] - * class MyProject\Tools\Cli\Tasks\MyTask extends Doctrine\ORM\Tools\Cli\AbstractTask { + * class MyProject\Tools\Cli\Tasks\MyTask extends Doctrine\ORM\Tools\Cli\AbstractTask + * { * public function run(); * public function basicHelp(); * public function extendedHelp(); @@ -47,8 +48,7 @@ use Doctrine\Common\Util\Inflector, * * To execute, just type any classify-able name: * - * [bash] - * cli.php my-task + * $ cli.php my-task * * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @link www.doctrine-project.org @@ -84,13 +84,15 @@ class Cli $ns = 'Doctrine\ORM\Tools\Cli\Tasks'; $this->addTasks(array( - 'help' => $ns . '\HelpTask', - 'version' => $ns . '\VersionTask', - 'schema-tool' => $ns . '\SchemaToolTask', - 'run-sql' => $ns . '\RunSqlTask', - 'run-dql' => $ns . '\RunDqlTask', - 'convert-mapping' => $ns . '\ConvertMappingTask', - 'generate-proxies'=> $ns . '\GenerateProxiesTask' + 'help' => $ns . '\HelpTask', + 'version' => $ns . '\VersionTask', + 'schema-tool' => $ns . '\SchemaToolTask', + 'run-sql' => $ns . '\RunSqlTask', + 'run-dql' => $ns . '\RunDqlTask', + 'convert-mapping' => $ns . '\ConvertMappingTask', + 'generate-proxies' => $ns . '\GenerateProxiesTask', + 'clear-cache' => $ns . '\ClearCacheTask', + 'ensure-production-settings' => $ns . '\EnsureProductionSettingsTask' )); } diff --git a/lib/Doctrine/ORM/Tools/Cli/Tasks/ClearCacheTask.php b/lib/Doctrine/ORM/Tools/Cli/Tasks/ClearCacheTask.php new file mode 100644 index 000000000..ad62ee36c --- /dev/null +++ b/lib/Doctrine/ORM/Tools/Cli/Tasks/ClearCacheTask.php @@ -0,0 +1,215 @@ +. + */ + +namespace Doctrine\ORM\Tools\Cli\Tasks; + +use Doctrine\Common\Cache\AbstractDriver; + +/** + * CLI Task to clear the cache of the various cache drivers + * + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.doctrine-project.org + * @since 2.0 + * @version $Revision$ + * @author Guilherme Blanco + * @author Jonathan Wage + * @author Roman Borschel + */ +class ClearCacheTask extends AbstractTask +{ + public function basicHelp() + { + $this->_writeSynopsis($this->getPrinter()); + } + + public function extendedHelp() + { + $printer = $this->getPrinter(); + + $printer->write('Task: ')->writeln('clear-cache', 'KEYWORD') + ->write('Synopsis: '); + $this->_writeSynopsis($printer); + + $printer->writeln('Description: Clear cache from configured query, result and metadata drivers.') + ->writeln('Options:') + ->write('--query', 'OPT_ARG') + ->writeln("\t\t\tClear the query cache.") + ->write('--result', 'OPT_ARG') + ->writeln("\t\tClear the result cache.") + ->write('--metadata', 'OPT_ARG') + ->writeln("\t\tClear the metadata cache.") + ->write('--id=', 'REQ_ARG') + ->writeln("\t\tThe id of the cache entry to delete (accepts * wildcards).") + ->write('--regex=', 'REQ_ARG') + ->writeln("\t\tDelete cache entries that match the given regular expression.") + ->write('--prefix=', 'REQ_ARG') + ->writeln("\tDelete cache entries that have the given prefix.") + ->write('--suffix=', 'REQ_ARG') + ->writeln("\tDelete cache entries that have the given suffix."); + } + + private function _writeSynopsis($printer) + { + $printer->write('clear-cache', 'KEYWORD') + ->write(' (--query | --result | --metadata)', 'OPT_ARG') + ->write(' [--id=]', 'REQ_ARG') + ->write(' [--regex=]', 'REQ_ARG') + ->write(' [--prefix=]', 'REQ_ARG') + ->writeln(' [--suffix=]', 'REQ_ARG'); + } + + public function validate() + { + if ( ! parent::validate()) { + return false; + } + + $printer = $this->getPrinter(); + $args = $this->getArguments(); + + // When clearing the query cache no need to specify + // id, regex, prefix or suffix. + if ((isset($args['query']) || isset($args['metadata'])) + && (isset($args['id']) + || isset($args['regex']) + || isset($args['prefix']) + || isset($args['suffix']))) { + + $printer->writeln('When clearing the query or metadata cache do not specify any --id, --regex, --prefix or --suffix.', 'ERROR'); + + return false; + } + + return true; + } + + public function run() + { + $printer = $this->getPrinter(); + $args = $this->getArguments(); + + $query = isset($args['query']); + $result = isset($args['result']); + $metadata = isset($args['metadata']); + $id = isset($args['id']) ? $args['id'] : null; + $regex = isset($args['regex']) ? $args['regex'] : null; + $prefix = isset($args['prefix']) ? $args['prefix'] : null; + $suffix = isset($args['suffix']) ? $args['suffix'] : null; + + $all = false; + if ( ! $query && ! $result && ! $metadata) { + $all = true; + } + + $configuration = $this->_em->getConfiguration(); + + if ($query || $all) { + $this->_doDelete( + 'query', + $configuration->getQueryCacheImpl(), + $id, + $regex, + $prefix, + $suffix + ); + } + + if ($result || $all) { + $this->_doDelete( + 'result', + $configuration->getResultCacheImpl(), + $id, + $regex, + $prefix, + $suffix + ); + } + + if ($metadata || $all) { + $this->_doDelete( + 'metadata', + $configuration->getMetadataCacheImpl(), + $id, + $regex, + $prefix, + $suffix + ); + } + } + + private function _doDelete($type, $cacheDriver, $id, $regex, $prefix, $suffix) + { + $printer = $this->getPrinter(); + + if ( ! $cacheDriver) { + $printer->writeln('No driver has been configured for the ' . $type . ' cache.', 'ERROR'); + return false; + } + + if ($id) { + $printer->writeln('Clearing ' . $type . ' cache entries that match the id "' . $id . '"', 'INFO'); + + $deleted = $cacheDriver->delete($id); + if (is_array($deleted)) { + $this->_printDeleted($printer, $type, $deleted); + } else if (is_bool($deleted) && $deleted) { + $this->_printDeleted($printer, $type, array($id)); + } + } + + if ($regex) { + $printer->writeln('Clearing ' . $type . ' cache entries that match the regular expression "' . $regex . '"', 'INFO'); + + $this->_printDeleted($printer, $type, $cacheDriver->deleteByRegex('/' . $regex. '/')); + } + + if ($prefix) { + $printer->writeln('Clearing ' . $type . ' cache entries that have the prefix "' . $prefix . '"', 'INFO'); + + $this->_printDeleted($printer, $type, $cacheDriver->deleteByPrefix($prefix)); + } + + if ($suffix) { + $printer->writeln('Clearing ' . $type . ' cache entries that have the suffix "' . $suffix . '"', 'INFO'); + + $this->_printDeleted($printer, $type, $cacheDriver->deleteBySuffix($suffix)); + } + + if ( ! $id && ! $regex && ! $prefix && ! $suffix) { + $printer->writeln('Clearing all ' . $type . ' cache entries', 'INFO'); + + $this->_printDeleted($printer, $type, $cacheDriver->deleteAll()); + } + } + + private function _printDeleted($printer, $type, array $ids) + { + if ( ! empty($ids)) { + foreach ($ids as $id) { + $printer->writeln(' - ' . $id); + } + } else { + $printer->writeln('No ' . $type . ' cache entries found', 'ERROR'); + } + $printer->writeln(""); + } +} \ No newline at end of file diff --git a/lib/Doctrine/ORM/Tools/Cli/Tasks/ConvertMappingTask.php b/lib/Doctrine/ORM/Tools/Cli/Tasks/ConvertMappingTask.php index f5b39c33b..174e38ac2 100644 --- a/lib/Doctrine/ORM/Tools/Cli/Tasks/ConvertMappingTask.php +++ b/lib/Doctrine/ORM/Tools/Cli/Tasks/ConvertMappingTask.php @@ -107,7 +107,7 @@ class ConvertMappingTask extends AbstractTask return false; } if ($args['to'] != 'annotation' && isset($args['extend'])) { - $printer->writeln('You can only use the --extend argument when converting to annoations.'); + $printer->writeln('You can only use the --extend argument when converting to annoations.', 'ERROR'); return false; } if ($args['from'][0] == 'database') { diff --git a/lib/Doctrine/ORM/Tools/Cli/Tasks/EnsureProductionSettingsTask.php b/lib/Doctrine/ORM/Tools/Cli/Tasks/EnsureProductionSettingsTask.php new file mode 100644 index 000000000..eefacca18 --- /dev/null +++ b/lib/Doctrine/ORM/Tools/Cli/Tasks/EnsureProductionSettingsTask.php @@ -0,0 +1,69 @@ +. + */ + +namespace Doctrine\ORM\Tools\Cli\Tasks; + +use Doctrine\Common\Cache\AbstractDriver; + +/** + * CLI Task to ensure that Doctrine is properly configured for a production environment. + * + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.doctrine-project.org + * @since 2.0 + * @version $Revision$ + * @author Guilherme Blanco + * @author Jonathan Wage + * @author Roman Borschel + */ +class EnsureProductionSettingsTask extends AbstractTask +{ + public function basicHelp() + { + $this->_writeSynopsis($this->getPrinter()); + } + + public function extendedHelp() + { + $printer = $this->getPrinter(); + + $printer->write('Task: ')->writeln('ensure-production-settings', 'KEYWORD') + ->write('Synopsis: '); + $this->_writeSynopsis($printer); + + $printer->writeln('Description: Verify that Doctrine is properly configured for a production environment.'); + } + + private function _writeSynopsis($printer) + { + $printer->writeln('ensure-production-settings', 'KEYWORD'); + } + + public function run() + { + $printer = $this->getPrinter(); + try { + $this->_em->getConfiguration()->ensureProductionSettings(); + } catch (\Doctrine\Common\DoctrineException $e) { + $printer->writeln($e->getMessage(), 'ERROR'); + } + } +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/Common/Cache/AllTests.php b/tests/Doctrine/Tests/Common/Cache/AllTests.php index cd3e42627..d3e6f4d77 100644 --- a/tests/Doctrine/Tests/Common/Cache/AllTests.php +++ b/tests/Doctrine/Tests/Common/Cache/AllTests.php @@ -19,9 +19,10 @@ class AllTests { $suite = new \Doctrine\Tests\DoctrineTestSuite('Doctrine Common Cache Tests'); + $suite->addTestSuite('Doctrine\Tests\Common\Cache\CacheTest'); $suite->addTestSuite('Doctrine\Tests\Common\Cache\ApcCacheTest'); $suite->addTestSuite('Doctrine\Tests\Common\Cache\ArrayCacheTest'); - //$suite->addTestSuite('Doctrine\Tests\Common\Cache\MemcacheCacheTest'); + $suite->addTestSuite('Doctrine\Tests\Common\Cache\MemcacheCacheTest'); $suite->addTestSuite('Doctrine\Tests\Common\Cache\XcacheCacheTest'); return $suite; diff --git a/tests/Doctrine/Tests/Common/Cache/CacheTest.php b/tests/Doctrine/Tests/Common/Cache/CacheTest.php new file mode 100644 index 000000000..5c0a96008 --- /dev/null +++ b/tests/Doctrine/Tests/Common/Cache/CacheTest.php @@ -0,0 +1,79 @@ +save('test_key1', '1'); + $cache->save('test_key2', '2'); + $this->assertEquals($cache->count(), 2); + } + + public function testDeleteAll() + { + $cache = new ArrayCache(); + $cache->save('test_key1', '1'); + $cache->save('test_key2', '2'); + $cache->deleteAll(); + + $this->assertEquals($cache->count(), 0); + } + + public function testDeleteByRegex() + { + $cache = new ArrayCache(); + $cache->save('test_key1', '1'); + $cache->save('test_key2', '2'); + $cache->deleteByRegex('/test_key[0-9]/'); + + $this->assertEquals($cache->count(), 0); + } + + public function testDeleteByPrefix() + { + $cache = new ArrayCache(); + $cache->save('test_key1', '1'); + $cache->save('test_key2', '2'); + $cache->deleteByPrefix('test_key'); + + $this->assertEquals($cache->count(), 0); + } + + public function testDeleteBySuffix() + { + $cache = new ArrayCache(); + $cache->save('1test_key', '1'); + $cache->save('2test_key', '2'); + $cache->deleteBySuffix('test_key'); + + $this->assertEquals($cache->count(), 0); + } + + public function testDeleteByWildcard() + { + $cache = new ArrayCache(); + $cache->save('test_key1', '1'); + $cache->save('test_key2', '2'); + $cache->delete('test_key*'); + + $this->assertEquals($cache->count(), 0); + } + + public function testNamespace() + { + $cache = new ArrayCache(); + $cache->setNamespace('test_'); + $cache->save('key1', 'test'); + $this->assertTrue($cache->contains('key1')); + + $ids = $cache->getIds(); + $this->assertTrue(in_array('test_key1', $ids)); + } +} \ No newline at end of file