Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 testing environment not getting set in Codeception unit tests

I'm using Laravel 5 and Codeception and I would like to use an in-memory SQLite database for my unit tests, however, I can't seem to get my environment set to "testing" in Codeception. I'm using the Laravel5 module and have the following defined in my unit.suite.yml file:

class_name: UnitTester
modules:
    enabled: [Asserts, UnitHelper, Laravel5]
    config:
        Laravel5:
            environment_file: .env.testing

I have a .env file which defines all my local settings, then a .env.testing file that defines all the testing-specific settings. However, it never seems to actually set the environment correctly.

To test the environment I just did:

$this->assertEquals('testing', \App::environment());

and I always get:

Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'testing'
+'local'

Anyone have any idea what I'm doing wrong?

like image 707
bb89 Avatar asked Apr 08 '15 00:04

bb89


3 Answers

Did you set the environment name in your .env.testing file?

APV_ENV=testing
like image 135
Laurence Avatar answered Oct 10 '22 00:10

Laurence


This could be because you have the env file defined in your global codeception config. In codeception.yml file in your project root directory check the value for params, should be something like following

params:
    - .env.testing
like image 34
xelber Avatar answered Oct 10 '22 00:10

xelber


I am completely new to laravel, but just found that the only way to get tests running on my local machine and (for example) Codeship, is to symlink .env when I need it. (I know - this isn't a very clean way, but hey, it works)

I have beside my local .env file:

#.env.testing
APP_ENV=testing
DB_CONNECTION=default_mysql
DB_DATABASE=test_db
... etc

And:

#.env.codeship
APP_ENV=codeship
DB_CONNECTION=codeship

For local tests I made an alias:

alias pl='rm .env; ln -s .env.testing .env; phpunit; rm .env; ln -s .env.local .env'

Where .env.local holds a copy of my local .env file.

And for Codeships test pipeline:

ln -s .env.codeship .env
php artisan migrate --seed
phpunit

Database configuration:

// CODESHIP
'codeship' => [
    'driver' => 'mysql',        
    'username' => getenv('MYSQL_USER'),
    'password' => getenv('MYSQL_PASSWORD'),
    // etc        
],

// LOCAL DEV // PHPUNIT
'default_mysql' => [
    'driver' => 'mysql',
    'username' => env('DB_USERNAME', 'localhost'),
    'password' => env('DB_PASSWORD', 'forge'),
    // etc        
],

If anybody got better ideas, I am happy to hear.

like image 37
everyman Avatar answered Oct 10 '22 01:10

everyman