Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

connecteev's avatar

Configuring PHPUnit to connect with a test DB

Here's what I'd like to do..

  • have a test DB in MySQL (backend_apis_laravel_testing) that gets used for all PhpUnit tests. I do not want to use Sqlite (because I don't know how to view the database and tables in sqlite)
  • clear any cache (if needed) before PHPUnit runs
  • migrate the DB (and seeds) before PHPUnit runs, and cleanup after
  • ignore all settings in phpunit.xml, and only use the .env.testing file (not sure what takes precedence - the .env.testing file, or the settings in phpunit.xml. I would like my .env.testing file to override the phpunit.xml - for reasons that will take too long to explain, I am not allowed to change the phpunit.xml file.

These are the .env.testing settings I would like to use:

APP_NAME=KB
APP_ENV=testing
APP_KEY=base64:SjuP4OPbjmKHASkC22SFF3HbU+p6K22Pi+YkPsxIIwU=
APP_DEBUG=true
APP_URL=http://localhost:8000
FRONTEND_CLIENT_URL=http://localhost:3000

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=backend_apis_laravel_testing
DB_USERNAME=root
DB_PASSWORD=root

And yet, running phpunit keeps using phpunit.xml's settings (and connecting to sqlite, which I don't want). I also don't know how to do the DB migration setup / teardown (and cache clearing) in Laravel 7. Tips appreciated.

0 likes
6 replies
Tray2's avatar

You really should be using the phpunit.xml configuration over the one in your .env file.

Use something like this in your phpunit.xml file

<server name="DB_HOST" value="127.0.0.1" />
<server name="DB_USER" value="root" />
<server name="DB_PASSWD" value="root" />
<server name="DB_DBNAME" value="backend_apis_laravel_testing" />

However I would recommend you stick with a sqlite in memory database since it's faster.

2 likes
connecteev's avatar

@tray2 I cant use sqlite, my DB is way too complex and I need to perform complex operations in the tests.

I also need to use .env.testing, but it looks like the phpunit.xml file takes precedence.

imrelaur's avatar

Have you tried removing any keys from phpunit.xml that start with DB_ prefix?

Here is my phpunit.xml server keys. No DB keys at all.

<server name="APP_ENV" value="testing"/>
<server name="BCRYPT_ROUNDS" value="4"/>
<server name="CACHE_DRIVER" value="array"/>
<server name="MAIL_DRIVER" value="array"/>
<server name="QUEUE_CONNECTION" value="sync"/>
<server name="SESSION_DRIVER" value="array"/>

And I use MySQL for testing and it works.

1 like
connecteev's avatar

@imre08 yes, that works.

What is a good way to fill the DB tables before phpunit runs, and clear them after?

Tray2's avatar
Tray2
Best Answer
Level 73

You can use the RefreshDatabase and then use a setUp method that runs before each test or just place the setup in the test

use RefreshDatabase;

 public function guests_can_list_all_books()
    {

        $author1 = factory(Author::class)->create();

        $author2 = factory(Author::class)->create();

Or you can have seeders that you seed with in your tests.

$this->call([
        UserSeeder::class,
        PostSeeder::class,
        CommentSeeder::class,
    ]);

The RefreshDatabase clears the database between every test or you can do it manually in a tearDown method.

https://dyclassroom.com/phpunit/phpunit-fixtures-setup-and-teardown

1 like

Please or to participate in this conversation.