Passed
Push — master ( 4cd94b...0d0336 )
by Mihail
02:45
created

FunctionsTest::test_session_function_singleton()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Koded\Session;
4
5
use Koded\Stdlib\Config;
6
use PHPUnit\Framework\TestCase;
7
8
9
class FunctionsTest extends TestCase
10
{
11
    public function test_session_function_singleton()
12
    {
13
        $this->assertSame(session(), session());
14
    }
15
16
    public function test_should_throw_exception_on_invalid_handler_class()
17
    {
18
        $this->expectException(SessionException::class);
19
        $this->expectExceptionCode(0);
20
        $this->expectExceptionMessage('Failed to load the session handler class. Requested Koded\Session\Handler\StdClassHandler');
21
22
        $config = (new Config)->import([
23
            'session' => [
24
                'save_handler' => \stdClass::class, // invalid session handler
25
            ]
26
        ]);
27
28
        session_create_custom_handler(new SessionConfiguration($config));
29
    }
30
31
    public function test_should_register_session_cookie()
32
    {
33
        $config = (new Config)->import([
34
            'session' => [
35
                'save_handler' => 'files',
36
37
                'use_cookies'     => true,
38
                'cookie_lifetime' => 120,
39
                'cookie_path'     => '/tmp',
40
                'cookie_secure'   => true,
41
                'cookie_httponly' => true,
42
            ]
43
        ]);
44
45
        session_register_custom_handler($config);
46
47
        if (version_compare(PHP_MINOR_VERSION, '3', '<')) {
48
            $this->assertEquals([
49
                'lifetime' => 120,
50
                'path'     => '/tmp',
51
                'domain'   => '',
52
                'secure'   => true,
53
                'httponly' => true,
54
            ], session_get_cookie_params());
55
        } else {
56
            $this->assertEquals([
57
                'lifetime' => 120,
58
                'path'     => '/tmp',
59
                'domain'   => '',
60
                'secure'   => true,
61
                'httponly' => true,
62
                'samesite' => '',
63
            ], session_get_cookie_params());
64
        }
65
    }
66
}
67