Laravel 4 test controllers called model

I'm having trouble getting a test to work in Laravel 4. I use the files .envto manage my database settings as described in the Laravel Configuration Guide - Protecting Sensitive Configuration

The file is app/config/database.php as follows:

'mysql' => array(
    'driver'    => 'mysql',
    'host'      => $_ENV['dbhost'],
    'database'  => $_ENV['database'],
    'username'  => $_ENV['dbusername'],
    'password'  => $_ENV['dbpassword'],
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
),

Checked controller method:

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()
    ));
}

Test:

<?php

# app/tests/controllers/TaxonomyControllerTest.php

class TaxonomyControllerTest extends TestCase
{
    public function testGetTaxonomies()
    {
        $this->action('GET', 'TaxonomyController@getTaxonomies');

        $this->assertResponseOk();
    }
}

The error I get is ErrorException: Undefined index: dbhost. I understand this because $_ENVvar is not populated in the CLI. So my question is: how should I handle db loans for testing?

Update:

, database.php app/config/testing, . , ? ?

+4
1

. , . , .env.development.php, .

.env.testing.php .

return array(

    /*
    |--------------------------------------------------------------------------
    | Database Credentials
    |--------------------------------------------------------------------------
    */
    'dbhost'     => '127.0.0.1',
    'database'   => 'database',
    'dbusername' => 'username',
    'dbpassword' => 'password',

);
+2

Source: https://habr.com/ru/post/1544808/


All Articles