OrmTestCase.php 2.15 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

namespace Doctrine\Tests;

/**
 * Base testcase class for all ORM testcases.
 */
class OrmTestCase extends DoctrineTestCase
{
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)
26
    {
27
        $config = new \Doctrine\ORM\Configuration();
28
        $config->setMetadataCacheImpl(self::getSharedMetadataCacheImpl());
29
        $config->setQueryCacheImpl(self::getSharedQueryCacheImpl());
30
        $eventManager = new \Doctrine\Common\EventManager();
31
        if ($conn === null) {
32
            $conn = array(
33 34 35 36
                'driverClass' => 'Doctrine\Tests\Mocks\DriverMock',
                'wrapperClass' => 'Doctrine\Tests\Mocks\ConnectionMock',
                'user' => 'john',
                'password' => 'wayne'
37 38
            );
        }
39 40 41 42
        if (is_array($conn)) {
            $conn = \Doctrine\DBAL\DriverManager::getConnection($conn, $config, $eventManager);
        }
        return \Doctrine\Tests\Mocks\EntityManagerMock::create($conn, $config, $eventManager);
43
    }
44 45 46

    private static function getSharedMetadataCacheImpl()
    {
47
        if (self::$_metadataCacheImpl === null) {
48
            self::$_metadataCacheImpl = new \Doctrine\Common\Cache\ArrayCache;
49 50 51
        }
        return self::$_metadataCacheImpl;
    }
52 53 54 55
    
    private static function getSharedQueryCacheImpl()
    {
        if (self::$_queryCacheImpl === null) {
56
            self::$_queryCacheImpl = new \Doctrine\Common\Cache\ArrayCache;
57 58 59
        }
        return self::$_queryCacheImpl;
    }
60
}