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.
Both tests succeed as Laravel uses the testing environment DB settings. Breaking the settings for the local environment does not impact the tests.
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?
This is expected.
You must delete the cached config file.
Isn't that what php artisan config:cache
does?
No. That makes a new config cache file.
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.
Most helpful comment
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.