|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yiisoft\Yii\Web\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use Nyholm\Psr7\Response; |
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
use Yiisoft\Yii\Web\Cookie; |
|
8
|
|
|
|
|
9
|
|
|
class CookieTest extends TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
private function getCookieHeader(Cookie $cookie): string |
|
12
|
|
|
{ |
|
13
|
|
|
$response = new Response(); |
|
14
|
|
|
$response = $cookie->addToResponse($response); |
|
15
|
|
|
return $response->getHeaderLine('Set-Cookie'); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
public function testInvalidName(): void |
|
19
|
|
|
{ |
|
20
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
21
|
|
|
new Cookie('test[]', 42); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function testInvalidValue(): void |
|
25
|
|
|
{ |
|
26
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
27
|
|
|
new Cookie('test', ';'); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function testDefaults(): void |
|
31
|
|
|
{ |
|
32
|
|
|
$cookie = new Cookie('test', 42); |
|
33
|
|
|
|
|
34
|
|
|
$this->assertSame('test=42; Path=/; Secure; HttpOnly; SameSite=Lax', $this->getCookieHeader($cookie)); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function testDomain(): void |
|
38
|
|
|
{ |
|
39
|
|
|
$cookie = (new Cookie('test', 42))->domain('yiiframework.com'); |
|
40
|
|
|
|
|
41
|
|
|
$this->assertSame('test=42; Domain=yiiframework.com; Path=/; Secure; HttpOnly; SameSite=Lax', $this->getCookieHeader($cookie)); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function testExpireAt(): void |
|
45
|
|
|
{ |
|
46
|
|
|
$expireDateTime = new \DateTime(); |
|
47
|
|
|
$expireDateTime->setTimezone(new \DateTimeZone('GMT')); |
|
48
|
|
|
$formattedDateTime = $expireDateTime->format('D, d M Y H:i:s T'); |
|
49
|
|
|
|
|
50
|
|
|
$cookie = (new Cookie('test', 42))->expireAt($expireDateTime); |
|
51
|
|
|
|
|
52
|
|
|
$this->assertSame("test=42; Expires=$formattedDateTime; Path=/; Secure; HttpOnly; SameSite=Lax", $this->getCookieHeader($cookie)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function testExpireWhenBrowserIsClosed(): void |
|
56
|
|
|
{ |
|
57
|
|
|
$cookie = (new Cookie('test', 42))->expireWhenBrowserIsClosed(); |
|
58
|
|
|
|
|
59
|
|
|
$this->assertSame('test=42; Path=/; Secure; HttpOnly; SameSite=Lax', $this->getCookieHeader($cookie)); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function testPath(): void |
|
63
|
|
|
{ |
|
64
|
|
|
$cookie = (new Cookie('test', 42))->path('/test'); |
|
65
|
|
|
|
|
66
|
|
|
$this->assertSame('test=42; Path=/test; Secure; HttpOnly; SameSite=Lax', $this->getCookieHeader($cookie)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function testInvalidPath(): void |
|
70
|
|
|
{ |
|
71
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
72
|
|
|
(new Cookie('test', 42))->path(';invalid'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function testSecure(): void |
|
76
|
|
|
{ |
|
77
|
|
|
$cookie = (new Cookie('test', 42))->secure(false); |
|
78
|
|
|
|
|
79
|
|
|
$this->assertSame('test=42; Path=/; HttpOnly; SameSite=Lax', $this->getCookieHeader($cookie)); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function testHttpOnly(): void |
|
83
|
|
|
{ |
|
84
|
|
|
$cookie = (new Cookie('test', 42))->httpOnly(false); |
|
85
|
|
|
|
|
86
|
|
|
$this->assertSame('test=42; Path=/; Secure; SameSite=Lax', $this->getCookieHeader($cookie)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function testInvalidSameSite(): void |
|
90
|
|
|
{ |
|
91
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
92
|
|
|
|
|
93
|
|
|
(new Cookie('test', 42))->sameSite('invalid'); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function testSameSite(): void |
|
97
|
|
|
{ |
|
98
|
|
|
$cookie = (new Cookie('test', 42))->sameSite(Cookie::SAME_SITE_NONE); |
|
99
|
|
|
|
|
100
|
|
|
$this->assertSame('test=42; Path=/; Secure; HttpOnly; SameSite=None', $this->getCookieHeader($cookie)); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function testFromSetCookieString(): void |
|
104
|
|
|
{ |
|
105
|
|
|
$setCookieString = 'sessionId=e8bb43229de9; Domain=foo.example.com; Path=/; Secure; HttpOnly; SameSite=Strict'; |
|
106
|
|
|
$cookie = (new Cookie('sessionId', 'e8bb43229de9', false)) |
|
107
|
|
|
->domain('foo.example.com') |
|
108
|
|
|
->path('/') |
|
109
|
|
|
->secure(true) |
|
110
|
|
|
->httpOnly(true) |
|
111
|
|
|
->sameSite(Cookie::SAME_SITE_STRICT); |
|
112
|
|
|
$cookie2 = Cookie::fromSetCookieString($setCookieString); |
|
113
|
|
|
|
|
114
|
|
|
$this->assertSame((string)$cookie, (string)$cookie2); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|