2009-01-22 22:38:10 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests;
|
|
|
|
|
2011-05-25 02:26:20 +04:00
|
|
|
use Doctrine\Common\Cache\ArrayCache;
|
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
/**
|
|
|
|
* Base testcase class for all ORM testcases.
|
|
|
|
*/
|
2009-11-01 01:23:36 +03:00
|
|
|
abstract class OrmTestCase extends DoctrineTestCase
|
2009-01-22 22:38:10 +03:00
|
|
|
{
|
2009-02-04 21:03:05 +03:00
|
|
|
/** The metadata cache that is shared between all ORM tests (except functional tests). */
|
|
|
|
private static $_metadataCacheImpl = null;
|
2009-05-21 23:18:14 +04:00
|
|
|
/** The query cache that is shared between all ORM tests (except functional tests). */
|
|
|
|
private static $_queryCacheImpl = null;
|
2009-02-04 21:03:05 +03:00
|
|
|
|
2011-05-25 02:26:20 +04:00
|
|
|
/**
|
|
|
|
* @param array $paths
|
|
|
|
* @return \Doctrine\Common\Annotations\AnnotationReader
|
|
|
|
*/
|
|
|
|
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()
|
|
|
|
);
|
2011-08-13 23:28:05 +04:00
|
|
|
}
|
|
|
|
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', '>=')) {
|
2011-05-25 02:26:20 +04:00
|
|
|
$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\\');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader, (array)$paths);
|
|
|
|
}
|
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
/**
|
|
|
|
* Creates an EntityManager for testing purposes.
|
2009-03-14 12:05:52 +03: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,
|
2009-01-22 22:38:10 +03:00
|
|
|
*
|
|
|
|
* @return Doctrine\ORM\EntityManager
|
|
|
|
*/
|
2009-11-01 01:23:36 +03:00
|
|
|
protected function _getTestEntityManager($conn = null, $conf = null, $eventManager = null, $withSharedMetadata = true)
|
2009-02-17 05:43:40 +03:00
|
|
|
{
|
2009-01-22 22:38:10 +03:00
|
|
|
$config = new \Doctrine\ORM\Configuration();
|
2009-11-01 01:23:36 +03:00
|
|
|
if($withSharedMetadata) {
|
|
|
|
$config->setMetadataCacheImpl(self::getSharedMetadataCacheImpl());
|
|
|
|
} else {
|
|
|
|
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
|
|
|
|
}
|
2010-04-11 13:06:54 +04:00
|
|
|
|
|
|
|
$config->setMetadataDriverImpl($config->newDefaultAnnotationDriver());
|
2010-04-10 19:35:18 +04:00
|
|
|
|
2009-05-21 23:18:14 +04:00
|
|
|
$config->setQueryCacheImpl(self::getSharedQueryCacheImpl());
|
2009-10-15 00:18:36 +04:00
|
|
|
$config->setProxyDir(__DIR__ . '/Proxies');
|
|
|
|
$config->setProxyNamespace('Doctrine\Tests\Proxies');
|
2009-01-22 22:38:10 +03:00
|
|
|
$eventManager = new \Doctrine\Common\EventManager();
|
2009-03-14 12:05:52 +03:00
|
|
|
if ($conn === null) {
|
2009-02-17 15:25:03 +03:00
|
|
|
$conn = array(
|
2009-01-22 22:38:10 +03:00
|
|
|
'driverClass' => 'Doctrine\Tests\Mocks\DriverMock',
|
|
|
|
'wrapperClass' => 'Doctrine\Tests\Mocks\ConnectionMock',
|
|
|
|
'user' => 'john',
|
|
|
|
'password' => 'wayne'
|
2009-02-17 15:25:03 +03:00
|
|
|
);
|
|
|
|
}
|
2009-07-20 16:05:19 +04:00
|
|
|
if (is_array($conn)) {
|
|
|
|
$conn = \Doctrine\DBAL\DriverManager::getConnection($conn, $config, $eventManager);
|
|
|
|
}
|
|
|
|
return \Doctrine\Tests\Mocks\EntityManagerMock::create($conn, $config, $eventManager);
|
2009-01-22 22:38:10 +03:00
|
|
|
}
|
2009-02-04 21:03:05 +03:00
|
|
|
|
|
|
|
private static function getSharedMetadataCacheImpl()
|
|
|
|
{
|
2009-03-14 12:05:52 +03:00
|
|
|
if (self::$_metadataCacheImpl === null) {
|
2009-07-07 00:34:54 +04:00
|
|
|
self::$_metadataCacheImpl = new \Doctrine\Common\Cache\ArrayCache;
|
2009-02-04 21:03:05 +03:00
|
|
|
}
|
|
|
|
return self::$_metadataCacheImpl;
|
|
|
|
}
|
2009-05-21 23:18:14 +04:00
|
|
|
|
|
|
|
private static function getSharedQueryCacheImpl()
|
|
|
|
{
|
|
|
|
if (self::$_queryCacheImpl === null) {
|
2009-07-07 00:34:54 +04:00
|
|
|
self::$_queryCacheImpl = new \Doctrine\Common\Cache\ArrayCache;
|
2009-05-21 23:18:14 +04:00
|
|
|
}
|
|
|
|
return self::$_queryCacheImpl;
|
|
|
|
}
|
2009-01-22 22:38:10 +03:00
|
|
|
}
|