Skip to content

Commit b81cdf0

Browse files
committed
jwt-session-4 Created unit test
1 parent b12db86 commit b81cdf0

File tree

5 files changed

+189
-0
lines changed

5 files changed

+189
-0
lines changed

.travis.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
language: php
2+
3+
php:
4+
- "7.2"
5+
- "7.1"
6+
- "7.0"
7+
- "5.6"
8+
9+
install:
10+
- composer install
11+
12+
script:
13+
- vendor/bin/phpunit

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,8 @@
1818
"php": ">=5.6.0",
1919
"byjg/jwt-wrapper": "1.0.*"
2020
},
21+
"require-dev": {
22+
"phpunit/phpunit": ">=5.7"
23+
},
2124
"license": "MIT"
2225
}

phpunit.xml.dist

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
To change this license header, choose License Headers in Project Properties.
4+
To change this template file, choose Tools | Templates
5+
and open the template in the editor.
6+
-->
7+
8+
<!-- see http://www.phpunit.de/wiki/Documentation -->
9+
<phpunit bootstrap="./vendor/autoload.php"
10+
colors="true"
11+
convertErrorsToExceptions="true"
12+
convertNoticesToExceptions="true"
13+
convertWarningsToExceptions="true"
14+
stopOnFailure="false">
15+
16+
<filter>
17+
<whitelist>
18+
<directory>./src</directory>
19+
</whitelist>
20+
</filter>
21+
22+
<testsuites>
23+
<testsuite name="Test Suite">
24+
<directory>./tests/</directory>
25+
</testsuite>
26+
</testsuites>
27+
28+
</phpunit>

src/JwtSession.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ public function write($session_id, $session_data)
173173

174174
if (!headers_sent()) {
175175
setcookie(self::COOKIE_PREFIX . $this->suffix, $token, null, '/', $this->cookieDomain);
176+
if (defined("SETCOOKIE_FORTEST")) {
177+
$_COOKIE[self::COOKIE_PREFIX . $this->suffix] = $token;
178+
}
176179
}
177180

178181
return true;

tests/JwtSessionTest.php

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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+
const SESSION_ID = "sessionid";
16+
17+
protected function setUp()
18+
{
19+
$this->object = new JwtSession("example.com", "secretKey");
20+
}
21+
22+
protected function tearDown()
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)
136+
{
137+
$this->object->write("SESSID", $serialize);
138+
$result = $this->object->read("SESSID");
139+
$this->assertEquals($serialize, $result);
140+
}
141+
142+
}

0 commit comments

Comments
 (0)