Repository navigation
Expand file tree
/
Copy pathCookieTest.php
More file actions
203 lines (170 loc) · 4.58 KB
/
Copy pathCookieTest.php
File metadata and controls
203 lines (170 loc) · 4.58 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
/**
* @author : Jakiboy
* @package : FloatPHP
* @subpackage : Classes Http Component Tests
* @version : 1.5.x
* @copyright : (c) 2018 - 2025 Jihad Sinnaour <me@jihadsinnaour.com>
* @link : https://floatphp.com
* @license : MIT
*
* This file if a part of FloatPHP Framework.
*/
declare(strict_types=1);
namespace FloatPHP\Tests\Classes\Http;
use PHPUnit\Framework\TestCase;
use FloatPHP\Classes\Http\Cookie;
/**
* Cookie class tests.
*/
final class CookieTest extends TestCase
{
protected function setUp(): void
{
// Clear cookies before each test
$_COOKIE = [];
}
protected function tearDown(): void
{
// Clear cookies after each test
$_COOKIE = [];
}
/**
* Test get cookie value.
*/
public function testGetCookie(): void
{
$_COOKIE['test_key'] = 'test_value';
$value = Cookie::get('test_key');
$this->assertEquals('test_value', $value);
}
/**
* Test get all cookies.
*/
public function testGetAllCookies(): void
{
$_COOKIE['key1'] = 'value1';
$_COOKIE['key2'] = 'value2';
$cookies = Cookie::get();
$this->assertIsArray($cookies);
$this->assertEquals('value1', $cookies['key1']);
$this->assertEquals('value2', $cookies['key2']);
}
/**
* Test get non-existent cookie.
*/
public function testGetNonExistentCookie(): void
{
$value = Cookie::get('non_existent');
$this->assertNull($value);
}
/**
* Test get when no cookies exist.
*/
public function testGetWhenNoCookiesExist(): void
{
$cookies = Cookie::get();
$this->assertNull($cookies);
}
/**
* Test check if cookie is set.
*/
public function testIsSetted(): void
{
$_COOKIE['test_key'] = 'test_value';
$this->assertTrue(Cookie::isSet('test_key'));
$this->assertFalse(Cookie::isSet('non_existent'));
}
/**
* Test check if any cookies are set.
*/
public function testIsSettedAny(): void
{
$this->assertFalse(Cookie::isSet());
$_COOKIE['test_key'] = 'test_value';
$this->assertTrue(Cookie::isSet());
}
/**
* Test unset specific cookie.
*/
public function testUnsetSpecificCookie(): void
{
$_COOKIE['key1'] = 'value1';
$_COOKIE['key2'] = 'value2';
Cookie::unset('key1');
$this->assertFalse(isset($_COOKIE['key1']));
$this->assertTrue(isset($_COOKIE['key2']));
}
/**
* Test unset all cookies.
*/
public function testUnsetAllCookies(): void
{
$_COOKIE['key1'] = 'value1';
$_COOKIE['key2'] = 'value2';
Cookie::unset();
$this->assertEmpty($_COOKIE);
}
/**
* Test cookie set method exists.
*/
public function testSetMethodExists(): void
{
$this->assertTrue(method_exists(Cookie::class, 'set'));
}
/**
* Test clear method exists.
*/
public function testClearMethodExists(): void
{
$this->assertTrue(method_exists(Cookie::class, 'clear'));
}
/**
* Test cookie with empty value.
*/
public function testCookieWithEmptyValue(): void
{
$_COOKIE['empty_key'] = '';
$value = Cookie::get('empty_key');
$this->assertEquals('', $value);
$this->assertTrue(Cookie::isSet('empty_key'));
}
/**
* Test cookie with null value.
*/
public function testCookieWithNullValue(): void
{
$_COOKIE['null_key'] = null;
$value = Cookie::get('null_key');
$this->assertNull($value);
$this->assertTrue(Cookie::isSet('null_key'));
}
/**
* Test cookie with numeric value.
*/
public function testCookieWithNumericValue(): void
{
$_COOKIE['numeric_key'] = 123;
$value = Cookie::get('numeric_key');
$this->assertEquals(123, $value);
}
/**
* Test cookie with boolean value.
*/
public function testCookieWithBooleanValue(): void
{
$_COOKIE['bool_key'] = true;
$value = Cookie::get('bool_key');
$this->assertTrue($value);
}
/**
* Test cookie with array value.
*/
public function testCookieWithArrayValue(): void
{
$_COOKIE['array_key'] = ['nested' => 'value'];
$value = Cookie::get('array_key');
$this->assertIsArray($value);
$this->assertEquals('value', $value['nested']);
}
}