Add a second local database for testing with ddev
The use of an in-memory SQLite database for PHPUnit and Pest tests is common practice. However, SQLite reaches its limits for larger or long-term projects, especially when it comes to certain database operations such as dropColumn()
or sorting columns. SQLite does not support these specific operations.
I use SQLite as long as I can, but after a certain point this becomes impossible. If you use DDEV, you can easily avoid the problem by adding a second MySql database to the container.
Create an additional database
In the .ddev/commands/web/
directory, place the file create_testing_database.sh
To execute this script every time the container is started, we add a hook in the DDEV configuration file .ddev/config.yaml
. Add the following section there:
Integration of the new database in PHPUnit or Pest
Once the second database is set up in DDEV, the next step is to use it in your PHPUnit or Pest tests. To do this, you need to customise the configuration files of these test frameworks or create a testing environment.
PHPUnit and Pest configuration
Change the database settings in the phpunit.xml
file. Make sure that the connection data refers to your new test database and the specific port. An example section could look like this:
The new testing database can now also be accessed via the DB client of choice: