Search code examples
phpenvironmentcodeception

Get current environment in Codeception 1.8


In Codeception 2.1 there is a new function current(), which helps to get current environment for tests: $scenario->current('env') I'm using version 1.8 and there is no such function. So, I was trying to add method current() to Scenario class manually, but it doesn't help:

protected $env = array(); 
protected $currents = array();

public function __construct(\Codeception\TestCase $test, $currents = array()) 
{ 
    $this->test = $test; 
    $this->currents = $currents; 
} 

public function env($env)
{
    if (!is_array($env)) { 
        $this->env[] = $env; 
        return; 
    } 
    foreach ($env as $e) { 
        $this->env($e); 
    } 
}

public function current($key) { 
    if (!isset($this->currents[$key])) { 
        echo $this->currents[$key]; 
       throw new TestRuntime("Current $key is not set in this scenario"); 
    } 
    return $this->currents[$key]; 
}

Solution

  • I'm using 2.1, but maybe something like this:

    pass \Codeception\Scenario $scenario as a second parameter

    function testSometing(acceptanceTester $I, \Codeception\Scenario $scenario)        
    {
         $env = $scenario->current();
    }
    

    I struggled with it for a while until I read this ticket: https://github.com/Codeception/Codeception/issues/2225