One thing I love about Java is that there are a number of databases that you can load into memory and run functional tests against. This is also an option in PHP thanks to Sqlite.
To do this in Symfony, you must configure the test db like this:
test:doctrine:param: dsn:"sqlite::memory:"
And setup the test to reload the db each time you call it:
<?php require_once dirname(__FILE__).'/../bootstrap/functional.php';$configuration = ProjectConfiguration::getApplicationConfiguration('admin','test', true);new sfDatabaseManager($configuration);Doctrine::createTablesFromModels(dirname(__FILE__).'/../../lib/model');Doctrine::loadData(sfConfig::get('sf_test_dir').'/fixtures');$t = new lime_test(2);
Update:
I had some trouble loading fixtures where the ids could not be autogenerated. In the end I ended up defining a separate function for loading files like these. Here’s the complete bootstrap file I’ve ended up with:
<?php/** This file is part of the symfony package.* (c) Fabien Potencier<fabien.potencier@symfony-project.com>** For the full copyright and license information, please view the LICENSE* file that was distributed with this source code.*/$_test_dir = realpath(dirname(__FILE__).'/..');// configurationrequire_once dirname(__FILE__).'/../../config/ProjectConfiguration.class.php';$configuration = ProjectConfiguration::hasActive() ? ProjectConfiguration::getActive() : new ProjectConfiguration(realpath($_test_dir.'/..'));// autoloader$autoload = sfSimpleAutoload::getInstance(sfConfig::get('sf_cache_dir').'/project_autoload.cache');$autoload->loadConfiguration(sfFinder::type('file')->name('autoload.yml')->in(array(sfConfig::get('sf_symfony_lib_dir').'/config/config',sfConfig::get('sf_config_dir'),)));$autoload->register();// limeinclude $configuration->getSymfonyLibDir().'/vendor/lime/lime.php';//$configuration = ProjectConfiguration::getApplicationConfiguration('admin','test', true);new sfDatabaseManager(ProjectConfiguration::getApplicationConfiguration('admin','test', true));Doctrine::createTablesFromModels(sfConfig::get('sf_root_dir').'/lib/model');Doctrine::loadData(sfConfig::get('sf_test_dir').'/fixtures');/*** Load testdata from an sqldump. This is used when running tests where the* id's are significant (i.e. fylker, kommuner and postnummer)* this function expects a connection to exist* @param $file string path to the file to load*/function loadSqlDump($file) {$sql = file_get_contents($file);if ($sql =='') throw new Exception("No sql found in $file");$conn2 = Doctrine_Manager::connection();$sql = trim($sql);$dbo = $conn2->getDbh();// sqlite doesn't like multiple statements$statements = explode(";", $sql);foreach ($statements as $stmt) {try {$n = $dbo->exec($stmt);} catch (Exception $e) {print"Error loading statement: \n$stmt\n:". $e->getMessage();}}}
Note that I place these DB tests in the unittest directory.
