How to properly change testing environment vars - Codeception and Laravel 5.1

8.4k views Asked by At

I am trying to use a sqlite db for testing purposes, as I don't want to touch my actual database:

In my unit.suite.yml file:

class_name: UnitTester
modules:
    enabled: [Asserts, UnitHelper]
    config:
        Db:
            dsn: 'sqlite:app/tests/_data/testdb.sqlite'
            user: ''
            password: ''
            dump: app/tests/_data/dump.sql

In my TestCase.php file I have a function which resets and migrates and seeds the db for testing:

        Artisan::call('migrate:reset');
        Artisan::call('migrate');
        Artisan::call('db:seed');

I also added the sqlite settings to app/config/testing/database.php:

<?php
// Codeception testing environment uses SQLITE

return [

    'default' => 'sqlite',

    'connections' => [
        'sqlite' => [
            'driver' => 'sqlite',
            'database' => 'app/tests/_data/testdb.sqlite',
            'prefix' => ''
        ],
    ]
];

I can verify that the database.php file is being read but something still prevents sqlite from being used, and I still end up hosing my REAL database (MySQL). How do I force Laravel/Codeception to use my test db?

6

There are 6 answers

3
Joel Joel Binks On BEST ANSWER

So I got fed up and decided to do this hacky thing:

In .env I added this:

DB_DEFAULT=mysql

then I added my sqlite_testing config to the database.php file and I altered the default setting like so:

'default' => env('DB_DEFAULT', 'mysql'),

Lastly, I added this to the beginning of my TestCase.php:

    putenv('DB_DEFAULT=sqlite_testing');

I'm sticking with this because it actually works, but any comment/suggestions are appreciated. I found this solution on laracasts site:

https://laracasts.com/discuss/channels/testing/how-to-specify-a-testing-database-in-laravel-5

1
Nick On

I use tests/_bootstrap.php for this purpose:

<?php
// This is global bootstrap for autoloading
putenv('MY_ENV_NAME=my_env_valye');
0
xelber On

In codeception.yml file (see documentation at https://codeception.com/docs/06-ModulesAndHelpers#Dynamic-Configuration-With-Parameters) in your project root directory check the value for params, should be something like following

params:
    - .env.testing
0
Miha Trtnik On

copying a part of my blog here ...

After searching around a bit, we suppose to include variables inside phpunit.xml. There are some preincluded ones

<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"/>
</php>

However I wanted to use .env files. So there is always a way :)

All you need to do now is call applications loadEnvironmentFrom($file) with proper env file. Since I only needed it for testing I added .env.testing.example and .env.testing (that is excluded from git). I added a call to method in /tests/TestCase.php

public function createApplication()
{
    $app = require __DIR__.'/../bootstrap/app.php';

    //THIS IS IT :)
    $app->loadEnvironmentFrom('.env.testing');

    $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();

    return $app;
}
0
carlosvini On

TL;DR:

APP_ENV=testing php artisan serve

If you run "php artisan help" you will see:

--env[=ENV]       The environment the command should run under.
  • --env=testing does not change the .env file anymore. So this information is incomplete.
  • Files like config/testing/database.php worked on Laravel 4.2, won't work on Laravel 5
  • PHPUnit sets environment variables through phpunit.xml, but it won't work with Codeception.
  • Codeception has a module for Laravel 5, there you can set environment_file to be .env.testing and then you can use this file for your tests. However it won't work on acceptance tests, since these tests open a browser. In this case you could customize your app.php and add this before return $app:

if (!empty($argv) && in_array('--env=testing', $argv)) { $app->loadEnvironmentFrom('.env.testing'); }

And then you can start your server with "php artisan serve --env=testing"

This is a workaround and i hope soon enough someone from Laravel will define the right way to do this.

UPDATE: This way is less messy:

APP_ENV=testing php artisan serve

And then:

if (getenv('APP_ENV') === 'testing') { 
    $app->loadEnvironmentFrom('.env.testing');
}

UPDATE 2: it seems you don't need the condition above in 5.2, or maybe never needed?

0
Max Barannyk On

1 - create .env file in the root of Codeception project and add LOGIN=adminENV in this .env file. Add lines below to your codeception.yml file:

params:
- .env

Again, Add lines below to your codeception.yml file at root level (without indentation):

test_key02: "%LOGIN%"

Now inside Codeception project

$myLogin = \Codeception\Configuration::config()['test_key02'];
    var_dump($myLogin);