How to access values from .env in Laravel 5.1 TestCase
7 replies
Run Dotenv
to get .env variables in TestCase step
public function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';
$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();
Dotenv::load(__DIR__.'/../');
$this->baseUrl = env('APP_URL', $this->baseUrl);
return $app;
}
+4
source to share
I can confirm that Christopher Raymond's suggestion above, replace
Dotenv::load call
from
this $app->loadEnvironmentFrom('.env.testing');
works with Laravel 5.4
See example:
protected $baseUrl = 'http://localhost';
/**
* Creates the application.
*
* @return \Illuminate\Foundation\Application
*/
public function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
$app->loadEnvironmentFrom('.env');
$this->baseUrl = env('APP_URL', $this->baseUrl);
return $app;
}
+2
source to share
I have this in my .env file :
APP_URL=http://project.dev
Then I modified the createApplication function in tests /TestCase.php
/**
* Creates the application.
*
* @return \Illuminate\Foundation\Application
*/
public function createApplication()
{
$this->baseUrl = env('APP_URL', $this->baseUrl);
$app = require __DIR__ . '/../bootstrap/app.php';
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
return $app;
}
0
source to share