Passed
Pull Request — master (#277)
by Kirill
03:11
created

SessionTest::testSessionRegenerateId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 34
rs 9.584
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Tests\Framework\Http;
13
14
use Spiral\Session\SessionInterface;
15
use Spiral\Tests\Framework\HttpTest;
16
17
class SessionTest extends HttpTest
18
{
19
    public function testSetSid(): void
20
    {
21
        $this->http->setHandler(function () {
22
            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...
23
        });
24
25
        $result = $this->get('/');
26
        $this->assertSame(200, $result->getStatusCode());
27
        $this->assertSame('1', $result->getBody()->__toString());
28
29
        $cookies = $this->fetchCookies($result->getHeader('Set-Cookie'));
30
        $this->assertArrayHasKey('sid', $cookies);
31
    }
32
33
    public function testSessionResume(): void
34
    {
35
        $this->http->setHandler(function () {
36
            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...
37
        });
38
39
        $result = $this->get('/');
40
        $this->assertSame(200, $result->getStatusCode());
41
        $this->assertSame('1', $result->getBody()->__toString());
42
43
        $cookies = $this->fetchCookies($result->getHeader('Set-Cookie'));
44
        $this->assertArrayHasKey('sid', $cookies);
45
        $result = $this->get('/', [], [], [
46
            'sid' => $cookies['sid']
47
        ]);
48
49
        $this->assertSame(200, $result->getStatusCode());
50
        $this->assertSame('2', $result->getBody()->__toString());
51
52
        $result = $this->get('/', [], [], ['sid' => $cookies['sid']]);
53
        $this->assertSame(200, $result->getStatusCode());
54
        $this->assertSame('3', $result->getBody()->__toString());
55
    }
56
57
    public function testSessionRegenerateId(): void
58
    {
59
        $this->http->setHandler(function () {
60
            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...
61
        });
62
63
        $result = $this->get('/');
64
        $this->assertSame(200, $result->getStatusCode());
65
        $this->assertSame('1', $result->getBody()->__toString());
66
67
        $cookies = $this->fetchCookies($result->getHeader('Set-Cookie'));
68
        $this->assertArrayHasKey('sid', $cookies);
69
70
        $result = $this->get('/', [], [], [
71
            'sid' => $cookies['sid']
72
        ]);
73
        $this->assertSame(200, $result->getStatusCode());
74
        $this->assertSame('2', $result->getBody()->__toString());
75
76
        $this->http->setHandler(function () {
77
            $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

77
            $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...
78
79
            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...
80
        });
81
82
        $result = $this->get('/', [], [], [
83
            'sid' => $cookies['sid']
84
        ]);
85
86
        $newCookies = $this->fetchCookies($result->getHeader('Set-Cookie'));
87
        $this->assertArrayHasKey('sid', $newCookies);
88
        $this->assertNotEquals($cookies['sid'], $newCookies['sid']);
89
        $this->assertSame(200, $result->getStatusCode());
90
        $this->assertSame('3', $result->getBody()->__toString());
91
    }
92
93
    public function testDestroySession(): void
94
    {
95
        $this->http->setHandler(function () {
96
            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...
97
        });
98
99
        $result = $this->get('/');
100
        $this->assertSame(200, $result->getStatusCode());
101
        $cookies = $this->fetchCookies($result->getHeader('Set-Cookie'));
102
        $this->assertArrayHasKey('sid', $cookies);
103
        $result = $this->get('/', [], [], [
104
            'sid' => $cookies['sid']
105
        ]);
106
        $this->assertSame(200, $result->getStatusCode());
107
        $this->assertSame('2', $result->getBody()->__toString());
108
        $this->http->setHandler(function () {
109
            $this->session()->destroy();
110
            $this->assertFalse($this->session()->isStarted());
111
112
            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...
113
        });
114
        $result = $this->get('/', [], [], [
115
            'sid' => $cookies['sid']
116
        ]);
117
        $this->assertSame(200, $result->getStatusCode());
118
        $this->assertSame('1', $result->getBody()->__toString());
119
    }
120
121
    private function session(): SessionInterface
122
    {
123
        return $this->app->get(SessionInterface::class);
124
    }
125
}
126