JwtSessionTest::testSerializeSessionData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
use ByJG\Session\JwtSession;
4
5
ob_start();
6
define("SETCOOKIE_FORTEST", "TESTCASE");
7
8
class JwtSessionTest extends \PHPUnit\Framework\TestCase
9
{
10
    /**
11
     * @var JwtSession
12
     */
13
    protected $object;
14
15
    /**
16
     * @var \ByJG\Session\SessionConfig
17
     */
18
    protected $sessionConfig;
19
20
    const SESSION_ID = "sessionid";
21
22
    protected function setUp()
23
    {
24
        $this->sessionConfig = (new \ByJG\Session\SessionConfig('example.com'))
25
            ->withSecret('secretKey');
26
27
        $this->object = new JwtSession($this->sessionConfig);
28
    }
29
30
    protected function tearDown()
31
    {
32
        header_remove();
33
        $_COOKIE = [];
34
        $this->object = null;
35
    }
36
37
38
    public function testDestroy()
39
    {
40
        $this->assertTrue($this->object->destroy(self::SESSION_ID));
41
    }
42
43
    public function testGc()
44
    {
45
        $this->assertTrue($this->object->gc(0));
46
    }
47
48
    public function testClose()
49
    {
50
        $this->assertTrue($this->object->close());
51
    }
52
53
    public function dataProvider()
54
    {
55
        $obj = new stdClass();
56
        $obj->prop1 = "value1";
57
        $obj->prop2 = "value2";
58
59
        return
60
        [
61
            [
62
                [
63
                    "text" => "simple string"
64
                ],
65
                "text|s:13:\"simple string\";"
66
            ],
67
            [
68
                [
69
                    "text" => "simple string",
70
                    "text2" => "another string",
71
                    "number" => 74
72
                ],
73
                "text|s:13:\"simple string\";text2|s:14:\"another string\";number|i:74;"
74
            ],
75
            [
76
                [
77
                    "text" => [ 1, 2, 3 ]
78
                ],
79
                "text|a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}"
80
            ],
81
            [
82
                [
83
                    "text" => [ "a" => 1, "b" => 2, "c" => 3 ]
84
                ],
85
                "text|a:3:{s:1:\"a\";i:1;s:1:\"b\";i:2;s:1:\"c\";i:3;}"
86
            ],
87
            [
88
                [
89
                    "text" => [ "a" => 1, "b" => 2, "c" => 3 ],
90
                    "single" => 2000
91
                ],
92
                "text|a:3:{s:1:\"a\";i:1;s:1:\"b\";i:2;s:1:\"c\";i:3;}single|i:2000;"
93
            ],
94
            [
95
                [
96
                    "text" => $obj
97
                ],
98
                "text|O:8:\"stdClass\":2:{s:5:\"prop1\";s:6:\"value1\";s:5:\"prop2\";s:6:\"value2\";}"
99
            ],
100
            [
101
                [
102
                    "text" => [ "a" => $obj ]
103
                ],
104
                "text|a:1:{s:1:\"a\";O:8:\"stdClass\":2:{s:5:\"prop1\";s:6:\"value1\";s:5:\"prop2\";s:6:\"value2\";}}"
105
            ],
106
            [
107
                [
108
                    "text" => [ $obj ]
109
                ],
110
                "text|a:1:{i:0;O:8:\"stdClass\":2:{s:5:\"prop1\";s:6:\"value1\";s:5:\"prop2\";s:6:\"value2\";}}"
111
            ]
112
        ];
113
    }
114
115
    /**
116
     * @dataProvider dataProvider
117
     * @param $input
118
     * @param $expected
119
     */
120
    public function testSerializeSessionData($input, $expected)
121
    {
122
        $result = $this->object->serializeSessionData($input);
123
        $this->assertEquals($expected, $result);
124
    }
125
126
    /**
127
     * @dataProvider dataProvider
128
     * @param $expected
129
     * @param $input
130
     * @throws Exception
131
     */
132
    public function testUnserializeData($expected, $input)
133
    {
134
        $result = $this->object->unSerializeSessionData($input);
135
        $this->assertEquals($expected, $result);
136
    }
137
138
    /**
139
     * @dataProvider dataProvider
140
     * @param $object
141
     * @param $serialize
142
     */
143
    public function testReadWrite($object, $serialize)
0 ignored issues
show
Unused Code introduced by
The parameter $object is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
144
    {
145
        $this->object->write("SESSID", $serialize);
146
        $result = $this->object->read("SESSID");
147
        $this->assertEquals($serialize, $result);
148
    }
149
150
}
151