It’s extremely important to have same state of the System Under Test. In most of the cases it will be possible by having same contents in a database for every test. I’ve decribed how to achieve it in Fully isolated tests in Symfony2 blog post about two years ago (btw. it’s most popular post on this blog). It was the time, when PHP’s Traits weren’t that popular.

In IsolatedTestTrait.php I introduced idea to rebuild schema with fixtures from scratch into sqlite file database. It will be done once, at the begining of test suite, then it will be copied and reused for each test in given test suite. In the end, file will be removed in tearDownAfterClass method. This significantly increases performance, since you don’t rebuild schema and don’t load fixtures for every test. It is also clean and non-intrusive, because you only type use IsolatedTestTrait; in tests cases that test something related to database state. Now you can easily conduct functional or integration tests within consistent, isolated environment.

PS. LiipFunctionalTestBundle comes with similar concepts, maybe it’s worth having a look.

IsolatedTestsTrait is available here as a Gist.

For your convenience I’m also putting the code below. Feel free to comment and propose improvements!
[gist id=”7137331″]