1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Jasny\Session; |
6
|
|
|
|
7
|
|
|
use Jasny\Session\Flash\FlashBag; |
8
|
|
|
use Jasny\Session\Flash\FlashTrait; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Session that only exists in local memory. |
12
|
|
|
* |
13
|
|
|
* @extends \ArrayObject<string,mixed> |
14
|
|
|
*/ |
15
|
|
|
class MockSession extends \ArrayObject implements SessionInterface |
16
|
|
|
{ |
17
|
|
|
use FlashTrait; |
18
|
|
|
|
19
|
|
|
/** @var array<string,mixed> */ |
20
|
|
|
protected array $initialData; |
21
|
|
|
protected int $status = \PHP_SESSION_ACTIVE; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* MockSession constructor. |
25
|
|
|
* |
26
|
|
|
* @param array<string,mixed> $input |
27
|
|
|
* @param FlashBag|null $flashBag |
28
|
|
|
*/ |
29
|
5 |
|
public function __construct(array $input = [], ?FlashBag $flashBag = null) |
30
|
|
|
{ |
31
|
5 |
|
$this->initialData = $input; |
32
|
5 |
|
$this->flashBag = $flashBag ?? new FlashBag(); |
33
|
|
|
|
34
|
5 |
|
parent::__construct($input); |
35
|
5 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inheritDoc |
39
|
|
|
*/ |
40
|
1 |
|
public function status(): int |
41
|
|
|
{ |
42
|
1 |
|
return $this->status; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritDoc |
47
|
|
|
*/ |
48
|
3 |
|
public function start(): void |
49
|
|
|
{ |
50
|
3 |
|
$this->status = \PHP_SESSION_ACTIVE; |
51
|
3 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritDoc |
55
|
|
|
*/ |
56
|
2 |
|
public function stop(): void |
57
|
|
|
{ |
58
|
2 |
|
$this->initialData = $this->getArrayCopy(); |
59
|
2 |
|
$this->status = \PHP_SESSION_NONE; |
60
|
2 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritDoc |
64
|
|
|
*/ |
65
|
2 |
|
public function abort(): void |
66
|
|
|
{ |
67
|
2 |
|
$this->exchangeArray($this->initialData); |
68
|
2 |
|
$this->status = \PHP_SESSION_NONE; |
69
|
2 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @inheritDoc |
73
|
|
|
*/ |
74
|
1 |
|
public function clear(): void |
75
|
|
|
{ |
76
|
1 |
|
$this->exchangeArray([]); |
77
|
1 |
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $offset |
82
|
|
|
*/ |
83
|
1 |
|
public function offsetUnset($offset): void |
84
|
|
|
{ |
85
|
1 |
|
if (parent::offsetExists($offset)) { |
86
|
1 |
|
parent::offsetUnset($offset); |
87
|
|
|
} |
88
|
1 |
|
} |
89
|
|
|
} |
90
|
|
|
|