Skip to content

Commit

Permalink
Streamline cache creation in tests (#9451)
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus authored Jan 31, 2022
1 parent f8de44c commit 92d27f2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 29 deletions.
20 changes: 5 additions & 15 deletions tests/Doctrine/Tests/OrmFunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Doctrine\Tests;

use Doctrine\Common\Cache\Cache;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Platforms\OraclePlatform;
Expand Down Expand Up @@ -61,9 +60,9 @@ abstract class OrmFunctionalTestCase extends OrmTestCase
/**
* The metadata cache shared between all functional tests.
*
* @var Cache|CacheItemPoolInterface|null
* @var CacheItemPoolInterface|null
*/
private static $_metadataCache = null;
private static $metadataCache = null;

/**
* The query cache shared between all functional tests.
Expand Down Expand Up @@ -711,12 +710,8 @@ protected function getEntityManager(
// NOTE: Functional tests use their own shared metadata cache, because
// the actual database platform used during execution has effect on some
// metadata mapping behaviors (like the choice of the ID generation).
if (self::$_metadataCache === null) {
if (isset($GLOBALS['DOCTRINE_CACHE_IMPL'])) {
self::$_metadataCache = new $GLOBALS['DOCTRINE_CACHE_IMPL']();
} else {
self::$_metadataCache = new ArrayAdapter();
}
if (self::$metadataCache === null) {
self::$metadataCache = new ArrayAdapter();
}

if (self::$queryCache === null) {
Expand All @@ -726,12 +721,7 @@ protected function getEntityManager(
//FIXME: two different configs! $conn and the created entity manager have
// different configs.
$config = new Configuration();
if (self::$_metadataCache instanceof CacheItemPoolInterface) {
$config->setMetadataCache(self::$_metadataCache);
} else {
$config->setMetadataCacheImpl(self::$_metadataCache);
}

$config->setMetadataCache(self::$metadataCache);
$config->setQueryCache(self::$queryCache);
$config->setProxyDir(__DIR__ . '/Proxies');
$config->setProxyNamespace('Doctrine\Tests\Proxies');
Expand Down
22 changes: 8 additions & 14 deletions tests/Doctrine/Tests/OrmTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ abstract class OrmTestCase extends DoctrineTestCase
*
* @var CacheItemPoolInterface|null
*/
private static $_metadataCache = null;
private static $metadataCache = null;

/**
* The query cache that is shared between all ORM tests (except functional tests).
Expand All @@ -54,7 +54,7 @@ abstract class OrmTestCase extends DoctrineTestCase
protected $secondLevelCacheLogger;

/** @var CacheItemPoolInterface|null */
protected $secondLevelCache = null;
private $secondLevelCache = null;

protected function createAnnotationDriver(array $paths = []): AnnotationDriver
{
Expand Down Expand Up @@ -125,28 +125,22 @@ protected function getTestEntityManager(
return EntityManagerMock::create($conn, $config, $eventManager);
}

protected function enableSecondLevelCache($log = true): void
protected function enableSecondLevelCache(bool $log = true): void
{
$this->isSecondLevelCacheEnabled = true;
$this->isSecondLevelCacheLogEnabled = $log;
}

private static function getSharedMetadataCacheImpl(): ?CacheItemPoolInterface
private static function getSharedMetadataCacheImpl(): CacheItemPoolInterface
{
if (self::$_metadataCache === null) {
self::$_metadataCache = new ArrayAdapter();
}

return self::$_metadataCache;
return self::$metadataCache
?? self::$metadataCache = new ArrayAdapter();
}

private static function getSharedQueryCache(): CacheItemPoolInterface
{
if (self::$queryCache === null) {
self::$queryCache = new ArrayAdapter();
}

return self::$queryCache;
return self::$queryCache
?? self::$queryCache = new ArrayAdapter();
}

protected function getSharedSecondLevelCache(): CacheItemPoolInterface
Expand Down

0 comments on commit 92d27f2

Please sign in to comment.