Completed
Push — master ( 8bc607...725931 )
by Alexander
14:55
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
10
class CookieTest extends TestCase
11
{
12
    private function getCookieHeader(Cookie $cookie): string
13
    {
14
        $response = new Response();
15
        $response = $cookie->addToResponse($response);
16
        return $response->getHeaderLine('Set-Cookie');
17
    }
18
19
    public function testInvalidName(): void
20
    {
21
        $this->expectException(\InvalidArgumentException::class);
22
        new Cookie('test[]', 42);
23
    }
24
25
    public function testDefaults(): void
26
    {
27
        $cookie = new Cookie('test', 42);
28
29
        $this->assertSame('test=42; expires=Thu, 01-Jan-1970 00:00:00 GMT; path=/; secure; httponly; samesite=Lax', $this->getCookieHeader($cookie));
30
    }
31
32
    public function testDomain(): void
33
    {
34
        $cookie = (new Cookie('test', 42))->domain('yiiframework.com');
35
36
        $this->assertSame('test=42; expires=Thu, 01-Jan-1970 00:00:00 GMT; path=/; domain=yiiframework.com; secure; httponly; samesite=Lax', $this->getCookieHeader($cookie));
37
    }
38
39
    public function testExpireAt(): void
40
    {
41
        $expireDateTime = new \DateTime();
42
        $expireDateTime->setTimezone(new \DateTimeZone('GMT'));
43
        $formattedDateTime = $expireDateTime->format('D, d-M-Y H:i:s T');
44
45
        $cookie = (new Cookie('test', 42))->expireAt($expireDateTime);
46
47
        $this->assertSame("test=42; expires=$formattedDateTime; path=/; secure; httponly; samesite=Lax", $this->getCookieHeader($cookie));
48
    }
49
50
    public function testExpireWhenBrowserIsClosed(): void
51
    {
52
        $cookie = (new Cookie('test', 42))->expireWhenBrowserIsClosed();
53
54
        $this->assertSame('test=42; expires=Thu, 01-Jan-1970 00:00:00 GMT; path=/; secure; httponly; samesite=Lax', $this->getCookieHeader($cookie));
55
    }
56
57
    public function testPath(): void
58
    {
59
        $cookie = (new Cookie('test', 42))->path('/test');
60
61
        $this->assertSame('test=42; expires=Thu, 01-Jan-1970 00:00:00 GMT; path=/test; secure; httponly; samesite=Lax', $this->getCookieHeader($cookie));
62
    }
63
64
    public function testSecure(): void
65
    {
66
        $cookie = (new Cookie('test', 42))->secure(false);
67
68
        $this->assertSame('test=42; expires=Thu, 01-Jan-1970 00:00:00 GMT; path=/; httponly; samesite=Lax', $this->getCookieHeader($cookie));
69
    }
70
71
    public function testHttpOnly(): void
72
    {
73
        $cookie = (new Cookie('test', 42))->httpOnly(false);
74
75
        $this->assertSame('test=42; expires=Thu, 01-Jan-1970 00:00:00 GMT; path=/; secure; samesite=Lax', $this->getCookieHeader($cookie));
76
    }
77
78
    public function testInvalidSameSite(): void
79
    {
80
        $this->expectException(\InvalidArgumentException::class);
81
82
        (new Cookie('test', 42))->sameSite('invalid');
83
    }
84
85
    public function testSameSite(): void
86
    {
87
        $cookie = (new Cookie('test', 42))->sameSite('');
88
89
        $this->assertSame('test=42; expires=Thu, 01-Jan-1970 00:00:00 GMT; path=/; secure; httponly', $this->getCookieHeader($cookie));
90
    }
91
}
92