Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bart's avatar
Level 13

[Codeception] Acceptance testing with session variables

Hi everybody,

I like to use the awesome codeception framework to perform acceptance test for my application. But I think I currently couldn't find the clue to deal with session.

I have the following setup:

routes.php

Route::get('/', function()
{
    session_start();
 $session = $_SESSION;
    session_write_close();

    if(array_key_exists('user', $session))
        return 'success';

    return 'fail';
});

acceptance.suite.yml:

class_name: AcceptanceTester
modules:
   enabled: [WebDriver]
   config:
      WebDriver:
         url: 'http://localhost/bart/testing/public/'
         browser: phantomjs
         capabilities:
             webStorageEnabled: true

SessionCest.php

<?php
use \AcceptanceTester;

class SessionCest
{
    public function _before(AcceptanceTester $I)
    {
        $_SESSION = [
            'active' => 1,
            'user' => 'bart',
            'expire' => time() + 600,
            'role' => -1
        ];
    }

    public function _after(AcceptanceTester $I)
    {
    }

    public function tryToTest(AcceptanceTester $I)
    {
        $I->amOnPage('/');
        $I->see('success');
    }
}

The problem is, that I always get "fail" instead of "success" - no matter what I change. So I would like to know, whether there is any way to set session variables before the suite runs?

Thanks and best regards!

0 likes
4 replies
bart's avatar
bart
OP
Best Answer
Level 13

Okay, it wasn't an easy one but I found the solution myself.

First of all you have to add the capability --cookies-file=[path to cookie file] for phantomjs. Then I do the following:

session_start();
$cookieData = file_get_contents([path to cookie file]);
$phpSessionId = substr($cookieData, strpos($cookieData, 'PHPSESSID=') + 10, 26);

session_id($phpSessionId);

$_SESSION = [
   'status' => 1,
   'expire' => time() + 600,
   'user' => 'bart',
   'role' => [-1],
];

session_write_close();
Flatline's avatar

I'm not that familiar with codeception, but I think the Laravel4 module should be able to handle that just fine.

bart's avatar
Level 13

@Flatline, you are totally right, but I have to use the default PHP session variable, because this part of code is just a module from our codebase, uses default PHP without any framework - currently. But I'm there to change it ;)

Flatline's avatar

@bart ah, got it. Good luck with that! Hope you can get them to change :)

Please or to participate in this conversation.