views:

259

answers:

1

Hi there,

From the Jobeet tutorial provided in Symfony website, I found out that I can load fixtures data each time I run unit test by using this script:

Doctrine_Core::loadData(sfConfig::get('sf_test_dir').'/fixtures');

However, I want to delete and reload all records from database each time I run a unit test. Currently, I'm doing this manually (Run symfony doctrine:build --all before each test). Can someone provided me a correct way to do this?

+2  A: 

I use the following in my test/bootstrap/unit.php file at the end:

$doctrine = new sfDoctrineDropDbTask($configuration->getEventDispatcher(), new sfAnsiColorFormatter());
$doctrine->run(array(), array("--no-confirmation","--env=test"));

$doctrine = new sfDoctrineBuildDbTask($configuration->getEventDispatcher(), new sfAnsiColorFormatter());
$doctrine->run(array(), array("--env=test"));

$doctrine = new sfDoctrineInsertSqlTask($configuration->getEventDispatcher(), new sfAnsiColorFormatter());
$doctrine->run(array(), array("--env=test"));

before loading the fixtures in. This works nicely for me, although it can get slow if you have a large schema and lots of fixtures. There's some tips over on the Web Mozarts blog about writing efficient tests, and there's a tip about using an sqlite in-memory database to speed it up.

richsage
Works perfectly. Thanks !
Andree