1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Enjoys\Cookie; |
4
|
|
|
|
5
|
|
|
use Enjoys\Cookie\Cookie; |
6
|
|
|
use Enjoys\Cookie\Options; |
7
|
|
|
use HttpSoft\ServerRequest\ServerRequestCreator; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
10
|
|
|
|
11
|
|
|
class CookieTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
private ServerRequestInterface $request; |
15
|
|
|
|
16
|
|
|
protected function setUp(): void |
17
|
|
|
{ |
18
|
|
|
$this->request = ServerRequestCreator::createFromGlobals(cookie: [ |
19
|
|
|
'installed_cookie' => 'value' |
20
|
|
|
]); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @runInSeparateProcess |
25
|
|
|
*/ |
26
|
|
|
public function testSetCookie() |
27
|
|
|
{ |
28
|
|
|
$cookie = new Cookie(new Options($this->request)); |
29
|
|
|
$this->assertSame(true, $cookie->setRaw('keyRaw', 'value<>')); |
30
|
|
|
$this->assertSame(true, $cookie->set('key', 'value<>')); |
31
|
|
|
$this->assertSame(true, $cookie->has('key')); |
32
|
|
|
$this->assertSame('value%3C%3E', $cookie->get('key')); |
33
|
|
|
$this->assertSame('value<>', $cookie->get('keyRaw')); |
34
|
|
|
$this->assertSame('value', $cookie->get('installed_cookie')); |
35
|
|
|
|
36
|
|
|
$cookie->delete('key'); |
37
|
|
|
$this->assertSame(null, $cookie->get('key')); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @runInSeparateProcess |
42
|
|
|
*/ |
43
|
|
|
public function testDeleteCookie(){ |
44
|
|
|
$request = $this->request->withCookieParams([ |
45
|
|
|
'cookie_key' => 'cookie_value' |
46
|
|
|
]); |
47
|
|
|
$cookie = new Cookie(new Options($request)); |
48
|
|
|
$this->assertSame('cookie_value', $cookie->get('cookie_key')); |
49
|
|
|
$cookie->delete('cookie_key'); |
50
|
|
|
$this->assertSame(null, $cookie->get('cookie_key')); |
51
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @runInSeparateProcess |
57
|
|
|
*/ |
58
|
|
|
public function testSetCookieFailedByExpired(){ |
59
|
|
|
$cookie = new Cookie(new Options($this->request)); |
60
|
|
|
$this->assertSame(true, $cookie->set('key', 'value', false)); |
61
|
|
|
$this->assertSame(true, $cookie->setRaw('keyRaw', 'value', false)); |
62
|
|
|
$this->assertSame(null, $cookie->get('key')); |
63
|
|
|
$this->assertSame(null, $cookie->get('keyRaw')); |
64
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
} |
69
|
|
|
|