|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PSR7SessionsTest\Storage\Session; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit_Framework_TestCase; |
|
6
|
|
|
use PSR7Sessions\Storage\Adapter\MemoryStorage; |
|
7
|
|
|
use PSR7Sessions\Storage\Adapter\StorageInterface; |
|
8
|
|
|
use PSR7Sessions\Storage\Id\SessionId; |
|
9
|
|
|
use PSR7Sessions\Storage\Session\StorableSession; |
|
10
|
|
|
use PSR7Sessions\Storage\Session\StorableSessionInterface; |
|
11
|
|
|
use PSR7Sessions\Storageless\Session\DefaultSessionData; |
|
12
|
|
|
use PSR7Sessions\Storageless\Session\SessionInterface; |
|
13
|
|
|
|
|
14
|
|
|
class StorableSessionTest extends PHPUnit_Framework_TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var SessionInterface */ |
|
17
|
|
|
private $wrappedSession; |
|
18
|
|
|
/** @var StorageInterface */ |
|
19
|
|
|
private $storage; |
|
20
|
|
|
/** @var \PSR7Sessions\Storage\Session\StorableSession */ |
|
21
|
|
|
private $session; |
|
22
|
|
|
|
|
23
|
|
|
public function setUp() |
|
24
|
|
|
{ |
|
25
|
|
|
$this->wrappedSession = DefaultSessionData::newEmptySession(); |
|
26
|
|
|
$this->storage = new MemoryStorage; |
|
27
|
|
|
$this->session = new StorableSession($this->wrappedSession, $this->storage); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function testGet() |
|
31
|
|
|
{ |
|
32
|
|
|
$key = 'test'; |
|
33
|
|
|
$val = 'foo'; |
|
34
|
|
|
$this->wrappedSession->set($key, $val); |
|
35
|
|
|
|
|
36
|
|
|
$this->assertSame($val, $this->session->get($key)); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function testRemove() |
|
40
|
|
|
{ |
|
41
|
|
|
$key = 'test'; |
|
42
|
|
|
$this->session->set($key, 'bar'); |
|
43
|
|
|
|
|
44
|
|
|
$this->session->remove($key); |
|
45
|
|
|
|
|
46
|
|
|
$this->assertFalse($this->session->has($key)); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function testClear() |
|
50
|
|
|
{ |
|
51
|
|
|
$this->session->set('foo', 'bar'); |
|
52
|
|
|
|
|
53
|
|
|
$this->session->clear(); |
|
54
|
|
|
|
|
55
|
|
|
$this->assertFalse($this->session->has('foo')); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function testHasChanged() |
|
59
|
|
|
{ |
|
60
|
|
|
$this->assertFalse($this->session->hasChanged()); |
|
61
|
|
|
|
|
62
|
|
|
$this->session->set('foo', 'bar'); |
|
63
|
|
|
|
|
64
|
|
|
$this->assertTrue($this->session->hasChanged()); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function testIsEmpty() |
|
68
|
|
|
{ |
|
69
|
|
|
$this->assertTrue($this->session->isEmpty()); |
|
70
|
|
|
|
|
71
|
|
|
$this->session->set('foo', 'bar'); |
|
72
|
|
|
|
|
73
|
|
|
$this->assertFalse($this->session->isEmpty()); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function testFromStorage() |
|
77
|
|
|
{ |
|
78
|
|
|
$id = new SessionId('test'); |
|
79
|
|
|
$wrappedSession = DefaultSessionData::newEmptySession(); |
|
80
|
|
|
$wrappedSession->set('foo', 'bar'); |
|
81
|
|
|
|
|
82
|
|
|
$session = StorableSession::fromId($wrappedSession, $this->storage, $id); |
|
83
|
|
|
|
|
84
|
|
|
$this->assertSame($id, $session->getId()); |
|
85
|
|
|
$this->assertSame('bar', $session->get('foo')); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function testSaveOnSet() |
|
89
|
|
|
{ |
|
90
|
|
|
$this->session->set('foo', 'bar'); |
|
91
|
|
|
|
|
92
|
|
|
$loaded = $this->reload($this->session); |
|
93
|
|
|
$this->assertSame('bar', $loaded->get('foo')); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function testSaveOnRemove() |
|
97
|
|
|
{ |
|
98
|
|
|
$this->session->set('foo', 'bar'); |
|
99
|
|
|
|
|
100
|
|
|
$this->session->remove('foo'); |
|
101
|
|
|
|
|
102
|
|
|
$loaded = $this->reload($this->session); |
|
103
|
|
|
$this->assertFalse($loaded->has('foo')); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function testSaveOnClear() |
|
107
|
|
|
{ |
|
108
|
|
|
$this->session->set('foo', 'bar'); |
|
109
|
|
|
|
|
110
|
|
|
$this->session->clear(); |
|
111
|
|
|
|
|
112
|
|
|
$loaded = $this->reload($this->session); |
|
113
|
|
|
$this->assertFalse($loaded->has('foo')); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
private function reload(StorableSessionInterface $session):StorableSessionInterface |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->storage->load($session->getId()); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|