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

SessionTest::testDiscard()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
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
    public function testGetAndSet()
16
    {
17
        $session = new Session();
18
        $session->set('get', 'set');
19
        self::assertEquals('set', $session->get('get'));
20
    }
21
22
    public function testHas()
23
    {
24
        $session = new Session();
25
        $session->set('has', 'has');
26
        self::assertTrue($session->has('has'));
27
    }
28
29
    public function testClose()
30
    {
31
        $session = new Session();
32
        $session->set('close', 'close');
33
        $session->close();
34
        self::assertEquals(PHP_SESSION_NONE, session_status());
35
        // because session_destroy() in tearDown doesn't work as expected
36
        // we need to open session and then destroy it
37
        $session->open();
38
        $session->destroy();
39
    }
40
41
    public function testRegenerateID()
42
    {
43
        $session = new Session();
44
        $session->open();
45
        $id = $session->getId();
46
        $session->regenerateId();
47
        self::assertNotEquals($id, $session->getId());
48
    }
49
50
    public function testDiscard()
51
    {
52
        $session = new Session();
53
        $session->set('discard', 'discard');
54
        $session->discard();
55
        self::assertEmpty($session->get('discard'));
56
    }
57
58
    public function testGetName()
59
    {
60
        $session = new Session();
61
        self::assertEquals($session->getName(), session_name());
62
    }
63
64
    public function testPull()
65
    {
66
        $session = new Session();
67
        $session->set('pull', 'pull');
68
        self::assertEquals('pull', $session->pull('pull'));
69
        self::assertEmpty($session->get('pull'));
70
    }
71
72
    public function testAll()
73
    {
74
        $session = new Session();
75
        $session->set('1', 1);
76
        $session->set('2', 2);
77
        self::assertEquals(['1' => 1, '2' => 2], $session->all());
78
    }
79
80
    public function testClear()
81
    {
82
        $session = new Session();
83
        $session->set('1', 1);
84
        $session->clear();
85
        self::assertEmpty($session->all());
86
    }
87
88
    public function testSetId()
89
    {
90
        $session = new Session();
91
        $session->setId('sessionId');
92
        $session->open();
93
        self::assertEquals(session_id(), $session->getId());
94
    }
95
96
    public function testGetCookieParameters()
97
    {
98
        $session = new Session();
99
        self::assertEquals(session_get_cookie_params(), $session->getCookieParameters());
100
    }
101
102
    /**
103
     * @expectedException \RuntimeException
104
     */
105
    public function testAlreadyStartedException()
106
    {
107
        $session = new Session();
108
        $session->set('1', 1);
109
        $session = new Session();
0 ignored issues
show
Unused Code introduced by
The assignment to $session is dead and can be removed.
Loading history...
110
    }
111
}
112