开发者

Symfony - Delete and reload all database records for each test

开发者 https://www.devze.com 2022-12-23 17:09 出处:网络
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:

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 th开发者_如何学编程is manually (Run symfony doctrine:build --all before each test). Can someone provided me a correct way to do this?


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.


If you want to just clear the tables, you can do so by adding empty arrays to your fixtures, then using Doctrine_Core::loadData(sfConfig::get('sf_test_dir').'/fixtures');

#Tables to emtpy    
User: []
Post: []

#Tables to load fixtures
Country:
  country1:
    name: United Kingdom
  country2:
    name: USA
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号