The QaFoo blog has a new tutorial posted showing you how to work with PHPUnit and database testing for acceptance testing of your application. Acceptance testing (or integration testing) generally exercises the tests with live data from a database rather that stubbed or mocked data in true unit testing.
Most of us do not use PHPUnit solely for Unit Tests but we also write Integration or Acceptance Tests with PHPUnit. One very common question then is how to interact with the database correctly in those tests. Let me show you the different options and their trade offs...
There are multiple aspects of database tests where our decision has impact on test atomicity and test runtime. All decisions boil down to: More test atomicity leads to longer test runs, and we can buy test speed by omitting test atomicity.
They talk more about the time added for testing with database functionality included and where running them might be most appropriate (local vs on the CI server). The article then talks about one of the main decisions around doing a full data/schema reset or just removing data when the tests start or end. They then get into this last point - where the reset should happen, before or after the tests are executed. They talk in detail about each option, breaking it down into a few options: before each test, before each test class or before the whole test run. The post ends with a section talking about "mocking the database away", a method usually used in traditional unit testing but points out that this can be highly prone to errors, especially if you attempt to replace one database system with another (like replacing MySQL with SQLite).