thelazzyriveryogi's avatar

Testing Database 5.4

I've been trying for a little while now to set up a testing database in laravel 5.4 If there are any up to date resources on doing this I would like to see it. I am wondering if a lot of the resources I have tried are obsolete.

Firstly, I am correct that once I setup the sqlite database for testing I do need to run migrations? Because thats when I am hitting my errors. As long as this is so lets proceed.

Are there any specific configurations I need to put in phpunit.xml???

I leave my .env alone correct? Do I need a .env.testing?

I did make one

APP_ENV=testing
APP_DEBUG=true
APP_LOG_LEVEL=debug

DB_HOST=127.0.0.1
DB_CONNECTION=sqlite.testing

or do I just update my config/database.php

'sqlite_testing' => [
    'driver' => 'sqlite',
    'database' => storage_path().'/database.sqlite',
    'prefix' => '',
],

I have tried several other paths such as the default

'database' => env('DB_DATABASE', database_path('database.sqlite')),

don't remember the exact variations I tried but I tried several. And I created the file in those locations when I tried it. One error I got while trying it that second way was "couldnt write to file" or something like that, so I changed permissions to try 777 but still got the same.

My current error message is "sqlite.testing" not configured.

Seems like something that should be easy but its giving me a bunch of trouble, any help would be appreciated. Ultimately I'd like the fastest setup so I would probably like the in memory sqlite setting if you can focus your answer towards that!

0 likes
10 replies
bastman69's avatar

insert those in your phpunit.xml

<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
thelazzyriveryogi's avatar

so do you mean i don't need to run migrations? do they occur automatically with running phpunit? that doesnt sound right to me....

my example tests still run but I am still getting "database [sqlite.testing] not configured" when i try to migrate

bastman69's avatar

if your tests needs are to migrate the database then you must use the DatabaseMigrations trait.

use DatabaseMigrations;

In my setup i dont use a .env.testing file. i have my developement db setup (mysql) and in my phpunit xml i have the test db details as per my previous message.

Spend 2 hours and check this series https://laracasts.com/series/phpunit-testing-in-laravel

thelazzyriveryogi's avatar

yea i watched it lol

when I did what he did to set it up i just got errors...

bastman69's avatar

The videos are one year old and for sure with and older version of laravel. Anyway leave this project as is and create a new one update your phpunit.xml as above, use the DatabaseTransactions trait and hopefullly it will work.

Laravel documentation is also reach of examples. https://laravel.com/docs/5.4/testing

thelazzyriveryogi's avatar

yea well i added databasemigrations trait and now im hitting on some new errors i think maybe thats doing what I am expecting just have to interpret these new errors and clean up any messes I made. probably some inconsistency in my migrations... seems I added have a not null column with default value null... mysql thinks this is ok??

Edit: inside the migration i am not adding a default value so much for the easy fix idea.

bastman69's avatar

While testing, errors are not bad. Try to fix them. new test, new errors, new fixing code, code refactoring, new errors etc. its normal situation.

thelazzyriveryogi's avatar

no no its migrations errors. its a real hassle and it has to do with sqlite having different requirements. migrations work perfectly for mysql but now its got all these issues for sqlite

thelazzyriveryogi's avatar

i solved the migrations problem as follows.

I moved my old pain in the butt migrations to database/migrationsBackupOriginal

I installed https://github.com/Xethron/migrations-generator

and ran a new set of migrations

now it all works fine

maybe I can move on with my life now.... I keep a backup of my old migrations and have a cleaner set of current migrations. hopeful!

definitely thanks for your help tho i thought this was gonna be a whole day affair but looks like I am where I want to be now. I won't be able to check the functionality of the temporary in memory database until later i think but ill let you know if its broke

Please or to participate in this conversation.