Search code examples
phpbddbehat

Access Behat command line option or flag in context method


Is it possible to get the value of a command-line option or see if a command-line flag is set from within a Behat context method?

I'm using a hook as described here to set the xdebug session cookie before steps. This works fine, but I'd like to be able to control whether the cookie is set from the command-line.

Currently I have something like this (simplified):

/**
 * @BeforeStep
 */
public function xdebugCookie()
{
    $this->getSession()->setCookie('XDEBUG_SESSION', 'PHPSTORM');
}

But ideally I'd like to be able to do something like this:

/**
 * @BeforeStep
 */
public function xdebugCookie()
{
    if ($this->cliFlagSet('xdebug')) { // does this kind of functionality exist?
        $this->getSession()->setCookie('XDEBUG_SESSION', 'PHPSTORM');
    }
}

Solution

  • You could use an environment variable to accomplish the same:

    /**
     * @BeforeStep
     */
    public function xdebugCookie()
    {
        if ('1' === getenv('XDEBUG')) {
            $this->getSession()->setCookie('XDEBUG_SESSION', 'PHPSTORM');
        }
    }
    

    This way you can define the variable while running behat:

    XDEBUG=1 ./bin/behat
    

    You could also export it to make sure all runs enable (or disable) the debugger:

    export XDEBUG=1
    ./bin/behat