Completed
Pull Request — master (#135)
by Zhukov
02:10
created

SessionTest::testRegenerateID()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Yiisoft\Yii\Web\Tests\Session;
4
5
use Yiisoft\Yii\Web\Session\Session;
6
use PHPUnit\Framework\TestCase;
7
8
class SessionTest extends TestCase
9
{
10
    public function tearDown()
11
    {
12
        @session_destroy();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for session_destroy(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

12
        /** @scrutinizer ignore-unhandled */ @session_destroy();

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
13
    }
14
15
    /**
16
     * @runInSeparateProcess
17
     */
18
    public function testGetAndSet()
19
    {
20
        $session = new Session();
21
        $session->set('get', 'set');
22
        self::assertEquals('set', $session->get('get'));
23
    }
24
25
    /**
26
     * @runInSeparateProcess
27
     */
28
    public function testHas()
29
    {
30
        $session = new Session();
31
        $session->set('has', 'has');
32
        self::assertTrue($session->has('has'));
33
    }
34
35
    /**
36
     * @runInSeparateProcess
37
     */
38
    public function testClose()
39
    {
40
        $session = new Session();
41
        $session->set('close', 'close');
42
        $session->close();
43
        self::assertEquals(PHP_SESSION_NONE, session_status());
44
        // because session_destroy() in tearDown doesn't work as expected
45
        // we need to open session and then destroy it
46
        $session->open();
47
        $session->destroy();
48
    }
49
50
    /**
51
     * @runInSeparateProcess
52
     */
53
    public function testRegenerateID()
54
    {
55
        $session = new Session();
56
        $session->open();
57
        $id = $session->getId();
58
        $session->regenerateId();
59
        self::assertNotEquals($id, $session->getId());
60
    }
61
62
    /**
63
     * @runInSeparateProcess
64
     */
65
    public function testDiscard()
66
    {
67
        $session = new Session();
68
        $session->set('discard', 'discard');
69
        $session->discard();
70
        self::assertEmpty($session->get('discard'));
71
    }
72
73
    public function testGetName()
74
    {
75
        $session = new Session();
76
        self::assertEquals($session->getName(), session_name());
77
    }
78
79
    /**
80
     * @runInSeparateProcess
81
     */
82
    public function testPull()
83
    {
84
        $session = new Session();
85
        $session->set('pull', 'pull');
86
        self::assertEquals('pull', $session->pull('pull'));
87
        self::assertEmpty($session->get('pull'));
88
    }
89
90
    /**
91
     * @runInSeparateProcess
92
     */
93
    public function testAll()
94
    {
95
        $session = new Session();
96
        $session->set('1', 1);
97
        $session->set('2', 2);
98
        self::assertEquals(['1' => 1, '2' => 2], $session->all());
99
    }
100
101
    /**
102
     * @runInSeparateProcess
103
     */
104
    public function testClear()
105
    {
106
        $session = new Session();
107
        $session->set('1', 1);
108
        $session->clear();
109
        self::assertEmpty($session->all());
110
    }
111
112
    /**
113
     * @runInSeparateProcess
114
     */
115
    public function testSetId()
116
    {
117
        $session = new Session();
118
        $session->setId('sessionId');
119
        $session->open();
120
        self::assertEquals(session_id(), $session->getId());
121
    }
122
123
    public function testGetCookieParameters()
124
    {
125
        $session = new Session();
126
        self::assertEquals(session_get_cookie_params(), $session->getCookieParameters());
127
    }
128
129
    /**
130
     * @expectedException \RuntimeException
131
     */
132
    public function testAlreadyStartedException()
133
    {
134
        $session = new Session();
135
        $session->set('1', 1);
136
        $session = new Session();
0 ignored issues
show
Unused Code introduced by
The assignment to $session is dead and can be removed.
Loading history...
137
    }
138
}
139