Search code examples
phptestingcodeception

Access settings in Codeception bootstrap code


I am upgrading tests from Codeception v2 to v4. The bootstrapping code is referenced in acceptance.suite.yml and loaded just fine.

Visible inside the _bootstrap.php file as of now there was a variable $settings, set by the surrounding Codeception code, that held information about all the live data, that was configured for the tests to run.

This variable is now gone. Printing get_defined_vars() only shows two variables set, strings that point to the current path and bootstrap file name.

How can I access the settings in bootstrapping code again?

I’ve looked at packagist, if there’d be a candidate for a split-off module, that would be of use here, but no candidate looked promising.

Edit: I’ve tried accessing the settings manually:

$settings = \Codeception\Configuration::suiteSettings('acceptance',
    \Codeception\Configuration::config());

However, this only allows me access to the “static” settings, i.e., basically as written in the according YAML files. What I need are the “final” settings, i.e., the ones after the environment is evaluated.


Solution

  • I’ve solved this problem by switching from a bootstrap file to an extension. Example:

    In codeception.yml:

    extensions:
        enabled:
            - Bootstrapper
    

    The class is found in lib/Bootstrapper.php via Composer’s class list feature, composer.json:

    {
        "autoload": {
            "classmap": [
                "lib/"
            ]
        }
    }
    

    and looks something like this:

    <?php
    
    use Codeception\Events;
    use Codeception\Extension;
    
    class Bootstrapper extends Extension {
    
        public static $events = [
            Events::SUITE_BEFORE => 'beforeSuite',
        ];
    
        public function beforeSuite() {
            $module = 'PhpBrowser';
            if ($this->hasModule('WebDriver')) {
                $module = 'WebDriver';
            }
    
            /* expose info, if we're in real-browser context */
            define('IS_REAL_BROWSER', $module === 'WebDriver');
    
            /* make sure the helper functions are loaded */
            require_once __DIR__.'/../tests/acceptance/_helpers.php';
        }
    }