Reputation: 13686
I'm having some trouble getting a test to work in Laravel 4. I'm using .env
files to manage my DB settings the way it is described in Laravel's Configuration manual - Protecting Sensitive Configuration
The app/config/database.php
file looks like:
'mysql' => array(
'driver' => 'mysql',
'host' => $_ENV['dbhost'],
'database' => $_ENV['database'],
'username' => $_ENV['dbusername'],
'password' => $_ENV['dbpassword'],
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
The controller method being tested:
public function getTaxonomies()
{
if (Input::has('page')) {
$limit = (Input::get('limit')) ? Input::get('limit') : 15;
$taxonomy = Taxonomy::with('photos')->paginate($limit)->toArray();
return Response::json(array(
'Taxonomies' => $taxonomy
));
}
return Response::json(array(
'Taxonomies' => Taxonomy::all()->load('photos')->toArray()
));
}
The test:
<?php
# app/tests/controllers/TaxonomyControllerTest.php
class TaxonomyControllerTest extends TestCase
{
public function testGetTaxonomies()
{
$this->action('GET', 'TaxonomyController@getTaxonomies');
$this->assertResponseOk();
}
}
The error I'm getting is ErrorException: Undefined index: dbhost
. I realize this is because the $_ENV
var is not being populated in CLI. So my question is, how am I supposed to handle db creds for testing?
Update:
So I added an empty database.php
file to my app/config/testing
folder and now I'm no longer getting that error. I'm assuming it's because the database isn't being called anymore? Should I just be using mockery to test with data?
Upvotes: 4
Views: 278
Reputation: 756
Note: You may create a file for each environment supported by your application. For example, the development environment will load the .env.development.php file if it exists.
Just create .env.testing.php
file and write your test database credentials in it.
return array(
/*
|--------------------------------------------------------------------------
| Database Credentials
|--------------------------------------------------------------------------
*/
'dbhost' => '127.0.0.1',
'database' => 'database',
'dbusername' => 'username',
'dbpassword' => 'password',
);
Upvotes: 2