In a library I'm writing, one can supply a PDO instance that is used for any data persistence. A number of drivers are supported, say MySQL & SQLite. The library itself includes some files that perform SQL queries, which I want to test (using phpUnit). Hence I want to run a general (driver-independent) unit test, one time for each driver.
Internet did not bring me a clear best-practice solution, so these are the approaches I've come up with so far:
- Use a dataProvider with ['mysql', 'sqlite'] and test on this; however its usage will than be limited to one test function only, which I dislike.
- Set up each database connection in
setUpBeforeClassfor each of the drivers, and in each test loop through these and perform the tests. From a OOP standpoint this also is not a good alternative, not to mention having to set up multiple connections to different drivers at the same time may be quite resource intensive. - Create an abstract general unit test class with an abstract
setupConnection()method. This class can than be extended with a proper database connection, for each of the required drivers. This seems the most viable option, however with a lot of files/classes to test in this way (more than 10.. well not a lot, but sufficiently many) this becomes a tedious and dirty approach to this problem.
Ideally, I sought for a way to say to phpunit 'Run this unit test file with these drivers'. Is this at all possible in a neat way? Or otherwise, what would be the alternatives?