|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PSR7SessionsTest\Storage\Adapter; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit_Framework_TestCase; |
|
6
|
|
|
use PSR7Sessions\Storage\Adapter\MemoryStorage; |
|
7
|
|
|
use PSR7Sessions\Storage\Id\UuidSessionId; |
|
8
|
|
|
use PSR7Sessions\Storage\Session\StorableSession; |
|
9
|
|
|
use PSR7Sessions\Storage\Session\StorableSessionInterface; |
|
10
|
|
|
|
|
11
|
|
|
class MemoryStorageTest extends PHPUnit_Framework_TestCase |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var MemoryStorage */ |
|
14
|
|
|
private $storage; |
|
15
|
|
|
|
|
16
|
|
|
protected function setUp() |
|
17
|
|
|
{ |
|
18
|
|
|
parent::setUp(); |
|
19
|
|
|
|
|
20
|
|
|
$this->storage = new MemoryStorage; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function testSaveAndLoad() |
|
24
|
|
|
{ |
|
25
|
|
|
$session = $this->createSession(); |
|
26
|
|
|
$session->set('foo', 'bar'); |
|
27
|
|
|
|
|
28
|
|
|
$this->storage->save($session); |
|
29
|
|
|
$loaded = $this->reload($session); |
|
30
|
|
|
|
|
31
|
|
|
$this->assertSame('bar', $loaded->get('foo')); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function testLoadUnknownId() |
|
35
|
|
|
{ |
|
36
|
|
|
$id = new UuidSessionId; |
|
37
|
|
|
$session = $this->storage->load($id); |
|
38
|
|
|
|
|
39
|
|
|
$this->assertSame($id, $session->getId()); |
|
40
|
|
|
$this->assertTrue($session->isEmpty()); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function testDestroy() |
|
44
|
|
|
{ |
|
45
|
|
|
$session = $this->createSession(); |
|
46
|
|
|
$session->set('foo', 'bar'); |
|
47
|
|
|
$this->storage->save($session); |
|
48
|
|
|
|
|
49
|
|
|
$this->storage->destroy($session->getId()); |
|
50
|
|
|
|
|
51
|
|
|
$loaded = $this->reload($session); |
|
52
|
|
|
$this->assertTrue($loaded->isEmpty()); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function testDestroyUnknownSession() |
|
56
|
|
|
{ |
|
57
|
|
|
$id = new UuidSessionId; |
|
58
|
|
|
$this->storage->destroy($id); |
|
59
|
|
|
|
|
60
|
|
|
$loaded = $this->storage->load($id); |
|
61
|
|
|
$this->assertTrue($loaded->isEmpty()); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
private function createSession():StorableSession |
|
65
|
|
|
{ |
|
66
|
|
|
return StorableSession::create($this->storage); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
private function reload(StorableSessionInterface $session):StorableSessionInterface |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->storage->load($session->getId()); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|