How to change vars test environment correctly - Codeception and Laravel 5.1

I am trying to use sqlite db for testing since I do not 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 that resets and migrates, and db seeds for testing:

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

I also added 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 does not allow sqlite to be used, and I still end up shunting my REAL database (MySQL). How to force Laravel / Codeception to use my test db?

+4
laravel codeception
source share
5 answers

So I got tired and decided to do this hacker thing:

In .env, I added the following:

 DB_DEFAULT=mysql 

then I added the sqlite_testing configuration to the database.php file and I changed the default setting as follows:

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

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

  putenv('DB_DEFAULT=sqlite_testing'); 

I stick with this because it really works, but any comments / suggestions are appreciated. I found this solution on laracasts website:

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

+2
source share

copying part of my blog here ...

After searching a bit, we assume to include the variables inside phpunit.xml. There are some pre-included

 <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 have to do is call loadEnvironmentFrom ($ file) with the corresponding env file. Since I only needed this for testing, I added .env.testing.example and .env.testing (this is excluded from git). I added a method call to /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; } 
+5
source share

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 no longer modifies the .env file. Therefore, this information is incomplete.
  • Files like config / testing / database.php that worked on Laravel 4.2 will not work on Laravel 5
  • PHPUnit sets environment variables via phpunit.xml , but does not work with Codeception.
  • Codeception has a module for Laravel 5 , there you can set environment_file as .env. testing , and then you can use this file for your tests. However, it will not work during acceptance tests, as these tests open the browser. <stroke> In this case, you can configure your app.php and add this before returning $ 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 that someone from Laravel will soon determine the right way to do this.

hit>

UPDATE: this method 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 you never need to?

+4
source share

In the codeception.yml file (see the documentation at https://codeception.com/docs/06-ModulesAndHelpers#Dynamic-Configuration-With-Parameters ) in the project root directory check the value for the parameters, there should be something as shown below

 params: - .env.testing 
+1
source share

I use tests/_bootstrap.php for this purpose:

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

All Articles