<commit_before><?php

use \photon\config\Container as Conf;

class SessionTest extends \photon\test\TestCase
{
    protected $conf;

    public function setup()
    {
        parent::setup();
        Conf::set('session_storage', '\photon\session\storage\MongoDB');
    }

    public function testEmptySession()
    {
        $req = \photon\test\HTTP::baseRequest();
        $mid = new \photon\session\Middleware();
        $this->assertEquals(false, $mid->process_request($req));

        $res = new \photon\http\Response('Hello!');
        $mid->process_response($req, $res);
    }

    public function testFilledSession()
    {
<commit_msg>Add unit test which use the mongodb session backend<commit_after><?php

use \photon\config\Container as Conf;

class SessionTest extends \photon\test\TestCase
{
    protected $conf;

    public function setup()
    {
        parent::setup();
        Conf::set('session_storage', '\photon\session\storage\MongoDB');
    }

    public function testEmptySession()
    {
        $req = \photon\test\HTTP::baseRequest();
        $mid = new \photon\session\Middleware();
        $this->assertEquals(false, $mid->process_request($req));

        $res = new \photon\http\Response('Hello!');
        $mid->process_response($req, $res);
        $this->assertEquals(false, isset($res->COOKIE['sid']));
        $this->assertEquals(false, isset($res->headers['Vary']));
    }

    public function testFilledSession()
    {
        // Receive a request, and store a counter in the answer
        $req = \photon\test\HTTP::baseRequest();
        $mid = new \photon\session\Middleware();
        $this->assertEquals(false, $mid->process_request($req));

        $req->session['cpt'] = 1234;
        $res = new \photon\http\Response('Hello!');
        $mid->process_response($req, $res);
        $this->assertEquals(true, isset($res->COOKIE['sid']));

        // Save the signed cookie
        $headers = $res->getHeaders();
        $rc = preg_match('/Set-Cookie: sid=([\w\.]+);/', $headers, $sid);
        $this->assertEquals($rc, 1);
        $sid = $sid[1];

        unset($req);
        unset($res);
        unset($mid);
        
        // Reload the session
        $req = \photon\test\HTTP::baseRequest('GET', '/', '', '', array(), array('cookie' => 'sid=' . $sid));
        $mid = new \photon\session\Middleware();
        $this->assertEquals(false, $mid->process_request($req));
        $cpt = $req->session['cpt'];
        $this->assertEquals(1234, $cpt);
{'fraction_non_alphanumeric': 0.1775813579750904, 'fraction_numerical': 0.004017677782241864, 'mean_word_length': 2.7219730941704037, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5406657', 'n_tokens_mistral': 764, 'n_tokens_neox': 738, 'n_words': 144}