Completed
Pull Request — master (#5)
by Joao
05:40
created

JwtSessionTest::testSerializeSessionData()   A

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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 6 and the first side effect is on line 5.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
use ByJG\Session\JwtSession;
4
5
ob_start();
6
define("SETCOOKIE_FORTEST", "TESTCASE");
7
8
class JwtSessionTest extends \PHPUnit\Framework\TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
9
{
10
    /**
11
     * @var JwtSession
12
     */
13
    protected $object;
14
15
    const SESSION_ID = "sessionid";
16
17
    protected function setUp()
18
    {
19
        $this->object = new JwtSession("example.com", "secretKey");
20
    }
21
22
    protected function tearDown()
0 ignored issues
show
Coding Style introduced by
tearDown uses the super-global variable $_COOKIE which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
23
    {
24
        header_remove();
25
        $_COOKIE = [];
26
        $this->object = null;
27
    }
28
29
30
    public function testDestroy()
31
    {
32
        $this->assertTrue($this->object->destroy(self::SESSION_ID));
33
    }
34
35
    public function testGc()
36
    {
37
        $this->assertTrue($this->object->gc(0));
38
    }
39
40
    public function testClose()
41
    {
42
        $this->assertTrue($this->object->close());
43
    }
44
45
    public function dataProvider()
46
    {
47
        $obj = new stdClass();
48
        $obj->prop1 = "value1";
49
        $obj->prop2 = "value2";
50
51
        return
52
        [
53
            [
54
                [
55
                    "text" => "simple string"
56
                ],
57
                "text|s:13:\"simple string\";"
58
            ],
59
            [
60
                [
61
                    "text" => "simple string",
62
                    "text2" => "another string",
63
                    "number" => 74
64
                ],
65
                "text|s:13:\"simple string\";text2|s:14:\"another string\";number|i:74;"
66
            ],
67
            [
68
                [
69
                    "text" => [ 1, 2, 3 ]
70
                ],
71
                "text|a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}"
72
            ],
73
            [
74
                [
75
                    "text" => [ "a" => 1, "b" => 2, "c" => 3 ]
76
                ],
77
                "text|a:3:{s:1:\"a\";i:1;s:1:\"b\";i:2;s:1:\"c\";i:3;}"
78
            ],
79
            [
80
                [
81
                    "text" => [ "a" => 1, "b" => 2, "c" => 3 ],
82
                    "single" => 2000
83
                ],
84
                "text|a:3:{s:1:\"a\";i:1;s:1:\"b\";i:2;s:1:\"c\";i:3;}single|i:2000;"
85
            ],
86
            [
87
                [
88
                    "text" => $obj
89
                ],
90
                "text|O:8:\"stdClass\":2:{s:5:\"prop1\";s:6:\"value1\";s:5:\"prop2\";s:6:\"value2\";}"
91
            ],
92
            [
93
                [
94
                    "text" => [ "a" => $obj ]
95
                ],
96
                "text|a:1:{s:1:\"a\";O:8:\"stdClass\":2:{s:5:\"prop1\";s:6:\"value1\";s:5:\"prop2\";s:6:\"value2\";}}"
97
            ],
98
            [
99
                [
100
                    "text" => [ $obj ]
101
                ],
102
                "text|a:1:{i:0;O:8:\"stdClass\":2:{s:5:\"prop1\";s:6:\"value1\";s:5:\"prop2\";s:6:\"value2\";}}"
103
            ]
104
        ];
105
    }
106
107
    /**
108
     * @dataProvider dataProvider
109
     * @param $input
110
     * @param $expected
111
     */
112
    public function testSerializeSessionData($input, $expected)
113
    {
114
        $result = $this->object->serializeSessionData($input);
115
        $this->assertEquals($expected, $result);
116
    }
117
118
    /**
119
     * @dataProvider dataProvider
120
     * @param $expected
121
     * @param $input
122
     * @throws Exception
123
     */
124
    public function testUnserializeData($expected, $input)
125
    {
126
        $result = $this->object->unSerializeSessionData($input);
127
        $this->assertEquals($expected, $result);
128
    }
129
130
    /**
131
     * @dataProvider dataProvider
132
     * @param $object
133
     * @param $serialize
134
     */
135
    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...
136
    {
137
        $this->object->write("SESSID", $serialize);
138
        $result = $this->object->read("SESSID");
139
        $this->assertEquals($serialize, $result);
140
    }
141
142
}
143