1
0
mirror of synced 2024-12-13 22:56:04 +03:00
doctrine2/tests/Doctrine/Tests/OrmTestCase.php

139 lines
5.1 KiB
PHP
Raw Normal View History

<?php
namespace Doctrine\Tests;
use Doctrine\Common\Cache\ArrayCache;
/**
* Base testcase class for all ORM testcases.
*/
abstract class OrmTestCase extends DoctrineTestCase
{
2012-12-14 22:55:49 +04:00
/**
* The metadata cache that is shared between all ORM tests (except functional tests).
*
* @var \Doctrine\Common\Cache\Cache|null
*/
private static $_metadataCacheImpl = null;
2011-12-20 01:56:19 +04:00
2012-12-14 22:55:49 +04:00
/**
* The query cache that is shared between all ORM tests (except functional tests).
*
* @var \Doctrine\Common\Cache\Cache|null
*/
private static $_queryCacheImpl = null;
/**
* @param array $paths
2012-12-14 22:55:49 +04:00
* @param mixed $alias
*
* @return \Doctrine\ORM\Mapping\Driver\AnnotationDriver
*/
protected function createAnnotationDriver($paths = array(), $alias = null)
{
if (version_compare(\Doctrine\Common\Version::VERSION, '3.0.0', '>=')) {
$reader = new \Doctrine\Common\Annotations\CachedReader(
new \Doctrine\Common\Annotations\AnnotationReader(), new ArrayCache()
);
}
else if (version_compare(\Doctrine\Common\Version::VERSION, '2.2.0-DEV', '>=')) {
// Register the ORM Annotations in the AnnotationRegistry
$reader = new \Doctrine\Common\Annotations\SimpleAnnotationReader();
$reader->addNamespace('Doctrine\ORM\Mapping');
$reader = new \Doctrine\Common\Annotations\CachedReader($reader, new ArrayCache());
}
else if (version_compare(\Doctrine\Common\Version::VERSION, '2.1.0-BETA3-DEV', '>=')) {
$reader = new \Doctrine\Common\Annotations\AnnotationReader();
$reader->setIgnoreNotImportedAnnotations(true);
$reader->setEnableParsePhpImports(false);
if ($alias) {
$reader->setAnnotationNamespaceAlias('Doctrine\ORM\Mapping\\', $alias);
} else {
$reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
}
$reader = new \Doctrine\Common\Annotations\CachedReader(
new \Doctrine\Common\Annotations\IndexedReader($reader), new ArrayCache()
);
} else {
$reader = new \Doctrine\Common\Annotations\AnnotationReader();
if ($alias) {
$reader->setAnnotationNamespaceAlias('Doctrine\ORM\Mapping\\', $alias);
} else {
$reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
}
}
\Doctrine\Common\Annotations\AnnotationRegistry::registerFile(
__DIR__ . "/../../../lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php");
return new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader, (array)$paths);
}
2011-12-20 01:56:19 +04:00
/**
* Creates an EntityManager for testing purposes.
2011-12-20 01:56:19 +04:00
*
* NOTE: The created EntityManager will have its dependant DBAL parts completely
* mocked out using a DriverMock, ConnectionMock, etc. These mocks can then
* be configured in the tests to simulate the DBAL behavior that is desired
* for a particular test,
*
2012-12-14 22:55:49 +04:00
* @param \Doctrine\DBAL\Connection|array $conn
* @param mixed $conf
* @param \Doctrine\Common\EventManager|null $eventManager
* @param bool $withSharedMetadata
*
* @return \Doctrine\ORM\EntityManager
*/
protected function _getTestEntityManager($conn = null, $conf = null, $eventManager = null, $withSharedMetadata = true)
{
2011-12-20 01:56:19 +04:00
$metadataCache = $withSharedMetadata
? self::getSharedMetadataCacheImpl()
: new \Doctrine\Common\Cache\ArrayCache;
2011-12-20 01:56:19 +04:00
$config = new \Doctrine\ORM\Configuration();
2011-12-20 01:56:19 +04:00
$config->setMetadataCacheImpl($metadataCache);
$config->setMetadataDriverImpl($config->newDefaultAnnotationDriver(array(), true));
$config->setQueryCacheImpl(self::getSharedQueryCacheImpl());
$config->setProxyDir(__DIR__ . '/Proxies');
$config->setProxyNamespace('Doctrine\Tests\Proxies');
2011-12-20 01:56:19 +04:00
if ($conn === null) {
$conn = array(
'driverClass' => 'Doctrine\Tests\Mocks\DriverMock',
'wrapperClass' => 'Doctrine\Tests\Mocks\ConnectionMock',
'user' => 'john',
'password' => 'wayne'
);
}
2011-12-20 01:56:19 +04:00
if (is_array($conn)) {
$conn = \Doctrine\DBAL\DriverManager::getConnection($conn, $config, $eventManager);
}
2011-12-20 01:56:19 +04:00
return \Doctrine\Tests\Mocks\EntityManagerMock::create($conn, $config, $eventManager);
}
2012-12-14 22:55:49 +04:00
/**
* @return \Doctrine\Common\Cache\Cache
*/
private static function getSharedMetadataCacheImpl()
{
if (self::$_metadataCacheImpl === null) {
self::$_metadataCacheImpl = new \Doctrine\Common\Cache\ArrayCache;
}
2011-12-20 01:56:19 +04:00
return self::$_metadataCacheImpl;
}
2011-12-20 01:56:19 +04:00
2012-12-14 22:55:49 +04:00
/**
* @return \Doctrine\Common\Cache\Cache
*/
private static function getSharedQueryCacheImpl()
{
if (self::$_queryCacheImpl === null) {
self::$_queryCacheImpl = new \Doctrine\Common\Cache\ArrayCache;
}
2011-12-20 01:56:19 +04:00
return self::$_queryCacheImpl;
}
}