CookieTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 24
c 3
b 0
f 1
dl 0
loc 53
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testSetCookie() 0 12 1
A setUp() 0 4 1
A testDeleteCookie() 0 8 1
A testSetCookieFailedByExpired() 0 6 1
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