Even if it's the best idea to write tests that need a real database connection, sometimes it's something needed. If you're using Symfony2, you can simply follow the "How to test Doctrine Repositories" part of Symfony documentation to do that.
But if you're running a large amount of tests you'll get the following MySQL error:
SQLSTATE  Too many connections
This means that you reached the maximum number of simultanous connections (
max_connections parameter in
my.cnf). To fix that behaviour, I had to change the
processIsolation parameter of PHPUnit in the
phpunit.xml file, like this:
backupGlobals = "false"
backupStaticAttributes = "false"
colors = "true"
convertErrorsToExceptions = "true"
convertNoticesToExceptions = "true"
convertWarningsToExceptions = "true"
processIsolation = "true"
stopOnFailure = "false"
syntaxCheck = "false"
bootstrap = "bootstrap.php.cache" >