-
Notifications
You must be signed in to change notification settings - Fork 0
/
CookieTest.php
139 lines (121 loc) · 3.67 KB
/
CookieTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
declare(strict_types=1);
namespace Spiral\Tests\Cookies;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Spiral\Cookies\Cookie;
class CookieTest extends TestCase
{
public function testAccess(): void
{
$cookie = new Cookie(
'cookie',
'value',
100,
'/',
'.domain.com',
true,
true
);
$this->assertSame('cookie', $cookie->getName());
$this->assertSame('value', $cookie->getValue());
$this->assertSame(time() + 100, $cookie->getExpires());
$this->assertSame('.domain.com', $cookie->getDomain());
$this->assertTrue($cookie->isSecure());
$this->assertTrue($cookie->isHttpOnly());
}
public function testCreateStaticAccess(): void
{
$cookie = Cookie::create(
'cookie',
'value',
100,
'/',
'.domain.com',
true,
true
);
$this->assertSame('cookie', $cookie->getName());
$this->assertSame('value', $cookie->getValue());
$this->assertSame(time() + 100, $cookie->getExpires());
$this->assertSame('.domain.com', $cookie->getDomain());
$this->assertTrue($cookie->isSecure());
$this->assertTrue($cookie->isHttpOnly());
}
public function testFallbackValues(): void
{
$cookie = new Cookie(
'cookie',
'value',
null,
null,
null,
true,
true
);
$this->assertNull($cookie->getExpires());
$this->assertNull($cookie->getPath());
$this->assertNull($cookie->getDomain());
}
public function testWithValue(): void
{
$cookie = new Cookie(
'cookie',
'value',
null,
null,
null,
true,
true
);
$cookie1 = $cookie->withValue('new-value');
$this->assertNotSame($cookie, $cookie1);
$this->assertSame('value', $cookie->getValue());
$this->assertSame('new-value', $cookie1->getValue());
}
public function testPack(): void
{
$cookie = new Cookie(
'cookie',
'value',
100,
'/',
'.domain.com',
true,
true
);
$this->assertSame($cookie->createHeader(), (string)$cookie);
$this->assertStringContainsString(
'cookie=value;',
$cookie->createHeader()
);
$this->assertStringContainsString(
'Max-Age=100; Path=/; Domain=.domain.com; Secure; HttpOnly',
$cookie->createHeader()
);
}
#[DataProvider('sameSiteProvider')]
public function testSameSite(?string $expected, bool $secure, ?string $sameSite): void
{
$cookie = new Cookie('', '', 0, '', '', $secure, false, $sameSite);
$this->assertSame($expected, $cookie->getSameSite());
if ($expected === null) {
$this->assertStringNotContainsString('SameSite=', $cookie->createHeader());
} else {
$this->assertStringContainsString("SameSite=$expected", $cookie->createHeader());
}
}
public static function sameSiteProvider(): \Traversable
{
yield [null, true, null];
yield [null, false, null];
yield [null, true, 'weird'];
yield [null, false, 'weird'];
yield ['Lax', true, 'lax'];
yield ['Lax', false, 'lax'];
yield ['Strict', true, 'strict'];
yield ['Strict', false, 'strict'];
yield ['None', true, 'none'];
yield ['Lax', false, 'none'];
}
}