orest's avatar
Level 13

set env variable in phpunit at runtime

by default the database variables in the phpunit.xml are the following:

 <server name="DB_CONNECTION" value="sqlite"/>
 <server name="DB_DATABASE" value=":memory:"/>

In one of my tests, i want to set the DB_CONNECTION to mysql because i test a functionality which uses whereJsonContains which is not supported by sqlite.

Is there a way to change the DB_CONNECTION at runtime ?

  • I have tried to use putenv and config but none of them work
  • I don't want the default connection to be mysql because it is considerably slower than using memory database
1 like
2 replies
slev1n's avatar

@orest

\Config::set('database.default', 'mysql');
\DB::purge();

User::first(); // will query your mysql database
5 likes

Please or to participate in this conversation.