Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

environment_file is not respected if laravel config cache exists (Laravel 7.x and 8.x) #37

Open
derMart opened this issue Dec 3, 2021 · 1 comment

Comments

@derMart
Copy link

derMart commented Dec 3, 2021

Hi,

documentation states (https://codeception.com/docs/modules/Laravel) that environment_file specifies the environment file to load for the tests.

Laravel loads env file in https://github.com/laravel/framework/blob/1f700400890e7c43233afb43951821b9d2b70f3c/src/Illuminate/Foundation/Bootstrap/LoadEnvironmentVariables.php#L86
That function is, however, only called if there is no existing config cache, see https://github.com/laravel/framework/blob/3b6732ff4c2a095e1edac6a3dab84d9b29243325/src/Illuminate/Foundation/Bootstrap/LoadEnvironmentVariables.php#L22

So, e.g. running php artisan config:cache will create a config cache with your default .env file.

Running vendor/bin/codecept run [...] will then ignore the environment_file as the config is not loaded, as there already is a cache using the default .env file. No warning or error whatever is thrown. The test will run using the .env file possibly clearing and filling your database with test cases.

Is this by design or a bug?
I haven't found anything in the docs of the module regarding this cache issue.

My current workaround will be to run php artisan config:clear before running any test, maybe even including this command in the setup routines of an extended Laravel codeception module. What is the best practise to avoid such problems?

EDIT: I would strongly advise to at least throw an error if a config cache exists or the config cache is different than what is specified in the environment_file

@derMart
Copy link
Author

derMart commented Dec 3, 2021

For anyone interested, my current workaround is to automatically clear the config cache when codecept is executed.
This is done by extending the Laravel module as follows:

use Codeception\Module\Laravel;

class UnCachedLaravel extends Laravel
{
    public function _initialize() {
        // delete config cache, so that testing environment_file will be used
        $fs = new FileSystem();
        $app = require $this->config['bootstrap_file'];
        $fs->delete($app->getCachedConfigPath());
        parent::_initialize();
    }
}

and then using that extended Module in the respective .suite.yml files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant