Doctrine_OrmFunctionalTestCase.php 4.78 KB
Newer Older
romanb's avatar
romanb committed
1
<?php
romanb's avatar
romanb committed
2 3 4

#namespace Doctrine\Tests;

romanb's avatar
romanb committed
5 6 7 8 9 10
/**
 * Base testcase class for all orm testcases.
 *
 */
class Doctrine_OrmFunctionalTestCase extends Doctrine_OrmTestCase
{
11 12
    protected $_em;

romanb's avatar
romanb committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    /**
     * The currently loaded model names of the fixtures for the testcase.
     */
    private $_loadedFixtures = array();
    
    /**
     * All loaded fixtures during test execution. Common fixture cache.
     */
    private static $_fixtures = array();
    
    /**
     * The names of all tables that were already exported. Each table is exported
     * only once. Then it's just filled & erased for each testmethod in a testcase
     * that uses one or more fixtures.
     */
    private static $_exportedTables = array();
    
    /**
     * Loads a data fixture into the database. This method must only be called
     * from within the setUp() method of testcases. The database will then be
     * populated with fresh data of all loaded fixtures for each test method.
     *
     * WARNING: A single testcase should never load fixtures from different scenarios of
     * the same package as the concistency and uniqueness of keys is not guaranteed.
     *
     * @param string $package  The package name. Must be one of Doctrine's test model packages
     *                         (forum, cms or ecommerce).
     * @param string $scenario The fixture scenario. A model package can have many fixture
     *                         scenarios. Within a scenario all primary keys and foreign keys
     *                         of fixtures are consistent and unique.
     * @param string $name     The name of the fixture to load from the specified package.
     */
    protected function loadFixture($package, $scenario, $name)
    {
        $uniqueName = $package . '/' . $scenario . '/' . $name;
        
        if ( ! isset(self::$_fixtures[$uniqueName])) {
            // load fixture file
            $fixtureFile = 'fixtures'
                    . DIRECTORY_SEPARATOR . $package
                    . DIRECTORY_SEPARATOR . $scenario
                    . DIRECTORY_SEPARATOR . $name
                    . '.php';
            require $fixtureFile;
            self::$_fixtures[$uniqueName] = $fixture;
        }
        
        $fixture = self::$_fixtures[$uniqueName];
61
        $this->_loadedFixtures[] = $fixture['table'];
romanb's avatar
romanb committed
62
        
63 64
        $conn = $this->sharedFixture['conn'];
        $tableName = $fixture['table'];
romanb's avatar
romanb committed
65 66
        
        if ( ! in_array($tableName, self::$_exportedTables)) {
67
            $conn->getSchemaManager()->exportClasses(array($fixture['model']));
romanb's avatar
romanb committed
68 69 70 71
            self::$_exportedTables[] = $tableName;
        }
        
        foreach ($fixture['rows'] as $row) {
72
            $conn->insert($tableName, $row);
romanb's avatar
romanb committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
        }
    }
    
    /**
     * Loads multiple fixtures of the same package and scenario.
     * This method must only be called from within the setUp() method of testcases.
     * The database will then be populated with fresh data of all loaded fixtures for each
     * test method.
     *
     * WARNING: A single testcase should never load fixtures from different scenarios of
     * the same package as the concistency and uniqueness of keys is not guaranteed.
     *
     * @param string $package  The package name. Must be one of Doctrine's test model packages
     *                         (forum, cms or ecommerce).
     * @param string $scenario The fixture scenario. A model package can have many fixture
     *                         scenarios. Within a scenario all primary keys and foreign keys
     *                         of fixtures are consistent and unique.
     * @param array $names     The names of the fixtures to load from the specified package.
     */
    protected function loadFixtures($package, $scenario, array $names)
    {
        foreach ($names as $name) {
            $this->loadFixture($package, $scenario, $name);
        }
    }
    
    /**
     * Sweeps the database tables of all used fixtures.
     */
    protected function tearDown()
    {
104 105 106
        $conn = $this->sharedFixture['conn'];
        foreach (array_reverse($this->_loadedFixtures) as $table) {
            $conn->exec("DELETE FROM " . $table);
romanb's avatar
romanb committed
107
        }
108
        $this->_em->clear();
romanb's avatar
romanb committed
109
    }
110 111 112 113

    protected function setUp()
    {
        if ( ! isset($this->sharedFixture['conn'])) {
114
            echo " --- CREATE CONNECTION ----";
115 116
            $this->sharedFixture['conn'] = Doctrine_TestUtil::getConnection();
        }
117 118 119
        if ( ! $this->_em) {
            $this->_em = $this->_getEntityManager();
        }
120 121 122 123 124 125 126 127
    }

    protected function _getEntityManager($config = null, $eventManager = null) {
        $config = new Doctrine_ORM_Configuration();
        $eventManager = new Doctrine_Common_EventManager();
        $conn = $this->sharedFixture['conn'];
        return Doctrine_ORM_EntityManager::create($conn, 'em', $config, $eventManager);
    }
romanb's avatar
romanb committed
128
}