connecteev
5 months ago
718
6
Testing

Configuring PHPUnit to connect with a test DB

Posted 5 months ago by connecteev

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.

Please sign in or create an account to participate in this conversation.