OrmTestCase.php 2.44 KB
Newer Older
1 2 3 4 5 6 7
<?php

namespace Doctrine\Tests;

/**
 * Base testcase class for all ORM testcases.
 */
8
abstract class OrmTestCase extends DoctrineTestCase
9
{
10 11
    /** The metadata cache that is shared between all ORM tests (except functional tests). */
    private static $_metadataCacheImpl = null;
12 13
    /** The query cache that is shared between all ORM tests (except functional tests). */
    private static $_queryCacheImpl = null;
14

15 16
    /**
     * Creates an EntityManager for testing purposes.
17 18 19 20 21
     * 
     * 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,
22 23 24
     *
     * @return Doctrine\ORM\EntityManager
     */
25
    protected function _getTestEntityManager($conn = null, $conf = null, $eventManager = null, $withSharedMetadata = true)
26
    {
27
        $config = new \Doctrine\ORM\Configuration();
28 29 30 31 32
        if($withSharedMetadata) {
            $config->setMetadataCacheImpl(self::getSharedMetadataCacheImpl());
        } else {
            $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
        }
33
        $config->setQueryCacheImpl(self::getSharedQueryCacheImpl());
34 35
        $config->setProxyDir(__DIR__ . '/Proxies');
        $config->setProxyNamespace('Doctrine\Tests\Proxies');
36
        $eventManager = new \Doctrine\Common\EventManager();
37
        if ($conn === null) {
38
            $conn = array(
39 40 41 42
                'driverClass' => 'Doctrine\Tests\Mocks\DriverMock',
                'wrapperClass' => 'Doctrine\Tests\Mocks\ConnectionMock',
                'user' => 'john',
                'password' => 'wayne'
43 44
            );
        }
45 46 47 48
        if (is_array($conn)) {
            $conn = \Doctrine\DBAL\DriverManager::getConnection($conn, $config, $eventManager);
        }
        return \Doctrine\Tests\Mocks\EntityManagerMock::create($conn, $config, $eventManager);
49
    }
50 51 52

    private static function getSharedMetadataCacheImpl()
    {
53
        if (self::$_metadataCacheImpl === null) {
54
            self::$_metadataCacheImpl = new \Doctrine\Common\Cache\ArrayCache;
55 56 57
        }
        return self::$_metadataCacheImpl;
    }
58 59 60 61
    
    private static function getSharedQueryCacheImpl()
    {
        if (self::$_queryCacheImpl === null) {
62
            self::$_queryCacheImpl = new \Doctrine\Common\Cache\ArrayCache;
63 64 65
        }
        return self::$_queryCacheImpl;
    }
66
}