Passed
Pull Request — master (#244)
by Alexander
02:43
created

CookieTest::testPath()   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
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 testInvalidValue(): void
38
    {
39
        $this->expectException(\InvalidArgumentException::class);
40
        (new Cookie('test'))->withValue(';');
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 testNegativeInterval(): void
70
    {
71
        $formattedExpire = (new \DateTimeImmutable())->setTimestamp(time() - 3600)->format(\DateTimeInterface::RFC7231);
72
        $negativeInterval = new \DateInterval('PT3600S');
73
        $negativeInterval->invert = 1;
74
        $cookie = (new Cookie('test', 42))->withMaxAge($negativeInterval);
75
76
        $this->assertSame("test=42; Expires=$formattedExpire; Max-Age=-3600; Path=/; Secure; HttpOnly; SameSite=Lax", $this->getCookieHeader($cookie));
77
    }
78
79
    public function testWithDomain(): void
80
    {
81
        $cookie = (new Cookie('test', 42))->withDomain('yiiframework.com');
82
83
        $this->assertSame('test=42; Domain=yiiframework.com; Path=/; Secure; HttpOnly; SameSite=Lax', $this->getCookieHeader($cookie));
84
    }
85
86
    public function testExpireWhenBrowserIsClosed(): void
87
    {
88
        $cookie = (new Cookie('test', 42))->expireWhenBrowserIsClosed();
89
90
        $this->assertSame('test=42; Path=/; Secure; HttpOnly; SameSite=Lax', $this->getCookieHeader($cookie));
91
    }
92
93
    public function testWithPath(): void
94
    {
95
        $cookie = (new Cookie('test', 42))->withPath('/test');
96
97
        $this->assertSame('test=42; Path=/test; Secure; HttpOnly; SameSite=Lax', $this->getCookieHeader($cookie));
98
    }
99
100
    public function testInvalidPath(): void
101
    {
102
        $this->expectException(\InvalidArgumentException::class);
103
        (new Cookie('test', 42))->withPath(';invalid');
104
    }
105
106
    public function testWithSecure(): void
107
    {
108
        $cookie = (new Cookie('test', 42))->withSecure(false);
109
110
        $this->assertSame('test=42; Path=/; HttpOnly; SameSite=Lax', $this->getCookieHeader($cookie));
111
    }
112
113
    public function testHttpOnly(): void
114
    {
115
        $cookie = (new Cookie('test', 42))->withHttpOnly(false);
116
117
        $this->assertSame('test=42; Path=/; Secure; SameSite=Lax', $this->getCookieHeader($cookie));
118
    }
119
120
    public function testInvalidSameSite(): void
121
    {
122
        $this->expectException(\InvalidArgumentException::class);
123
124
        (new Cookie('test', 42))->withSameSite('invalid');
125
    }
126
127
    public function testSameSiteNone(): void
128
    {
129
        $cookie = (new Cookie('test', 42))->withSameSite(Cookie::SAME_SITE_NONE);
130
131
        $this->assertSame('test=42; Path=/; Secure; HttpOnly; SameSite=None', $this->getCookieHeader($cookie));
132
    }
133
134
    public function testFromCookieString(): void
135
    {
136
        $expireDate = (new \DateTimeImmutable())->setTimestamp(time() + 3600);
137
        $setCookieString = 'sessionId=e8bb43229de9; Domain=foo.example.com; ';
138
        $setCookieString .= 'Expires=' . $expireDate->format(\DateTimeInterface::RFC7231) . '; ';
139
        $setCookieString .= 'Max-Age=3600; Path=/; Secure; HttpOnly; SameSite=Strict';
140
141
        $cookie = new Cookie(
142
            'sessionId',
143
            'e8bb43229de9',
144
            $expireDate,
0 ignored issues
show
Bug introduced by
It seems like $expireDate can also be of type false; however, parameter $expires of Yiisoft\Yii\Web\Cookie::__construct() does only seem to accept DateTimeInterface|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

144
            /** @scrutinizer ignore-type */ $expireDate,
Loading history...
145
            'foo.example.com',
146
            '/',
147
            true,
148
            true,
149
            Cookie::SAME_SITE_STRICT
150
        );
151
        $cookie2 = Cookie::fromCookieString($setCookieString);
152
153
        $this->assertSame((string)$cookie, (string)$cookie2);
154
    }
155
156
    public function testGetters(): void
157
    {
158
        $cookie = new Cookie('test', '', null, null, null, null, null, null);
159
        $this->assertEquals('test', $cookie->getName());
160
        $this->assertEquals('', $cookie->getValue());
161
        $this->assertNull($cookie->getExpires());
162
        $this->assertNull($cookie->getDomain());
163
        $this->assertNull($cookie->getPath());
164
        $this->assertFalse($cookie->isSecure());
165
        $this->assertFalse($cookie->isHttpOnly());
166
        $this->assertNull($cookie->getSameSite());
167
168
        $cookie = $cookie->withValue('testValue');
169
        $this->assertEquals('testValue', $cookie->getValue());
170
171
        $expiry = new \DateTimeImmutable();
172
        $cookie = $cookie->withExpires($expiry);
173
        $this->assertEquals($expiry->getTimestamp(), $cookie->getExpires()->getTimestamp());
174
175
        $cookie = $cookie->withDomain('yiiframework.com');
176
        $this->assertEquals('yiiframework.com', $cookie->getDomain());
177
178
        $cookie = $cookie->withPath('/path');
179
        $this->assertEquals('/path', $cookie->getPath());
180
181
        $cookie = $cookie->withSecure(true);
182
        $this->assertTrue($cookie->isSecure());
183
184
        $cookie = $cookie->withHttpOnly(true);
185
        $this->assertTrue($cookie->isHttpOnly());
186
187
        $cookie = $cookie->withSameSite(Cookie::SAME_SITE_LAX);
188
        $this->assertEquals(Cookie::SAME_SITE_LAX, $cookie->getSameSite());
189
    }
190
}
191