7

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?

3 Answers 3

4

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

APV_ENV=testing
Sign up to request clarification or add additional context in comments.

5 Comments

Yes, I have that set as well as all the other testing-specific variables in my .env.testing file (trust me, I've checked and double checked that many times!)
So is it loading any of the data from your .env.testing file at all?
Okay, so that is bizarre... apparently all the variables are getting loaded from .env.testing correctly except APP_ENV. That is still showing as 'local' even though I have it set to 'testing'... Thanks for getting me to check that out as I probably wouldn't have looked into that. Any idea why this is happening though?
There are some solutions here: stackoverflow.com/questions/31061888/…
Late to the party, but APP_ENV was probably set to local in your .env.testing file!
4

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

Comments

1

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.

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.