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

CookieTest::testFromSetCookieString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 15
c 2
b 1
f 0
nc 1
nop 0
dl 0
loc 19
rs 9.7666
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
    public function testExpireWhenBrowserIsClosed(): void
80
    {
81
        $cookie = (new Cookie('test', 42))->expireWhenBrowserIsClosed();
82
83
        $this->assertSame('test=42; Path=/; Secure; HttpOnly; SameSite=Lax', $this->getCookieHeader($cookie));
84
    }
85
86
    public function testPath(): void
87
    {
88
        $cookie = (new Cookie('test', 42))->path('/test');
89
90
        $this->assertSame('test=42; Path=/test; Secure; HttpOnly; SameSite=Lax', $this->getCookieHeader($cookie));
91
    }
92
93
    public function testInvalidPath(): void
94
    {
95
        $this->expectException(\InvalidArgumentException::class);
96
        (new Cookie('test', 42))->path(';invalid');
97
    }
98
99
    public function testSecure(): void
100
    {
101
        $cookie = (new Cookie('test', 42))->secure(false);
102
103
        $this->assertSame('test=42; Path=/; HttpOnly; SameSite=Lax', $this->getCookieHeader($cookie));
104
    }
105
106
    public function testHttpOnly(): void
107
    {
108
        $cookie = (new Cookie('test', 42))->httpOnly(false);
109
110
        $this->assertSame('test=42; Path=/; Secure; SameSite=Lax', $this->getCookieHeader($cookie));
111
    }
112
113
    public function testInvalidSameSite(): void
114
    {
115
        $this->expectException(\InvalidArgumentException::class);
116
117
        (new Cookie('test', 42))->sameSite('invalid');
118
    }
119
120
    public function testSameSiteNone(): void
121
    {
122
        $cookie = (new Cookie('test', 42))->sameSite(Cookie::SAME_SITE_NONE);
123
124
        $this->assertSame('test=42; Path=/; Secure; HttpOnly; SameSite=None', $this->getCookieHeader($cookie));
125
    }
126
127
    public function testFromSetCookieString(): void
128
    {
129
        $expireDate = new \DateTimeImmutable();
130
        $maxAge = new \DateInterval('PT3600S');
131
        $setCookieString = 'sessionId=e8bb43229de9; Domain=foo.example.com; ';
132
        $setCookieString .= 'Expires=' . $expireDate->format(\DateTimeInterface::RFC7231) . '; ';
133
        $setCookieString .= 'Max-Age=3600; Path=/; Secure; HttpOnly; SameSite=Strict';
134
135
        $cookie = (new Cookie('sessionId', 'e8bb43229de9', false))
136
            ->expireAt($expireDate)
137
            ->maxAge($maxAge)
138
            ->domain('foo.example.com')
139
            ->path('/')
140
            ->secure(true)
141
            ->httpOnly(true)
142
            ->sameSite(Cookie::SAME_SITE_STRICT);
143
        $cookie2 = Cookie::fromSetCookieString($setCookieString);
144
145
        $this->assertSame((string)$cookie, (string)$cookie2);
146
    }
147
}
148