2008-05-24 21:37:26 +04:00
|
|
|
<?php
|
2009-01-04 19:15:32 +03:00
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
namespace Doctrine\Tests;
|
2009-01-04 19:15:32 +03:00
|
|
|
|
2008-05-24 21:37:26 +04:00
|
|
|
/**
|
|
|
|
* Base testcase class for all orm testcases.
|
|
|
|
*
|
2009-02-04 21:03:05 +03:00
|
|
|
* @since 2.0
|
2008-05-24 21:37:26 +04:00
|
|
|
*/
|
2009-01-22 22:38:10 +03:00
|
|
|
class OrmFunctionalTestCase extends OrmTestCase
|
2008-05-24 21:37:26 +04:00
|
|
|
{
|
2009-02-04 21:03:05 +03:00
|
|
|
/* The metadata cache shared between all functional tests. */
|
|
|
|
private static $_metadataCacheImpl = null;
|
|
|
|
|
|
|
|
/** The EntityManager for this testcase. */
|
2009-01-12 16:34:41 +03:00
|
|
|
protected $_em;
|
|
|
|
|
2008-05-24 21:37:26 +04:00
|
|
|
/**
|
|
|
|
* 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];
|
2009-01-07 20:46:02 +03:00
|
|
|
$this->_loadedFixtures[] = $fixture['table'];
|
2008-05-24 21:37:26 +04:00
|
|
|
|
2009-01-07 20:46:02 +03:00
|
|
|
$conn = $this->sharedFixture['conn'];
|
|
|
|
$tableName = $fixture['table'];
|
2008-05-24 21:37:26 +04:00
|
|
|
|
|
|
|
if ( ! in_array($tableName, self::$_exportedTables)) {
|
2009-01-07 20:46:02 +03:00
|
|
|
$conn->getSchemaManager()->exportClasses(array($fixture['model']));
|
2008-05-24 21:37:26 +04:00
|
|
|
self::$_exportedTables[] = $tableName;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($fixture['rows'] as $row) {
|
2009-01-07 20:46:02 +03:00
|
|
|
$conn->insert($tableName, $row);
|
2008-05-24 21:37:26 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-02-04 21:03:05 +03:00
|
|
|
* Sweeps the database tables of all used fixtures and clears the EntityManager.
|
2008-05-24 21:37:26 +04:00
|
|
|
*/
|
|
|
|
protected function tearDown()
|
|
|
|
{
|
2009-01-07 20:46:02 +03:00
|
|
|
$conn = $this->sharedFixture['conn'];
|
|
|
|
foreach (array_reverse($this->_loadedFixtures) as $table) {
|
|
|
|
$conn->exec("DELETE FROM " . $table);
|
2008-05-24 21:37:26 +04:00
|
|
|
}
|
2009-01-12 16:34:41 +03:00
|
|
|
$this->_em->clear();
|
2008-05-24 21:37:26 +04:00
|
|
|
}
|
2009-01-07 20:46:02 +03:00
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
if ( ! isset($this->sharedFixture['conn'])) {
|
2009-02-02 14:55:50 +03:00
|
|
|
echo PHP_EOL . " --- CREATE CONNECTION ----" . PHP_EOL;
|
2009-01-22 22:38:10 +03:00
|
|
|
$this->sharedFixture['conn'] = TestUtil::getConnection();
|
2009-01-07 20:46:02 +03:00
|
|
|
}
|
2009-01-12 16:34:41 +03:00
|
|
|
if ( ! $this->_em) {
|
|
|
|
$this->_em = $this->_getEntityManager();
|
|
|
|
}
|
2009-01-07 20:46:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function _getEntityManager($config = null, $eventManager = null) {
|
2009-02-04 21:03:05 +03:00
|
|
|
// NOTE: Functional tests use their own shared metadata cache, because
|
|
|
|
// the actual database platform used during execution has effect on some
|
|
|
|
// metadata mapping behaviors (like the choice of the ID generation).
|
|
|
|
if (is_null(self::$_metadataCacheImpl)) {
|
|
|
|
self::$_metadataCacheImpl = new \Doctrine\ORM\Cache\ArrayCache;
|
|
|
|
}
|
2009-01-22 22:38:10 +03:00
|
|
|
$config = new \Doctrine\ORM\Configuration();
|
2009-02-04 21:03:05 +03:00
|
|
|
$config->setMetadataCacheImpl(self::$_metadataCacheImpl);
|
2009-01-22 22:38:10 +03:00
|
|
|
$eventManager = new \Doctrine\Common\EventManager();
|
2009-01-07 20:46:02 +03:00
|
|
|
$conn = $this->sharedFixture['conn'];
|
2009-02-17 14:02:11 +03:00
|
|
|
return \Doctrine\ORM\EntityManager::create($conn, $config, $eventManager);
|
2009-01-07 20:46:02 +03:00
|
|
|
}
|
2008-05-24 21:37:26 +04:00
|
|
|
}
|