0

I want to modify DB::connection so if I am running tests it will override a given connection.

I have lots of hard-coded connections in the models like this DB::connection('mysql2') so I want to override the mysql2 connection with mysql_testing.

How can I go about this?

1
  • Don't do this. The phpunit.xml file lets you override any .env values while running tests, via stuff like <env name="APP_ENV" value="testing"/>. Override the DB_CONNECTION environment variable and have at it. Commented Oct 11, 2017 at 18:44

1 Answer 1

1

What I usually do is adding in phpunit.xml:

<env name="DB_CONNECTION" value="mysql_testing"/>

Then in config/database.php I create duplicate of mysql connection something like this:

'mysql_testing' => [
    'driver' => 'mysql',
    'host' => env('DB_TESTING_HOST', 'localhost'),
    'database' => env('DB_TESTING_DATABASE', 'forge'),
    'username' => env('DB_TESTING_USERNAME', 'forge'),
    'password' => env('DB_TESTING_PASSWORD', ''),
    'charset' => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix' => '',
    'strict' => false,
],

and then in .env I define testing connection settings:

#TESTING

DB_TESTING_HOST=127.0.0.1
DB_TESTING_DATABASE=test_database
DB_TESTING_USERNAME=root
DB_TESTING_PASSWORD=pass

It's working without any problems plus you don't need to switch anything in .env if you are running tests and testing application manually on same machine/in same directory

EDIT

If you have situation like this, probably modifying multiple models is not the best way but if you want to use same database regardless of connection set you can do something like that:

Assuming you have set environment to testing what can be done using:

<env name="APP_ENV" value="testing"/>

in your phpunit.xml file you can do go into your AppServiceProvider class and in register method do something like this:

if ($this->app->environment('testing')) {
    $this->app['config']->set('database.connections.mysql2.database', 'yourcustom testing database');
    // ... 
    $this->app['config']->set('database.connections.mysql15.database', 'yourcustom testing database');
}

I haven't tried it but it should work - it should make that when running tests you can set all your connections to same testing database (or to other databases if you wish)

Sign up to request clarification or add additional context in comments.

5 Comments

The problem with that is I have lots of hard-coded connections in the models like this DB::connection('mysql2') so I want to override the mysql2 connection with mysql_testing which won't work by just setting it in phpunit.xml
Won't that only set the connection until DB::connection('mysql2') is called?
What do you mean? When you use it, it should connect to database you set in service provider. So if in your model you are using this connection mysql2 and and in service provider you set it to different database this different database should be used
I see now, I think that will work. Need to give it a test.
@Jack I'd handle mysql2 by having a DB2_CONNECTION .env value. Make it configurable, which lets you override that config for testing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.