forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCookieStoreTest.php
More file actions
111 lines (92 loc) · 3.17 KB
/
CookieStoreTest.php
File metadata and controls
111 lines (92 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Cookie;
use CodeIgniter\Cookie\Exceptions\CookieException;
use CodeIgniter\Test\CIUnitTestCase;
use DateTimeImmutable;
use PHPUnit\Framework\Attributes\Group;
/**
* @internal
*/
#[Group('Others')]
final class CookieStoreTest extends CIUnitTestCase
{
private array $defaults;
protected function setUp(): void
{
parent::setUp();
$this->defaults = Cookie::setDefaults();
}
protected function tearDown(): void
{
Cookie::setDefaults($this->defaults);
}
public function testCookieStoreInitialization(): void
{
$cookies = [
new Cookie('dev', 'cookie'),
new Cookie('prod', 'cookie', ['raw' => true]),
];
$store = new CookieStore($cookies);
$this->assertCount(2, $store);
$this->assertTrue($store->has('dev'));
$this->assertSame($cookies[0], $store->get('dev'));
$this->assertTrue($store->has('prod'));
$this->assertTrue($store->has('prod', '', 'cookie'));
$this->assertSame($cookies[1], $store->get('prod'));
$this->assertFalse($store->has('test'));
$this->assertSame($cookies, array_values($store->display()));
$this->assertSame($cookies, array_values(iterator_to_array($store->getIterator())));
$this->expectException(CookieException::class);
$store->get('test');
}
public function testCookieStoreInitViaHeaders(): void
{
$cookies = [
'dev=cookie; Max-Age=3600',
'prod=cookie; Path=/web',
'test,def=cookie; SameSite=Lax',
];
$store = CookieStore::fromCookieHeaders($cookies, true);
$this->assertCount(2, $store);
$this->assertTrue($store->has('dev'));
$this->assertTrue($store->has('prod'));
$this->assertFalse($store->has('test,def'));
}
public function testInvalidCookieStored(): void
{
$this->expectException(CookieException::class);
new CookieStore([new DateTimeImmutable('now')]); // @phpstan-ignore argument.type
}
public function testPutRemoveCookiesInStore(): void
{
$cookies = [
new Cookie('dev', 'cookie'),
new Cookie('prod', 'cookie', ['raw' => true]),
];
$store = new CookieStore($cookies);
$bottle = $store->put(new Cookie('test', 'cookie'));
$jar = $store->remove('dev');
$this->assertNotSame($store->display(), $bottle->display());
$this->assertFalse($store->has('test'));
$this->assertTrue($bottle->has('test'));
$this->assertTrue($store->has('dev'));
$this->assertFalse($jar->has('dev'));
}
public function testCookiesFunction(): void
{
$a = cookies();
$b = cookies([], false);
$this->assertInstanceOf(CookieStore::class, $a);
$this->assertInstanceOf(CookieStore::class, $b);
$this->assertNotSame($a, $b);
}
}