简体   繁体   中英

How to setup a Doctrine test to have automatic rollback of the transaction (to avoid dirtying db)?

I would like to have tests dealing with Doctrine not persist stuff in the main Symfony dev database. Preferably by not storing stuff at all (rolling back per test). How would I do this? Are there ready-made frameworks/libs/setups I can use?

At work we have a really smooth working Java setup where database tests extends custom JUnit test classes we have created. TransactionallyIsolatedITest for running everything within a transaction that is rolled back (fast), FullyIsolatedITest for testing stuff that do their own begin() , commit() , stuff, etc. This creates and tears down a database created from a template for each test (heavy/slow).

Having something like this would be beautful when doing Symfony development in PHP.

You could try overriding the setUp method, inside that you can get the enityManager instance like this $em = self::getContainer()->get('doctrine')->getManager(); and then purge the database via the Doctrine ORM purger

$purger = new Doctrine\Common\DataFixtures\Purger\ORMPurger($em);
$purger->purge();

在 Symfony 6 文档中,他们实际上描述并展示了如何在每次测试之前使用DAMADoctrineTestBundle重置数据库。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM