Passed
Pull Request — master (#244)
by
unknown
03:11
created

CookieCollectionTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Yii\Web\CookieCollection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 testConstructorWithInvalidArray(): void
22
    {
23
        $this->expectException(\InvalidArgumentException::class);
24
        $collection = new CookieCollection([new Cookie('test'), 'string']);
0 ignored issues
show
Unused Code introduced by
The assignment to $collection is dead and can be removed.
Loading history...
25
    }
26
27
    public function testIssetAndUnset(): void
28
    {
29
        $this->assertFalse(isset($this->collection['test']));
30
        $this->collection->add(new Cookie('test'));
31
        $this->assertTrue(isset($this->collection['test']));
32
        unset($this->collection['test']);
33
        $this->assertFalse(isset($this->collection['test']));
34
        $this->assertCount(0, $this->collection);
35
    }
36
37
    public function testRemovingNonExistingEntryReturnsNull(): void
38
    {
39
        $this->assertEquals(null, $this->collection->remove('key_does_not_exist'));
40
    }
41
42
    public function testExists(): void
43
    {
44
        $this->collection->add(new Cookie('one', 'oneValue'));
45
        $this->collection->add(new Cookie('two', 'twoValue'));
46
        $exists = $this->collection->exists(static function (Cookie $cookie, string $name) {
47
            return $name === 'one' && $cookie->getValue() === 'oneValue';
48
        });
49
        $this->assertTrue($exists);
50
        $exists = $this->collection->exists(static function (Cookie $cookie, string $name) {
51
            return $name === 'two' && $cookie->getValue() === 'wrongValue';
52
        });
53
        $this->assertFalse($exists);
54
    }
55
56
    public function testWalk(): void
57
    {
58
        $this->collection->add(new Cookie('one', 'oneValue'));
59
        $this->collection->add(new Cookie('two', 'twoValue'));
60
        $this->collection->walk(static function (Cookie &$cookie, string $name) {
61
            if ($name === 'two') {
62
                $cookie = $cookie->withValue('modifiedTwoValue');
63
            }
64
        });
65
66
        $this->assertEquals('modifiedTwoValue', $this->collection->get('two')->getValue());
67
    }
68
69
    public function testArrayAccess(): void
70
    {
71
        $cookieOne = new Cookie('one');
72
        $cookieTwo = new Cookie('two');
73
74
        $this->collection[] = $cookieOne;
75
        $this->collection['two'] = $cookieTwo;
76
77
        $this->assertEquals($cookieOne, $this->collection['one']);
78
        $this->assertEquals($cookieTwo, $this->collection['two']);
79
80
        $this->assertCount(2, $this->collection);
81
    }
82
83
    public function testContains(): void
84
    {
85
        $cookie = new Cookie('test');
86
        $this->collection->add($cookie);
87
        $this->assertTrue($this->collection->contains($cookie));
88
    }
89
90
    public function testGet(): void
91
    {
92
        $cookie = new Cookie('test');
93
        $this->collection->add($cookie);
94
        $this->assertEquals($cookie, $this->collection->get('test'));
95
    }
96
97
    public function testGetValue(): void
98
    {
99
        $this->collection->add(new Cookie('test', 'testVal'));
100
        $this->assertEquals('testVal', $this->collection->getValue('test'));
101
    }
102
103
    public function testGetKeys(): void
104
    {
105
        $this->collection->add(new Cookie('one'));
106
        $this->collection->add(new Cookie('two'));
107
        $this->assertEquals(['one', 'two'], $this->collection->getKeys());
108
    }
109
110
    public function testGetValues(): void
111
    {
112
        $cookieOne = new Cookie('one');
113
        $cookieTwo = new Cookie('two');
114
        $collection = new CookieCollection([$cookieOne, $cookieTwo]);
115
        $this->assertEquals([$cookieOne, $cookieTwo], $collection->getValues());
116
    }
117
118
    public function testCount(): void
119
    {
120
        $this->collection[] = new Cookie('one');
121
        $this->collection[] = new Cookie('two');
122
        $this->assertEquals(2, $this->collection->count());
123
        $this->assertCount(2, $this->collection);
124
    }
125
126
    public function testClear(): void
127
    {
128
        $this->collection[] = new Cookie('one');
129
        $this->collection[] = new Cookie('two');
130
        $this->collection->clear();
131
        $this->assertEmpty($this->collection);
132
    }
133
134
    public function testIsEmpty(): void
135
    {
136
        $this->assertTrue($this->collection->isEmpty());
137
    }
138
139
    public function testRemove(): void
140
    {
141
        $cookieOne = new Cookie('one');
142
        $cookieTwo = new Cookie('two');
143
        $collection = new CookieCollection([$cookieOne, $cookieTwo]);
144
145
        $cookie = $collection->remove('one');
146
        $this->assertNotNull($cookie);
147
        $this->assertEquals($cookieOne, $cookie);
148
        $this->assertFalse($collection->has($cookie->getName()));
149
        $this->assertNull($collection->remove('one'));
150
    }
151
152
    public function testToArray(): void
153
    {
154
        $cookieOne = new Cookie('one');
155
        $cookieTwo = new Cookie('two');
156
        $collection = new CookieCollection([$cookieOne, $cookieTwo]);
157
158
        $expected = ['one' => $cookieOne, 'two' => $cookieTwo];
159
        $this->assertEquals($expected, $collection->toArray());
160
    }
161
162
    public function testIterator(): void
163
    {
164
        $cookieOne = new Cookie('one');
165
        $cookieTwo = new Cookie('two');
166
        $this->collection->add($cookieOne);
167
        $this->collection->add($cookieTwo);
168
169
        $this->assertIsIterable($this->collection);
170
    }
171
172
    public function testExpire(): void
173
    {
174
        $this->collection->add(new Cookie('test'));
175
        $this->collection->get('test')->expire();
176
177
        $this->assertTrue($this->collection->get('test')->isExpired());
178
    }
179
180
    public function testFromArray(): void
181
    {
182
        $cookieArray = ['one' => 'oneValue', 'two' => 'twoValue'];
183
        $collection = CookieCollection::fromArray($cookieArray);
184
185
        $this->assertCount(2, $collection);
186
        $this->assertInstanceOf(Cookie::class, $collection['one']);
187
    }
188
189
    public function testFromArrayWithInvalidArray(): void
190
    {
191
        $cookieArray = ['one', 'two'];
192
193
        $this->expectException(\InvalidArgumentException::class);
194
        $collection = CookieCollection::fromArray($cookieArray);
0 ignored issues
show
Unused Code introduced by
The assignment to $collection is dead and can be removed.
Loading history...
195
    }
196
197
    public function testFromArrayWithMalformedArray(): void
198
    {
199
        $cookieArray = ['one' => 'oneValue', 'two'];
200
201
        $this->expectException(\InvalidArgumentException::class);
202
        $collection = CookieCollection::fromArray($cookieArray);
0 ignored issues
show
Unused Code introduced by
The assignment to $collection is dead and can be removed.
Loading history...
203
    }
204
205
    public function testFromArrayWithEmptyArray(): void
206
    {
207
        $collection = CookieCollection::fromArray([]);
208
209
        $this->assertCount(0, $collection);
210
    }
211
212
    public function testFromArrayWithInvalidArgument(): void
213
    {
214
        $cookieArray = ['one', 'two'];
215
        $this->expectException(\InvalidArgumentException::class);
216
        CookieCollection::fromArray($cookieArray);
217
    }
218
219
    public function testAddToResponse(): void
220
    {
221
        $response = (new Response())->withHeader('Set-Cookie', 'oldCookie=oldValue;Secure');
222
        $this->collection->add(new Cookie('one', 'oneValue'));
223
        $this->collection->add(new Cookie('two', 'twoValue', new \DateTimeImmutable()));
224
225
        $response = $this->collection->addToResponse($response);
226
        $this->assertCount(3, $response->getHeader('Set-Cookie'));
227
        $this->assertEquals('oldCookie=oldValue;Secure', $response->getHeader('Set-Cookie')[0]);
228
    }
229
230
    public function testSetToResponse(): void
231
    {
232
        $response = (new Response())->withHeader('Set-Cookie', 'oldCookie=oldValue;Secure');
233
        $this->collection->add(new Cookie('one', 'oneValue'));
234
        $this->collection->add(new Cookie('two', 'twoValue', new \DateTimeImmutable()));
235
236
        $response = $this->collection->setToResponse($response);
237
        $this->assertCount(2, $response->getHeader('Set-Cookie'));
238
        $this->assertEquals('one=oneValue; Path=/; Secure; HttpOnly; SameSite=Lax', $response->getHeader('Set-Cookie')[0]);
239
    }
240
241
    public function testFromResponse(): void
242
    {
243
        $response = new Response();
244
        $response = $response->withAddedHeader('Set-Cookie', 'one=oneValue; Path=/; Secure; HttpOnly; SameSite=Lax');
245
        $response = $response->withAddedHeader('Set-Cookie', 'two=twoValue; Path=/; Secure; HttpOnly; SameSite=Lax');
246
247
        $collection = CookieCollection::fromResponse($response);
248
        $this->assertCount(2, $collection);
249
    }
250
}
251