Search code examples
phpsymfonyfunctional-testing

Do not impact database with test


In symfony 1.x, with some code it was possible to run test without modify database. For example, one of my test test the registration of user. After executing this test, ths user is always in database. How can we execute tests without modifying database permanently ?


Solution

  • First of all, there is this excellent article by Alexandre who describes how to rollback database after each test.

    What is also possible is using sqlite as test-database because it is in-memory and therefor much faster. This article describes how to do this. This approach is handy if you need to handle data between tests, like creating an object and then editing it. This is not good pratice but sometimes there is no way around.

    I use a split approach, having a sqlite database for speed and doing a rollback after every test as described by Alexandre. It works very well :-)