Passed
Push — master ( 974680...fc5316 )
by Kirill
03:59
created

CookieTest::testSameSite()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 3
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Tests\Cookies;
13
14
use PHPUnit\Framework\TestCase;
15
use Spiral\Cookies\Cookie;
16
17
class CookieTest extends TestCase
18
{
19
    public function testAccess(): void
20
    {
21
        $cookie = new Cookie(
22
            'cookie',
23
            'value',
24
            100,
25
            '/',
26
            '.domain.com',
27
            true,
28
            true
29
        );
30
31
        $this->assertSame('cookie', $cookie->getName());
32
        $this->assertSame('value', $cookie->getValue());
33
        $this->assertSame(time() + 100, $cookie->getExpires());
34
        $this->assertSame('.domain.com', $cookie->getDomain());
35
        $this->assertTrue($cookie->isSecure());
36
        $this->assertTrue($cookie->isHttpOnly());
37
    }
38
39
    public function testCreateStaticAccess(): void
40
    {
41
        $cookie = Cookie::create(
42
            'cookie',
43
            'value',
44
            100,
45
            '/',
46
            '.domain.com',
47
            true,
48
            true
49
        );
50
51
        $this->assertSame('cookie', $cookie->getName());
52
        $this->assertSame('value', $cookie->getValue());
53
        $this->assertSame(time() + 100, $cookie->getExpires());
54
        $this->assertSame('.domain.com', $cookie->getDomain());
55
        $this->assertTrue($cookie->isSecure());
56
        $this->assertTrue($cookie->isHttpOnly());
57
    }
58
59
    public function testFallbackValues(): void
60
    {
61
        $cookie = new Cookie(
62
            'cookie',
63
            'value',
64
            null,
65
            null,
66
            null,
67
            true,
68
            true
69
        );
70
71
        $this->assertNull($cookie->getExpires());
72
        $this->assertNull($cookie->getPath());
73
        $this->assertNull($cookie->getDomain());
74
    }
75
76
    public function testWithValue(): void
77
    {
78
        $cookie = new Cookie(
79
            'cookie',
80
            'value',
81
            null,
82
            null,
83
            null,
84
            true,
85
            true
86
        );
87
88
        $cookie1 = $cookie->withValue('new-value');
89
90
        $this->assertNotSame($cookie, $cookie1);
91
        $this->assertSame('value', $cookie->getValue());
92
        $this->assertSame('new-value', $cookie1->getValue());
93
    }
94
95
    public function testPack(): void
96
    {
97
        $cookie = new Cookie(
98
            'cookie',
99
            'value',
100
            100,
101
            '/',
102
            '.domain.com',
103
            true,
104
            true
105
        );
106
107
        $this->assertSame($cookie->createHeader(), (string)$cookie);
108
        $this->assertStringContainsString(
109
            'cookie=value;',
110
            $cookie->createHeader()
111
        );
112
113
        $this->assertStringContainsString(
114
            'Max-Age=100; Path=/; Domain=.domain.com; Secure; HttpOnly',
115
            $cookie->createHeader()
116
        );
117
    }
118
119
    /**
120
     * @dataProvider sameSiteProvider
121
     * @param             $expected
122
     * @param bool        $secure
123
     * @param string|null $sameSite
124
     */
125
    public function testSameSite($expected, bool $secure, ?string $sameSite): void
126
    {
127
        $cookie = new Cookie('', '', 0, '', '', $secure, false, $sameSite);
128
        $this->assertSame($expected, $cookie->getSameSite());
129
130
        if ($expected === null) {
131
            $this->assertStringNotContainsString('SameSite=', $cookie->createHeader());
132
        } else {
133
            $this->assertStringContainsString("SameSite=$expected", $cookie->createHeader());
134
        }
135
    }
136
137
    /**
138
     * @return iterable
139
     */
140
    public function sameSiteProvider(): iterable
141
    {
142
        return [
143
            [null, true, null],
144
            [null, false, null],
145
            [null, true, 'weird'],
146
            [null, false, 'weird'],
147
            ['Lax', true, 'lax'],
148
            ['Lax', false, 'lax'],
149
            ['Strict', true, 'strict'],
150
            ['Strict', false, 'strict'],
151
            ['None', true, 'none'],
152
            ['Lax', false, 'none'],
153
        ];
154
    }
155
}
156