laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.25k stars 10.92k forks source link

Wrong DB connection used in tests when using the DatabaseMigrations trait #12874

Closed carlosafonso closed 8 years ago

carlosafonso commented 8 years ago

I might just be doing something horribly wrong, but I came across the following behavior in Laravel 5.2.26 with PHP 7.0.4 under OS X 10.11.

I usually work with a Postgres DB in my local environment, and my tests run on an in-memory Sqlite DB. Right now it looks like Laravel ignores the settings for the testing environment.

Please follow these steps to reproduce the issue:

1- Configure you local environment (.env) to use an existing DB, and set the correct connection parameters.

APP_ENV=local
[...]
DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_DATABASE=foo
[...]

2- Modify phpunit.xml to have different DB settings to be used when running the tests:

<!-- (these are as they come out of the box) -->
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<!-- (i added this one) -->
<env name="DB_CONNECTION" value="sqlite"/>

3- Create a test case like this one (it extends the default TestCase):

use Illuminate\Foundation\Testing\DatabaseMigrations;

class DummyTest extends TestCase
{
    use DatabaseMigrations;

    public function testDummy()
    {
        dump(env('APP_ENV'), env('DB_CONNECTION'));
    }
}

4- Run the tests.

5- Change the settings in the .env file, use a non-existent DB name; then run php artisan clear-compiled and php artisan config:cache.

6- Run the tests again.

Expected result

Both tests succeed as Laravel uses the testing environment DB settings. Breaking the settings for the local environment does not impact the tests.

Actual result

The second test run fails with the following error (bar is the non-existent DB name we changed in step 5):

1) DummyTest::testDummy
PDOException: SQLSTATE[08006] [7] FATAL:  database "bar" does not exist

/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:55
/vendor/laravel/framework/src/Illuminate/Database/Connectors/PostgresConnector.php:36
/vendor/laravel/framework/src/Illuminate/Database/Connectors/ConnectionFactory.php:61
[...]

Furthermore, the dump() call outputs the correct values (in my example above, testing and sqlite respectively.

It looks like the framework loads the correct testing settings but ignores them when running the migrations, using the ones from the local environment instead.

Did I miss some step or is this a bug?

GrahamCampbell commented 8 years ago

This is expected.

GrahamCampbell commented 8 years ago

You must delete the cached config file.

carlosafonso commented 8 years ago

Isn't that what php artisan config:cache does?

GrahamCampbell commented 8 years ago

No. That makes a new config cache file.

carlosafonso commented 8 years ago

I see. Indeed, running php artisan config:clear seems to solve this. Thank you.

If you're open to PRs on the framework's documentation I'm willing to add this to the Testing section. I think developers could benefit from knowing this in advance, if you find it appropriate.