Skip to content

Commit ff060b3

Browse files
authored
Merge pull request #9 from ppavlovic/master
Ported changes from releaase/2.x to master
2 parents 15dfdcf + 9afa2fd commit ff060b3

3 files changed

Lines changed: 187 additions & 7 deletions

File tree

composer.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,22 @@
4343
},
4444
"scripts": {
4545
"unit-test": [
46-
"./vendor/bin/phpunit -c tests/unit/phpunit.xml --colors=always --coverage-html tests/unit/coverage"
46+
"XDEBUG_MODE=coverage php8.3 ./vendor/bin/phpunit -c tests/unit/phpunit.xml --colors=always --coverage-html tests/unit/coverage"
4747
],
4848
"test-coverage": [
49-
"./vendor/bin/phpunit --colors=always -c tests/unit/phpunit.xml --coverage-text"
49+
"XDEBUG_MODE=coverage php8.3 ./vendor/bin/phpunit --colors=always -c tests/unit/phpunit.xml --coverage-text"
5050
],
5151
"test-report": [
52-
"./vendor/bin/phpunit --colors=always -c tests/unit/phpunit.xml --coverage-clover=tests/unit/coverage/code-coverage.xml"
52+
"XDEBUG_MODE=coverage php8.3 ./vendor/bin/phpunit --colors=always -c tests/unit/phpunit.xml --coverage-clover=tests/unit/coverage/code-coverage.xml"
5353
],
5454
"code-coverage": [
55-
"./vendor/bin/code-coverage -p 90 -f tests/unit/coverage/code-coverage.xml"
55+
"XDEBUG_MODE=coverage php8.3 ./vendor/bin/code-coverage -p 90 -f tests/unit/coverage/code-coverage.xml"
5656
],
5757
"psr2": [
58-
"./vendor/bin/phpcs --colors --standard=PSR2 src/"
58+
"XDEBUG_MODE=coverage php8.3 ./vendor/bin/phpcs --colors --standard=PSR2 src/"
5959
],
6060
"psr2-fix": [
61-
"./vendor/bin/phpcbf --colors --standard=PSR2 src/"
61+
"XDEBUG_MODE=coverage php8.3 ./vendor/bin/phpcbf --colors --standard=PSR2 src/"
6262
]
6363
}
6464
}

src/Session.php

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,26 @@ class Session
3535
*/
3636
private $domainName;
3737

38+
/**
39+
* @var string
40+
*/
41+
private $cookiePath = '/';
42+
43+
/**
44+
* @var bool
45+
*/
46+
private $cookieHttpOnly = false;
47+
48+
/**
49+
* @var bool
50+
*/
51+
private $cookieSecure = false;
52+
53+
/**
54+
* @var string
55+
*/
56+
private $cookieSameSite = '';
57+
3858
/**
3959
* @param array $options
4060
*/
@@ -127,13 +147,48 @@ public function setDomainName($domainName): Session
127147
return $this;
128148
}
129149

150+
public function cookiePath(string $path)
151+
{
152+
$this->cookiePath = $path;
153+
return $this;
154+
}
155+
156+
public function cookieHttpOnly(bool $value)
157+
{
158+
$this->cookieHttpOnly = $value;
159+
return $this;
160+
}
161+
162+
public function cookieSecure(bool $value)
163+
{
164+
$this->cookieSecure = $value;
165+
return $this;
166+
}
167+
168+
public function cookieSameSite(string $value)
169+
{
170+
$validValues = ['', 'Lax', 'Strict'];
171+
if (!in_array($value, $validValues)) {
172+
throw new \InvalidArgumentException('Invalid value for cookieSameSite');
173+
}
174+
$this->cookieSameSite = $value;
175+
return $this;
176+
}
177+
130178
/**
131179
* @return Session
132180
* @throws MissingDomainNameException
133181
*/
134182
public function start(): Session
135183
{
136-
session_set_cookie_params($this->getLifetime(), '/', $this->getDomainName());
184+
session_set_cookie_params([
185+
'lifetime' => $this->getLifetime(),
186+
'path' => $this->cookiePath,
187+
'domain' => $this->getDomainName(),
188+
'secure' => $this->cookieSecure,
189+
'httponly' => $this->cookieHttpOnly,
190+
'samesite' => $this->cookieSameSite,
191+
]);
137192

138193
$this->manager = new SessionManager($this->getConfig());
139194
$this->manager

tests/unit/src/SessionTest.php

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,131 @@ public function testGetDomainNameException()
2929
$this->session->getDomainName();
3030
}
3131

32+
public function testCookiePathDefault()
33+
{
34+
$reflection = new \ReflectionClass($this->session);
35+
$property = $reflection->getProperty('cookiePath');
36+
$property->setAccessible(true);
37+
$this->assertEquals('/', $property->getValue($this->session));
38+
}
39+
40+
public function testCookiePathSetter()
41+
{
42+
$result = $this->session->cookiePath('/custom/path');
43+
$this->assertInstanceOf(Session::class, $result);
44+
45+
$reflection = new \ReflectionClass($this->session);
46+
$property = $reflection->getProperty('cookiePath');
47+
$property->setAccessible(true);
48+
$this->assertEquals('/custom/path', $property->getValue($this->session));
49+
}
50+
51+
public function testCookieHttpOnlyDefault()
52+
{
53+
$reflection = new \ReflectionClass($this->session);
54+
$property = $reflection->getProperty('cookieHttpOnly');
55+
$property->setAccessible(true);
56+
$this->assertFalse($property->getValue($this->session));
57+
}
58+
59+
public function testCookieHttpOnlySetter()
60+
{
61+
$result = $this->session->cookieHttpOnly(true);
62+
$this->assertInstanceOf(Session::class, $result);
63+
64+
$reflection = new \ReflectionClass($this->session);
65+
$property = $reflection->getProperty('cookieHttpOnly');
66+
$property->setAccessible(true);
67+
$this->assertTrue($property->getValue($this->session));
68+
}
69+
70+
public function testCookieSecureDefault()
71+
{
72+
$reflection = new \ReflectionClass($this->session);
73+
$property = $reflection->getProperty('cookieSecure');
74+
$property->setAccessible(true);
75+
$this->assertFalse($property->getValue($this->session));
76+
}
77+
78+
public function testCookieSecureSetter()
79+
{
80+
$result = $this->session->cookieSecure(true);
81+
$this->assertInstanceOf(Session::class, $result);
82+
83+
$reflection = new \ReflectionClass($this->session);
84+
$property = $reflection->getProperty('cookieSecure');
85+
$property->setAccessible(true);
86+
$this->assertTrue($property->getValue($this->session));
87+
}
88+
89+
public function testCookieSameSiteDefault()
90+
{
91+
$reflection = new \ReflectionClass($this->session);
92+
$property = $reflection->getProperty('cookieSameSite');
93+
$property->setAccessible(true);
94+
$this->assertEquals('', $property->getValue($this->session));
95+
}
96+
97+
public function testCookieSameSiteSetterWithEmptyString()
98+
{
99+
$result = $this->session->cookieSameSite('');
100+
$this->assertInstanceOf(Session::class, $result);
101+
102+
$reflection = new \ReflectionClass($this->session);
103+
$property = $reflection->getProperty('cookieSameSite');
104+
$property->setAccessible(true);
105+
$this->assertEquals('', $property->getValue($this->session));
106+
}
107+
108+
public function testCookieSameSiteSetterWithLax()
109+
{
110+
$result = $this->session->cookieSameSite('Lax');
111+
$this->assertInstanceOf(Session::class, $result);
112+
113+
$reflection = new \ReflectionClass($this->session);
114+
$property = $reflection->getProperty('cookieSameSite');
115+
$property->setAccessible(true);
116+
$this->assertEquals('Lax', $property->getValue($this->session));
117+
}
118+
119+
public function testCookieSameSiteSetterWithStrict()
120+
{
121+
$result = $this->session->cookieSameSite('Strict');
122+
$this->assertInstanceOf(Session::class, $result);
123+
124+
$reflection = new \ReflectionClass($this->session);
125+
$property = $reflection->getProperty('cookieSameSite');
126+
$property->setAccessible(true);
127+
$this->assertEquals('Strict', $property->getValue($this->session));
128+
}
129+
130+
public function testCookieSameSiteSetterWithInvalidValue()
131+
{
132+
$this->expectException(\InvalidArgumentException::class);
133+
$this->expectExceptionMessage('Invalid value for cookieSameSite');
134+
$this->session->cookieSameSite('None');
135+
}
136+
137+
public function testCookieSameSiteSetterWithInvalidValueRandom()
138+
{
139+
$this->expectException(\InvalidArgumentException::class);
140+
$this->expectExceptionMessage('Invalid value for cookieSameSite');
141+
$this->session->cookieSameSite('InvalidValue');
142+
}
143+
144+
public function testFluentInterfaceChaining()
145+
{
146+
$result = $this->session
147+
->setDomainName('example.com')
148+
->cookiePath('/app')
149+
->cookieHttpOnly(true)
150+
->cookieSecure(true)
151+
->cookieSameSite('Strict');
152+
153+
$this->assertInstanceOf(Session::class, $result);
154+
$this->assertEquals('example.com', $this->session->getDomainName());
155+
}
156+
32157
protected function setUp(): void
33158
{
34159
$this->session = new Session([]);

0 commit comments

Comments
 (0)