I have a .env
file containing my database connection details, as is normal for Laravel 5. I want to override these for testing, which I can do in phpunit.
Been struggling with this for a few months now and just came across this Github issue today. From the solutions proposed there, here's what you should do in your CreatesApplication.php file (to delete the cached config in order to have Laravel load the test environment):
/**
* Creates the application.
*
* @return \Illuminate\Foundation\Application
*/
public function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
$this->clearCache(); // NEW LINE -- Testing doesn't work properly with cached stuff.
return $app;
}
/**
* Clears Laravel Cache.
*/
protected function clearCache()
{
$commands = ['clear-compiled', 'cache:clear', 'view:clear', 'config:clear', 'route:clear'];
foreach ($commands as $command) {
\Illuminate\Support\Facades\Artisan::call($command);
}
}
If you're still experiencing this issue after the above modification, you can go further by rebuilding the entire application as follows:
public function createApplication()
{
$createApp = function() {
$app = require __DIR__.'/../bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
return $app;
};
$app = $createApp();
if ($app->environment() !== 'testing') {
$this->clearCache();
$app = $createApp();
}
return $app;
}
This is working just fine for me.
Updated
For Laravel 5.8 users, you may create a .env.testing
file in the root of your project.
Use a different db, like my_app_testing.
So, it will be, in .env
DB_DATABASE=clinical_managment
and in .env.testing
DB_DATABASE=clinical_managment_testing
Then, make config clear.
php artisan config:clear
Re-run the test. In my setup, it works.
Copy your .env
to .env.testing
, then edit the .env.testing
file and change the APP_ENV
parameter to make it like this APP_ENV=testing
this way you will be able to specify your settings int this new file
In case you don't want to create a new .env.testing
file you have to specify your variables in the phpunit.xml
in the php section with the values you need, something like this
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value="testing"/>
</php>
Just use the key values in the name section and the value of that key in the value section.
For this example I am specifying phpunit to use an sqlite database with the name of testing.
By the way in config/database.php I added this
'default' => env('DB_CONNECTION', 'mysql'),
to use mysql by default unless I specify something diferent, as in this case.
I did all the steps in @Sambhu Singh answer as well as followed his link. But didn't work for me in L5.5
When migrating, adding/setting APP_ENV to 'testing' in front of the artisan command worked for me:
APP_ENV=testing php artisan migrate --database=sqlite_testing