Passed
Push — master ( ff614d...6c840c )
by Anton
05:03 queued 02:24
created

SessionTest::testDestroySession()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
dl 0
loc 26
rs 9.6666
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Spiral\Framework\Http;
10
11
use Spiral\Framework\HttpTest;
12
use Spiral\Session\SessionInterface;
13
14
class SessionTest extends HttpTest
15
{
16
    public function testSetSid()
17
    {
18
        $this->http->setHandler(function () {
19
            return ++$this->session()->getSection('cli')->value;
0 ignored issues
show
Bug introduced by
Accessing value on the interface Spiral\Session\SessionSectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
20
        });
21
22
        $result = $this->get('/');
23
        $this->assertSame(200, $result->getStatusCode());
24
        $this->assertSame('1', $result->getBody()->__toString());
25
26
        $cookies = $this->fetchCookies($result->getHeader('Set-Cookie'));
27
        $this->assertArrayHasKey('sid', $cookies);
28
    }
29
30
    public function testSessionResume()
31
    {
32
        $this->http->setHandler(function () {
33
            return ++$this->session()->getSection('cli')->value;
0 ignored issues
show
Bug introduced by
Accessing value on the interface Spiral\Session\SessionSectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
34
        });
35
36
        $result = $this->get('/');
37
        $this->assertSame(200, $result->getStatusCode());
38
        $this->assertSame('1', $result->getBody()->__toString());
39
40
        $cookies = $this->fetchCookies($result->getHeader('Set-Cookie'));
41
        $this->assertArrayHasKey('sid', $cookies);
42
        $result = $this->get('/', [], [], [
43
            'sid' => $cookies['sid']
44
        ]);
45
46
        $this->assertSame(200, $result->getStatusCode());
47
        $this->assertSame('2', $result->getBody()->__toString());
48
49
        $result = $this->get('/', [], [], ['sid' => $cookies['sid']]);
50
        $this->assertSame(200, $result->getStatusCode());
51
        $this->assertSame('3', $result->getBody()->__toString());
52
    }
53
54
    public function testSessionRegenerateId()
55
    {
56
        $this->http->setHandler(function () {
57
            return ++$this->session()->getSection('cli')->value;
0 ignored issues
show
Bug introduced by
Accessing value on the interface Spiral\Session\SessionSectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
58
        });
59
60
        $result = $this->get('/');
61
        $this->assertSame(200, $result->getStatusCode());
62
        $this->assertSame('1', $result->getBody()->__toString());
63
64
        $cookies = $this->fetchCookies($result->getHeader('Set-Cookie'));
65
        $this->assertArrayHasKey('sid', $cookies);
66
67
        $result = $this->get('/', [], [], [
68
            'sid' => $cookies['sid']
69
        ]);
70
        $this->assertSame(200, $result->getStatusCode());
71
        $this->assertSame('2', $result->getBody()->__toString());
72
73
        $this->http->setHandler(function () {
74
            $this->session()->regenerateID(false);
0 ignored issues
show
Unused Code introduced by
The call to Spiral\Session\SessionInterface::regenerateID() has too many arguments starting with false. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
            $this->session()->/** @scrutinizer ignore-call */ regenerateID(false);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
75
76
            return ++$this->session()->getSection('cli')->value;
0 ignored issues
show
Bug introduced by
Accessing value on the interface Spiral\Session\SessionSectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
77
        });
78
79
        $result = $this->get('/', [], [], [
80
            'sid' => $cookies['sid']
81
        ]);
82
83
        $newCookies = $this->fetchCookies($result->getHeader('Set-Cookie'));
84
        $this->assertArrayHasKey('sid', $newCookies);
85
        $this->assertNotEquals($cookies['sid'], $newCookies['sid']);
86
        $this->assertSame(200, $result->getStatusCode());
87
        $this->assertSame('3', $result->getBody()->__toString());
88
    }
89
90
    public function testDestroySession()
91
    {
92
        $this->http->setHandler(function () {
93
            return ++$this->session()->getSection('cli')->value;
0 ignored issues
show
Bug introduced by
Accessing value on the interface Spiral\Session\SessionSectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
94
        });
95
96
        $result = $this->get('/');
97
        $this->assertSame(200, $result->getStatusCode());
98
        $cookies = $this->fetchCookies($result->getHeader('Set-Cookie'));
99
        $this->assertArrayHasKey('sid', $cookies);
100
        $result = $this->get('/', [], [], [
101
            'sid' => $cookies['sid']
102
        ]);
103
        $this->assertSame(200, $result->getStatusCode());
104
        $this->assertSame('2', $result->getBody()->__toString());
105
        $this->http->setHandler(function () {
106
            $this->session()->destroy();
107
            $this->assertFalse($this->session()->isStarted());
108
109
            return ++$this->session()->getSection('cli')->value;
0 ignored issues
show
Bug introduced by
Accessing value on the interface Spiral\Session\SessionSectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
110
        });
111
        $result = $this->get('/', [], [], [
112
            'sid' => $cookies['sid']
113
        ]);
114
        $this->assertSame(200, $result->getStatusCode());
115
        $this->assertSame('1', $result->getBody()->__toString());
116
    }
117
118
    private function session(): SessionInterface
119
    {
120
        return $this->app->get(SessionInterface::class);
121
    }
122
}
123