1
0
mirror of synced 2025-01-08 10:07:10 +03:00
doctrine2/tests/lib/Doctrine_OrmTestCase.php

142 lines
5.5 KiB
PHP
Raw Normal View History

2008-02-09 21:59:19 +03:00
<?php
/**
* Base testcase class for all orm testcases.
*
*/
class Doctrine_OrmTestCase extends Doctrine_TestCase
{
2008-02-10 18:27:01 +03:00
/**
* The currently loaded model names of the fixtures for the testcase.
*/
2008-02-10 17:32:05 +03:00
private $_loadedFixtures = array();
2008-02-10 18:27:01 +03:00
/**
* All loaded fixtures during test execution. Common fixture cache.
*/
2008-02-10 17:32:05 +03:00
private static $_fixtures = array();
2008-02-10 18:27:01 +03:00
/**
* 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.
*/
2008-02-10 17:32:05 +03:00
private static $_exportedTables = array();
2008-02-10 20:29:24 +03:00
/**
* setUp()
2008-02-10 20:33:46 +03:00
*
* Note: This setUp() and the one of DbalTestCase currently look identical. However,
* please dont pull this method up. In the future with a separation of Dbal/Orm
* this setUp() will take care of a ORM connection/session/manager initialization
* and the DBAL setUp() will take care of just a DBAL connection.
2008-02-10 20:29:24 +03:00
*/
protected function setUp()
{
// Setup a db connection if there is none, yet. This makes it possible
// to run tests that use a connection standalone.
if ( ! isset($this->sharedFixture['connection'])) {
2008-02-10 22:08:31 +03:00
$this->sharedFixture['connection'] = Doctrine_TestUtil::getConnection();
2008-02-10 20:29:24 +03:00
}
}
2008-02-10 18:27:01 +03:00
/**
* 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.
*
2008-02-10 18:51:55 +03:00
* 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.
*
2008-02-10 18:27:01 +03:00
* @param string $package The package name. Must be one of Doctrine's test model packages
* (forum, cms or ecommerce).
2008-02-10 18:51:55 +03:00
* @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.
2008-02-10 18:27:01 +03:00
* @param string $name The name of the fixture to load from the specified package.
*/
2008-02-10 18:45:30 +03:00
protected function loadFixture($package, $scenario, $name)
2008-02-10 17:32:05 +03:00
{
2008-02-10 18:45:30 +03:00
$uniqueName = $package . '/' . $scenario . '/' . $name;
2008-02-10 17:32:05 +03:00
if ( ! isset(self::$_fixtures[$uniqueName])) {
// load fixture file
2008-02-10 18:45:30 +03:00
$fixtureFile = 'fixtures'
. DIRECTORY_SEPARATOR . $package
. DIRECTORY_SEPARATOR . $scenario
. DIRECTORY_SEPARATOR . $name
. '.php';
2008-02-10 17:32:05 +03:00
require $fixtureFile;
self::$_fixtures[$uniqueName] = $fixture;
}
$fixture = self::$_fixtures[$uniqueName];
$this->_loadedFixtures[] = $fixture['model'];
$conn = $this->sharedFixture['connection'];
$classMetadata = $conn->getClassMetadata($fixture['model']);
$tableName = $classMetadata->getTableName();
if ( ! in_array($tableName, self::$_exportedTables)) {
$conn->export->exportClasses(array($fixture['model']));
self::$_exportedTables[] = $tableName;
}
foreach ($fixture['rows'] as $row) {
2008-02-12 15:31:28 +03:00
$conn->insert($tableName, $row);
2008-02-10 17:32:05 +03:00
}
}
2008-02-10 18:27:01 +03:00
/**
2008-02-10 18:51:55 +03:00
* Loads multiple fixtures of the same package and scenario.
2008-02-10 18:27:01 +03:00
* 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.
*
2008-02-10 18:51:55 +03:00
* 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.
*
2008-02-10 18:27:01 +03:00
* @param string $package The package name. Must be one of Doctrine's test model packages
* (forum, cms or ecommerce).
2008-02-10 18:51:55 +03:00
* @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.
2008-02-10 18:27:01 +03:00
* @param array $names The names of the fixtures to load from the specified package.
*/
2008-02-10 18:45:30 +03:00
protected function loadFixtures($package, $scenario, array $names)
2008-02-10 17:32:05 +03:00
{
foreach ($names as $name) {
2008-02-10 18:45:30 +03:00
$this->loadFixture($package, $scenario, $name);
2008-02-10 17:32:05 +03:00
}
}
2008-02-10 18:27:01 +03:00
/**
* Sweeps the database tables of all used fixtures.
*/
2008-02-10 17:32:05 +03:00
protected function tearDown()
{
$conn = $this->sharedFixture['connection'];
foreach (array_reverse($this->_loadedFixtures) as $model) {
$conn->exec("DELETE FROM " . $conn->getClassMetadata($model)->getTableName());
}
}
/*
2008-02-09 21:59:19 +03:00
public function loadFixturesPackage($package, $models = array())
{
$packagePath = 'fixtures' . DIRECTORY_SEPARATOR . $package;
if ( ! file_exists($packagePath)) {
throw new Exception("Could not find fixtures package: $package.");
}
$modelsPath = $packagePath . DIRECTORY_SEPARATOR . 'models';
$dataPath = $packagePath . DIRECTORY_SEPARATOR . 'data';
Doctrine::loadModels($modelsPath);
Doctrine::createTablesFromModels($modelsPath);
$data = new Doctrine_Data();
$data->importData($dataPath, 'yml', $models);
}
2008-02-10 17:32:05 +03:00
*/
2008-02-09 21:59:19 +03:00
}