Passed
Push — master ( 3c5346...dd6497 )
by Alexander
17:04 queued 14:30
created

CookieTest::testGetters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 24
nc 1
nop 0
dl 0
loc 33
rs 9.536
c 0
b 0
f 0
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
final 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 testDefaults(): void
25
    {
26
        $cookie = new Cookie('test', 42);
27
28
        $this->assertSame('test=42; Path=/; Secure; HttpOnly; SameSite=Lax', $this->getCookieHeader($cookie));
29
    }
30
31
    public function testWithValue(): void
32
    {
33
        $cookie = (new Cookie('test'))->withValue(42);
34
        $this->assertSame('test=42; Path=/; Secure; HttpOnly; SameSite=Lax', $this->getCookieHeader($cookie));
35
    }
36
37
    public function testValueThatIsEncoded(): void
38
    {
39
        $cookieString = (string)(new Cookie('test'))->withValue(';');
40
        $this->assertSame('test=%3B; Path=/; Secure; HttpOnly; SameSite=Lax', $cookieString);
41
    }
42
43
    public function testWithExpires(): void
44
    {
45
        $expireDateTime = new \DateTime('+1 year');
46
        $expireDateTime->setTimezone(new \DateTimeZone('GMT'));
47
        $formattedDateTime = $expireDateTime->format(\DateTimeInterface::RFC7231);
48
        $maxAge = $expireDateTime->getTimestamp() - time();
49
50
        $cookie = (new Cookie('test', 42))->withExpires($expireDateTime);
51
52
        $this->assertSame("test=42; Expires=$formattedDateTime; Max-Age=$maxAge; Path=/; Secure; HttpOnly; SameSite=Lax", $this->getCookieHeader($cookie));
53
    }
54
55
    public function testIsExpired(): void
56
    {
57
        $cookie = (new Cookie('test', 42))->withExpires((new \DateTimeImmutable('-5 years')));
58
        $this->assertTrue($cookie->isExpired());
59
    }
60
61
    public function testWithMaxAge(): void
62
    {
63
        $formattedExpire = (new \DateTimeImmutable())->setTimestamp(time() + 3600)->format(\DateTimeInterface::RFC7231);
64
        $cookie = (new Cookie('test', 42))->withMaxAge(new \DateInterval('PT3600S'));
65
66
        $this->assertSame("test=42; Expires=$formattedExpire; Max-Age=3600; Path=/; Secure; HttpOnly; SameSite=Lax", $this->getCookieHeader($cookie));
67
    }
68
69
    public function testExpire(): void
70
    {
71
        $cookie = (new Cookie('test', 42))->expire();
72
        $this->assertTrue($cookie->isExpired());
73
    }
74
75
    public function testNegativeInterval(): void
76
    {
77
        $formattedExpire = (new \DateTimeImmutable())->setTimestamp(time() - 3600)->format(\DateTimeInterface::RFC7231);
78
        $negativeInterval = new \DateInterval('PT3600S');
79
        $negativeInterval->invert = 1;
80
        $cookie = (new Cookie('test', 42))->withMaxAge($negativeInterval);
81
82
        $this->assertSame("test=42; Expires=$formattedExpire; Max-Age=-3600; Path=/; Secure; HttpOnly; SameSite=Lax", $this->getCookieHeader($cookie));
83
    }
84
85
    public function testWithDomain(): void
86
    {
87
        $cookie = (new Cookie('test', 42))->withDomain('yiiframework.com');
88
89
        $this->assertSame('test=42; Domain=yiiframework.com; Path=/; Secure; HttpOnly; SameSite=Lax', $this->getCookieHeader($cookie));
90
    }
91
92
    public function testExpireWhenBrowserIsClosed(): void
93
    {
94
        $cookie = (new Cookie('test', 42))->expireWhenBrowserIsClosed();
95
96
        $this->assertSame('test=42; Path=/; Secure; HttpOnly; SameSite=Lax', $this->getCookieHeader($cookie));
97
    }
98
99
    public function testWithPath(): void
100
    {
101
        $cookie = (new Cookie('test', 42))->withPath('/test');
102
103
        $this->assertSame('test=42; Path=/test; Secure; HttpOnly; SameSite=Lax', $this->getCookieHeader($cookie));
104
    }
105
106
    public function testInvalidPath(): void
107
    {
108
        $this->expectException(\InvalidArgumentException::class);
109
        (new Cookie('test', 42))->withPath(';invalid');
110
    }
111
112
    public function testWithSecure(): void
113
    {
114
        $cookie = (new Cookie('test', 42))->withSecure(false);
115
116
        $this->assertSame('test=42; Path=/; HttpOnly; SameSite=Lax', $this->getCookieHeader($cookie));
117
    }
118
119
    public function testHttpOnly(): void
120
    {
121
        $cookie = (new Cookie('test', 42))->withHttpOnly(false);
122
123
        $this->assertSame('test=42; Path=/; Secure; SameSite=Lax', $this->getCookieHeader($cookie));
124
    }
125
126
    public function testInvalidSameSite(): void
127
    {
128
        $this->expectException(\InvalidArgumentException::class);
129
130
        (new Cookie('test', 42))->withSameSite('invalid');
131
    }
132
133
    public function testSameSiteNone(): void
134
    {
135
        $cookie = (new Cookie('test', 42))->withSameSite(Cookie::SAME_SITE_NONE);
136
137
        $this->assertSame('test=42; Path=/; Secure; HttpOnly; SameSite=None', $this->getCookieHeader($cookie));
138
    }
139
140
    public function testFromCookieString(): void
141
    {
142
        $expireDate = new \DateTimeImmutable('+60 minutes');
143
        $setCookieString = 'sessionId=e8bb43229de9; Domain=foo.example.com; ';
144
        $setCookieString .= 'Expires=' . $expireDate->format(\DateTimeInterface::RFC7231) . '; ';
145
        $setCookieString .= 'Max-Age=3600; Path=/; Secure; SameSite=Strict; ExtraKey';
146
147
        $cookie = new Cookie(
148
            'sessionId',
149
            'e8bb43229de9',
150
            $expireDate,
151
            'foo.example.com',
152
            '/',
153
            true,
154
            false,
155
            Cookie::SAME_SITE_STRICT
156
        );
157
        $cookie2 = Cookie::fromCookieString($setCookieString);
158
159
        $this->assertSame((string)$cookie, (string)$cookie2);
160
    }
161
162
    public function testFromCookieStringWithInvalidString(): void
163
    {
164
        $this->expectException(\InvalidArgumentException::class);
165
        Cookie::fromCookieString('');
166
    }
167
168
    public function testGetters(): void
169
    {
170
        $cookie = new Cookie('test', '', null, null, null, null, null, null);
171
        $this->assertEquals('test', $cookie->getName());
172
        $this->assertEquals('', $cookie->getValue());
173
        $this->assertNull($cookie->getExpires());
174
        $this->assertNull($cookie->getDomain());
175
        $this->assertNull($cookie->getPath());
176
        $this->assertFalse($cookie->isSecure());
177
        $this->assertFalse($cookie->isHttpOnly());
178
        $this->assertNull($cookie->getSameSite());
179
180
        $cookie = $cookie->withValue('testValue');
181
        $this->assertEquals('testValue', $cookie->getValue());
182
183
        $expiry = new \DateTimeImmutable();
184
        $cookie = $cookie->withExpires($expiry);
185
        $this->assertEquals($expiry->getTimestamp(), $cookie->getExpires()->getTimestamp());
186
187
        $cookie = $cookie->withDomain('yiiframework.com');
188
        $this->assertEquals('yiiframework.com', $cookie->getDomain());
189
190
        $cookie = $cookie->withPath('/path');
191
        $this->assertEquals('/path', $cookie->getPath());
192
193
        $cookie = $cookie->withSecure(true);
194
        $this->assertTrue($cookie->isSecure());
195
196
        $cookie = $cookie->withHttpOnly(true);
197
        $this->assertTrue($cookie->isHttpOnly());
198
199
        $cookie = $cookie->withSameSite(Cookie::SAME_SITE_LAX);
200
        $this->assertEquals(Cookie::SAME_SITE_LAX, $cookie->getSameSite());
201
    }
202
}
203