Passed
Pull Request — master (#244)
by
unknown
02:51
created

CookieTest::testSameSite()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
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
use function Sodium\add;
10
11
final class CookieTest extends TestCase
12
{
13
    private function getCookieHeader(Cookie $cookie): string
14
    {
15
        $response = new Response();
16
        $response = $cookie->addToResponse($response);
17
        return $response->getHeaderLine('Set-Cookie');
18
    }
19
20
    public function testInvalidName(): void
21
    {
22
        $this->expectException(\InvalidArgumentException::class);
23
        new Cookie('test[]', 42);
24
    }
25
26
    public function testInvalidValue(): void
27
    {
28
        $this->expectException(\InvalidArgumentException::class);
29
        new Cookie('test', ';');
30
    }
31
32
    public function testDefaults(): void
33
    {
34
        $cookie = new Cookie('test', 42);
35
36
        $this->assertSame('test=42; Path=/; Secure; HttpOnly; SameSite=Lax', $this->getCookieHeader($cookie));
37
    }
38
39
    public function testDomain(): void
40
    {
41
        $cookie = (new Cookie('test', 42))->domain('yiiframework.com');
42
43
        $this->assertSame('test=42; Domain=yiiframework.com; Path=/; Secure; HttpOnly; SameSite=Lax', $this->getCookieHeader($cookie));
44
    }
45
46
    public function testExpireAt(): void
47
    {
48
        $expireDateTime = new \DateTime();
49
        $expireDateTime->setTimezone(new \DateTimeZone('GMT'));
50
        $formattedDateTime = $expireDateTime->format(\DateTimeInterface::RFC7231);
51
52
        $cookie = (new Cookie('test', 42))->expireAt($expireDateTime);
53
54
        $this->assertSame("test=42; Expires=$formattedDateTime; Path=/; Secure; HttpOnly; SameSite=Lax", $this->getCookieHeader($cookie));
55
    }
56
57
    public function testMaxAge(): void
58
    {
59
        $maxAge = new \DateInterval('PT3600S');
60
        $formattedDateTime = (new \DateTimeImmutable())->add($maxAge)->format(\DateTimeInterface::RFC7231);
61
62
        $cookie = (new Cookie('test', 42))->maxAge($maxAge);
63
        $this->assertSame("test=42; Expires=$formattedDateTime; Max-Age=3600; Path=/; Secure; HttpOnly; SameSite=Lax", $this->getCookieHeader($cookie));
64
    }
65
66
    public function testIsExpired(): void
67
    {
68
        $cookie = (new Cookie('test', 42))->expireAt((new \DateTimeImmutable('-5 years')));
69
        $this->assertTrue($cookie->isExpired());
70
    }
71
72
    public function testExpire(): void
73
    {
74
        $formattedDateTime = (new \DateTimeImmutable('-5 years'))->format(\DateTimeInterface::RFC7231);
75
        $cookie = (new Cookie('test', 42))->expire();
76
        $this->assertSame("test=42; Expires=$formattedDateTime; Path=/; Secure; HttpOnly; SameSite=Lax", $this->getCookieHeader($cookie));
77
78
    }
79
80
    public function testExpireWhenBrowserIsClosed(): void
81
    {
82
        $cookie = (new Cookie('test', 42))->expireWhenBrowserIsClosed();
83
84
        $this->assertSame('test=42; Path=/; Secure; HttpOnly; SameSite=Lax', $this->getCookieHeader($cookie));
85
    }
86
87
    public function testPath(): void
88
    {
89
        $cookie = (new Cookie('test', 42))->path('/test');
90
91
        $this->assertSame('test=42; Path=/test; Secure; HttpOnly; SameSite=Lax', $this->getCookieHeader($cookie));
92
    }
93
94
    public function testInvalidPath(): void
95
    {
96
        $this->expectException(\InvalidArgumentException::class);
97
        (new Cookie('test', 42))->path(';invalid');
98
    }
99
100
    public function testSecure(): void
101
    {
102
        $cookie = (new Cookie('test', 42))->secure(false);
103
104
        $this->assertSame('test=42; Path=/; HttpOnly; SameSite=Lax', $this->getCookieHeader($cookie));
105
    }
106
107
    public function testHttpOnly(): void
108
    {
109
        $cookie = (new Cookie('test', 42))->httpOnly(false);
110
111
        $this->assertSame('test=42; Path=/; Secure; SameSite=Lax', $this->getCookieHeader($cookie));
112
    }
113
114
    public function testInvalidSameSite(): void
115
    {
116
        $this->expectException(\InvalidArgumentException::class);
117
118
        (new Cookie('test', 42))->sameSite('invalid');
119
    }
120
121
    public function testSameSiteNone(): void
122
    {
123
        $cookie = (new Cookie('test', 42))->sameSite(Cookie::SAME_SITE_NONE);
124
125
        $this->assertSame('test=42; Path=/; Secure; HttpOnly; SameSite=None', $this->getCookieHeader($cookie));
126
    }
127
128
    public function testFromSetCookieString(): void
129
    {
130
        $expireDate = new \DateTimeImmutable();
131
        $maxAge = new \DateInterval('PT3600S');
132
        $setCookieString = 'sessionId=e8bb43229de9; Domain=foo.example.com; ';
133
        $setCookieString .= 'Expires=' . $expireDate->format(\DateTimeInterface::RFC7231) . '; ';
134
        $setCookieString .= 'Max-Age=3600; Path=/; Secure; HttpOnly; SameSite=Strict';
135
136
        $cookie = (new Cookie('sessionId', 'e8bb43229de9', false))
137
            ->expireAt($expireDate)
138
            ->maxAge($maxAge)
139
            ->domain('foo.example.com')
140
            ->path('/')
141
            ->secure(true)
142
            ->httpOnly(true)
143
            ->sameSite(Cookie::SAME_SITE_STRICT);
144
        $cookie2 = Cookie::fromSetCookieString($setCookieString);
145
146
        $this->assertSame((string)$cookie, (string)$cookie2);
147
    }
148
}
149