Shovels's avatar

Test database

What's best practise when it comes to testing database transactions?

I'm using MySQL on Homestead (all works fine). But if I run Laravel's 'seeInDatabase' test locally I get a connection error. This is due to the db connection string in .env being 'localhost', not 'localhost:33060'

I assumed that by creating a .env.testing file and setting the db connection string to 'localhost:33060' PHPunit would use this to connect (but it doesn't)

I can run the tests on the VM, and they work as expected - but the notifications aren't being passed through.

I'm guessing that I'm approaching this from the wrong direction. Any help greatly appreciated.

0 likes
8 replies
pixelpeter's avatar
Level 19

AFAIK you have to set your database connection in phpunit.xml.

    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="DB_HOST" value="localhost"/>
        ...
    </php>
1 like
pixelpeter's avatar

In codeception for you functional suite you would set your database configuration in functional.suite.yml like this:

    config:
        Laravel5:
            environment_file: .env.testing
Shovels's avatar

Thanks @pixelpeter - I'm using the testing suite built into Laravel 5.1.

Adding the DB connection strings to phpunit.xml appears to have worked - but this begs the question why you would have a .env.testing file? Surely it would switch to using this configuration for the testing environment.

The next thing I'm attempting is to run the migrations on the test DB using

php artisan migrate --env=testing

But, again the env parameter is ignored and it attempts to run the migrations on the database specified in the .env file.

@JeffreyWay am I missing the point here? Thanks :)

1 like
pixelpeter's avatar

Just add another connection settings to your database config (like "sqlite_testing") and migrate like this

php artisan migrate --database=sqlite_testing

Things have changed with 5.1, and not everything to the best ...

1 like
Shovels's avatar

Cheers @pixelpeter I did see someone else who had done the same, but was thinking that it didn't seem 'right' when migrate has the option to set the environment :)

Ahh well... means to an end and all that.

Thanks again for your help.

pixelpeter's avatar

You're welcome

I'm also not satisfied with this solution either, but right now it seems to be the way we have to go.

Kind of awkward when all the other things in Laravel are so beautifully elegant

Please or to participate in this conversation.