4

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?

0

1 Answer 1

2

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',

);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.