smnhunt's avatar

Different database in different tests - is it possible?

I know that it is possible to setup a separate database for testing and local.

I have been using an in memory sqlite database to test my models/repositories and seeders but would now like to work with a live database to test my api.

Is it possible to have different tests use different databases? Perhaps there is a property that can be overridden on a test by test basis?

0 likes
4 replies
thefuzzy0ne's avatar

Can you not just override the ENV variables to change the database?

ctroms's avatar

Yes you can. Setup a new connection in config/database.php and override the DB_CONNECTION env variable in phpunit.xml in the configuration section. I think in this lesson Jeffrey goes through the exact steps to create an sqlite testing database. If it isn't that lesson it is one of them in that series.

smnhunt's avatar

Where do you think would be the best place to do this? In the test setUp() method?

bobbybouwmann's avatar
Level 88

@smnhunt The seeInDatabase function also has accepts an extra parameter for a database connection

protected function seeInDatabase($table, array $data, $connection = null)

Not sure what you want to test. But I think you can do something with this as well ;)

For example

public function setUp()
{
    $this->connection = 'sqlite-testing';
}

public function test_something()
{
    $this->seeInDatabase('users', [
        'id' => 1,
    ], $this->connection);
}
1 like

Please or to participate in this conversation.