From 0990d6475601e5993e81cbbb4463f7505b5a065e Mon Sep 17 00:00:00 2001 From: Christian Schmidt Date: Sat, 8 Nov 2014 15:05:56 +0100 Subject: [PATCH 1/3] Ensure metadata cache is not ArrayCache in production --- lib/Doctrine/ORM/Configuration.php | 3 + lib/Doctrine/ORM/ORMException.php | 8 +++ .../Doctrine/Tests/ORM/ConfigurationTest.php | 64 +++++++++++++------ 3 files changed, 54 insertions(+), 21 deletions(-) diff --git a/lib/Doctrine/ORM/Configuration.php b/lib/Doctrine/ORM/Configuration.php index 02433b93c..e92a04cd5 100644 --- a/lib/Doctrine/ORM/Configuration.php +++ b/lib/Doctrine/ORM/Configuration.php @@ -388,6 +388,9 @@ class Configuration extends \Doctrine\DBAL\Configuration if ( ! $this->getMetadataCacheImpl()) { throw ORMException::metadataCacheNotConfigured(); } + if ($this->getMetadataCacheImpl() instanceof ArrayCache) { + throw ORMException::metadataCacheUsesArrayCache(); + } if ($this->getAutoGenerateProxyClasses()) { throw ORMException::proxyClassesAlwaysRegenerating(); diff --git a/lib/Doctrine/ORM/ORMException.php b/lib/Doctrine/ORM/ORMException.php index 799fd1b8d..0cea4e087 100644 --- a/lib/Doctrine/ORM/ORMException.php +++ b/lib/Doctrine/ORM/ORMException.php @@ -232,6 +232,14 @@ class ORMException extends Exception return new self('Class Metadata Cache is not configured.'); } + /** + * @return ORMException + */ + public static function metadataCacheUsesArrayCache() + { + return new self('Metadata Cache uses ArrayCache.'); + } + /** * @return ORMException */ diff --git a/tests/Doctrine/Tests/ORM/ConfigurationTest.php b/tests/Doctrine/Tests/ORM/ConfigurationTest.php index 4d0c28349..c7fe0981b 100644 --- a/tests/Doctrine/Tests/ORM/ConfigurationTest.php +++ b/tests/Doctrine/Tests/ORM/ConfigurationTest.php @@ -2,6 +2,7 @@ namespace Doctrine\Tests\ORM; +use Doctrine\Common\Cache\ArrayCache; use Doctrine\ORM\Mapping as AnnotationNamespace; use Doctrine\ORM\Configuration; use Doctrine\ORM\ORMException; @@ -136,31 +137,52 @@ class ConfigurationTest extends PHPUnit_Framework_TestCase $this->configuration->getNamedQuery('NonExistingQuery'); } - public function ensureProductionSettings() + protected function setProductionSettings($skipCache = false) { $cache = $this->getMock('Doctrine\Common\Cache\Cache'); - $this->configuration->setAutoGenerateProxyClasses(true); - - try { - $this->configuration->ensureProductionSettings(); - $this->fail('Didn\'t check all production settings'); - } catch (ORMException $e) {} - - $this->configuration->setQueryCacheImpl($cache); - - try { - $this->configuration->ensureProductionSettings(); - $this->fail('Didn\'t check all production settings'); - } catch (ORMException $e) {} - - $this->configuration->setMetadataCacheImpl($cache); - - try { - $this->configuration->ensureProductionSettings(); - $this->fail('Didn\'t check all production settings'); - } catch (ORMException $e) {} $this->configuration->setAutoGenerateProxyClasses(false); + if ($skipCache !== 'query') { + $this->configuration->setQueryCacheImpl($cache); + } + if ($skipCache !== 'metadata') { + $this->configuration->setMetadataCacheImpl($cache); + } + } + + public function testEnsureProductionSettings() + { + $this->setProductionSettings(); + $this->configuration->ensureProductionSettings(); + } + + public function testEnsureProductionSettingsQueryCache() + { + $this->setProductionSettings('query'); + $this->setExpectedException('Doctrine\ORM\ORMException', 'Query Cache is not configured.'); + $this->configuration->ensureProductionSettings(); + } + + public function testEnsureProductionSettingsMetadataCache() + { + $this->setProductionSettings('metadata'); + $this->setExpectedException('Doctrine\ORM\ORMException', 'Metadata Cache is not configured.'); + $this->configuration->ensureProductionSettings(); + } + + public function testEnsureProductionSettingsMetadataArrayCache() + { + $this->setProductionSettings(); + $this->configuration->setMetadataCacheImpl(new ArrayCache()); + $this->setExpectedException('Doctrine\ORM\ORMException', 'Metadata Cache uses ArrayCache.'); + $this->configuration->ensureProductionSettings(); + } + + public function testEnsureProductionSettingsAutoGenerateProxyClasses() + { + $this->setProductionSettings(); + $this->configuration->setAutoGenerateProxyClasses(true); + $this->setExpectedException('Doctrine\ORM\ORMException', 'Proxy Classes are always regenerating.'); $this->configuration->ensureProductionSettings(); } From c973d8df1a601dc69a418015c7b96c2f5f749752 Mon Sep 17 00:00:00 2001 From: Christian Schmidt Date: Tue, 11 Nov 2014 07:35:52 +0100 Subject: [PATCH 2/3] Code style fixes. --- lib/Doctrine/ORM/Configuration.php | 9 +++-- lib/Doctrine/ORM/ORMException.php | 7 +++- .../Doctrine/Tests/ORM/ConfigurationTest.php | 38 ++++++++++++++++--- 3 files changed, 43 insertions(+), 11 deletions(-) diff --git a/lib/Doctrine/ORM/Configuration.php b/lib/Doctrine/ORM/Configuration.php index e92a04cd5..3c8a44762 100644 --- a/lib/Doctrine/ORM/Configuration.php +++ b/lib/Doctrine/ORM/Configuration.php @@ -385,11 +385,14 @@ class Configuration extends \Doctrine\DBAL\Configuration throw ORMException::queryCacheNotConfigured(); } - if ( ! $this->getMetadataCacheImpl()) { + $metadataCacheImpl = $this->getMetadataCacheImpl(); + + if ( ! $metadataCacheImpl) { throw ORMException::metadataCacheNotConfigured(); } - if ($this->getMetadataCacheImpl() instanceof ArrayCache) { - throw ORMException::metadataCacheUsesArrayCache(); + + if ($metadataCacheImpl instanceof ArrayCache) { + throw ORMException::metadataCacheUsesNonPersistentCache($metadataCacheImpl); } if ($this->getAutoGenerateProxyClasses()) { diff --git a/lib/Doctrine/ORM/ORMException.php b/lib/Doctrine/ORM/ORMException.php index 0cea4e087..d4894e825 100644 --- a/lib/Doctrine/ORM/ORMException.php +++ b/lib/Doctrine/ORM/ORMException.php @@ -19,6 +19,7 @@ namespace Doctrine\ORM; +use Doctrine\Common\Cache\Cache; use Exception; /** @@ -233,11 +234,13 @@ class ORMException extends Exception } /** + * @param Cache $cache + * * @return ORMException */ - public static function metadataCacheUsesArrayCache() + public static function metadataCacheUsesNonPersistentCache(Cache $cache) { - return new self('Metadata Cache uses ArrayCache.'); + return new self('Metadata Cache uses a non-persistent cache driver, ' . get_class($cache) . '.'); } /** diff --git a/tests/Doctrine/Tests/ORM/ConfigurationTest.php b/tests/Doctrine/Tests/ORM/ConfigurationTest.php index c7fe0981b..4b51312ba 100644 --- a/tests/Doctrine/Tests/ORM/ConfigurationTest.php +++ b/tests/Doctrine/Tests/ORM/ConfigurationTest.php @@ -2,6 +2,7 @@ namespace Doctrine\Tests\ORM; +use Doctrine\Common\Proxy\AbstractProxyFactory; use Doctrine\Common\Cache\ArrayCache; use Doctrine\ORM\Mapping as AnnotationNamespace; use Doctrine\ORM\Configuration; @@ -137,15 +138,22 @@ class ConfigurationTest extends PHPUnit_Framework_TestCase $this->configuration->getNamedQuery('NonExistingQuery'); } + /** + * Configures $this->configuration to use production settings. + * + * @param boolean $skipCache Do not configure a cache of this type, either "query" or "metadata". + */ protected function setProductionSettings($skipCache = false) { + $this->configuration->setAutoGenerateProxyClasses(AbstractProxyFactory::AUTOGENERATE_NEVER); + $cache = $this->getMock('Doctrine\Common\Cache\Cache'); - $this->configuration->setAutoGenerateProxyClasses(false); - if ($skipCache !== 'query') { + if ('query' !== $skipCache) { $this->configuration->setQueryCacheImpl($cache); } - if ($skipCache !== 'metadata') { + + if ('metadata' !== $skipCache) { $this->configuration->setMetadataCacheImpl($cache); } } @@ -174,14 +182,32 @@ class ConfigurationTest extends PHPUnit_Framework_TestCase { $this->setProductionSettings(); $this->configuration->setMetadataCacheImpl(new ArrayCache()); - $this->setExpectedException('Doctrine\ORM\ORMException', 'Metadata Cache uses ArrayCache.'); + $this->setExpectedException( + 'Doctrine\ORM\ORMException', + 'Metadata Cache uses a non-persistent cache driver, Doctrine\Common\Cache\ArrayCache.'); $this->configuration->ensureProductionSettings(); } - public function testEnsureProductionSettingsAutoGenerateProxyClasses() + public function testEnsureProductionSettingsAutoGenerateProxyClassesAlways() { $this->setProductionSettings(); - $this->configuration->setAutoGenerateProxyClasses(true); + $this->configuration->setAutoGenerateProxyClasses(AbstractProxyFactory::AUTOGENERATE_ALWAYS); + $this->setExpectedException('Doctrine\ORM\ORMException', 'Proxy Classes are always regenerating.'); + $this->configuration->ensureProductionSettings(); + } + + public function testEnsureProductionSettingsAutoGenerateProxyClassesFileNotExists() + { + $this->setProductionSettings(); + $this->configuration->setAutoGenerateProxyClasses(AbstractProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS); + $this->setExpectedException('Doctrine\ORM\ORMException', 'Proxy Classes are always regenerating.'); + $this->configuration->ensureProductionSettings(); + } + + public function testEnsureProductionSettingsAutoGenerateProxyClassesEval() + { + $this->setProductionSettings(); + $this->configuration->setAutoGenerateProxyClasses(AbstractProxyFactory::AUTOGENERATE_EVAL); $this->setExpectedException('Doctrine\ORM\ORMException', 'Proxy Classes are always regenerating.'); $this->configuration->ensureProductionSettings(); } From 3287ce12a49c9475d7c42825042ec8ea6395f07f Mon Sep 17 00:00:00 2001 From: Christian Schmidt Date: Tue, 11 Nov 2014 08:31:36 +0100 Subject: [PATCH 3/3] Fix namespace collission --- lib/Doctrine/ORM/ORMException.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/ORM/ORMException.php b/lib/Doctrine/ORM/ORMException.php index d4894e825..cbbada57d 100644 --- a/lib/Doctrine/ORM/ORMException.php +++ b/lib/Doctrine/ORM/ORMException.php @@ -19,7 +19,7 @@ namespace Doctrine\ORM; -use Doctrine\Common\Cache\Cache; +use Doctrine\Common\Cache\Cache as CacheDriver; use Exception; /** @@ -234,11 +234,11 @@ class ORMException extends Exception } /** - * @param Cache $cache + * @param \Doctrine\Common\Cache\Cache $cache * * @return ORMException */ - public static function metadataCacheUsesNonPersistentCache(Cache $cache) + public static function metadataCacheUsesNonPersistentCache(CacheDriver $cache) { return new self('Metadata Cache uses a non-persistent cache driver, ' . get_class($cache) . '.'); }