PHPUnit: how can I remove test data from my database after the tests are complete?

前端 未结 1 1667
谎友^
谎友^ 2021-01-13 23:09

I have a Zend Framework application (version 1.11) that uses Doctrine 2. I\'ve got PHPUnit set up to run tests on my models and forms and whatnot. The tests work great, but

相关标签:
1条回答
  • 2021-01-14 00:02

    1. Regarding "cleaning up":

    Because you are using InnoDB you can use transactions to restore the DB to the same state as it was before the test started:

    So in setUp() you would add $this->em->getConnection()->beginTransaction(); and in tearDown() $this->em->getConnection()->rollback(); This restores the database to the previous state. Also have a look at MySQL handbook on "The InnoDB Transaction Model and Locking" to make sure that this does not interfere with any other data in your application (keyword: isolation level).

    2. Regarding rolling back auto increment ids:

    As far as I know this is not (easily) possible. There is a thread about it on SO in which it is stated that your application should not care if there are gaps between the auto increment ids. If you are using a test database (which is highly recommended) this should be even a lesser concern.

    0 讨论(0)
提交回复
热议问题