Laravel PHPUnit Test Uses production configuration by Default

I came across an interesting problem today. I was Using laravel’s PHPUnit test framework to test a Eloquent class in my local environment. When I ran simple tests, I got that laravel eloquent find i was always returning null. Printing query log and running them was not helping.

So, I guessed whether laravel is actually connecting to my local database at all. So, I tried printing

var_dump(Config::get('database'));

In my test function. And surprisingly, PHPUnit outputted production servers database configurations! It should have used the environment I am testing! Anyway, that’s an architectural problem. So, here is the fix:

Copy the ‘database.php’ from your local environment to testing environment.

‘app/config/local/database.php’  to  ‘app/config/testing/database.php’

It will work with this manual trick.

But remember now, if you want to test in production server, “which we never do”, PHPUnit will use local database connection!

Related problems:

Laravel Eloquent::Find() is returning null

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s