Passed
Push — master ( 3c979b...72c793 )
by Kirill
03:02
created

FactoryTest::testMultipleSessions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 18
rs 9.8333
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\Session;
13
14
use PHPUnit\Framework\TestCase;
15
use Spiral\Core\Container;
16
use Spiral\Session\Config\SessionConfig;
17
use Spiral\Session\Exception\SessionException;
18
use Spiral\Session\Handler\FileHandler;
19
use Spiral\Session\Session;
20
use Spiral\Session\SessionFactory;
21
use Spiral\Session\SessionInterface;
22
23
class FactoryTest extends TestCase
24
{
25
    public function tearDown(): void
26
    {
27
        if ((int)session_status() === PHP_SESSION_ACTIVE) {
28
            session_abort();
29
        }
30
    }
31
32
    public function testConstructInvalid(): void
33
    {
34
        $this->expectException(SessionException::class);
35
        $factory = new SessionFactory(new SessionConfig([
36
            'lifetime' => 86400,
37
            'cookie'   => 'SID',
38
            'secure'   => false,
39
            'handler'  => FileHandler::class,
40
            'handlers' => [
41
                //No directory
42
            ]
43
        ]), new Container());
44
45
        $factory->initSession('sig', 'sessionid');
46
    }
47
48
    public function testAlreadyStarted(): void
49
    {
50
        $this->expectException(SessionException::class);
51
        $factory = new SessionFactory(new SessionConfig([
52
            'lifetime' => 86400,
53
            'cookie'   => 'SID',
54
            'secure'   => false,
55
            'handler'  => FileHandler::class,
56
            'handlers' => [
57
                //No directory
58
            ]
59
        ]), new Container());
60
61
        $factory->initSession('sig', 'sessionid');
62
    }
63
64
    public function testMultipleSessions(): void
65
    {
66
        $this->expectExceptionMessage('Unable to initiate session, session already started');
67
        $this->expectException(SessionException::class);
68
        $factory = new SessionFactory(new SessionConfig([
69
            'lifetime' => 86400,
70
            'cookie'   => 'SID',
71
            'secure'   => false,
72
            'handler'  => null,
73
            'handlers' => []
74
        ]), $c = new Container());
75
76
        $c->bind(SessionInterface::class, Session::class);
77
78
        $session = $factory->initSession('sig');
79
        $session->resume();
80
81
        $factory->initSession('sig', $session->getID());
82
    }
83
}
84