|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Web\Tests; |
|
6
|
|
|
|
|
7
|
|
|
use Nyholm\Psr7\Response; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
use Yiisoft\Yii\Web\Cookie; |
|
10
|
|
|
use Yiisoft\Yii\Web\CookieCollection; |
|
11
|
|
|
|
|
12
|
|
|
final class CookieCollectionTest extends TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
private CookieCollection $collection; |
|
15
|
|
|
|
|
16
|
|
|
protected function setUp(): void |
|
17
|
|
|
{ |
|
18
|
|
|
$this->collection = new CookieCollection([]); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function testIssetAndUnset(): void |
|
22
|
|
|
{ |
|
23
|
|
|
$this->assertFalse(isset($this->collection['test'])); |
|
24
|
|
|
$this->collection->add(new Cookie('test')); |
|
25
|
|
|
$this->assertTrue(isset($this->collection['test'])); |
|
26
|
|
|
unset($this->collection['test']); |
|
27
|
|
|
$this->assertFalse(isset($this->collection['test'])); |
|
28
|
|
|
$this->assertCount(0, $this->collection); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function testRemovingNonExistingEntryReturnsNull(): void |
|
32
|
|
|
{ |
|
33
|
|
|
$this->assertEquals(null, $this->collection->remove('key_does_not_exist')); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function testExists(): void |
|
37
|
|
|
{ |
|
38
|
|
|
$this->collection->add(new Cookie('one', 'oneValue')); |
|
39
|
|
|
$this->collection->add(new Cookie('two', 'twoValue')); |
|
40
|
|
|
$exists = $this->collection->exists(static function (string $name, Cookie $cookie) { |
|
41
|
|
|
return $name === 'one' && $cookie->getValue() === 'oneValue'; |
|
42
|
|
|
}); |
|
43
|
|
|
$this->assertTrue($exists); |
|
44
|
|
|
$exists = $this->collection->exists(static function (string $name, Cookie $cookie) { |
|
45
|
|
|
return $name === 'two' && $cookie->getValue() === 'wrongValue'; |
|
46
|
|
|
}); |
|
47
|
|
|
$this->assertFalse($exists); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testArrayAccess(): void |
|
51
|
|
|
{ |
|
52
|
|
|
$cookieOne = new Cookie('one'); |
|
53
|
|
|
$cookieTwo = new Cookie('two'); |
|
54
|
|
|
|
|
55
|
|
|
$this->collection[] = $cookieOne; |
|
56
|
|
|
$this->collection['two'] = $cookieTwo; |
|
57
|
|
|
|
|
58
|
|
|
$this->assertEquals($cookieOne, $this->collection['one']); |
|
59
|
|
|
$this->assertEquals($cookieTwo, $this->collection['two']); |
|
60
|
|
|
|
|
61
|
|
|
$this->assertCount(2, $this->collection); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function testContains(): void |
|
65
|
|
|
{ |
|
66
|
|
|
$cookie = new Cookie('test'); |
|
67
|
|
|
$this->collection->add($cookie); |
|
68
|
|
|
$this->assertTrue($this->collection->contains($cookie)); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function testGet(): void |
|
72
|
|
|
{ |
|
73
|
|
|
$cookie = new Cookie('test'); |
|
74
|
|
|
$this->collection->add($cookie); |
|
75
|
|
|
$this->assertEquals($cookie, $this->collection->get('test')); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function testGetValue(): void |
|
79
|
|
|
{ |
|
80
|
|
|
$this->collection->add(new Cookie('test', 'testVal')); |
|
81
|
|
|
$this->assertEquals('testVal', $this->collection->getValue('test')); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function testGetNames(): void |
|
85
|
|
|
{ |
|
86
|
|
|
$this->collection->add(new Cookie('one')); |
|
87
|
|
|
$this->collection->add(new Cookie('two')); |
|
88
|
|
|
$this->assertEquals(['one', 'two'], $this->collection->getNames()); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function testGetCookies(): void |
|
92
|
|
|
{ |
|
93
|
|
|
$cookieOne = new Cookie('one'); |
|
94
|
|
|
$cookieTwo = new Cookie('two'); |
|
95
|
|
|
$collection = new CookieCollection([$cookieOne, $cookieTwo]); |
|
96
|
|
|
$this->assertEquals([$cookieOne, $cookieTwo], $collection->getCookies()); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function testCount(): void |
|
100
|
|
|
{ |
|
101
|
|
|
$this->collection[] = new Cookie('one'); |
|
102
|
|
|
$this->collection[] = new Cookie('two'); |
|
103
|
|
|
$this->assertEquals(2, $this->collection->count()); |
|
104
|
|
|
$this->assertCount(2, $this->collection); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function testClear(): void |
|
108
|
|
|
{ |
|
109
|
|
|
$this->collection[] = new Cookie('one'); |
|
110
|
|
|
$this->collection[] = new Cookie('two'); |
|
111
|
|
|
$this->collection->clear(); |
|
112
|
|
|
$this->assertEmpty($this->collection); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function testIsEmpty(): void |
|
116
|
|
|
{ |
|
117
|
|
|
$this->assertTrue($this->collection->isEmpty()); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function testRemove(): void |
|
121
|
|
|
{ |
|
122
|
|
|
$cookieOne = new Cookie('one'); |
|
123
|
|
|
$cookieTwo = new Cookie('two'); |
|
124
|
|
|
$collection = new CookieCollection([$cookieOne, $cookieTwo]); |
|
125
|
|
|
|
|
126
|
|
|
$cookie = $collection->remove('one'); |
|
127
|
|
|
$this->assertEquals($cookieOne, $cookie); |
|
128
|
|
|
$this->assertFalse($collection->contains($cookie)); |
|
|
|
|
|
|
129
|
|
|
$this->assertNull($collection->remove('one')); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function testToArray(): void |
|
133
|
|
|
{ |
|
134
|
|
|
$cookieOne = new Cookie('one'); |
|
135
|
|
|
$cookieTwo = new Cookie('two'); |
|
136
|
|
|
$collection = new CookieCollection([$cookieOne, $cookieTwo]); |
|
137
|
|
|
|
|
138
|
|
|
$expected = ['one' => $cookieOne, 'two' => $cookieTwo]; |
|
139
|
|
|
$this->assertEquals($expected, $collection->toArray()); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public function testIterator(): void |
|
143
|
|
|
{ |
|
144
|
|
|
$cookieOne = new Cookie('one'); |
|
145
|
|
|
$cookieTwo = new Cookie('two'); |
|
146
|
|
|
$this->collection->add($cookieOne); |
|
147
|
|
|
$this->collection->add($cookieTwo); |
|
148
|
|
|
|
|
149
|
|
|
$this->assertIsIterable($this->collection); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
public function testExpire(): void |
|
153
|
|
|
{ |
|
154
|
|
|
$this->collection->add(new Cookie('test')); |
|
155
|
|
|
$this->collection->expire('test'); |
|
156
|
|
|
|
|
157
|
|
|
$this->assertTrue($this->collection->get('test')->isExpired()); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function testFromArray(): void |
|
161
|
|
|
{ |
|
162
|
|
|
$cookieArray = ['one' => 'oneValue', 'two' => 'twoValue']; |
|
163
|
|
|
$collection = CookieCollection::fromArray($cookieArray); |
|
164
|
|
|
|
|
165
|
|
|
$this->assertCount(2, $collection); |
|
166
|
|
|
$this->assertInstanceOf(Cookie::class, $collection['one']); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
public function testFromArrayWithInvalidArgument(): void |
|
170
|
|
|
{ |
|
171
|
|
|
$cookieArray = ['one', 'two']; |
|
172
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
173
|
|
|
CookieCollection::fromArray($cookieArray); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
public function testFromResponse(): void |
|
177
|
|
|
{ |
|
178
|
|
|
$response = new Response(); |
|
179
|
|
|
$response = $response->withAddedHeader('Set-Cookie', 'one=oneValue; Path=/; Secure; HttpOnly; SameSite=Lax'); |
|
180
|
|
|
$response = $response->withAddedHeader('Set-Cookie', 'two=twoValue; Path=/; Secure; HttpOnly; SameSite=Lax'); |
|
181
|
|
|
|
|
182
|
|
|
$collection = CookieCollection::fromResponse($response); |
|
183
|
|
|
$this->assertCount(2, $collection); |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|