|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of Blitz PHP framework. |
| 5 | + * |
| 6 | + * (c) 2022 Dimitri Sitchet Tomkeu <devcode.dst@gmail.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the LICENSE file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +use BlitzPHP\Session\Cookie\Cookie; |
| 13 | + |
| 14 | +use function Kahlan\expect; |
| 15 | + |
| 16 | +describe('Session / Cookie / Cookie', function (): void { |
| 17 | + beforeEach(function (): void { |
| 18 | + // Réinitialise les valeurs par défaut avant chaque test |
| 19 | + Cookie::setDefaults([ |
| 20 | + 'expires' => null, |
| 21 | + 'path' => '/', |
| 22 | + 'domain' => '', |
| 23 | + 'secure' => false, |
| 24 | + 'httponly' => false, |
| 25 | + 'samesite' => null, |
| 26 | + ]); |
| 27 | + }); |
| 28 | + |
| 29 | + describe('Création', function (): void { |
| 30 | + it('Devrait créer un cookie avec des valeurs basiques', function (): void { |
| 31 | + $cookie = new Cookie('test', 'value'); |
| 32 | + |
| 33 | + expect($cookie->getName())->toBe('test'); |
| 34 | + expect($cookie->getValue())->toBe('value'); |
| 35 | + expect($cookie->getPath())->toBe('/'); |
| 36 | + expect($cookie->isSecure())->toBe(false); |
| 37 | + expect($cookie->isHttpOnly())->toBe(false); |
| 38 | + }); |
| 39 | + |
| 40 | + it('Devrait créer un cookie avec la méthode create()', function (): void { |
| 41 | + $cookie = Cookie::create('test', 'value', [ |
| 42 | + 'path' => '/admin', |
| 43 | + 'secure' => true, |
| 44 | + 'httponly' => true, |
| 45 | + 'samesite' => 'Strict' |
| 46 | + ]); |
| 47 | + |
| 48 | + expect($cookie->getName())->toBe('test'); |
| 49 | + expect($cookie->getPath())->toBe('/admin'); |
| 50 | + expect($cookie->isSecure())->toBe(true); |
| 51 | + expect($cookie->isHttpOnly())->toBe(true); |
| 52 | + expect($cookie->getSameSite())->toBe('Strict'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('Devrait créer un cookie avec une date d\'expiration', function (): void { |
| 56 | + $expires = new DateTimeImmutable('+1 hour'); |
| 57 | + $cookie = new Cookie('test', 'value', $expires); |
| 58 | + |
| 59 | + expect($cookie->getExpiry())->toBeAnInstanceOf(DateTimeInterface::class); |
| 60 | + expect($cookie->getExpiresTimestamp())->toBe($expires->getTimestamp()); |
| 61 | + }); |
| 62 | + |
| 63 | + it('Devrait créer un cookie avec expiration numérique', function (): void { |
| 64 | + $timestamp = time() + 3600; |
| 65 | + $cookie = Cookie::create('test', 'value', ['expires' => $timestamp]); |
| 66 | + |
| 67 | + expect($cookie->getExpiresTimestamp())->toBe($timestamp); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + describe('Validation', function (): void { |
| 72 | + it('Devrait rejeter un nom de cookie vide', function (): void { |
| 73 | + expect(function (): void { |
| 74 | + new Cookie('', 'value'); |
| 75 | + })->toThrow(new InvalidArgumentException('Le nom du cookie ne peut pas être vide.')); |
| 76 | + }); |
| 77 | + |
| 78 | + it('Devrait rejeter un nom de cookie avec caractères invalides', function (): void { |
| 79 | + expect(function (): void { |
| 80 | + new Cookie('test=value', 'value'); |
| 81 | + })->toThrow(new InvalidArgumentException('Le nom du cookie "test=value" contient des caractères invalides.')); |
| 82 | + }); |
| 83 | + |
| 84 | + it('Devrait rejeter une valeur SameSite invalide', function (): void { |
| 85 | + expect(function (): void { |
| 86 | + new Cookie('test', 'value', null, null, null, null, null, 'Invalid'); |
| 87 | + })->toThrow(new InvalidArgumentException('Samesite value must be either of: Lax, Strict, None')); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('Méthodes with* (immuabilité)', function (): void { |
| 92 | + it('Devrait créer une nouvelle instance avec withName()', function (): void { |
| 93 | + $original = new Cookie('original', 'value'); |
| 94 | + $modified = $original->withName('modified'); |
| 95 | + |
| 96 | + expect($original->getName())->toBe('original'); |
| 97 | + expect($modified->getName())->toBe('modified'); |
| 98 | + expect($original)->not->toBe($modified); |
| 99 | + }); |
| 100 | + |
| 101 | + it('Devrait créer une nouvelle instance avec withValue()', function (): void { |
| 102 | + $original = new Cookie('test', 'original'); |
| 103 | + $modified = $original->withValue('modified'); |
| 104 | + |
| 105 | + expect($original->getValue())->toBe('original'); |
| 106 | + expect($modified->getValue())->toBe('modified'); |
| 107 | + }); |
| 108 | + |
| 109 | + it('Devrait créer une nouvelle instance avec withPath()', function (): void { |
| 110 | + $original = new Cookie('test', 'value', null, '/original'); |
| 111 | + $modified = $original->withPath('/modified'); |
| 112 | + |
| 113 | + expect($original->getPath())->toBe('/original'); |
| 114 | + expect($modified->getPath())->toBe('/modified'); |
| 115 | + }); |
| 116 | + |
| 117 | + it('Devrait créer une nouvelle instance avec withSecure()', function (): void { |
| 118 | + $original = new Cookie('test', 'value', null, null, null, false); |
| 119 | + $modified = $original->withSecure(true); |
| 120 | + |
| 121 | + expect($original->isSecure())->toBe(false); |
| 122 | + expect($modified->isSecure())->toBe(true); |
| 123 | + }); |
| 124 | + |
| 125 | + it('Devrait créer une nouvelle instance avec withHttpOnly()', function (): void { |
| 126 | + $original = new Cookie('test', 'value', null, null, null, null, false); |
| 127 | + $modified = $original->withHttpOnly(true); |
| 128 | + |
| 129 | + expect($original->isHttpOnly())->toBe(false); |
| 130 | + expect($modified->isHttpOnly())->toBe(true); |
| 131 | + }); |
| 132 | + |
| 133 | + it('Devrait créer une nouvelle instance avec withSameSite()', function (): void { |
| 134 | + $original = new Cookie('test', 'value', null, null, null, null, null, 'Lax'); |
| 135 | + $modified = $original->withSameSite('Strict'); |
| 136 | + |
| 137 | + expect($original->getSameSite())->toBe('Lax'); |
| 138 | + expect($modified->getSameSite())->toBe('Strict'); |
| 139 | + }); |
| 140 | + |
| 141 | + it('Devrait créer une nouvelle instance avec withExpiry()', function (): void { |
| 142 | + $original = new Cookie('test', 'value', new DateTimeImmutable('+1 hour')); |
| 143 | + $newExpiry = new DateTimeImmutable('+2 hours'); |
| 144 | + $modified = $original->withExpiry($newExpiry); |
| 145 | + |
| 146 | + expect($original->getExpiry()->getTimestamp())->toBeLessThan($modified->getExpiry()->getTimestamp()); |
| 147 | + }); |
| 148 | + }); |
| 149 | + |
| 150 | + describe('Expiration', function (): void { |
| 151 | + it('Devrait détecter un cookie expiré', function (): void { |
| 152 | + $expired = new Cookie('test', 'value', new DateTimeImmutable('-1 hour')); |
| 153 | + expect($expired->isExpired())->toBe(true); |
| 154 | + }); |
| 155 | + |
| 156 | + it('Devrait détecter un cookie non expiré', function (): void { |
| 157 | + $valid = new Cookie('test', 'value', new DateTimeImmutable('+1 hour')); |
| 158 | + expect($valid->isExpired())->toBe(false); |
| 159 | + }); |
| 160 | + |
| 161 | + it('Devrait créer un cookie qui n\'expire jamais', function (): void { |
| 162 | + $cookie = (new Cookie('test', 'value'))->withNeverExpire(); |
| 163 | + expect($cookie->isExpired())->toBe(false); |
| 164 | + }); |
| 165 | + |
| 166 | + it('Devrait créer un cookie expiré', function (): void { |
| 167 | + $cookie = (new Cookie('test', 'value'))->withExpired(); |
| 168 | + expect($cookie->isExpired())->toBe(true); |
| 169 | + }); |
| 170 | + }); |
| 171 | + |
| 172 | + describe('Valeurs complexes', function (): void { |
| 173 | + it('Devrait gérer les valeurs de tableau', function (): void { |
| 174 | + $data = ['user' => 'john', 'role' => 'admin']; |
| 175 | + $cookie = new Cookie('test', $data); |
| 176 | + |
| 177 | + expect($cookie->getValue())->toBe($data); |
| 178 | + expect($cookie->isExpanded())->toBe(true); |
| 179 | + }); |
| 180 | + |
| 181 | + it('Devrait lire des valeurs imbriquées avec read()', function (): void { |
| 182 | + $data = ['user' => ['name' => 'john', 'role' => 'admin']]; |
| 183 | + $cookie = new Cookie('test', $data); |
| 184 | + |
| 185 | + expect($cookie->read('user.name'))->toBe('john'); |
| 186 | + expect($cookie->read('user.role'))->toBe('admin'); |
| 187 | + expect($cookie->read('nonexistent'))->toBeNull(); |
| 188 | + }); |
| 189 | + |
| 190 | + it('Devrait vérifier l\'existence de valeurs avec check()', function (): void { |
| 191 | + $data = ['user' => ['name' => 'john']]; |
| 192 | + $cookie = new Cookie('test', $data); |
| 193 | + |
| 194 | + expect($cookie->check('user.name'))->toBe(true); |
| 195 | + expect($cookie->check('user.nonexistent'))->toBe(false); |
| 196 | + }); |
| 197 | + |
| 198 | + it('Devrait ajouter des valeurs avec withAddedValue()', function (): void { |
| 199 | + $original = new Cookie('test', ['user' => 'john']); |
| 200 | + $modified = $original->withAddedValue('user.role', 'admin'); |
| 201 | + |
| 202 | + expect($modified->read('user.role'))->toBe('admin'); |
| 203 | + }); |
| 204 | + |
| 205 | + it('Devrait supprimer des valeurs avec withoutAddedValue()', function (): void { |
| 206 | + $original = new Cookie('test', ['user' => 'john', 'role' => 'admin']); |
| 207 | + $modified = $original->withoutAddedValue('role'); |
| 208 | + |
| 209 | + expect($modified->read('role'))->toBeNull(); |
| 210 | + expect($modified->read('user'))->toBe('john'); |
| 211 | + }); |
| 212 | + }); |
| 213 | + |
| 214 | + describe('Parsing d\'en-têtes', function (): void { |
| 215 | + it('Devrait parser un en-tête Set-Cookie simple', function (): void { |
| 216 | + $header = 'test=value; path=/; secure; httponly'; |
| 217 | + $cookie = Cookie::createFromHeaderString($header); |
| 218 | + |
| 219 | + expect($cookie->getName())->toBe('test'); |
| 220 | + expect($cookie->getScalarValue())->toBe('value'); |
| 221 | + expect($cookie->getPath())->toBe('/'); |
| 222 | + expect($cookie->isSecure())->toBe(true); |
| 223 | + expect($cookie->isHttpOnly())->toBe(true); |
| 224 | + }); |
| 225 | + |
| 226 | + it('Devrait parser un en-tête avec expiration', function (): void { |
| 227 | + $header = 'test=value; expires=Mon, 01-Aug-2025 12:00:00 GMT; path=/'; |
| 228 | + $cookie = Cookie::createFromHeaderString($header); |
| 229 | + |
| 230 | + expect($cookie->getName())->toBe('test'); |
| 231 | + expect($cookie->getExpiry())->toBeAnInstanceOf(DateTimeInterface::class); |
| 232 | + }); |
| 233 | + |
| 234 | + it('Devrait parser un en-tête avec SameSite', function (): void { |
| 235 | + $header = 'test=value; path=/; samesite=Strict'; |
| 236 | + $cookie = Cookie::createFromHeaderString($header); |
| 237 | + |
| 238 | + expect($cookie->getSameSite())->toBe('Strict'); |
| 239 | + }); |
| 240 | + |
| 241 | + it('Devrait ignorer les valeurs SameSite invalides', function (): void { |
| 242 | + $header = 'test=value; path=/; samesite=Invalid'; |
| 243 | + $cookie = Cookie::createFromHeaderString($header); |
| 244 | + |
| 245 | + expect($cookie->getSameSite())->toBeNull(); |
| 246 | + }); |
| 247 | + }); |
| 248 | + |
| 249 | + describe('Génération d\'en-têtes', function (): void { |
| 250 | + it('Devrait générer une valeur d\'en-tête correcte', function (): void { |
| 251 | + $cookie = new Cookie('test', 'value', null, '/admin', 'example.com', true, true, 'Strict'); |
| 252 | + $header = $cookie->toHeaderValue(); |
| 253 | + |
| 254 | + expect($header)->toContain('test=value'); |
| 255 | + expect($header)->toContain('path=/admin'); |
| 256 | + expect($header)->toContain('domain=example.com'); |
| 257 | + expect($header)->toContain('secure'); |
| 258 | + expect($header)->toContain('httponly'); |
| 259 | + expect($header)->toContain('samesite=Strict'); |
| 260 | + }); |
| 261 | + |
| 262 | + it('Devrait encoder les valeurs URL', function (): void { |
| 263 | + $cookie = new Cookie('test', 'value with spaces'); |
| 264 | + $header = $cookie->toHeaderValue(); |
| 265 | + |
| 266 | + expect($header)->toContain('test=value%20with%20spaces'); |
| 267 | + }); |
| 268 | + |
| 269 | + it('Devrait générer un en-tête sans attributs optionnels', function (): void { |
| 270 | + $cookie = new Cookie('test', 'value'); |
| 271 | + $header = $cookie->toHeaderValue(); |
| 272 | + |
| 273 | + expect($header)->toBe('test=value; path=/'); |
| 274 | + }); |
| 275 | + }); |
| 276 | + |
| 277 | + describe('ID et identification', function (): void { |
| 278 | + it('Devrait générer un ID unique', function (): void { |
| 279 | + $cookie1 = new Cookie('test', 'value', null, '/path1', 'domain.com'); |
| 280 | + $cookie2 = new Cookie('test', 'value', null, '/path2', 'domain.com'); |
| 281 | + $cookie3 = new Cookie('test', 'value', null, '/path1', 'other.com'); |
| 282 | + |
| 283 | + expect($cookie1->getId())->not->toBe($cookie2->getId()); |
| 284 | + expect($cookie1->getId())->not->toBe($cookie3->getId()); |
| 285 | + expect($cookie1->getId())->toBe('test;domain.com;/path1'); |
| 286 | + }); |
| 287 | + }); |
| 288 | + |
| 289 | + describe('Conversion', function (): void { |
| 290 | + it('Devrait convertir en tableau', function (): void { |
| 291 | + $cookie = new Cookie('test', 'value', new DateTimeImmutable('@1234567890'), '/path', 'domain.com', true, true, 'Lax'); |
| 292 | + $array = $cookie->toArray(); |
| 293 | + |
| 294 | + expect($array)->toBe([ |
| 295 | + 'name' => 'test', |
| 296 | + 'value' => 'value', |
| 297 | + 'expires' => 1234567890, |
| 298 | + 'path' => '/path', |
| 299 | + 'domain' => 'domain.com', |
| 300 | + 'secure' => true, |
| 301 | + 'httponly' => true, |
| 302 | + 'samesite' => 'Lax', |
| 303 | + ]); |
| 304 | + }); |
| 305 | + |
| 306 | + it('Devrait retourner les options', function (): void { |
| 307 | + $cookie = new Cookie('test', 'value', null, '/path', 'domain.com', true, false, 'Strict'); |
| 308 | + $options = $cookie->getOptions(); |
| 309 | + |
| 310 | + expect($options)->toContainKey('path'); |
| 311 | + expect($options)->toContainKey('domain'); |
| 312 | + expect($options)->toContainKey('secure'); |
| 313 | + expect($options)->toContainKey('httponly'); |
| 314 | + expect($options)->toContainKey('samesite'); |
| 315 | + }); |
| 316 | + }); |
| 317 | + |
| 318 | + describe('Valeurs par défaut', function (): void { |
| 319 | + it('Devrait utiliser les valeurs par défaut configurées', function (): void { |
| 320 | + Cookie::setDefaults([ |
| 321 | + 'path' => '/admin', |
| 322 | + 'secure' => true, |
| 323 | + 'httponly' => true, |
| 324 | + 'samesite' => 'Lax' |
| 325 | + ]); |
| 326 | + |
| 327 | + $cookie = new Cookie('test', 'value'); |
| 328 | + |
| 329 | + expect($cookie->getPath())->toBe('/admin'); |
| 330 | + expect($cookie->isSecure())->toBe(true); |
| 331 | + expect($cookie->isHttpOnly())->toBe(true); |
| 332 | + expect($cookie->getSameSite())->toBe('Lax'); |
| 333 | + }); |
| 334 | + }); |
| 335 | +}); |
0 commit comments